Subversion Repositories SvarDOS

Rev

Rev 1963 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of the pkg (SvarDOS) project.
1678 mateusz.vi 3
 * Copyright (C) Mateusz Viste 2012-2024
219 mateuszvis 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 "helpers.h"   /* slash2backslash() */
1965 mateusz.vi 14
#include "svarlang.lib\svarlang.h"
253 mateuszvis 15
 
219 mateuszvis 16
#include "pkgrem.h"
17
 
18
 
19
struct dirliststruct {
20
  struct dirliststruct *next;
21
  char dirname[2]; /* this must be the last item in the structure */
22
};
23
 
24
 
25
/* adds a directory to dirlist, if not already present */
235 mateuszvis 26
static struct dirliststruct *rememberdir(struct dirliststruct *dirlist, const char *path) {
219 mateuszvis 27
  struct dirliststruct *res;
28
  /* if already present, do nothing */
29
  for (res = dirlist; res != NULL; res = res->next) {
30
    if (strcasecmp(res->dirname, path) == 0) return(dirlist);
31
  }
32
  /* not in the list yet - add it */
33
  res = malloc(sizeof(struct dirliststruct) + strlen(path));
34
  if (res == NULL) {  /* out of memory */
1965 mateusz.vi 35
    outputnl(svarlang_str(2,14)); /* "Out of memory!" */
219 mateuszvis 36
    return(NULL);
37
  }
38
  strcpy(res->dirname, path);
39
  res->next = dirlist;
40
  return(res);
41
}
42
 
43
 
44
/* explode a path into subdirectories, and remember each one inside dirlist */
45
static struct dirliststruct *rememberpath(struct dirliststruct *dirlist, char *path) {
46
  int x, gameover = 0;
47
  /* iterate on the path, and add each subdirectory */
48
  for (x = 0;; x++) {
49
    switch (path[x]) {
50
      case 0:
51
        gameover = 1;
52
      case '/':
53
      case '\\':
54
        path[x] = 0;
55
        dirlist = rememberdir(dirlist, path);
56
        path[x] = '\\';
57
    }
58
    if (gameover != 0) break;
59
  }
60
  return(dirlist);
61
}
62
 
63
 
64
/* removes a package from the system. Returns 0 on success, non-zero otherwise */
235 mateuszvis 65
int pkgrem(const char *pkgname, const char *dosdir) {
66
  char fpath[256];
253 mateuszvis 67
  char buff[256];
219 mateuszvis 68
  FILE *flist;
69
  int lastdirsep;
70
  struct dirliststruct *dirlist = NULL; /* used to remember directories to remove */
71
 
1678 mateusz.vi 72
  /* open the (legacy) listing file at %DOSDIR%\packages\pkgname.lst
73
   * if not exists then fall back to appinfo\pkgname.lsm */
219 mateuszvis 74
  sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname);
253 mateuszvis 75
  flist = fopen(fpath, "rb");
76
  if (flist == NULL) {
1678 mateusz.vi 77
    sprintf(fpath, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
78
    flist = fopen(fpath, "rb");
79
    if (flist == NULL) {
1965 mateusz.vi 80
      /* "Package %s is not installed, so not removed" */
81
      sprintf(buff, svarlang_str(4,0), pkgname);
82
      outputnl(buff);
1678 mateusz.vi 83
      return(-1);
84
    }
219 mateuszvis 85
  }
86
 
1678 mateusz.vi 87
  /* remove all files/folders listed in pkgname.lsm but NOT pkgname.lsm */
253 mateuszvis 88
  while (freadtokval(flist, buff, sizeof(buff), NULL, 0) == 0) {
89
    int x;
219 mateuszvis 90
 
1678 mateusz.vi 91
    /* skip empty lines */
92
    if (buff[0] == 0) continue;
93
 
94
    /* change all slash to backslash */
95
    slash2backslash(buff);
96
 
97
    /* skip garbage */
98
    if ((buff[1] != ':') || (buff[2] != '\\')) continue;
99
 
100
    /* trim out CRC information (if present) */
101
    trimfnamecrc(buff);
102
 
219 mateuszvis 103
    /* remember the path part for removal later */
104
    lastdirsep = -1;
253 mateuszvis 105
    for (x = 1; buff[x] != 0; x++) {
106
      if ((buff[x] == '\\') && (buff[x - 1] != ':')) lastdirsep = x;
219 mateuszvis 107
    }
108
    if (lastdirsep > 0) {
253 mateuszvis 109
      buff[lastdirsep] = 0;
110
      dirlist = rememberpath(dirlist, buff);
111
      buff[lastdirsep] = '\\';
219 mateuszvis 112
    }
253 mateuszvis 113
 
219 mateuszvis 114
    /* if it's a directory, skip it */
253 mateuszvis 115
    if (buff[strlen(buff) - 1] == '\\') continue;
116
 
1678 mateusz.vi 117
    /* do not delete pkgname.lst at this point because I am using it (will be
118
     * deleted later) */
253 mateuszvis 119
    if (strcasecmp(buff, fpath) == 0) continue;
120
 
121
    /* remove it */
1965 mateusz.vi 122
    output(svarlang_str(4,4)); /* removing */
123
    output(" ");
124
    outputnl(buff);
253 mateuszvis 125
    unlink(buff);
219 mateuszvis 126
  }
127
 
1678 mateusz.vi 128
  /* close the lsm file */
219 mateuszvis 129
  fclose(flist);
130
 
253 mateuszvis 131
  /* iterate over dirlist and remove directories if empty, from longest to shortest */
219 mateuszvis 132
  while (dirlist != NULL) {
133
    struct dirliststruct *dirlistpos, *previousdir;
134
    /* find the longest path, and put it on top */
135
    previousdir = dirlist;
136
    for (dirlistpos = dirlist->next; dirlistpos != NULL; dirlistpos = dirlistpos->next) {
137
      if (strlen(dirlistpos->dirname) > strlen(dirlist->dirname)) {
138
        previousdir->next = dirlistpos->next;
139
        dirlistpos->next = dirlist;
140
        dirlist = dirlistpos;
141
        dirlistpos = previousdir;
142
      } else {
143
        previousdir = dirlistpos;
144
      }
145
    }
253 mateuszvis 146
    /* printf("RMDIR %s\n", dirlist->dirname); */
147
    rmdir(dirlist->dirname);
219 mateuszvis 148
    /* free the allocated memory for this entry */
149
    dirlistpos = dirlist;
150
    dirlist = dirlistpos->next;
151
    free(dirlistpos);
152
  }
153
 
253 mateuszvis 154
  /* remove the lst file */
1965 mateusz.vi 155
  output(svarlang_str(4,4)); /* "removing" */
156
  output(" ");
157
  outputnl(fpath);
219 mateuszvis 158
  unlink(fpath);
1678 mateusz.vi 159
 
1965 mateusz.vi 160
  sprintf(buff, svarlang_str(4,5), pkgname); /* "Package %s has been removed." */
161
  outputnl(buff);
219 mateuszvis 162
  return(0);
163
}