Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
219 mateuszvis 1
/*
994 mateusz.vi 2
 * This file is part of pkg (SvarDOS package manager)
1678 mateusz.vi 3
 * Copyright (C) 2012-2024 Mateusz Viste
219 mateuszvis 4
 *
5
 * It contains a few helper function...
6
 */
7
 
8
 
9
#include <ctype.h>    /* tolower() */
240 mateuszvis 10
#include <direct.h>   /* provides the mkdir() prototype */
219 mateuszvis 11
#include <string.h>   /* */
12
#include <stdio.h>    /* sprintf() */
13
#include <stdlib.h>   /* atoi() */
14
#include <sys/stat.h> /* mkdir() */
15
 
256 mateuszvis 16
#include "trim.h"
219 mateuszvis 17
#include "helpers.h"
18
 
19
 
1964 mateusz.vi 20
/* converts a CRC32 into a (hex) string */
21
char *crc32tostring(char *s, unsigned long val) {
22
  signed char i;
23
  static char h[] = "0123456789ABCDEF";
24
  for (i = 7; i >= 0; i--) {
25
    s[i] = h[val & 15];
26
    val >>= 4;
27
  }
28
  s[8] = 0;
29
  return(s);
30
}
1893 mateusz.vi 31
 
1964 mateusz.vi 32
 
1893 mateusz.vi 33
/* outputs a NUL-terminated string to stdout */
34
void output(const char *s) {
35
  _asm {
36
    push ds
37
    push es
38
    /* get length of s into CX */
39
    mov ax, 0x4000 /* ah=DOS "write to file" and AL=0 for NUL matching */
40
    lds dx, s      /* set DS:DX to string (required for later) */
41
    push ds
42
    pop es         /* make sure es=ds (scasb uses es) */
43
    mov di, dx     /* set di to string (for NULL matching) */
44
    mov cx, 0xffff /* preset cx to 65535 (-1) */
45
    cld            /* clear DF so scasb increments DI */
46
    repne scasb    /* cmp al, es:[di], inc di, dec cx until match found */
47
    /* CX contains (65535 - strlen(s)) now */
48
    not cx         /* reverse all bits so I get (strlen(s) + 1) */
49
    dec cx         /* this is CX length */
50
    jz WRITEDONE   /* do nothing for empty strings */
51
 
52
    /* output by writing to stdout */
53
    /* mov ah, 0x40 */  /* DOS 2+ -- write to file via handle */
54
    xor bh, bh
55
    mov bl, 1 /* set handle (1=stdout 2=stderr) */
56
    /* mov cx, xxx */ /* write CX bytes */
57
    /* mov dx, s   */ /* DS:DX is the source of bytes to "write" */
58
    int 0x21
59
    WRITEDONE:
60
    pop es
61
    pop ds
62
  }
63
}
64
 
65
 
1959 mateusz.vi 66
void outputnl(const char *s) {
67
  output(s);
68
  output("\r\n");
69
}
70
 
71
 
219 mateuszvis 72
/* change all / to \ in a string */
73
void slash2backslash(char *str) {
74
  int x;
75
  for (x = 0; str[x] != 0; x++) {
76
    if (str[x] == '/') str[x] = '\\';
77
  }
78
}
79
 
80
 
1678 mateusz.vi 81
/* trim CRC from a filename and returns a pointer to the CRC part.
82
 * this is used to parse filename lines from LSM files */
83
char *trimfnamecrc(char *fname) {
84
  while (*fname) {
85
    if (*fname == '?') {
86
      *fname = 0;
87
      return(fname + 1);
88
    }
89
    fname++;
90
  }
1959 mateusz.vi 91
  return(NULL);
1678 mateusz.vi 92
}
93
 
94
 
219 mateuszvis 95
void removeDoubleBackslashes(char *str) {
96
  char *curpos;
97
  int x;
98
  for (;;) {
251 mateuszvis 99
    curpos = strstr(str, "\\\\");
219 mateuszvis 100
    if (curpos == NULL) return; /* job done */
101
    for (x = 1; curpos[x] != 0; x++) {
102
      curpos[x - 1] = curpos[x];
103
    }
104
    curpos[x - 1] = 0;
105
  }
106
}
107
 
108
 
