Subversion Repositories SvarDOS

Rev

Rev 1979 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of PKG (SvarDOS)
1678 mateusz.vi 3
 * Copyright (C) 2013-2024 Mateusz Viste
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() */
2235 bernd.boec 10
#include <strings.h>
219 mateuszvis 11
#include <sys/types.h>
228 mateuszvis 12
#include <direct.h> /* opendir() and friends */
219 mateuszvis 13
 
14
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
15
#include "libunzip.h"  /* zip_freelist()... */
16
#include "lsm.h"
2235 bernd.boec 17
#include "svarlang.lib/svarlang.h"
250 mateuszvis 18
 
219 mateuszvis 19
#include "showinst.h"  /* include self for control */
20
 
21
 
250 mateuszvis 22
int showinstalledpkgs(const char *filterstr, const char *dosdir) {
219 mateuszvis 23
  DIR *dp;
24
  struct dirent *ep;
250 mateuszvis 25
  char buff[256];
26
  char ver[16];
27
  int matchfound = 0;
219 mateuszvis 28
 
1678 mateusz.vi 29
  sprintf(buff, "%s\\appinfo", dosdir);
250 mateuszvis 30
  dp = opendir(buff);
31
  if (dp == NULL) {
1965 mateusz.vi 32
    output(svarlang_str(9,0)); /* "ERROR: Could not access directory */
33
    output(" ");
34
    outputnl(buff);
219 mateuszvis 35
    return(-1);
36
  }
37
 
250 mateuszvis 38
  while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
39
    int tlen = strlen(ep->d_name);
40
    if (ep->d_name[0] == '.') continue; /* ignore '.', '..', and hidden directories */
41
    if (tlen < 4) continue; /* files must be at least 5 bytes long ("x.lst") */
1678 mateusz.vi 42
    if (strcasecmp(ep->d_name + tlen - 4, ".lsm") != 0) continue;  /* if not an .lsm file, skip it silently */
43
    ep->d_name[tlen - 4] = 0; /* trim out the ".lsm" suffix */
219 mateuszvis 44
 
250 mateuszvis 45
    if (filterstr != NULL) {
1678 mateusz.vi 46
      if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* skip if not matching the non-NULL filter */
250 mateuszvis 47
    }
219 mateuszvis 48
 
1979 mateusz.vi 49
    output(ep->d_name);
50
 
250 mateuszvis 51
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
52
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
1972 mateusz.vi 53
    readlsm(buff, "version", ver, sizeof(ver));
250 mateuszvis 54
 
1964 mateusz.vi 55
    output(" ");
1972 mateusz.vi 56
    output(ver);
1979 mateusz.vi 57
 
58
    {
59
      unsigned short l = strlen(ver) + strlen(ep->d_name);
60
      readlsm(buff, "description", buff+1, 62);
61
      buff[0] = ' ';
62
      while (l++ < 16) output(" ");
63
      outputnl(buff);
64
    }
65
 
250 mateuszvis 66
    matchfound = 1;
219 mateuszvis 67
  }
1963 mateusz.vi 68
  if (matchfound == 0) outputnl(svarlang_str(5, 0)); /* "No package matched the search." */
219 mateuszvis 69
 
250 mateuszvis 70
  closedir(dp);
71
  return(0);
219 mateuszvis 72
}
73
 
74
 
75
/* frees a linked list of filenames */
76
void pkg_freeflist(struct flist_t *flist) {
77
  while (flist != NULL) {
78
    struct flist_t *victim = flist;
79
    flist = flist->next;
80
    free(victim);
81
  }
82
}
83
 
84
 
85
/* returns a linked list of the files that belong to package pkgname */
232 mateuszvis 86
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
219 mateuszvis 87
  struct flist_t *res = NULL, *newnode;
88
  FILE *fd;
250 mateuszvis 89
  char buff[256];
254 mateuszvis 90
 
219 mateuszvis 91
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
250 mateuszvis 92
  fd = fopen(buff, "rb");
93
  if (fd == NULL) {
1678 mateusz.vi 94
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
95
    fd = fopen(buff, "rb");
96
    if (fd == NULL) {
1965 mateusz.vi 97
      sprintf(buff, svarlang_str(9, 1), pkgname); /* "ERROR: Local package '%s' not found." */
98
      outputnl(buff);
1678 mateusz.vi 99
      return(NULL);
100
    }
219 mateuszvis 101
  }
1678 mateusz.vi 102
 
219 mateuszvis 103
  /* iterate through all lines of the file */
254 mateuszvis 104
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
1678 mateusz.vi 105
    /* skip empty lines */
106
    if (buff[0] == 0) continue;
107
 
108
    /* normalize slashes to backslashes */
109
    slash2backslash(buff);
110
 
111
    /* skip garbage */
112
    if ((buff[1] != ':') || (buff[2] != '\\')) continue;
113
 
114
    /* skip directories */
115
    if (buff[strlen(buff) - 1] == '\\') continue;
116
 
117
    /* trim ? trailer (may contain the file's CRC) */
118
    trimfnamecrc(buff);
119
 
219 mateuszvis 120
    /* add the new node to the result */
254 mateuszvis 121
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
219 mateuszvis 122
    if (newnode == NULL) {
1965 mateusz.vi 123
      outputnl(svarlang_str(2,14));  /* "Out of memory!" */
219 mateuszvis 124
      continue;
125
    }
126
    newnode->next = res;
127
    strcpy(newnode->fname, buff);
128
    res = newnode;
129
  }
130
  fclose(fd);
131
  return(res);
132
}
133
 
134
 
135
/* Prints files owned by a package */
242 mateuszvis 136
int listfilesofpkg(const char *pkgname, const char *dosdir) {
219 mateuszvis 137
  struct flist_t *flist, *flist_ptr;
138
  /* load the list of files belonging to pkgname */
139
  flist = pkg_loadflist(pkgname, dosdir);
242 mateuszvis 140
  if (flist == NULL) return(-1);
219 mateuszvis 141
  /* display each filename on screen */
142
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
1963 mateusz.vi 143
    outputnl(flist_ptr->fname);
219 mateuszvis 144
  }
145
  /* free the list of files */
146
  pkg_freeflist(flist);
242 mateuszvis 147
  return(0);
219 mateuszvis 148
}