forked from aristanetworks/bst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.c
80 lines (70 loc) · 1.72 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
/* Copyright © 2020 Arista Networks, Inc. All rights reserved.
*
* Use of this source code is governed by the MIT license that can be found
* in the LICENSE file.
*/
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h>
/* cleanpath cleans the specified absolute filepath. It does the following:
* removes duplicate slashes
* removes . components
* cancels inner .. components with preceding components */
void cleanpath(char *path) {
if (path[0] == '/') {
++path;
}
char *out = path;
char *start = path;
while (*path != '\0') {
if (*path == '/') {
// empty component
++path;
} else if (*path == '.' && (*(path+1) == '\0' || *(path+1) == '/')) {
// . component
++path;
} else if (*path == '.' && *(path+1) == '.' && (*(path+2) == '\0' || *(path+2) == '/')) {
// .. component
path += 2;
if (out > start) {
--out;
for (; out > start && *out != '/'; --out) {
continue;
}
}
} else {
// normal component
if (out != start) {
*out = '/';
++out;
}
for (; *path != '\0' && *path != '/'; ++path, ++out) {
*out = *path;
}
}
}
*out = '\0';
}
/* This constructs a path in `out`, using a format string and arguments.
`out` must be of size PATH_MAX. */
void makepath_r(char *out, char *fmt, ...) {
va_list vl;
va_start(vl, fmt);
if (vsnprintf(out, PATH_MAX, fmt, vl) >= PATH_MAX) {
errx(1, "makepath: resulting path larger than PATH_MAX.");
}
va_end(vl);
cleanpath(out);
}
char *makepath(char *fmt, ...) {
static char buf[PATH_MAX];
va_list vl;
va_start(vl, fmt);
if (vsnprintf(buf, PATH_MAX, fmt, vl) >= PATH_MAX) {
errx(1, "makepath: resulting path larger than PATH_MAX.");
}
va_end(vl);
cleanpath(buf);
return buf;
}