Subversion Repositories SvarDOS

Rev

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