Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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