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 "getdelim.h"
|
|
|
15 |
#include "helpers.h" /* fdnpkg_strcasestr(), slash2backslash()... */
|
|
|
16 |
#include "kprintf.h"
|
|
|
17 |
#include "libunzip.h" /* zip_freelist()... */
|
|
|
18 |
#include "lsm.h"
|
|
|
19 |
#include "rtrim.h"
|
250 |
mateuszvis |
20 |
|
219 |
mateuszvis |
21 |
#include "showinst.h" /* include self for control */
|
|
|
22 |
|
|
|
23 |
|
250 |
mateuszvis |
24 |
int showinstalledpkgs(const char *filterstr, const char *dosdir) {
|
219 |
mateuszvis |
25 |
DIR *dp;
|
|
|
26 |
struct dirent *ep;
|
250 |
mateuszvis |
27 |
char buff[256];
|
|
|
28 |
char ver[16];
|
|
|
29 |
int matchfound = 0;
|
219 |
mateuszvis |
30 |
|
250 |
mateuszvis |
31 |
sprintf(buff, "%s\\packages", dosdir);
|
|
|
32 |
dp = opendir(buff);
|
|
|
33 |
if (dp == NULL) {
|
|
|
34 |
kitten_printf(9, 0, "Error: Could not access directory %s", buff);
|
219 |
mateuszvis |
35 |
puts("");
|
|
|
36 |
return(-1);
|
|
|
37 |
}
|
|
|
38 |
|
250 |
mateuszvis |
39 |
while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
|
|
|
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 */
|
219 |
mateuszvis |
45 |
|
250 |
mateuszvis |
46 |
if (filterstr != NULL) {
|
|
|
47 |
if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
|
|
|
48 |
}
|
219 |
mateuszvis |
49 |
|
250 |
mateuszvis |
50 |
/* load the metadata from %DOSDIR\APPINFO\*.lsm */
|
|
|
51 |
sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
|
|
|
52 |
readlsm(buff, ver, sizeof(ver));
|
|
|
53 |
|
|
|
54 |
printf("%s %s", ep->d_name, ver);
|
|
|
55 |
puts("");
|
|
|
56 |
matchfound = 1;
|
219 |
mateuszvis |
57 |
}
|
250 |
mateuszvis |
58 |
if (matchfound == 0) kitten_puts(5, 0, "No package matched the search.");
|
219 |
mateuszvis |
59 |
|
250 |
mateuszvis |
60 |
closedir(dp);
|
|
|
61 |
return(0);
|
219 |
mateuszvis |
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
/* frees a linked list of filenames */
|
|
|
66 |
void pkg_freeflist(struct flist_t *flist) {
|
|
|
67 |
while (flist != NULL) {
|
|
|
68 |
struct flist_t *victim = flist;
|
|
|
69 |
flist = flist->next;
|
|
|
70 |
free(victim);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
/* returns a linked list of the files that belong to package pkgname */
|
232 |
mateuszvis |
76 |
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
|
219 |
mateuszvis |
77 |
struct flist_t *res = NULL, *newnode;
|
|
|
78 |
FILE *fd;
|
|
|
79 |
char *lineptr;
|
250 |
mateuszvis |
80 |
char buff[256];
|
219 |
mateuszvis |
81 |
int getdelimlen, fnamelen;
|
|
|
82 |
size_t getdelimcount = 0;
|
|
|
83 |
sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
|
250 |
mateuszvis |
84 |
fd = fopen(buff, "rb");
|
|
|
85 |
if (fd == NULL) {
|
219 |
mateuszvis |
86 |
kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
|
|
|
87 |
puts("");
|
|
|
88 |
return(NULL);
|
|
|
89 |
}
|
|
|
90 |
/* iterate through all lines of the file */
|
|
|
91 |
for (;;) {
|
|
|
92 |
lineptr = NULL;
|
|
|
93 |
getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
|
|
|
94 |
if (getdelimlen < 0) { /* EOF */
|
|
|
95 |
free(lineptr);
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
rtrim(lineptr); /* right-trim the filename */
|
|
|
99 |
slash2backslash(lineptr); /* change all / to \ */
|
|
|
100 |
if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
|
|
|
101 |
if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
|
|
|
102 |
if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
|
|
|
103 |
fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
|
|
|
104 |
} else { /* else it's a relative path starting at %dosdir% */
|
|
|
105 |
fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
|
|
|
106 |
}
|
|
|
107 |
free(lineptr); /* free the memory occupied by the line */
|
|
|
108 |
/* add the new node to the result */
|
|
|
109 |
newnode = malloc(sizeof(struct flist_t) + fnamelen);
|
|
|
110 |
if (newnode == NULL) {
|
|
|
111 |
kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
|
|
|
112 |
continue;
|
|
|
113 |
}
|
|
|
114 |
newnode->next = res;
|
|
|
115 |
strcpy(newnode->fname, buff);
|
|
|
116 |
res = newnode;
|
|
|
117 |
}
|
|
|
118 |
fclose(fd);
|
|
|
119 |
return(res);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
/* Prints files owned by a package */
|
242 |
mateuszvis |
124 |
int listfilesofpkg(const char *pkgname, const char *dosdir) {
|
219 |
mateuszvis |
125 |
struct flist_t *flist, *flist_ptr;
|
|
|
126 |
/* load the list of files belonging to pkgname */
|
|
|
127 |
flist = pkg_loadflist(pkgname, dosdir);
|
242 |
mateuszvis |
128 |
if (flist == NULL) return(-1);
|
219 |
mateuszvis |
129 |
/* display each filename on screen */
|
|
|
130 |
for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
|
242 |
mateuszvis |
131 |
puts(flist_ptr->fname);
|
219 |
mateuszvis |
132 |
}
|
|
|
133 |
/* free the list of files */
|
|
|
134 |
pkg_freeflist(flist);
|
242 |
mateuszvis |
135 |
return(0);
|
219 |
mateuszvis |
136 |
}
|