Subversion Repositories SvarDOS

Rev

Rev 225 | Rev 240 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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