9 |
|
|
10 |
#include "libbb.h" |
#include "libbb.h" |
11 |
|
|
12 |
char *bb_simplify_path(const char *path) |
char* FAST_FUNC bb_simplify_path(const char *path) |
13 |
{ |
{ |
14 |
char *s, *start, *p; |
char *s, *start, *p; |
15 |
|
|
16 |
if (path[0] == '/') |
if (path[0] == '/') |
17 |
start = xstrdup(path); |
start = xstrdup(path); |
18 |
else { |
else { |
19 |
s = xgetcwd(NULL); |
s = xrealloc_getcwd_or_warn(NULL); |
20 |
start = concat_path_file(s, path); |
start = concat_path_file(s, path); |
21 |
free(s); |
free(s); |
22 |
} |
} |
26 |
if (*p == '/') { |
if (*p == '/') { |
27 |
if (*s == '/') { /* skip duplicate (or initial) slash */ |
if (*s == '/') { /* skip duplicate (or initial) slash */ |
28 |
continue; |
continue; |
29 |
} else if (*s == '.') { |
} |
30 |
if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ |
if (*s == '.') { |
31 |
|
if (s[1] == '/' || !s[1]) { /* remove extra '.' */ |
32 |
continue; |
continue; |
33 |
} else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { |
} |
34 |
|
if ((s[1] == '.') && (s[2] == '/' || !s[2])) { |
35 |
++s; |
++s; |
36 |
if (p > start) { |
if (p > start) { |
37 |
while (*--p != '/'); /* omit previous dir */ |
while (*--p != '/') /* omit previous dir */ |
38 |
|
continue; |
39 |
} |
} |
40 |
continue; |
continue; |
41 |
} |
} |