Subversion Repositories SvarDOS

Rev

Rev 613 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 613 Rev 614
1
/*
1
/*
2
 * This file is part of PKG (SvarDOS)
2
 * This file is part of PKG (SvarDOS)
3
 * Copyright (C) 2013-2022 Mateusz Viste
3
 * Copyright (C) 2013-2022 Mateusz Viste
4
 */
4
 */
5
 
5
 
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <ctype.h>    /* tolower() */
7
#include <ctype.h>    /* tolower() */
8
#include <stdlib.h>   /* atoi(), qsort() - not using it after all, redefining it manually later */
8
#include <stdlib.h>   /* atoi(), qsort() - not using it after all, redefining it manually later */
9
#include <string.h>   /* strlen() */
9
#include <string.h>   /* strlen() */
10
#include <sys/types.h>
10
#include <sys/types.h>
11
#include <direct.h> /* opendir() and friends */
11
#include <direct.h> /* opendir() and friends */
12
 
12
 
13
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
13
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
14
#include "kprintf.h"
14
#include "kprintf.h"
15
#include "libunzip.h"  /* zip_freelist()... */
15
#include "libunzip.h"  /* zip_freelist()... */
16
#include "lsm.h"
16
#include "lsm.h"
17
#include "svarlang.lib\svarlang.h"
17
#include "svarlang.lib\svarlang.h"
18
 
18
 
19
#include "showinst.h"  /* include self for control */
19
#include "showinst.h"  /* include self for control */
20
 
20
 
21
 
21
 
22
int showinstalledpkgs(const char *filterstr, const char *dosdir) {
22
int showinstalledpkgs(const char *filterstr, const char *dosdir) {
23
  DIR *dp;
23
  DIR *dp;
24
  struct dirent *ep;
24
  struct dirent *ep;
25
  char buff[256];
25
  char buff[256];
26
  char ver[16];
26
  char ver[16];
27
  int matchfound = 0;
27
  int matchfound = 0;
28
 
28
 
29
  sprintf(buff, "%s\\packages", dosdir);
29
  sprintf(buff, "%s\\packages", dosdir);
30
  dp = opendir(buff);
30
  dp = opendir(buff);
31
  if (dp == NULL) {
31
  if (dp == NULL) {
32
    kitten_printf(9, 0, buff); /* "ERROR: Could not access directory %s" */
32
    kitten_printf(9, 0, buff); /* "ERROR: Could not access directory %s" */
33
    puts("");
33
    puts("");
34
    return(-1);
34
    return(-1);
35
  }
35
  }
36
 
36
 
37
  while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
37
  while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (statically allocated) */
38
    int tlen = strlen(ep->d_name);
38
    int tlen = strlen(ep->d_name);
39
    if (ep->d_name[0] == '.') continue; /* ignore '.', '..', and hidden directories */
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") */
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 */
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 */
42
    ep->d_name[tlen - 4] = 0; /* trim out the ".lst" suffix */
43
 
43
 
44
    if (filterstr != NULL) {
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 */
45
      if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
46
    }
46
    }
47
 
47
 
48
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
48
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
49
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
49
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
50
    readlsm(buff, ver, sizeof(ver));
50
    readlsm(buff, ver, sizeof(ver));
51
 
51
 
52
    printf("%s %s", ep->d_name, ver);
52
    printf("%s %s", ep->d_name, ver);
53
    puts("");
53
    puts("");
54
    matchfound = 1;
54
    matchfound = 1;
55
  }
55
  }
56
  if (matchfound == 0) puts(svarlang_str(5, 0)); /* "No package matched the search." */
56
  if (matchfound == 0) puts(svarlang_str(5, 0)); /* "No package matched the search." */
57
 
57
 
58
  closedir(dp);
58
  closedir(dp);
59
  return(0);
59
  return(0);
60
}
60
}
61
 
61
 
62
 
62
 
