Subversion Repositories SvarDOS

Rev

Rev 243 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 243 Rev 250
Line 9... Line 9...
9
#include <string.h>   /* strlen() */
9
#include <string.h>   /* strlen() */
10
#include <sys/types.h>
10
#include <sys/types.h>
11
#include <direct.h> /* opendir() and friends */
11
#include <direct.h> /* opendir() and friends */
12
 
12
 
13
#include "fdnpkg.h"
13
#include "fdnpkg.h"
14
#include "fileexst.h"
-
 
15
#include "getdelim.h"
14
#include "getdelim.h"
16
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
15
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
17
#include "kprintf.h"
16
#include "kprintf.h"
18
#include "libunzip.h"  /* zip_freelist()... */
17
#include "libunzip.h"  /* zip_freelist()... */
19
#include "lsm.h"
18
#include "lsm.h"
20
#include "pkginst.h"
-
 
21
#include "pkgrem.h"
-
 
22
#include "rtrim.h"
19
#include "rtrim.h"
23
#include "showinst.h"  /* include self for control */
-
 
24
#include "version.h"
-
 
25
 
-
 
26
 
20
 
27
/* this is a wrapper around strcasecmp(), to be used by qsort() */
-
 
28
static int strcompare(const void *str1, const void *str2) {
21
#include "showinst.h"  /* include self for control */
29
  char **s1 = (char **)str1;
-
 
30
  char **s2 = (char **)str2;
-
 
31
  return(strcasecmp(*s1, *s2));
-
 
32
}
-
 
33
 
22
 
34
 
23
 
35
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, const char *filterstr, const char *dosdir) {
24
int showinstalledpkgs(const char *filterstr, const char *dosdir) {
36
  DIR *dp;
25
  DIR *dp;
37
  int packagelist_len = 0, x;
-
 
38
  struct dirent *ep;
26
  struct dirent *ep;
39
  #define verstr_maxlen 64
27
  char buff[256];
40
  char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
-
 
41
  sprintf(pkgdir, "%s\\packages", dosdir);
-
 
42
  dp = opendir(pkgdir);
28
  char ver[16];
43
  if (dp != NULL) {
29
  int matchfound = 0;
44
    while ((ep = readdir(dp)) != NULL) {
-
 
45
      if (ep->d_name[0] != '.') { /* ignore '.', '..', and hidden directories */
-
 
46
        if (strlen(ep->d_name) > 4) {
-
 
47
          int tlen = strlen(ep->d_name);
-
 
48
          if ((ep->d_name[tlen - 4] != '.') || (tolower(ep->d_name[tlen - 3]) != 'l') || (tolower(ep->d_name[tlen - 2]) != 's') || (tolower(ep->d_name[tlen - 1]) != 't')) continue;  /* if it's not an .lst file, skip it silently */
-
 
49
 
30
 
50
          if (filterstr != NULL) {
-
 
51
            if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
-
 
52
          }
-
 
53
          if (packagelist_len >= packagelist_maxlen) {
-
 
54
            closedir(dp);
-
 
55
            return(-1); /* if not enough place in the list - return an error */
-
 
56
          }
-
 
57
          packagelist[packagelist_len] = strdup(ep->d_name);
-
 
58
          packagelist[packagelist_len][strlen(packagelist[packagelist_len]) - 4] = 0; /* cut out the .lst extension */
-
 
59
          packagelist_len += 1;
31
  sprintf(buff, "%s\\packages", dosdir);
60
        }
-
 
61
      }
-
 
62
    }
-
 
63
    closedir(dp);
32
  dp = opendir(buff);
64
    qsort(packagelist, packagelist_len, sizeof (char **), strcompare); /* sort the package list */
-
 
65
    for (x = 0; x < packagelist_len; x++) {
-
 
66
      /* for each package, load the metadata from %DOSDIR\APPINFO\*.lsm */
-
 
67
      sprintf(lsmfilename, "%s\\appinfo\\%s.lsm", dosdir, packagelist[x]);
-
 
68
      if (readlsm(lsmfilename, verstr, verstr_maxlen) != 0) sprintf(verstr, "(unknown version)");
-
 
69
      packagelist_ver[x] = strdup(verstr);
-
 
70
    }
-
 
71
    return(packagelist_len);
-
 
72
  } else {
33
  if (dp == NULL) {
73
    kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
34
    kitten_printf(9, 0, "Error: Could not access directory %s", buff);
74
    puts("");
35
    puts("");
75
    return(-1);
36
    return(-1);
76
  }
37
  }
77
}
-
 
78
 
38
 
-
 
39
  while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
79
#define packagelist_maxlen 1024
40
    int tlen = strlen(ep->d_name);
-
 
41
    if (ep->d_name[0] == '.') continue; /* ignore '.', '..', and hidden directories */
-
 
42
    if (tlen < 4) continue; /* files must be at least 5 bytes long ("x.lst") */
-
 
43
    if (strcasecmp(ep->d_name + tlen - 4, ".lst") != 0) continue;  /* if not an .lst file, skip it silently */
-
 
44
    ep->d_name[tlen - 4] = 0; /* trim out the ".lst" suffix */
80
 
45
 
81
void showinstalledpkgs(const char *filterstr, const char *dosdir) {
-
 
82
  char *packagelist[packagelist_maxlen];
-
 
83
  char *packagelist_ver[packagelist_maxlen];
-
 
84
  int packagelist_len, x;
-
 
85
 
-
 
86
  /* load the list of packages */
46
    if (filterstr != NULL) {
87
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir);   /* Populate the packages list */
47
      if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
88
  if (packagelist_len < 0) return;
-
 
89
  if (packagelist_len == 0) {
-
 
90
    kitten_puts(5, 0, "No package matched the search.");
-
 
91
    return;
-
 
92
  }
48
    }
93
 
49
 
94
  /* iterate through all packages */
50
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
95
  for (x = 0; x < packagelist_len; x++) {
51
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
96
    /* print the package/version couple on screen */
52
    readlsm(buff, ver, sizeof(ver));
-
 
53
 
97
    printf("%s %s\n", packagelist[x], packagelist_ver[x]);
54
    printf("%s %s", ep->d_name, ver);
-
 
55
    puts("");
-
 
56
    matchfound = 1;
98
  }
57
  }
