-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpath.c
503 lines (438 loc) · 11.6 KB
/
path.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* @file path.c
* @brief Path manipulation helper functions
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.4.4
**/
//Dependencies
#include <string.h>
#include <ctype.h>
#include "path.h"
/**
* @brief Test if the path is absolute
* @param[in] path NULL-terminated string that contains the path
* @return TRUE is the path is absolute, else FALSE
**/
bool_t pathIsAbsolute(const char_t *path)
{
//Determine if the path is absolute or relative
if(path[0] == '/' || path[0] == '\\')
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* @brief Test if the path is relative
* @param[in] path NULL-terminated string that contains the path
* @return TRUE is the path is relative, else FALSE
**/
bool_t pathIsRelative(const char_t *path)
{
//Determine if the path is absolute or relative
if(path[0] == '/' || path[0] == '\\')
{
return FALSE;
}
else
{
return TRUE;
}
}
/**
* @brief Extract the file name from the supplied path
* @param[in] path NULL-terminated string that contains the path
* @return Pointer to the file name
**/
const char_t *pathGetFilename(const char_t *path)
{
size_t n;
//Retrieve the length of the path
n = osStrlen(path);
//Skip trailing slash or backslash characters
while(n > 0)
{
//Forward slash or backslash character found?
if(path[n - 1] != '/' && path[n - 1] != '\\')
break;
//Previous character
n--;
}
//Search the string for the last separator
while(n > 0)
{
//Forward slash or backslash character found?
if(path[n - 1] == '/' || path[n - 1] == '\\')
break;
//Previous character
n--;
}
//Return a pointer to the file name
return path + n;
}
/**
* @brief Remove the trailing file name from the supplied path
* @param[in] path NULL-terminated string that contains the path
**/
void pathRemoveFilename(char_t *path)
{
char_t *p;
//Remove the trailing file name and backslash from a path
p = (char_t *) pathGetFilename(path);
*p = '\0';
}
/**
* @brief Copy a path
* @param[out] dest Pointer to the destination buffer
* @param[in] src Pointer to the source path
* @param[in] maxLen Maximum pathname length
**/
void pathCopy(char_t *dest, const char_t *src, size_t maxLen)
{
size_t n;
//Get the length of the source path
n = osStrlen(src);
//Limit the number of characters to be copied
n = MIN(n, maxLen);
//Copy the string
osMemcpy(dest, src, n);
//Properly terminate the string with a NULL character
dest[n] = '\0';
}
/**
* @brief Simplify a path
* @param[in] path NULL-terminated string containing the path to be canonicalized
**/
void pathCanonicalize(char_t *path)
{
size_t i;
size_t j;
size_t k;
//Move to the beginning of the string
i = 0;
k = 0;
//Replace backslashes with forward slashes
while(path[i] != '\0')
{
//Forward slash or backslash separator found?
if(path[i] == '/' || path[i] == '\\')
{
path[k++] = '/';
while(path[i] == '/' || path[i] == '\\')
{
i++;
}
}
else
{
path[k++] = path[i++];
}
}
//Properly terminate the string with a NULL character
path[k] = '\0';
//Move back to the beginning of the string
i = 0;
j = 0;
k = 0;
//Parse the entire string
do
{
//Forward slash separator found?
if(path[i] == '/' || path[i] == '\0')
{
//"." element found?
if((i - j) == 1 && osStrncmp(path + j, ".", 1) == 0)
{
//Check whether the pathname is empty?
if(k == 0)
{
if(path[i] == '\0')
{
path[k++] = '.';
}
else if(path[i] == '/' && path[i + 1] == '\0')
{
path[k++] = '.';
path[k++] = '/';
}
}
else if(k > 1)
{
//Remove the final slash if necessary
if(path[i] == '\0')
k--;
}
}
//".." element found?
else if((i - j) == 2 && osStrncmp(path + j, "..", 2) == 0)
{
//Check whether the pathname is empty?
if(k == 0)
{
path[k++] = '.';
path[k++] = '.';
//Append a slash if necessary
if(path[i] == '/')
path[k++] = '/';
}
else if(k > 1)
{
//Search the path for the previous slash
for(j = 1; j < k; j++)
{
if(path[k - j - 1] == '/')
break;
}
//Slash separator found?
if(j < k)
{
if(osStrncmp(path + k - j, "..", 2) == 0)
{
path[k++] = '.';
path[k++] = '.';
}
else
{
k = k - j - 1;
}
//Append a slash if necessary
if(k == 0 && path[0] == '/')
{
path[k++] = '/';
}
else if(path[i] == '/')
{
path[k++] = '/';
}
}
//No slash separator found?
else
{
if(k == 3 && osStrncmp(path, "..", 2) == 0)
{
path[k++] = '.';
path[k++] = '.';
//Append a slash if necessary
if(path[i] == '/')
path[k++] = '/';
}
else if(path[i] == '\0')
{
k = 0;
path[k++] = '.';
}
else if(path[i] == '/' && path[i + 1] == '\0')
{
k = 0;
path[k++] = '.';
path[k++] = '/';
}
else
{
k = 0;
}
}
}
}
else
{
//Copy directory name
osMemmove(path + k, path + j, i - j);
//Advance write pointer
k += i - j;
//Append a slash if necessary
if(path[i] == '/')
path[k++] = '/';
}
//Move to the next token
while(path[i] == '/')
{
i++;
}
j = i;
}
} while(path[i++] != '\0');
//Properly terminate the string with a NULL character
path[k] = '\0';
}
/**
* @brief Add a slash to the end of a string
* @param[in,out] path NULL-terminated string that represents the path
* @param[in] maxLen Maximum pathname length
**/
void pathAddSlash(char_t *path, size_t maxLen)
{
size_t n;
//Retrieve the length of the string
n = osStrlen(path);
//Add a slash character only if necessary
if(!n)
{
//Check the length of the resulting string
if(maxLen >= 1)
osStrcpy(path, "/");
}
else if(path[n - 1] != '/' && path[n - 1] != '\\')
{
//Check the length of the resulting string
if(maxLen >= (n + 1))
osStrcat(path, "/");
}
}
/**
* @brief Remove the trailing slash from a given path
* @param[in,out] path NULL-terminated string that contains the path
**/
void pathRemoveSlash(char_t *path)
{
char_t *end;
//Skip the leading slash character
if(pathIsAbsolute(path))
path++;
//Search for the first slash character to be removed
for(end = NULL; *path != '\0'; path++)
{
if(*path != '/' && *path != '\\')
{
end = NULL;
}
else if(!end)
{
end = path;
}
}
//Remove the trailing slash characters
if(end)
*end = '\0';
}
/**
* @brief Concatenate two paths
* @param[in,out] path NULL-terminated string containing the first path
* @param[in] more NULL-terminated string containing the second path
* @param[in] maxLen Maximum pathname length
**/
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
{
size_t n1;
size_t n2;
//Append a slash character to the first path
if(*path != '\0')
pathAddSlash(path, maxLen);
//Skip any slash character at the beginning of the second path
while(*more == '/' || *more == '\\')
{
more++;
}
//Retrieve the length of the first path
n1 = osStrlen(path);
//Retrieve the length of second path
n2 = osStrlen(more);
//Check the length of the resulting string
if(n1 < maxLen)
{
//Limit the number of characters to be copied
n2 = MIN(n2, maxLen - n1);
//Concatenate the resulting string
osMemcpy(path + n1, more, n2);
//Properly terminate the string with a NULL character
path[n1 + n2] = '\0';
}
}
/**
* @brief Check whether a file name matches the specified pattern
* @param[in] path NULL-terminated string that contains the path to be matched
* @param[in] pattern NULL-terminated string that contains the pattern for
* which to search. The pattern may contain wildcard characters
* @return TRUE if the path matches the specified pattern, else FALSE
**/
bool_t pathMatch(const char_t *path, const char_t *pattern)
{
size_t i = 0;
size_t j = 0;
//Parse the pattern string
while(pattern[j] != '\0')
{
//Any wildcard character found?
if(pattern[j] == '?')
{
//The question mark matches a single character
if(path[i] == '\0')
{
return FALSE;
}
else
{
//Advance position in pathname
i++;
//Advance position in pattern string
j++;
}
}
else if(pattern[j] == '*')
{
//The asterisk sign matches zero or more characters
if(path[i] == '\0')
{
//Advance position in pattern string
j++;
}
else if(pathMatch(path + i, pattern + j + 1))
{
return TRUE;
}
else
{
//Advance position in pathname
i++;
}
}
else
{
//Case insensitive comparison
if(osTolower(path[i]) != osTolower(pattern[j]))
{
return FALSE;
}
else
{
//Advance position in pathname
i++;
//Advance position in pattern string
j++;
}
}
}
//Check whether the file name matches the specified pattern
if(path[i] == '\0' && pattern[j] == '\0')
{
return TRUE;
}
else
{
return FALSE;
}
}