Subversion Repositories SvarDOS

Rev

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