Subversion Repositories SvarDOS

Rev

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

Rev 225 Rev 235
1
/*
1
/*
2
 * This file is part of the FDNPKG project.
2
 * This file is part of the FDNPKG project.
3
 * Copyright (C) Mateusz Viste 2012-2016. All rights reserved.
3
 * Copyright (C) Mateusz Viste 2012-2016. All rights reserved.
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
 
-
 
12
#ifdef __WATCOMC__
-
 
13
#include <direct.h>  /* watcom needs this for the rmdir() prototype */
11
#include <direct.h>  /* watcom needs this for the rmdir() prototype */
14
#endif
-
 
15
 
12
 
16
#include "fileexst.h"
13
#include "fileexst.h"
17
#include "getdelim.h"
14
#include "getdelim.h"
18
#include "helpers.h"   /* slash2backslash() */
15
#include "helpers.h"   /* slash2backslash() */
19
#include "kprintf.h"
16
#include "kprintf.h"
20
#include "pkgrem.h"
17
#include "pkgrem.h"
21
#include "rtrim.h"
18
#include "rtrim.h"
22
#include "version.h"
19
#include "version.h"
23
 
20
 
24
 
21
 
25
struct dirliststruct {
22
struct dirliststruct {
26
  struct dirliststruct *next;
23
  struct dirliststruct *next;
27
  char dirname[2]; /* this must be the last item in the structure */
24
  char dirname[2]; /* this must be the last item in the structure */
28
};
25
};
29
 
26
 
30
 
27
 
31
/* adds a directory to dirlist, if not already present */
28
/* adds a directory to dirlist, if not already present */
32
static struct dirliststruct *rememberdir(struct dirliststruct *dirlist, char *path) {
29
static struct dirliststruct *rememberdir(struct dirliststruct *dirlist, const char *path) {
33
  struct dirliststruct *res;
30
  struct dirliststruct *res;
34
  /* if already present, do nothing */
31
  /* if already present, do nothing */
35
  for (res = dirlist; res != NULL; res = res->next) {
32
  for (res = dirlist; res != NULL; res = res->next) {
36
    if (strcasecmp(res->dirname, path) == 0) return(dirlist);
33
    if (strcasecmp(res->dirname, path) == 0) return(dirlist);
37
  }
34
  }
38
  /* not in the list yet - add it */
35
  /* not in the list yet - add it */
39
  res = malloc(sizeof(struct dirliststruct) + strlen(path));
36
  res = malloc(sizeof(struct dirliststruct) + strlen(path));
40
  if (res == NULL) {  /* out of memory */
37
  if (res == NULL) {  /* out of memory */
41
    kitten_printf(4, 3, "Out of memory! Could not store directory %s!", path);
38
    kitten_printf(4, 3, "Out of memory! Could not store directory %s!", path);
42
    puts("");
39
    puts("");
43
    return(NULL);
40
    return(NULL);
44
  }
41
  }
45
  strcpy(res->dirname, path);
42
  strcpy(res->dirname, path);
46
  res->next = dirlist;
43
  res->next = dirlist;
47
  return(res);
44
  return(res);
48
}
45
}
49
 
46
 
50
 
47
 
51
/* explode a path into subdirectories, and remember each one inside dirlist */
48
/* explode a path into subdirectories, and remember each one inside dirlist */
52
static struct dirliststruct *rememberpath(struct dirliststruct *dirlist, char *path) {
49
static struct dirliststruct *rememberpath(struct dirliststruct *dirlist, char *path) {
53
  int x, gameover = 0;
50
  int x, gameover = 0;
54
  /* iterate on the path, and add each subdirectory */
51
  /* iterate on the path, and add each subdirectory */
55
  for (x = 0;; x++) {
52
  for (x = 0;; x++) {
56
    switch (path[x]) {
53
    switch (path[x]) {
57
      case 0:
54
      case 0:
58
        gameover = 1;
55
        gameover = 1;
59
      case '/':
56
      case '/':
60
      case '\\':
57
      case '\\':
61
        path[x] = 0;
58
        path[x] = 0;
62
        dirlist = rememberdir(dirlist, path);
59
        dirlist = rememberdir(dirlist, path);
63
        path[x] = '\\';
60
        path[x] = '\\';
64
    }
61
    }
65
    if (gameover != 0) break;
62
    if (gameover != 0) break;
66
  }
63
  }
67
  return(dirlist);
64
  return(dirlist);
68
}
65
}
69
 
66
 
70
 
67
 
