aboutsummaryrefslogtreecommitdiff
path: root/libbb/dirname.c
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
committerManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
commitcad5364599eb5062d59e0c397ed638ddd61a8d5d (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91 /libbb/dirname.c
parente01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff)
downloadbusybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip
Major coreutils update.
Diffstat (limited to 'libbb/dirname.c')
-rw-r--r--libbb/dirname.c60
1 files changed, 37 insertions, 23 deletions
diff --git a/libbb/dirname.c b/libbb/dirname.c
index df9a49daa..81298730b 100644
--- a/libbb/dirname.c
+++ b/libbb/dirname.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini dirname function. 3 * dirname implementation for busybox (for libc's missing one)
4 * 4 *
5 * Copyright (C) 2001 Matt Kraai. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -17,39 +17,53 @@
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* Note: The previous busybox implementation did not handle NULL path
24 * and also moved a pointer before path, which is not portable in C.
25 * So I replaced it with my uClibc version.
20 */ 26 */
21 27
22#include <string.h> 28#include <string.h>
23#include "libbb.h" 29#include "libbb.h"
24 30
25#if defined __UCLIBC__ || __GNU_LIBRARY___ < 5 31#if __GNU_LIBRARY__ < 5
26
27/* Return a string containing the path name of the parent
28 * directory of PATH. */
29 32
33extern
30char *dirname(char *path) 34char *dirname(char *path)
31{ 35{
32 char *s; 36 static const char null_or_empty_or_noslash[] = ".";
33 37 register char *s;
34 /* Go to the end of the string. */ 38 register char *last;
35 s = path + strlen(path) - 1; 39 char *first;
36
37 /* Strip off trailing /s (unless it is also the leading /). */
38 while (path < s && s[0] == '/')
39 s--;
40 40
41 /* Strip the last component. */ 41 last = s = path;
42 while (path <= s && s[0] != '/')
43 s--;
44 42
45 while (path < s && s[0] == '/') 43 if (s != NULL) {
46 s--;
47 44
48 if (s < path) 45 LOOP:
49 return "."; 46 while (*s && (*s != '/')) ++s;
47 first = s;
48 while (*s == '/') ++s;
49 if (*s) {
50 last = first;
51 goto LOOP;
52 }
50 53
51 s[1] = '\0'; 54 if (last == path) {
52 return path; 55 if (*last != '/') {
56 goto DOT;
57 }
58 if ((*++last == '/') && (last[1] == 0)) {
59 ++last;
60 }
61 }
62 *last = 0;
63 return path;
64 }
65 DOT:
66 return (char *) null_or_empty_or_noslash;
53} 67}
54 68
55#endif 69#endif