Subversion Repositories SvarDOS

Rev

Rev 250 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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