Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of PKG (SvarDOS)
3
 * Copyright (C) 2013-2021 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"
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
 
250 mateuszvis 28
  sprintf(buff, "%s\\packages", dosdir);
29
  dp = opendir(buff);
30
  if (dp == NULL) {
31
    kitten_printf(9, 0, "Error: Could not access directory %s", buff);
219 mateuszvis 32
    puts("");
33
    return(-1);
34
  }
35
 
250 mateuszvis 36
  while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
37
    int tlen = strlen(ep->d_name);
38
    if (ep->d_name[0] == '.') continue; /* ignore '.', '..', and hidden directories */
39
    if (tlen < 4) continue; /* files must be at least 5 bytes long ("x.lst") */
40
    if (strcasecmp(ep->d_name + tlen - 4, ".lst") != 0) continue;  /* if not an .lst file, skip it silently */
41
    ep->d_name[tlen - 4] = 0; /* trim out the ".lst" suffix */
219 mateuszvis 42
 
250 mateuszvis 43
    if (filterstr != NULL) {
44
      if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
45
    }
219 mateuszvis 46
 
250 mateuszvis 47
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
48
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
49
    readlsm(buff, ver, sizeof(ver));
50
 
51
    printf("%s %s", ep->d_name, ver);
52
    puts("");
53
    matchfound = 1;
219 mateuszvis 54
  }
250 mateuszvis 55
  if (matchfound == 0) kitten_puts(5, 0, "No package matched the search.");
219 mateuszvis 56
 
250 mateuszvis 57
  closedir(dp);
58
  return(0);
219 mateuszvis 59
}
60
 
61
 
62
/* frees a linked list of filenames */
63
void pkg_freeflist(struct flist_t *flist) {
64
  while (flist != NULL) {
65
    struct flist_t *victim = flist;
66
    flist = flist->next;
67
    free(victim);
68
  }
69
}
70
 
71
 
72
/* returns a linked list of the files that belong to package pkgname */
232 mateuszvis 73
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
219 mateuszvis 74
  struct flist_t *res = NULL, *newnode;
75
  FILE *fd;
250 mateuszvis 76
  char buff[256];
254 mateuszvis 77
 
219 mateuszvis 78
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
250 mateuszvis 79
  fd = fopen(buff, "rb");
80
  if (fd == NULL) {
219 mateuszvis 81
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
82
    puts("");
83
    return(NULL);
84
  }
85
  /* iterate through all lines of the file */
254 mateuszvis 86
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
87
    slash2backslash(buff); /* change all / to \ */
88
    if (buff[0] == 0) continue; /* skip empty lines */
89
    if (buff[strlen(buff) - 1] == '\\') continue; /* skip directories */
219 mateuszvis 90
    /* add the new node to the result */
254 mateuszvis 91
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
219 mateuszvis 92
    if (newnode == NULL) {
93
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
94
      continue;
95
    }
96
    newnode->next = res;
97
    strcpy(newnode->fname, buff);
98
    res = newnode;
99
  }
100
  fclose(fd);
101
  return(res);
102
}
103
 
104
 
105
/* Prints files owned by a package */
242 mateuszvis 106
int listfilesofpkg(const char *pkgname, const char *dosdir) {
219 mateuszvis 107
  struct flist_t *flist, *flist_ptr;
108
  /* load the list of files belonging to pkgname */
109
  flist = pkg_loadflist(pkgname, dosdir);
242 mateuszvis 110
  if (flist == NULL) return(-1);
219 mateuszvis 111
  /* display each filename on screen */
112
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
242 mateuszvis 113
    puts(flist_ptr->fname);
219 mateuszvis 114
  }
115
  /* free the list of files */
116
  pkg_freeflist(flist);
242 mateuszvis 117
  return(0);
219 mateuszvis 118
}