71
/* removes a package from the system. Returns 0 on success, non-zero otherwise */
68
/* removes a package from the system. Returns 0 on success, non-zero otherwise */
72
int pkgrem(char *pkgname, char *dosdir) {
69
int pkgrem(const char *pkgname, const char *dosdir) {
73
  char fpath[512];
70
  char fpath[256];
74
  char shellcmd[512];
71
  char shellcmd[256];
75
  char *lineptr;
72
  char *lineptr;
76
  FILE *flist;
73
  FILE *flist;
77
  int getdelimlen;
74
  int getdelimlen;
78
  int lastdirsep;
75
  int lastdirsep;
79
  int x;
76
  int x;
80
  size_t getdelimcount = 0;
77
  size_t getdelimcount = 0;
81
  struct dirliststruct *dirlist = NULL; /* used to remember directories to remove */
78
  struct dirliststruct *dirlist = NULL; /* used to remember directories to remove */
82
  char pkglistfile[512];
79
  char pkglistfile[256];
83
 
80
 
84
  /* Check if the file %DOSDIR%\packages\pkgname.lst exists (if not, the package is not installed) */
81
  /* Check if the file %DOSDIR%\packages\pkgname.lst exists (if not, the package is not installed) */
85
  sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname);
82
  sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname);
86
  if (fileexists(fpath) == 0) { /* file does not exist */
83
  if (fileexists(fpath) == 0) { /* file does not exist */
87
    kitten_printf(4, 0, "Package %s is not installed, so not removed.", pkgname);
84
    kitten_printf(4, 0, "Package %s is not installed, so not removed.", pkgname);
88
    puts("");
85
    puts("");
89
    return(-1);
86
    return(-1);
90
  }
87
  }
91
 
88
 
92
  /* open the file %DOSDIR%\packages\pkgname.lst */
89
  /* open the file %DOSDIR%\packages\pkgname.lst */
93
  flist = fopen(fpath, "r");
90
  flist = fopen(fpath, "r");
94
  if (flist == NULL) {
91
  if (flist == NULL) {
95
    kitten_puts(4, 1, "Error opening lst file!");
92
    kitten_puts(4, 1, "Error opening lst file!");
96
    return(-2);
93
    return(-2);
97
  }
94
  }
98
 
95
 
99
  sprintf(pkglistfile, "packages\\%s.lst", pkgname);
96
  sprintf(pkglistfile, "packages\\%s.lst", pkgname);
100
 
97
 
101
  /* remove all files/folders listed in pkgname.lst but NOT pkgname.lst */
98
  /* remove all files/folders listed in pkgname.lst but NOT pkgname.lst */
102
  for (;;) {
99
  for (;;) {
103
    /* read line from file */
100
    /* read line from file */
104
    lineptr = NULL;
101
    lineptr = NULL;
105
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', flist);
102
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', flist);
106
    if (getdelimlen < 0) {
103
    if (getdelimlen < 0) {
107
      free(lineptr);
104
      free(lineptr);
108
      break;
105
      break;
109
    }
106
    }
110
    rtrim(lineptr);  /* right-trim the filename */
107
    rtrim(lineptr);  /* right-trim the filename */
111
    slash2backslash(lineptr); /* change all / to \ */
108
    slash2backslash(lineptr); /* change all / to \ */
112
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) {
109
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) {
113
      free(lineptr); /* free the memory occupied by the line */
110
      free(lineptr); /* free the memory occupied by the line */
114
      continue; /* skip empty lines */
111
      continue; /* skip empty lines */
115
    }
112
    }
116
    /* remember the path part for removal later */
113
    /* remember the path part for removal later */
117
    lastdirsep = -1;
114
    lastdirsep = -1;
118
    for (x = 1; lineptr[x] != 0; x++) {
115
    for (x = 1; lineptr[x] != 0; x++) {
119
      if ((lineptr[x] == '\\') && (lineptr[x - 1] != ':')) lastdirsep = x;
116
      if ((lineptr[x] == '\\') && (lineptr[x - 1] != ':')) lastdirsep = x;
120
    }
117
    }
121
    if (lastdirsep > 0) {
118
    if (lastdirsep > 0) {
122
      lineptr[lastdirsep] = 0;
119
      lineptr[lastdirsep] = 0;
123
      dirlist = rememberpath(dirlist, lineptr);
120
      dirlist = rememberpath(dirlist, lineptr);
124
      lineptr[lastdirsep] = '\\';
121
      lineptr[lastdirsep] = '\\';
125
    }
122
    }
126
    /* if it's a directory, skip it */
123
    /* if it's a directory, skip it */
