Subversion Repositories SvarDOS

Rev

Rev 225 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
2
 * This file is part of fdnpkg
3
 * Copyright (C) 2012-2017 Mateusz Viste
4
 *
5
 * It contains a few helper function...
6
 */
7
 
8
 
9
#include <ctype.h>    /* tolower() */
10
#include <string.h>   /* */
11
#include <stdio.h>    /* sprintf() */
12
#include <stdlib.h>   /* atoi() */
13
#include <sys/stat.h> /* mkdir() */
14
 
15
#include "version.h"
16
 
17
#ifdef __WATCOMC__
18
  #include <direct.h>  /* provides the mkdir() prototype */
19
  #define MAKEDIR(x) mkdir(x);
20
#else
21
  #define MAKEDIR(x) mkdir(x, S_IWUSR); /* S_IWUSR is to make the directory READONLY bit NOT set */
22
#endif
23
 
24
#include "helpers.h"
25
 
26
 
27
/* translates a version string into a array of integer values. The array must be 8-position long.
28
   returns 0 if parsing was successful, non-zero otherwise.
29
   Accepted formats follow:
30
    300.12.1
31
    1
32
    12.2.34.2-4.5
33
    1.2c
34
    1.01
35
    2013-12-31   */
36
static int versiontointarr(char *verstr, int *arr) {
37
  int i, vlen, dotcounter = 1, firstcharaftersep = 0;
38
  char *digits[8];
39
  char verstrcopy[16];
40
 
41
  /* fill the array with zeroes first */
42
  for (i = 0; i < 8; i++) arr[i] = 0;
43
 
44
  /* first extensively validate the input */
45
  if (verstr == NULL) return(-1);
46
  vlen = strlen(verstr);
47
  if (vlen == 0) return(-1);
48
  if (vlen > 15) return(-1);
49
  if ((verstr[0] < '0') || (verstr[0] > '9')) return(-1); /* a version string must start with a 0..9 digit */
50
  if ((tolower(verstr[vlen - 1]) >= 'a') && (tolower(verstr[vlen - 1]) <= 'z') && (vlen > 1)) { /* remove any letter from the end, and use it as a (very) minor differenciator */
51
    vlen -= 1;
52
    arr[7] = tolower(verstr[vlen]);
53
  }
54
  if ((verstr[vlen - 1] < '0') || (verstr[vlen - 1] > '9')) return(-1); /* a version string must end with a 0..9 digit */
55
 
56
  digits[0] = verstrcopy;
57
  for (i = 0; i < vlen; i++) {
58
    verstrcopy[i] = verstr[i];
59
    switch (verstr[i]) {
60
      case '.':
61
      case '-':
62
        if (i == 0) return(-1);
63
        if (verstrcopy[i-1] == 0) return(-1); /* do not allow two separators in a row */
64
        if (dotcounter > 6) return(-1);
65
        digits[dotcounter++] = &verstrcopy[i + 1];
66
        verstrcopy[i] = 0;
67
        firstcharaftersep = 1;
68
        break;
69
      case '0':
70
        /* if this is a zero right after a separator, and trailed with a digit
71
         * (as in '1.01'), then enforce a separator first */
72
        if ((firstcharaftersep != 0) && (verstr[i+1] >= '0') && (verstr[i+1] <= '9')) {
73
          if (dotcounter > 6) return(-1);
74
          digits[dotcounter++] = &verstrcopy[i + 1];
75
          verstrcopy[i] = 0;
76
        }
77
        break;
78
      case '1':
79
      case '2':
80
      case '3':
81
      case '4':
82
      case '5':
83
      case '6':
84
      case '7':
85
      case '8':
86
      case '9':
87
        firstcharaftersep = 0;
88
        break;
89
      default: /* do not allow any character different than 0..9 or '.' */
90
        return(-1);
91
        break;
92
    }
93
  }
94
  verstrcopy[i] = 0;
95
  /* now that we know the input is sane, let's process it */
96
  for (i = 0; i < dotcounter; i++) {
97
    int tmpint;
98
    tmpint = atoi(digits[i]);
99
    if ((tmpint < 0) || (tmpint > 32000)) return(-1);
100
    arr[i] = tmpint;
101
  }
102
  return(0);
103
}
104
 
105
 
106
/* compares version strings v1 and v2. Returns 1 if v2 is newer than v1, 0 otherwise, and -1 if comparison is unable to tell */
107
int isversionnewer(char *v1, char *v2) {
108
  int x1[8], x2[8], i;
109
  if ((v1 == NULL) || (v2 == NULL)) return(-1); /* if input is invalid (NULL), don't continue */
110
  if (strcasecmp(v1, v2) == 0) return(0);  /* if both versions are the same, don't continue */
111
  /* check versions of the decimal format 1.23... */
112
  if ((versiontointarr(v1, x1) != 0) || (versiontointarr(v2, x2) != 0)) return(-1);
113
  for (i = 0; i < 8; i++) {
114
    if (x2[i] > x1[i]) return(1);
115
    if (x2[i] < x1[i]) return(0);
116
  }
117
  return(0);
118
}
119
 
120
 
121
/* change all / to \ in a string */
122
void slash2backslash(char *str) {
123
  int x;
124
  for (x = 0; str[x] != 0; x++) {
125
    if (str[x] == '/') str[x] = '\\';
126
  }
127
}
128
 
129
 
130
/* change all \ to / in a string */
131
void backslash2slash(char *str) {
132
  int x;
133
  for (x = 0; str[x] != 0; x++) {
134
    if (str[x] == '\\') str[x] = '/';
135
  }
136
}
137
 
