Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
219 mateuszvis 1
/*
228 mateuszvis 2
 * This file is part of PKGINST (SvarDOS)
3
 * Copyright (C) 2013-2021 Mateusz Viste. All rights reserved.
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
 
232 mateuszvis 13
#include "fdnpkg.h"
219 mateuszvis 14
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
15
#include "kprintf.h"
16
#include "libunzip.h"  /* zip_freelist()... */
17
#include "lsm.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
 
250 mateuszvis 29
  sprintf(buff, "%s\\packages", dosdir);
30
  dp = opendir(buff);
31
  if (dp == NULL) {
32
    kitten_printf(9, 0, "Error: Could not access directory %s", buff);
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") */
41
    if (strcasecmp(ep->d_name + tlen - 4, ".lst") != 0) continue;  /* if not an .lst file, skip it silently */
42
    ep->d_name[tlen - 4] = 0; /* trim out the ".lst" suffix */
219 mateuszvis 43
 
250 mateuszvis 44
    if (filterstr != NULL) {
45
      if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
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
  }
250 mateuszvis 56
  if (matchfound == 0) kitten_puts(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) {
219 mateuszvis 82
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
83
    puts("");
84
    return(NULL);
85
  }
86
  /* iterate through all lines of the file */
254 mateuszvis 87
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
88
    slash2backslash(buff); /* change all / to \ */
89
    if (buff[0] == 0) continue; /* skip empty lines */
90
    if (buff[strlen(buff) - 1] == '\\') continue; /* skip directories */
219 mateuszvis 91
    /* add the new node to the result */
254 mateuszvis 92
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
219 mateuszvis 93
    if (newnode == NULL) {
94
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
95
      continue;
96
    }
97
    newnode->next = res;
98
    strcpy(newnode->fname, buff);
99
    res = newnode;
100
  }
101
  fclose(fd);
102
  return(res);
103
}
104
 
105
 
106
/* Prints files owned by a package */
242 mateuszvis 107
int listfilesofpkg(const char *pkgname, const char *dosdir) {
219 mateuszvis 108
  struct flist_t *flist, *flist_ptr;
109
  /* load the list of files belonging to pkgname */
110
  flist = pkg_loadflist(pkgname, dosdir);
242 mateuszvis 111
  if (flist == NULL) return(-1);
219 mateuszvis 112
  /* display each filename on screen */
113
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
242 mateuszvis 114
    puts(flist_ptr->fname);
219 mateuszvis 115
  }
116
  /* free the list of files */
117
  pkg_freeflist(flist);
242 mateuszvis 118
  return(0);
219 mateuszvis 119
}