Subversion Repositories SvarDOS

Rev

Rev 269 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 269 Rev 613
1
/*
1
/*
2
 * This file is part of the pkg (SvarDOS) project.
2
 * This file is part of the pkg (SvarDOS) project.
3
 * Copyright (C) Mateusz Viste 2012-2021
3
 * Copyright (C) Mateusz Viste 2012-2022
4
 */
4
 */
5
 
5
 
6
#include <ctype.h>    /* toupper() */
6
#include <ctype.h>    /* toupper() */
7
#include <stdio.h>
7
#include <stdio.h>
8
#include <string.h>    /* strlen() */
8
#include <string.h>    /* strlen() */
9
#include <stdlib.h>    /* free() */
9
#include <stdlib.h>    /* free() */
10
#include <unistd.h>    /* rmdir(), unlink() */
10
#include <unistd.h>    /* rmdir(), unlink() */
11
#include <direct.h>  /* watcom needs this for the rmdir() prototype */
11
#include <direct.h>  /* watcom needs this for the rmdir() prototype */
12
 
12
 
13
#include "helpers.h"   /* slash2backslash() */
13
#include "helpers.h"   /* slash2backslash() */
14
#include "kprintf.h"
14
#include "kprintf.h"
15
 
15
 
16
#include "pkgrem.h"
16
#include "pkgrem.h"
17
 
17
 
18
 
18
 
19
struct dirliststruct {
19
struct dirliststruct {
20
  struct dirliststruct *next;
20
  struct dirliststruct *next;
21
  char dirname[2]; /* this must be the last item in the structure */
21
  char dirname[2]; /* this must be the last item in the structure */
22
};
22
};
23
 
23
 
24
 
24
 
25
/* adds a directory to dirlist, if not already present */
25
/* adds a directory to dirlist, if not already present */
26
static struct dirliststruct *rememberdir(struct dirliststruct *dirlist, const char *path) {
26
static struct dirliststruct *rememberdir(struct dirliststruct *dirlist, const char *path) {
27
  struct dirliststruct *res;
27
  struct dirliststruct *res;
28
  /* if already present, do nothing */
28
  /* if already present, do nothing */
29
  for (res = dirlist; res != NULL; res = res->next) {
29
  for (res = dirlist; res != NULL; res = res->next) {
30
    if (strcasecmp(res->dirname, path) == 0) return(dirlist);
30
    if (strcasecmp(res->dirname, path) == 0) return(dirlist);
31
  }
31
  }
32
  /* not in the list yet - add it */
32
  /* not in the list yet - add it */
33
  res = malloc(sizeof(struct dirliststruct) + strlen(path));
33
  res = malloc(sizeof(struct dirliststruct) + strlen(path));
34
  if (res == NULL) {  /* out of memory */
34
  if (res == NULL) {  /* out of memory */
35
    kitten_printf(4, 3, "Out of memory! Could not store directory %s!", path);
35
    kitten_printf(4, 3, path); /* "Out of memory! Could not store directory %s!" */
36
    puts("");
36
    puts("");
37
    return(NULL);
37
    return(NULL);
38
  }
38
  }
39
  strcpy(res->dirname, path);
39
  strcpy(res->dirname, path);
40
  res->next = dirlist;
40
  res->next = dirlist;
41
  return(res);
41
  return(res);
42
}
42
}
43
 
43
 
44
 
44
 
45
/* explode a path into subdirectories, and remember each one inside dirlist */
45
/* explode a path into subdirectories, and remember each one inside dirlist */
46
static struct dirliststruct *rememberpath(struct dirliststruct *dirlist, char *path) {
46
static struct dirliststruct *rememberpath(struct dirliststruct *dirlist, char *path) {
47
  int x, gameover = 0;
47
  int x, gameover = 0;
48
  /* iterate on the path, and add each subdirectory */
48
  /* iterate on the path, and add each subdirectory */
49
  for (x = 0;; x++) {
49
  for (x = 0;; x++) {
50
    switch (path[x]) {
50
    switch (path[x]) {
51
      case 0:
51
      case 0:
52
        gameover = 1;
52
        gameover = 1;
53
      case '/':
53
      case '/':
54
      case '\\':
54
      case '\\':
55
        path[x] = 0;
55
        path[x] = 0;
56
        dirlist = rememberdir(dirlist, path);
56
        dirlist = rememberdir(dirlist, path);
57
        path[x] = '\\';
57
        path[x] = '\\';
58
    }
58
    }
59
    if (gameover != 0) break;
59
    if (gameover != 0) break;
60
  }
60
  }
61
  return(dirlist);
61
  return(dirlist);
62
}
62
}
63
 
63
 
64
 
64
 
65
/* removes a package from the system. Returns 0 on success, non-zero otherwise */
65
/* removes a package from the system. Returns 0 on success, non-zero otherwise */
66
int pkgrem(const char *pkgname, const char *dosdir) {
66
int pkgrem(const char *pkgname, const char *dosdir) {
67
  char fpath[256];
67
  char fpath[256];
68
  char buff[256];
68
  char buff[256];
69
  FILE *flist;
69
  FILE *flist;
70
  int lastdirsep;
70
  int lastdirsep;
71
  struct dirliststruct *dirlist = NULL; /* used to remember directories to remove */
71
  struct dirliststruct *dirlist = NULL; /* used to remember directories to remove */
72
 
72
 
73
  /* open the file %DOSDIR%\packages\pkgname.lst */
73
  /* open the file %DOSDIR%\packages\pkgname.lst */
74
  sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname);
74
  sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname);