138
 
139
void removeDoubleBackslashes(char *str) {
140
  char *curpos;
141
  int x;
142
  for (;;) {
143
    curpos = fdnpkg_strcasestr(str, "\\\\");
144
    if (curpos == NULL) return; /* job done */
145
    for (x = 1; curpos[x] != 0; x++) {
146
      curpos[x - 1] = curpos[x];
147
    }
148
    curpos[x - 1] = 0;
149
  }
150
}
151
 
152
 
153
/* converts a string to all lowercase */
154
void strtolower(char *mystring) {
155
  int x;
156
  for (x = 0; mystring[x] != 0; x++) mystring[x] = tolower(mystring[x]);
157
}
158
 
159
 
160
/* Find the first occurrence of find in s, ignore case. */
161
char *fdnpkg_strcasestr(const char *s, const char *find) {
162
  char c, sc;
163
  size_t len;
164
  if ((c = *find++) != 0) {
165
    c = tolower((unsigned char)c);
166
    len = strlen(find);
167
    do {
168
      do {
169
        if ((sc = *s++) == 0) return(NULL);
170
      } while ((char)tolower((unsigned char)sc) != c);
171
    } while (strncasecmp(s, find, len) != 0);
172
    s--;
173
  }
174
  return((char *)s);
175
}
176
 
177
 
178
/* Creates directories recursively */
179
void mkpath(char *dirs) {
180
  int x;
181
  char savechar;
182
  for (x = 0; dirs[x] != 0; x++) {
183
    if (((dirs[x] == '/') || (dirs[x] == '\\')) && (x > 0)) {
184
      if (dirs[x - 1] != ':') { /* avoid d:\ stuff */
185
        savechar = dirs[x];
186
        dirs[x] = 0;
187
        /* make the dir */
188
        MAKEDIR(dirs);
189
        dirs[x] = savechar;
190
      }
191
    }
192
  }
193
}
194
 
195
 
196
/* remap drive, if needed (hack for situations where fdinst is used to
197
 * install an OS to a drive that will change its letter post-install) */
198
void mapdrives(char *s, char *mapdrv) {
199
  int i = 0;
200
  if (mapdrv == NULL) return;
201
  if ((s == NULL) || (s[0] == 0) || (s[1] != ':')) return;
202
  while (mapdrv[i] != 0) {
203
    if (toupper(mapdrv[i]) == toupper(s[0])) {
204
      s[0] = toupper(mapdrv[i + 1]);
205
      return;
206
    }
207
    i += 2;
208
  }
209
}
210
 
211
 
212
/* */
213
void unmapdrives(char *s, char *mapdrv) {
214
  int i = 0;
215
  if (mapdrv == NULL) return;
216
  if ((s == NULL) || (s[0] == 0) || (s[1] != ':')) return;
217
  while (mapdrv[i] != 0) {
218
    if (toupper(mapdrv[i + 1]) == toupper(s[0])) {
219
      s[0] = toupper(mapdrv[i]);
220
      return;
221
    }
222
    i += 2;
223
  }
224
}
225
 
226
 
227
/* returns a pointer to the start of the filename, out of a path\to\file string, and
228
   fills respath with the local folder where the file should be placed. */
229
char *computelocalpath(char *longfilename, char *respath, char *dosdir, struct customdirs *dirlist) {
230
  int x, lastsep = 0, firstsep = -1;
231
  char savedchar;
232
  char *shortfilename, *pathstart;
233
  pathstart = longfilename;
234
  for (x = 0; longfilename[x] != 0; x++) {
235
    if ((longfilename[x] == '/') || (longfilename[x] == '\\')) {
236
      lastsep = x;
237
      if (firstsep < 0) firstsep = x;
238
    }
239
  }
240
  shortfilename = &longfilename[lastsep + 1];
241
  /* look for possible custom path */
242
  if (firstsep > 0) {
243
    savedchar = longfilename[firstsep];
244
    longfilename[firstsep] = 0;
245
    for (; dirlist != NULL; dirlist = dirlist->next) {
246
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
247
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
248
        pathstart = &longfilename[firstsep + 1];
249
        dosdir = dirlist->location;
250
        break;
251
      }
252
    }
253
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
254
  }
255
  /* apply the default (DOSDIR) path */
256
  savedchar = longfilename[lastsep + 1];
257
  longfilename[lastsep + 1] = 0;
258
  sprintf(respath, "%s\\%s", dosdir, pathstart);
259
  slash2backslash(respath);
260
  removeDoubleBackslashes(respath);
261
  longfilename[lastsep + 1] = savedchar;
262
  return(shortfilename);
263
}
264
 
265
 
266
/* detect local paths (eg. C:\REPO). Returns 1 if the url looks like a local path, zero otherwise. */
267
int detect_localpath(char *url) {
268
  if (url[0] != 0) {
269
    if (url[1] != 0) {
270
      if ((url[1] == ':') && ((url[2] == '\\') || (url[2] == '/'))) return(1);
271
    }
272
  }
273
  return(0);
274
}
275
 
276
 
277
/* analyzes a filename string and returns the pointer to the file's extension
278
 * (which can be empty) */
279
char *getfext(char *fname) {
280
  char *res = NULL;
281
  for (; *fname != 0; fname++) {
282
    if (*fname == '.') res = fname + 1;
283
  }
284
  /* if no dot found, then point to the string's null terminator */
285
  if (res == NULL) return(fname);
286
  return(res);
287
}