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
/*
228 mateuszvis 2
 * This file is part of PKGINST (SvarDOS)
3
 * Copyright (C) 2013-2021 Mateusz Viste. All rights reserved.
219 mateuszvis 4
 */
5
 
6
#include <stdio.h>
7
#include <ctype.h>    /* tolower() */
8
#include <stdlib.h>   /* atoi(), qsort() - not using it after all, redefining it manually later */
9
#include <string.h>   /* strlen() */
10
#include <sys/types.h>
228 mateuszvis 11
#include <direct.h> /* opendir() and friends */
219 mateuszvis 12
 
13
#include "fdnpkg.h"   /* PKGINST_UPDATE */
14
#include "fileexst.h"
15
#include "getdelim.h"
16
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
17
#include "kprintf.h"
18
#include "libunzip.h"  /* zip_freelist()... */
19
#include "lsm.h"
20
#include "pkgdb.h"
21
#include "pkginst.h"
22
#include "pkgrem.h"
23
#include "rtrim.h"
24
#include "showinst.h"  /* include self for control */
25
#include "version.h"
26
 
27
 
28
/* this is a wrapper around strcasecmp(), to be used by qsort() */
29
static int strcompare(const void *str1, const void *str2) {
30
  char **s1 = (char **)str1;
31
  char **s2 = (char **)str2;
32
  return(strcasecmp(*s1, *s2));
33
}
34
 
35
 
36
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, char *filterstr, char *dosdir) {
37
  DIR *dp;
38
  int packagelist_len = 0, x;
39
  struct dirent *ep;
40
  #define verstr_maxlen 64
41
  char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
42
  sprintf(pkgdir, "%s\\packages", dosdir);
43
  dp = opendir(pkgdir);
44
  if (dp != NULL) {
45
    while ((ep = readdir(dp)) != NULL) {
46
      if (ep->d_name[0] != '.') { /* ignore '.', '..', and hidden directories */
47
        if (strlen(ep->d_name) > 4) {
48
          int tlen = strlen(ep->d_name);
49
          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 */
50
 
51
          if (filterstr != NULL) {
52
            if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
53
          }
54
          if (packagelist_len >= packagelist_maxlen) {
55
            closedir(dp);
56
            return(-1); /* if not enough place in the list - return an error */
57
          }
58
          packagelist[packagelist_len] = strdup(ep->d_name);
59
          packagelist[packagelist_len][strlen(packagelist[packagelist_len]) - 4] = 0; /* cut out the .lst extension */
60
          packagelist_len += 1;
61
        }
62
      }
63
    }
64
    closedir(dp);
65
    qsort(packagelist, packagelist_len, sizeof (char **), strcompare); /* sort the package list */
66
    for (x = 0; x < packagelist_len; x++) {
67
      /* for each package, load the metadata from %DOSDIR\APPINFO\*.lsm */
68
      sprintf(lsmfilename, "%s\\appinfo\\%s.lsm", dosdir, packagelist[x]);
69
      if (readlsm(lsmfilename, verstr, verstr_maxlen) != 0) sprintf(verstr, "(unknown version)");
70
      packagelist_ver[x] = strdup(verstr);
71
    }
72
    return(packagelist_len);
73
  } else {
74
    kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
75
    puts("");
76
    return(-1);
77
  }
78
}
79
 
80
#define packagelist_maxlen 1024
81
 
82
void showinstalledpkgs(char *filterstr, char *dosdir) {
83
  char *packagelist[packagelist_maxlen];
84
  char *packagelist_ver[packagelist_maxlen];
85
  int packagelist_len, x;
86
 
87
  /* load the list of packages */
88
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir);   /* Populate the packages list */
89
  if (packagelist_len < 0) return;
90
  if (packagelist_len == 0) {
91
    kitten_puts(5, 0, "No package matched the search.");
92
    return;
93
  }
94
 
95
  /* iterate through all packages */
96
  for (x = 0; x < packagelist_len; x++) {
97
    /* print the package/version couple on screen */
98
    printf("%s %s\n", packagelist[x], packagelist_ver[x]);
99
  }
100
}
101
 
102
 
103
/* frees a linked list of filenames */
104
void pkg_freeflist(struct flist_t *flist) {
105
  while (flist != NULL) {
106
    struct flist_t *victim = flist;
107
    flist = flist->next;
108
    free(victim);
109
  }
110
}
111
 
112
 
113
/* returns a linked list of the files that belong to package pkgname */
114
struct flist_t *pkg_loadflist(char *pkgname, char *dosdir) {
115
  struct flist_t *res = NULL, *newnode;
116
  FILE *fd;
117
  char *lineptr;
118
  char buff[512];
119
  int getdelimlen, fnamelen;
120
  size_t getdelimcount = 0;
121
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
122
  if (fileexists(buff) == 0) { /* file does not exist */
123
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
124
    puts("");
125
    return(NULL);
126
  }
127
  fd = fopen(buff, "rb");
128
  if (fd == NULL) {
129
    kitten_puts(4, 1, "Error opening lst file!");
130
    return(NULL);
131
  }
132
  /* iterate through all lines of the file */
133
  for (;;) {
134
    lineptr = NULL;
135
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
136
    if (getdelimlen < 0) { /* EOF */
137
      free(lineptr);
138
      break;
139
    }
140
    rtrim(lineptr);  /* right-trim the filename */
141
    slash2backslash(lineptr); /* change all / to \ */
142
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
143
    if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
144
    if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
145
      fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
146
    } else { /* else it's a relative path starting at %dosdir% */
147
      fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
148
    }
149
    free(lineptr); /* free the memory occupied by the line */
150
    /* add the new node to the result */
151
    newnode = malloc(sizeof(struct flist_t) + fnamelen);
152
    if (newnode == NULL) {
153
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
154
      continue;
155
    }
156
    newnode->next = res;
157
    strcpy(newnode->fname, buff);
158
    res = newnode;
159
  }
160
  fclose(fd);
161
  return(res);
162
}
163
 
164
 
165
/* Prints files owned by a package */
166
void listfilesofpkg(char *pkgname, char *dosdir) {
167
  struct flist_t *flist, *flist_ptr;
168
  /* load the list of files belonging to pkgname */
169
  flist = pkg_loadflist(pkgname, dosdir);
170
  /* display each filename on screen */
171
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
172
    printf("%s\n", flist_ptr->fname);
173
  }
174
  /* free the list of files */
175
  pkg_freeflist(flist);
176
}