109
/* Find the first occurrence of find in s, ignore case. */
110
char *fdnpkg_strcasestr(const char *s, const char *find) {
111
  char c, sc;
112
  size_t len;
113
  if ((c = *find++) != 0) {
114
    c = tolower((unsigned char)c);
115
    len = strlen(find);
116
    do {
117
      do {
118
        if ((sc = *s++) == 0) return(NULL);
119
      } while ((char)tolower((unsigned char)sc) != c);
120
    } while (strncasecmp(s, find, len) != 0);
121
    s--;
122
  }
123
  return((char *)s);
124
}
125
 
126
 
127
/* Creates directories recursively */
128
void mkpath(char *dirs) {
129
  int x;
130
  char savechar;
131
  for (x = 0; dirs[x] != 0; x++) {
132
    if (((dirs[x] == '/') || (dirs[x] == '\\')) && (x > 0)) {
133
      if (dirs[x - 1] != ':') { /* avoid d:\ stuff */
134
        savechar = dirs[x];
135
        dirs[x] = 0;
136
        /* make the dir */
240 mateuszvis 137
        mkdir(dirs);
219 mateuszvis 138
        dirs[x] = savechar;
139
      }
140
    }
141
  }
142
}
143
 
144
 
145
/* returns a pointer to the start of the filename, out of a path\to\file string, and
146
   fills respath with the local folder where the file should be placed. */
1892 mateusz.vi 147
char *computelocalpath(char *longfilename, char *respath, const char *dosdir, const struct customdirs *dirlist, char bootdrive) {
219 mateuszvis 148
  int x, lastsep = 0, firstsep = -1;
149
  char savedchar;
150
  char *shortfilename, *pathstart;
151
  pathstart = longfilename;
152
  for (x = 0; longfilename[x] != 0; x++) {
153
    if ((longfilename[x] == '/') || (longfilename[x] == '\\')) {
154
      lastsep = x;
155
      if (firstsep < 0) firstsep = x;
156
    }
157
  }
1878 mateusz.vi 158
  /* if it's a file without any directory, then it goes to BOOTDRIVE:\ (COMMAND.COM, KERNEL.SYS...) */
994 mateusz.vi 159
  if (firstsep < 0) {
1892 mateusz.vi 160
    sprintf(respath, "%c:\\", bootdrive);
994 mateusz.vi 161
    return(longfilename);
162
  }
163
  /* */
219 mateuszvis 164
  shortfilename = &longfilename[lastsep + 1];
165
  /* look for possible custom path */
166
  if (firstsep > 0) {
167
    savedchar = longfilename[firstsep];
168
    longfilename[firstsep] = 0;
169
    for (; dirlist != NULL; dirlist = dirlist->next) {
170
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
171
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
172
        pathstart = &longfilename[firstsep + 1];
173
        dosdir = dirlist->location;
174
        break;
175
      }
176
    }
177
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
178
  }
179
  /* apply the default (DOSDIR) path */
180
  savedchar = longfilename[lastsep + 1];
181
  longfilename[lastsep + 1] = 0;
182
  sprintf(respath, "%s\\%s", dosdir, pathstart);
183
  slash2backslash(respath);
184
  removeDoubleBackslashes(respath);
185
  longfilename[lastsep + 1] = savedchar;
186
  return(shortfilename);
187
}
188
 
189
 
190
/* analyzes a filename string and returns the pointer to the file's extension
191
 * (which can be empty) */
192
char *getfext(char *fname) {
193
  char *res = NULL;
194
  for (; *fname != 0; fname++) {
195
    if (*fname == '.') res = fname + 1;
196
  }
197
  /* if no dot found, then point to the string's null terminator */
198
  if (res == NULL) return(fname);
199
  return(res);
200
}
248 mateuszvis 201
 
202
 
203
/* reads a line from a "token = value" file, returns 0 on success
204
 * val (if not NULL) is updated with a pointer to the "value" part
205
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
206
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
207
  int bytebuff, linelen = 0;
208
  if (val != NULL) *val = NULL;
209
  for (;;) {
210
    bytebuff = fgetc(fd);
211
    if (bytebuff == EOF) {
212
      if (linelen == 0) return(-1);
213
      break;
214
    }
215
    if (bytebuff < 0) return(-1);
216
    if ((*val == NULL) && (bytebuff == delim)) {
217
      line[linelen++] = 0;
218
      *val = line + linelen;
219
      continue;
220
    }
221
    if (bytebuff == '\r') continue; /* ignore CR */
222
    if (bytebuff == '\n') break;
223
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
224
  }
225
  /* terminate line and trim token and value (if any) */
226
  line[linelen] = 0;
227
  trim(line);
228
  if ((val != NULL) && (*val != NULL)) trim(*val);
229
  return(0);
230
}