Subversion Repositories SvarDOS

Rev

Rev 219 | Go to most recent revision | Details | Compare with Previous | 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 */
225 mateuszvis 72
int pkgrem(char *pkgname, char *dosdir) {
219 mateuszvis 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
  if (fileexists(fpath) == 0) { /* file does not exist */
87
    kitten_printf(4, 0, "Package %s is not installed, so not removed.", pkgname);
88
    puts("");
89
    return(-1);
90
  }
91
 
92
  /* open the file %DOSDIR%\packages\pkgname.lst */
93
  flist = fopen(fpath, "r");
94
  if (flist == NULL) {
95
    kitten_puts(4, 1, "Error opening lst file!");
96
    return(-2);
97
  }
98
 
99
  sprintf(pkglistfile, "packages\\%s.lst", pkgname);
100
 
101
  /* remove all files/folders listed in pkgname.lst but NOT pkgname.lst */
102
  for (;;) {
103
    /* read line from file */
104
    lineptr = NULL;
105
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', flist);
106
    if (getdelimlen < 0) {
107
      free(lineptr);
108
      break;
109
    }
110
    rtrim(lineptr);  /* right-trim the filename */
111
    slash2backslash(lineptr); /* change all / to \ */
112
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) {
113
      free(lineptr); /* free the memory occupied by the line */
114
      continue; /* skip empty lines */
115
    }
116
    /* remember the path part for removal later */
117
    lastdirsep = -1;
118
    for (x = 1; lineptr[x] != 0; x++) {
119
      if ((lineptr[x] == '\\') && (lineptr[x - 1] != ':')) lastdirsep = x;
120
    }
121
    if (lastdirsep > 0) {
122
      lineptr[lastdirsep] = 0;
123
      dirlist = rememberpath(dirlist, lineptr);
124
      lineptr[lastdirsep] = '\\';
125
    }
126
    /* if it's a directory, skip it */
127
    if (lineptr[strlen(lineptr) - 1] == '\\') {
128
      free(lineptr); /* free the memory occupied by the line */
129
      continue;
130
    }
131
    /* it's a file - remove it */
132
    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 */
134
        sprintf(shellcmd, "%s", lineptr);
135
      } else { /* else it's a relative path starting at %dosdir% */
136
        sprintf(shellcmd, "%s\\%s", dosdir, lineptr);
137
      }
138
      kitten_printf(4, 4, "removing %s", shellcmd);
139
      puts("");
140
      unlink(shellcmd);
141
    }
142
    free(lineptr); /* free the memory occupied by the line */
143
  }
144
 
145
  /* close the file */
146
  fclose(flist);
147
 
148
  /* iterate through dirlist and remove directories if empty, from longest to shortest */
149
  while (dirlist != NULL) {
150
    struct dirliststruct *dirlistpos, *previousdir;
151
    /* find the longest path, and put it on top */
152
    previousdir = dirlist;
153
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
154
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
155
        previousdir->next = dirlistpos->next;
156
        dirlistpos->next = dirlist;
157
        dirlist = dirlistpos;
158
        dirlistpos = previousdir;
159
      } else {
160
        previousdir = dirlistpos;
161
      }
162
    }
163
    if ((dirlist->dirname[0] == '\\') || (dirlist->dirname[1] == ':')) { /* this is an absolute path */
164
      sprintf(shellcmd, "%s", dirlist->dirname);
165
    } else { /* else it's a relative path starting at %dosdir% */
166
      sprintf(shellcmd, "%s\\%s", dosdir, dirlist->dirname);
167
    }
168
    /* printf("RMDIR %s\n", shellcmd); */
169
    rmdir(shellcmd);
170
    /* free the allocated memory for this entry */
171
    dirlistpos = dirlist;
172
    dirlist = dirlistpos->next;
173
    free(dirlistpos);
174
  }
175
 
176
  /* remove %DOSDIR%\packages\pkgname.lst */
177
  unlink(fpath);
178
  kitten_printf(4, 5, "Package %s has been removed.", pkgname);
179
  puts("");
180
  return(0);
181
}