63
/* frees a linked list of filenames */
63
/* frees a linked list of filenames */
64
void pkg_freeflist(struct flist_t *flist) {
64
void pkg_freeflist(struct flist_t *flist) {
65
  while (flist != NULL) {
65
  while (flist != NULL) {
66
    struct flist_t *victim = flist;
66
    struct flist_t *victim = flist;
67
    flist = flist->next;
67
    flist = flist->next;
68
    free(victim);
68
    free(victim);
69
  }
69
  }
70
}
70
}
71
 
71
 
72
 
72
 
73
/* returns a linked list of the files that belong to package pkgname */
73
/* returns a linked list of the files that belong to package pkgname */
74
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
74
struct flist_t *pkg_loadflist(const char *pkgname, const char *dosdir) {
75
  struct flist_t *res = NULL, *newnode;
75
  struct flist_t *res = NULL, *newnode;
76
  FILE *fd;
76
  FILE *fd;
77
  char buff[256];
77
  char buff[256];
78
 
78
 
79
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
79
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
80
  fd = fopen(buff, "rb");
80
  fd = fopen(buff, "rb");
81
  if (fd == NULL) {
81
  if (fd == NULL) {
82
    kitten_printf(9, 1, pkgname); /* "ERROR: Local package '%s' not found." */
82
    kitten_printf(9, 1, pkgname); /* "ERROR: Local package '%s' not found." */
83
    puts("");
83
    puts("");
84
    return(NULL);
84
    return(NULL);
85
  }
85
  }
86
  /* iterate through all lines of the file */
86
  /* iterate through all lines of the file */
87
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
87
  while (freadtokval(fd, buff, sizeof(buff), NULL, 0) == 0) {
88
    slash2backslash(buff); /* change all / to \ */
88
    slash2backslash(buff); /* change all / to \ */
89
    if (buff[0] == 0) continue; /* skip empty lines */
89
    if (buff[0] == 0) continue; /* skip empty lines */
90
    if (buff[strlen(buff) - 1] == '\\') continue; /* skip directories */
90
    if (buff[strlen(buff) - 1] == '\\') continue; /* skip directories */
91
    /* add the new node to the result */
91
    /* add the new node to the result */
92
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
92
    newnode = malloc(sizeof(struct flist_t) + strlen(buff));
93
    if (newnode == NULL) {
93
    if (newnode == NULL) {
94
      kitten_printf(2, 14, "malloc failure"); /* "Out of memory! (%s)" */
94
      kitten_printf(2, 14, "malloc failure"); /* "Out of memory! (%s)" */
95
      continue;
95
      continue;
96
    }
96
    }
97
    newnode->next = res;
97
    newnode->next = res;
98
    strcpy(newnode->fname, buff);
98
    strcpy(newnode->fname, buff);
99
    res = newnode;
99
    res = newnode;
100
  }
100
  }
101
  fclose(fd);
101
  fclose(fd);
102
  return(res);
102
  return(res);
103
}
103
}
104
 
104
 
105
 
105
 
106
/* Prints files owned by a package */
106
/* Prints files owned by a package */
107
int listfilesofpkg(const char *pkgname, const char *dosdir) {
107
int listfilesofpkg(const char *pkgname, const char *dosdir) {
108
  struct flist_t *flist, *flist_ptr;
108
  struct flist_t *flist, *flist_ptr;
109
  /* load the list of files belonging to pkgname */
109
  /* load the list of files belonging to pkgname */
110
  flist = pkg_loadflist(pkgname, dosdir);
110
  flist = pkg_loadflist(pkgname, dosdir);
111
  if (flist == NULL) return(-1);
111
  if (flist == NULL) return(-1);
112
  /* display each filename on screen */
112
  /* display each filename on screen */
113
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
113
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
114
    puts(flist_ptr->fname);
114
    puts(flist_ptr->fname);
115
  }
115
  }
116
  /* free the list of files */
116
  /* free the list of files */
117
  pkg_freeflist(flist);
117
  pkg_freeflist(flist);
118
  return(0);
118
  return(0);
119
}
119
}
120
 
120