75
  flist = fopen(fpath, "rb");
75
  flist = fopen(fpath, "rb");
76
  if (flist == NULL) {
76
  if (flist == NULL) {
77
    kitten_printf(4, 0, "Package %s is not installed, so not removed.", pkgname);
77
    kitten_printf(4, 0, pkgname); /* "Package %s is not installed, so not removed." */
78
    puts("");
78
    puts("");
79
    return(-1);
79
    return(-1);
80
  }
80
  }
81
 
81
 
82
  /* remove all files/folders listed in pkgname.lst but NOT pkgname.lst */
82
  /* remove all files/folders listed in pkgname.lst but NOT pkgname.lst */
83
  while (freadtokval(flist, buff, sizeof(buff), NULL, 0) == 0) {
83
  while (freadtokval(flist, buff, sizeof(buff), NULL, 0) == 0) {
84
    int x;
84
    int x;
85
    slash2backslash(buff); /* change all slash to backslash */
85
    slash2backslash(buff); /* change all slash to backslash */
86
    if (buff[0] == 0) continue; /* skip empty lines */
86
    if (buff[0] == 0) continue; /* skip empty lines */
87
 
87
 
88
    /* remember the path part for removal later */
88
    /* remember the path part for removal later */
89
    lastdirsep = -1;
89
    lastdirsep = -1;
90
    for (x = 1; buff[x] != 0; x++) {
90
    for (x = 1; buff[x] != 0; x++) {
91
      if ((buff[x] == '\\') && (buff[x - 1] != ':')) lastdirsep = x;
91
      if ((buff[x] == '\\') && (buff[x - 1] != ':')) lastdirsep = x;
92
    }
92
    }
93
    if (lastdirsep > 0) {
93
    if (lastdirsep > 0) {
94
      buff[lastdirsep] = 0;
94
      buff[lastdirsep] = 0;
95
      dirlist = rememberpath(dirlist, buff);
95
      dirlist = rememberpath(dirlist, buff);
96
      buff[lastdirsep] = '\\';
96
      buff[lastdirsep] = '\\';
97
    }
97
    }
98
 
98
 
99
    /* if it's a directory, skip it */
99
    /* if it's a directory, skip it */
100
    if (buff[strlen(buff) - 1] == '\\') continue;
100
    if (buff[strlen(buff) - 1] == '\\') continue;
101
 
101
 
102
    /* do not delete pkgname.lst at this point - I am using it (will be deleted later) */
102
    /* do not delete pkgname.lst at this point - I am using it (will be deleted later) */
103
    if (strcasecmp(buff, fpath) == 0) continue;
103
    if (strcasecmp(buff, fpath) == 0) continue;
104
 
104
 
105
    /* remove it */
105
    /* remove it */
106
    kitten_printf(4, 4, "removing %s", buff);
106
    kitten_printf(4, 4, buff); /* "removing %s" */
107
    puts("");
107
    puts("");
108
    unlink(buff);
108
    unlink(buff);
109
  }
109
  }
110
 
110
 
111
  /* close the lst file */
111
  /* close the lst file */
112
  fclose(flist);
112
  fclose(flist);
113
 
113
 
114
  /* iterate over dirlist and remove directories if empty, from longest to shortest */
114
  /* iterate over dirlist and remove directories if empty, from longest to shortest */
115
  while (dirlist != NULL) {
115
  while (dirlist != NULL) {
116
    struct dirliststruct *dirlistpos, *previousdir;
116
    struct dirliststruct *dirlistpos, *previousdir;
117
    /* find the longest path, and put it on top */
117
    /* find the longest path, and put it on top */
118
    previousdir = dirlist;
118
    previousdir = dirlist;
119
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
119
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
120
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
120
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
121
        previousdir->next = dirlistpos->next;
121
        previousdir->next = dirlistpos->next;
122
        dirlistpos->next = dirlist;
122
        dirlistpos->next = dirlist;
123
        dirlist = dirlistpos;
123
        dirlist = dirlistpos;
124
        dirlistpos = previousdir;
124
        dirlistpos = previousdir;
125
      } else {
125
      } else {
126
        previousdir = dirlistpos;
126
        previousdir = dirlistpos;
127
      }
127
      }
128
    }
128
    }
129
    /* printf("RMDIR %s\n", dirlist->dirname); */
129
    /* printf("RMDIR %s\n", dirlist->dirname); */
130
    rmdir(dirlist->dirname);
130
    rmdir(dirlist->dirname);
131
    /* free the allocated memory for this entry */
131
    /* free the allocated memory for this entry */
132
    dirlistpos = dirlist;
132
    dirlistpos = dirlist;
133
    dirlist = dirlistpos->next;
133
    dirlist = dirlistpos->next;
134
    free(dirlistpos);
134
    free(dirlistpos);
135
  }
135
  }
136
 
136
 
137
  /* remove the lst file */
137
  /* remove the lst file */
138
  unlink(fpath);
138
  unlink(fpath);
139
  kitten_printf(4, 5, "Package %s has been removed.", pkgname);
139
  kitten_printf(4, 5, pkgname); /* "Package %s has been removed." */
140
  puts("");
140
  puts("");
141
  return(0);
141
  return(0);
142
}
142
}
143
 
143