diff options
author | Matt Kraai <kraai@debian.org> | 2001-08-14 17:10:08 +0000 |
---|---|---|
committer | Matt Kraai <kraai@debian.org> | 2001-08-14 17:10:08 +0000 |
commit | 0a6859031493951de02cc00e18f0da2bd305fc05 (patch) | |
tree | b6f4f4f69192156419086d4a09532b60b542df15 | |
parent | c639a35f50014bf9d43b6893b7ed92564e69a9a9 (diff) | |
download | busybox-w32-0a6859031493951de02cc00e18f0da2bd305fc05.tar.gz busybox-w32-0a6859031493951de02cc00e18f0da2bd305fc05.tar.bz2 busybox-w32-0a6859031493951de02cc00e18f0da2bd305fc05.zip |
Rewritten by Manuel Novoa III.
-rw-r--r-- | libbb/simplify_path.c | 67 |
1 files changed, 28 insertions, 39 deletions
diff --git a/libbb/simplify_path.c b/libbb/simplify_path.c index 4641bae88..cf5b838a1 100644 --- a/libbb/simplify_path.c +++ b/libbb/simplify_path.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * simplify_path implementation for busybox | 3 | * simplify_path implementation for busybox |
4 | * | 4 | * |
5 | * | 5 | * |
6 | * Copyright (C) 2001 Vladimir N. Oleynik <dzo@simtreas.ru> | 6 | * Copyright (C) 2001 Manuel Novoa III <mjn3@opensource.lineo.com> |
7 | * | 7 | * |
8 | * | 8 | * |
9 | * This program is free software; you can redistribute it and/or modify | 9 | * This program is free software; you can redistribute it and/or modify |
@@ -26,53 +26,42 @@ | |||
26 | 26 | ||
27 | #include "libbb.h" | 27 | #include "libbb.h" |
28 | 28 | ||
29 | static inline char *strcpy_overlap(char *dst, const char *src) | ||
30 | { | ||
31 | char *ptr = dst; | ||
32 | |||
33 | do *dst++ = *src; while (*src++); | ||
34 | return ptr; | ||
35 | } | ||
36 | |||
37 | char *simplify_path(const char *path) | 29 | char *simplify_path(const char *path) |
38 | { | 30 | { |
39 | char *s, *start, *next; | 31 | char *s, *start, *p; |
40 | 32 | ||
41 | if (path[0] == '/') | 33 | if (path[0] == '/') |
42 | start = xstrdup(path); | 34 | start = xstrdup(path); |
43 | else { | 35 | else { |
44 | s = xgetcwd(NULL); | 36 | s = xgetcwd(NULL); |
45 | start = concat_path_file(s, path); | 37 | start = concat_path_file(s, path); |
46 | free(s); | 38 | free(s); |
47 | } | 39 | } |
48 | s = start; | 40 | p = s = start; |
49 | /* remove . and .. */ | ||
50 | while(*s) { | ||
51 | if(*s++ == '/' && (*s == '/' || *s == 0)) { | ||
52 | /* remove duplicate and trailing slashes */ | ||
53 | s = strcpy_overlap(s-1, s); | ||
54 | } | ||
55 | else if(*(s-1) == '.' && *(s-2)=='/') { | ||
56 | if(*s == '/' || *s == 0) { | ||
57 | /* remove . */ | ||
58 | s = strcpy_overlap(s-1, s); /* maybe set // */ | ||
59 | s--; | ||
60 | } else if(*s == '.') { | ||
61 | next = s+1; /* set after ".." */ | ||
62 | if(*next == '/' || *next == 0) { /* "../" */ | ||
63 | if((s-=2) > start) | ||
64 | /* skip previous dir */ | ||
65 | do s--; while(*s != '/'); | ||
66 | /* remove previous dir */ | ||
67 | strcpy_overlap(s, next); | ||
68 | } | ||
69 | 41 | ||
42 | do { | ||
43 | if (*p == '/') { | ||
44 | if (*s == '/') { /* skip duplicate (or initial) slash */ | ||
45 | continue; | ||
46 | } else if (*s == '.') { | ||
47 | if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ | ||
48 | continue; | ||
49 | } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { | ||
50 | ++s; | ||
51 | if (p > start) { | ||
52 | while (*--p != '/'); /* omit previous dir */ | ||
53 | } | ||
54 | continue; | ||
55 | } | ||
70 | } | 56 | } |
71 | } | 57 | } |
58 | *++p = *s; | ||
59 | } while (*++s); | ||
60 | |||
61 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ | ||
62 | ++p; /* so keep last character */ | ||
72 | } | 63 | } |
73 | if(start[0]==0) { | 64 | *p = 0; |
74 | start[0]='/'; | 65 | |
75 | start[1]=0; | ||
76 | } | ||
77 | return start; | 66 | return start; |
78 | } | 67 | } |