Subversion Repositories SvarDOS

Rev

Rev 1965 | Go to most recent revision | 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
 
250 mateuszvis 48
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
49
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
1972 mateusz.vi 50
    readlsm(buff, "version", ver, sizeof(ver));
51
    readlsm(buff, "description", buff, 80 - 2 - strlen(ver) - strlen(ep->d_name));
250 mateuszvis 52
 
1964 mateusz.vi 53
    output(ep->d_name);
54
    output(" ");
1972 mateusz.vi 55
    output(ver);
56
    output(" ");
57
    outputnl(buff);
250 mateuszvis 58
    matchfound = 1;
219 mateuszvis 59
  }
1963 mateusz.vi 60
  if (matchfound == 0) outputnl(svarlang_str(5, 0)); /* "No package matched the search." */
219 mateuszvis 61
 
250 mateuszvis 62
  closedir(dp);
63
  return(0);
219 mateuszvis 64
}
65
 
66
 
67
/* frees a linked list of filenames */
68
void pkg_freeflist(struct flist_t *flist) {
69
  while (flist != NULL) {
70
    struct flist_t *victim = flist;
71
    flist = flist->next;
72
    free(victim);
73
  }
74
}
75
 
76
 
77
/* returns a linked list of the files that belong to package pkgname */
232 mateuszvis 78
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
219 mateuszvis 79
  struct flist_t *res = NULL, *newnode;
80
  FILE *fd;
250 mateuszvis 81
  char buff[256];
254 mateuszvis 82
 
219 mateuszvis 83
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
250 mateuszvis 84
  fd = fopen(buff, "rb");
85
  if (fd == NULL) {
1678 mateusz.vi 86
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
87
    fd = fopen(buff, "rb");
88
    if (fd == NULL) {
1965 mateusz.vi 89
      sprintf(buff, svarlang_str(9, 1), pkgname); /* "ERROR: Local package '%s' not found." */
90
      outputnl(buff);
1678 mateusz.vi 91
      return(NULL);
92
    }
219 mateuszvis 93
  }
1678 mateusz.vi 94
 
219 mateuszvis 95
  /* iterate through all lines of the file */
254 mateuszvis 96
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
1678 mateusz.vi 97
    /* skip empty lines */
98
    if (buff[0] == 0) continue;
99
 
100
    /* normalize slashes to backslashes */
101
    slash2backslash(buff);
102
 
103
    /* skip garbage */
104
    if ((buff[1] != ':') || (buff[2] != '\\')) continue;
105
 
106
    /* skip directories */
107
    if (buff[strlen(buff) - 1] == '\\') continue;
108
 
109
    /* trim ? trailer (may contain the file's CRC) */
110
    trimfnamecrc(buff);
111
 
219 mateuszvis 112
    /* add the new node to the result */
254 mateuszvis 113
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
219 mateuszvis 114
    if (newnode == NULL) {
1965 mateusz.vi 115
      outputnl(svarlang_str(2,14));  /* "Out of memory!" */
219 mateuszvis 116
      continue;
117
    }
118
    newnode->next = res;
119
    strcpy(newnode->fname, buff);
120
    res = newnode;
121
  }
122
  fclose(fd);
123
  return(res);
124
}
125
 
126
 
127
/* Prints files owned by a package */
242 mateuszvis 128
int listfilesofpkg(const char *pkgname, const char *dosdir) {
219 mateuszvis 129
  struct flist_t *flist, *flist_ptr;
130
  /* load the list of files belonging to pkgname */
131
  flist = pkg_loadflist(pkgname, dosdir);
242 mateuszvis 132
  if (flist == NULL) return(-1);
219 mateuszvis 133
  /* display each filename on screen */
134
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
1963 mateusz.vi 135
    outputnl(flist_ptr->fname);
219 mateuszvis 136
  }
137
  /* free the list of files */
138
  pkg_freeflist(flist);
242 mateuszvis 139
  return(0);
219 mateuszvis 140
}