Upgrade netcat a lot. Make -e able to take the rest of the command line as
[oweals/busybox.git] / libbb / simplify_path.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * bb_simplify_path implementation for busybox
4  *
5  * Copyright (C) 2001  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <stdlib.h>
11 #include "libbb.h"
12
13 char *bb_simplify_path(const char *path)
14 {
15         char *s, *start, *p;
16
17         if (path[0] == '/')
18                 start = bb_xstrdup(path);
19         else {
20                 s = xgetcwd(NULL);
21                 start = concat_path_file(s, path);
22                 free(s);
23         }
24         p = s = start;
25
26         do {
27                 if (*p == '/') {
28                         if (*s == '/') {        /* skip duplicate (or initial) slash */
29                                 continue;
30                         } else if (*s == '.') {
31                                 if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */
32                                         continue;
33                                 } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) {
34                                         ++s;
35                                         if (p > start) {
36                                                 while (*--p != '/');    /* omit previous dir */
37                                         }
38                                         continue;
39                                 }
40                         }
41                 }
42                 *++p = *s;
43         } while (*++s);
44
45         if ((p == start) || (*p != '/')) {      /* not a trailing slash */
46                 ++p;                                    /* so keep last character */
47         }
48         *p = 0;
49
50         return start;
51 }