-
 
58
  if (matchfound == 0) kitten_puts(5, 0, "No package matched the search.");
-
 
59
 
-
 
60
  closedir(dp);
-
 
61
  return(0);
99
}
62
}
100
 
63
 
101
 
64
 
102
/* frees a linked list of filenames */
65
/* frees a linked list of filenames */
103
void pkg_freeflist(struct flist_t *flist) {
66
void pkg_freeflist(struct flist_t *flist) {
Line 112... Line 75...
112
/* returns a linked list of the files that belong to package pkgname */
75
/* returns a linked list of the files that belong to package pkgname */
113
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
76
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
114
  struct flist_t *res = NULL, *newnode;
77
  struct flist_t *res = NULL, *newnode;
115
  FILE *fd;
78
  FILE *fd;
116
  char *lineptr;
79
  char *lineptr;
117
  char buff[512];
80
  char buff[256];
118
  int getdelimlen, fnamelen;
81
  int getdelimlen, fnamelen;
119
  size_t getdelimcount = 0;
82
  size_t getdelimcount = 0;
120
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
83
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
121
  if (fileexists(buff) == 0) { /* file does not exist */
-
 
122
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
-
 
123
    puts("");
-
 
124
    return(NULL);
-
 
125
  }
-
 
126
  fd = fopen(buff, "rb");
84
  fd = fopen(buff, "rb");
127
  if (fd == NULL) {
85
  if (fd == NULL) {
128
    kitten_puts(4, 1, "Error opening lst file!");
86
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
-
 
87
    puts("");
129
    return(NULL);
88
    return(NULL);
130
  }
89
  }
131
  /* iterate through all lines of the file */
90
  /* iterate through all lines of the file */
132
  for (;;) {
91
  for (;;) {
133
    lineptr = NULL;
92
    lineptr = NULL;