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 | |||||
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 12 | char* FAST_FUNC 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 { |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 19 | s = xrealloc_getcwd_or_warn(NULL); |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 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; | ||||
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 29 | } |
30 | if (*s == '.') { | ||||
31 | if (s[1] == '/' || !s[1]) { /* remove extra '.' */ | ||||
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 32 | continue; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 33 | } |
34 | if ((s[1] == '.') && (s[2] == '/' || !s[2])) { | ||||
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 35 | ++s; |
36 | if (p > start) { | ||||
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 37 | while (*--p != '/') /* omit previous dir */ |
38 | continue; | ||||
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 39 | } |
40 | continue; | ||||
41 | } | ||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 42 | } |
43 | } | ||||
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 44 | *++p = *s; |
45 | } while (*++s); | ||||
46 | |||||
47 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ | ||||
48 | ++p; /* so keep last character */ | ||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 49 | } |
Matt Kraai | 0a68590 | 2001-08-14 17:10:08 +0000 | [diff] [blame] | 50 | *p = 0; |
51 | |||||
Matt Kraai | a7cecbc | 2001-08-10 15:05:27 +0000 | [diff] [blame] | 52 | return start; |
53 | } |