Subversion Repositories SvarDOS

Rev

Rev 234 | Rev 243 | 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
 
232 mateuszvis 13
#include "fdnpkg.h"
219 mateuszvis 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 "pkginst.h"
21
#include "pkgrem.h"
22
#include "rtrim.h"
23
#include "showinst.h"  /* include self for control */
24
#include "version.h"
25
 
26
 
27
/* this is a wrapper around strcasecmp(), to be used by qsort() */
28
static int strcompare(const void *str1, const void *str2) {
29
  char **s1 = (char **)str1;
30
  char **s2 = (char **)str2;
31
  return(strcasecmp(*s1, *s2));
32
}
33
 
34
 
232 mateuszvis 35
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, char *filterstr, const char *dosdir) {
219 mateuszvis 36
  DIR *dp;
37
  int packagelist_len = 0, x;
38
  struct dirent *ep;
39
  #define verstr_maxlen 64
40
  char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
41
  sprintf(pkgdir, "%s\\packages", dosdir);
42
  dp = opendir(pkgdir);
43
  if (dp != NULL) {
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
 
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;
60
        }
61
      }
62
    }
63
    closedir(dp);
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 {
73
    kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
74
    puts("");
75
    return(-1);
76
  }
77
}
78
 
79
#define packagelist_maxlen 1024
80
 
232 mateuszvis 81
void showinstalledpkgs(char *filterstr, const char *dosdir) {
219 mateuszvis 82
  char *packagelist[packagelist_maxlen];
83
  char *packagelist_ver[packagelist_maxlen];
84
  int packagelist_len, x;
85
 
86
  /* load the list of packages */
87
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir);   /* Populate the packages list */
88
  if (packagelist_len < 0) return;
89
  if (packagelist_len == 0) {
90
    kitten_puts(5, 0, "No package matched the search.");
91
    return;
92
  }
93
 
94
  /* iterate through all packages */
95
  for (x = 0; x < packagelist_len; x++) {
96
    /* print the package/version couple on screen */
97
    printf("%s %s\n", packagelist[x], packagelist_ver[x]);
98
  }
99
}
100
 
101
 
102
/* frees a linked list of filenames */
103
void pkg_freeflist(struct flist_t *flist) {
104
  while (flist != NULL) {
105
    struct flist_t *victim = flist;
106
    flist = flist->next;
107
    free(victim);
108
  }
109
}
110
 
111
 
112
/* returns a linked list of the files that belong to package pkgname */
232 mateuszvis 113
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
219 mateuszvis 114
  struct flist_t *res = NULL, *newnode;
115
  FILE *fd;
116
  char *lineptr;
117
  char buff[512];
118
  int getdelimlen, fnamelen;
119
  size_t getdelimcount = 0;
120
  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");
127
  if (fd == NULL) {
128
    kitten_puts(4, 1, "Error opening lst file!");
129
    return(NULL);
130
  }
131
  /* iterate through all lines of the file */
132
  for (;;) {
133
    lineptr = NULL;
134
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
135
    if (getdelimlen < 0) { /* EOF */
136
      free(lineptr);
137
      break;
138
    }
139
    rtrim(lineptr);  /* right-trim the filename */
140
    slash2backslash(lineptr); /* change all / to \ */
141
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
142
    if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
143
    if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
144
      fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
145
    } else { /* else it's a relative path starting at %dosdir% */
146
      fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
147
    }
148
    free(lineptr); /* free the memory occupied by the line */
149
    /* add the new node to the result */
150
    newnode = malloc(sizeof(struct flist_t) + fnamelen);
151
    if (newnode == NULL) {
152
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
153
      continue;
154
    }
155
    newnode->next = res;
156
    strcpy(newnode->fname, buff);
157
    res = newnode;
158
  }
159
  fclose(fd);
160
  return(res);
161
}
162
 
163
 
164
/* Prints files owned by a package */
242 mateuszvis 165
int listfilesofpkg(const char *pkgname, const char *dosdir) {
219 mateuszvis 166
  struct flist_t *flist, *flist_ptr;
167
  /* load the list of files belonging to pkgname */
168
  flist = pkg_loadflist(pkgname, dosdir);
242 mateuszvis 169
  if (flist == NULL) return(-1);
219 mateuszvis 170
  /* display each filename on screen */
171
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
242 mateuszvis 172
    puts(flist_ptr->fname);
219 mateuszvis 173
  }
174
  /* free the list of files */
175
  pkg_freeflist(flist);
242 mateuszvis 176
  return(0);
219 mateuszvis 177
}