aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-08-14 17:10:08 +0000
committerkraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-08-14 17:10:08 +0000
commitfc385deb4270283400ffcfa9fc464c5bbf20e481 (patch)
treeb6f4f4f69192156419086d4a09532b60b542df15
parent19dd6dbe00456c4ec01d0d4cbc16418256d13f47 (diff)
downloadbusybox-w32-fc385deb4270283400ffcfa9fc464c5bbf20e481.tar.gz
busybox-w32-fc385deb4270283400ffcfa9fc464c5bbf20e481.tar.bz2
busybox-w32-fc385deb4270283400ffcfa9fc464c5bbf20e481.zip
Rewritten by Manuel Novoa III.
git-svn-id: svn://busybox.net/trunk/busybox@3283 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--libbb/simplify_path.c67
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
29static 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
37char *simplify_path(const char *path) 29char *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}