Subversion Repositories SvarDOS

Rev

Rev 1965 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1959 mateusz.vi 1
/*
2
 * This file is part of the pkg (SvarDOS) project.
3
 * Copyright (C) Mateusz Viste 2012-2024
4
 */
5
 
1961 mateusz.vi 6
#include <direct.h> /* opendir() and friends */
1959 mateusz.vi 7
#include <stdio.h>
8
#include <string.h>    /* strlen() */
9
#include <stdlib.h>    /* free() */
10
 
11
#include "crc32.h"
12
#include "helpers.h"
13
#include "svarlang.lib\svarlang.h"
14
 
15
#include "healthck.h"
16
 
17
 
1961 mateusz.vi 18
static void clrcurline(unsigned short screenwidth);
19
#pragma aux clrcurline = \
20
"mov ah, 0x02" \
21
"mov dl, 0x0D" \
22
"int 0x21" \
23
"mov ax, 0x0A20" /* int 10h "write char to screen" */ \
24
"xor bx, bx"     /* video page and attribute (attr on PCjr) */ \
25
"int 0x10" \
26
parm [cx] \
27
modify [ax bx dx]
28
 
29
 
1959 mateusz.vi 30
/* checks the health of a package (or all packages).
31
 * Returns 0 on success, non-zero otherwise */
1968 mateusz.vi 32
int healthcheck(unsigned char *buff15k, const char *pkgname, const char *dosdir, unsigned char extendedcheck) {
1959 mateusz.vi 33
  char *crc, *ext;
34
  FILE *flist, *fd;
35
  unsigned long goodcrc, realcrc;
36
  unsigned short errcount = 0;
1961 mateusz.vi 37
  DIR *dp = NULL;
38
  struct dirent *ep;
1959 mateusz.vi 39
 
40
  if (pkgname == NULL) {
1968 mateusz.vi 41
    sprintf(buff15k, "%s\\appinfo", dosdir);
42
    dp = opendir(buff15k);
1961 mateusz.vi 43
    if (dp == NULL) {
1965 mateusz.vi 44
      output(svarlang_str(9, 0)); /* "ERROR: Could not access directory:" */
1968 mateusz.vi 45
      outputnl(buff15k);
1961 mateusz.vi 46
      return(-1);
47
    }
48
 
49
    AGAIN:
50
 
51
    /* if check is system-wide then fetch next package */
52
    while ((ep = readdir(dp)) != NULL) { /* readdir() result must never be freed (static allocation) */
53
      int tlen = strlen(ep->d_name);
54
      if (ep->d_name[0] == '.') continue; /* ignore '.', '..', and hidden directories */
55
      if (tlen < 4) continue; /* files must be at least 5 bytes long ("x.lst") */
56
      if (strcasecmp(ep->d_name + tlen - 4, ".lsm") != 0) continue;  /* if not an .lsm file, skip it silently */
57
      ep->d_name[tlen - 4] = 0; /* trim out the ".lsm" suffix */
58
      break;
59
    }
60
    if (ep == NULL) {
61
      closedir(dp);
62
      goto DONE;
63
    }
64
    pkgname = ep->d_name;
1959 mateusz.vi 65
  }
66
 
1961 mateusz.vi 67
 
68
  /************************************************************
69
   * pkgname is valid now so let's proceed with serious stuff *
70
   ************************************************************/
71
 
1959 mateusz.vi 72
  /* open the (legacy) listing file at %DOSDIR%\packages\pkgname.lst
73
   * if not exists then fall back to appinfo\pkgname.lsm */
1968 mateusz.vi 74
  sprintf(buff15k, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
75
  flist = fopen(buff15k, "rb");
1959 mateusz.vi 76
  if (flist == NULL) {
1968 mateusz.vi 77
    sprintf(buff15k, svarlang_str(4,0), pkgname); /* "Package %s is not installed, so not removed." */
78
    outputnl(buff15k);
1959 mateusz.vi 79
    return(-1);
80
  }
81
 
82
  /* iterate over all files listed in pkgname.lsm */
1968 mateusz.vi 83
  while (freadtokval(flist, buff15k, 15 * 1024, NULL, 0) == 0) {
1959 mateusz.vi 84
 
85
    /* skip empty lines */
1968 mateusz.vi 86
    if (buff15k[0] == 0) continue;
1959 mateusz.vi 87
 
88
    /* change all slash to backslash */
1968 mateusz.vi 89
    slash2backslash(buff15k);
1959 mateusz.vi 90
 
91
    /* skip garbage */
1968 mateusz.vi 92
    if ((buff15k[1] != ':') || (buff15k[2] != '\\')) continue;
1959 mateusz.vi 93
 
94
    /* trim out CRC information and get the ptr to it (if present) */
1968 mateusz.vi 95
    crc = trimfnamecrc(buff15k);
1959 mateusz.vi 96
    if (crc == NULL) continue;
97
 
98
    goodcrc = strtoul(crc, NULL, 16);
1968 mateusz.vi 99
    strlwr(buff15k); /* turn filename lower case - this is needed for aesthetics
100
    when printing errors, but also for matching extensions */
101
    ext = getfext(buff15k);
1959 mateusz.vi 102
 
1961 mateusz.vi 103
    /* skip non-executable files (unless healthcheck+) */
104
    if ((extendedcheck == 0) &&
105
        (strcmp(ext, "bat") != 0) &&
106
        (strcmp(ext, "bin") != 0) &&
107
        (strcmp(ext, "com") != 0) &&
108
        (strcmp(ext, "dll") != 0) &&
109
        (strcmp(ext, "drv") != 0) &&
110
        (strcmp(ext, "exe") != 0) &&
111
        (strcmp(ext, "ovl") != 0) &&
112
        (strcmp(ext, "sys") != 0)) continue;
1959 mateusz.vi 113
 
114
    output("[");
115
    output(pkgname);
116
    output("] ");
1968 mateusz.vi 117
    output(buff15k);
1959 mateusz.vi 118
 
119
    realcrc = CRC32_INITVAL;
1968 mateusz.vi 120
    fd = fopen(buff15k, "rb");
1959 mateusz.vi 121
    if (fd == NULL) {
1968 mateusz.vi 122
      output(" ");
123
      outputnl(svarlang_str(11,1));
1959 mateusz.vi 124
      continue;
125
    }
126
    for (;;) {
127
      unsigned short bufflen;
1968 mateusz.vi 128
      bufflen = fread(buff15k, 1, 15 * 1024, fd);
1959 mateusz.vi 129
      if (bufflen == 0) break;
1968 mateusz.vi 130
      crc32_feed(&realcrc, buff15k, bufflen);
1959 mateusz.vi 131
    }
132
    fclose(fd);
133
 
134
    crc32_finish(&realcrc);
135
 
136
    if (goodcrc != realcrc) {
1968 mateusz.vi 137
      output(" ");
138
      outputnl(svarlang_str(11,0)); /* BAD CRC */
1959 mateusz.vi 139
      errcount++;
140
      continue;
141
    }
142
 
1961 mateusz.vi 143
    clrcurline(80);
1959 mateusz.vi 144
 
145
  }
146
 
147
  /* close the lsm file */
148
  fclose(flist);
149
 
1961 mateusz.vi 150
  if (dp != NULL) goto AGAIN;
151
 
152
  DONE:
153
 
1959 mateusz.vi 154
  if (errcount == 0) {
155
    outputnl(svarlang_strid(0x0A00));
156
  } else {
1968 mateusz.vi 157
    sprintf(buff15k, svarlang_str(11,2), errcount);
158
    outputnl(buff15k);
1959 mateusz.vi 159
  }
160
  return(0);
161
}