Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||||
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * bb_simplify_path implementation for busybox |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org> |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 8 | */ |
9 | |||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
11 | |||||
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | char *bb_simplify_path(const char *path) |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 13 | { |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 14 | char *s, *start, *p; |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 15 | |
16 | if (path[0] == '/') | ||||
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 17 | start = xstrdup(path); |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 18 | else { |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 19 | s = xgetcwd(NULL); |
20 | start = concat_path_file(s, path); | ||||
21 | free(s); | ||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 22 | } |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 23 | p = s = start; |
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 24 | |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 25 | do { |
26 | if (*p == '/') { | ||||
27 | if (*s == '/') { /* skip duplicate (or initial) slash */ | ||||
28 | continue; | ||||
29 | } else if (*s == '.') { | ||||
30 | if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ | ||||
31 | continue; | ||||
32 | } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { | ||||
33 | ++s; | ||||
34 | if (p > start) { | ||||
35 | while (*--p != '/'); /* omit previous dir */ | ||||
36 | } | ||||
37 | continue; | ||||
38 | } | ||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 39 | } |
40 | } | ||||
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 41 | *++p = *s; |
42 | } while (*++s); | ||||
43 | |||||
44 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ | ||||
45 | ++p; /* so keep last character */ | ||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 46 | } |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 47 | *p = 0; |
48 | |||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 49 | return start; |
50 | } |