Subversion Repositories SvarDOS

Rev

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