Subversion Repositories SvarDOS

Rev

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