Subversion Repositories SvarDOS

Rev

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

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