127
    if (lineptr[strlen(lineptr) - 1] == '\\') {
124
    if (lineptr[strlen(lineptr) - 1] == '\\') {
128
      free(lineptr); /* free the memory occupied by the line */
125
      free(lineptr); /* free the memory occupied by the line */
129
      continue;
126
      continue;
130
    }
127
    }
131
    /* it's a file - remove it */
128
    /* it's a file - remove it */
132
    if (strcasecmp(pkglistfile, lineptr) != 0) { /* never delete pkgname.lst at this point - it will be deleted later */
129
    if (strcasecmp(pkglistfile, lineptr) != 0) { /* never delete pkgname.lst at this point - it will be deleted later */
133
      if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
130
      if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
134
        sprintf(shellcmd, "%s", lineptr);
131
        sprintf(shellcmd, "%s", lineptr);
135
      } else { /* else it's a relative path starting at %dosdir% */
132
      } else { /* else it's a relative path starting at %dosdir% */
136
        sprintf(shellcmd, "%s\\%s", dosdir, lineptr);
133
        sprintf(shellcmd, "%s\\%s", dosdir, lineptr);
137
      }
134
      }
138
      kitten_printf(4, 4, "removing %s", shellcmd);
135
      kitten_printf(4, 4, "removing %s", shellcmd);
139
      puts("");
136
      puts("");
140
      unlink(shellcmd);
137
      unlink(shellcmd);
141
    }
138
    }
142
    free(lineptr); /* free the memory occupied by the line */
139
    free(lineptr); /* free the memory occupied by the line */
143
  }
140
  }
144
 
141
 
145
  /* close the file */
142
  /* close the file */
146
  fclose(flist);
143
  fclose(flist);
147
 
144
 
148
  /* iterate through dirlist and remove directories if empty, from longest to shortest */
145
  /* iterate through dirlist and remove directories if empty, from longest to shortest */
149
  while (dirlist != NULL) {
146
  while (dirlist != NULL) {
150
    struct dirliststruct *dirlistpos, *previousdir;
147
    struct dirliststruct *dirlistpos, *previousdir;
151
    /* find the longest path, and put it on top */
148
    /* find the longest path, and put it on top */
152
    previousdir = dirlist;
149
    previousdir = dirlist;
153
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
150
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
154
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
151
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
155
        previousdir->next = dirlistpos->next;
152
        previousdir->next = dirlistpos->next;
156
        dirlistpos->next = dirlist;
153
        dirlistpos->next = dirlist;
157
        dirlist = dirlistpos;
154
        dirlist = dirlistpos;
158
        dirlistpos = previousdir;
155
        dirlistpos = previousdir;
159
      } else {
156
      } else {
160
        previousdir = dirlistpos;
157
        previousdir = dirlistpos;
161
      }
158
      }
162
    }
159
    }
163
    if ((dirlist->dirname[0] == '\\') || (dirlist->dirname[1] == ':')) { /* this is an absolute path */
160
    if ((dirlist->dirname[0] == '\\') || (dirlist->dirname[1] == ':')) { /* this is an absolute path */
164
      sprintf(shellcmd, "%s", dirlist->dirname);
161
      sprintf(shellcmd, "%s", dirlist->dirname);
165
    } else { /* else it's a relative path starting at %dosdir% */
162
    } else { /* else it's a relative path starting at %dosdir% */
166
      sprintf(shellcmd, "%s\\%s", dosdir, dirlist->dirname);
163
      sprintf(shellcmd, "%s\\%s", dosdir, dirlist->dirname);
167
    }
164
    }
168
    /* printf("RMDIR %s\n", shellcmd); */
165
    /* printf("RMDIR %s\n", shellcmd); */
169
    rmdir(shellcmd);
166
    rmdir(shellcmd);
170
    /* free the allocated memory for this entry */
167
    /* free the allocated memory for this entry */
171
    dirlistpos = dirlist;
168
    dirlistpos = dirlist;
172
    dirlist = dirlistpos->next;
169
    dirlist = dirlistpos->next;
173
    free(dirlistpos);
170
    free(dirlistpos);
174
  }
171
  }
175
 
172
 
176
  /* remove %DOSDIR%\packages\pkgname.lst */
173
  /* remove %DOSDIR%\packages\pkgname.lst */
177
  unlink(fpath);
174
  unlink(fpath);
178
  kitten_printf(4, 5, "Package %s has been removed.", pkgname);
175
  kitten_printf(4, 5, "Package %s has been removed.", pkgname);
179
  puts("");
176
  puts("");
180
  return(0);
177
  return(0);
181
}
178
}
182
 
179