Annotation of /tags/mkinitrd-6_1_12/busybox/libbb/simplify_path.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/libbb/simplify_path.c
File MIME type: text/plain
File size: 1002 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/libbb/simplify_path.c
File MIME type: text/plain
File size: 1002 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* 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 "libbb.h" | ||
11 | |||
12 | char *bb_simplify_path(const char *path) | ||
13 | { | ||
14 | char *s, *start, *p; | ||
15 | |||
16 | if (path[0] == '/') | ||
17 | start = xstrdup(path); | ||
18 | else { | ||
19 | s = xgetcwd(NULL); | ||
20 | start = concat_path_file(s, path); | ||
21 | free(s); | ||
22 | } | ||
23 | p = s = start; | ||
24 | |||
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 | } | ||
39 | } | ||
40 | } | ||
41 | *++p = *s; | ||
42 | } while (*++s); | ||
43 | |||
44 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ | ||
45 | ++p; /* so keep last character */ | ||
46 | } | ||
47 | *p = 0; | ||
48 | |||
49 | return start; | ||
50 | } |