Subversion Repositories SvarDOS

Rev

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