aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbb/get_last_path_component.c63
1 files changed, 27 insertions, 36 deletions
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c
index 85c7609c9..6af726c83 100644
--- a/libbb/get_last_path_component.c
+++ b/libbb/get_last_path_component.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * get_last_path_component implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> 5 * Copyright (C) 2001 Manuel Novoa III <mjn3@opensource.lineo.com>
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,49 +17,40 @@
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 *
20 */ 21 */
21 22
22#include <stdio.h> 23/* Set to 1 if you want basename() behavior for NULL or "". */
23#include <string.h> 24/* WARNING!!! Doing so will break basename applet at least! */
24#include "libbb.h" 25#define EMULATE_BASENAME 0
25
26 26
27char *get_last_path_component(char *path) 27char *get_last_path_component(char *path)
28{ 28{
29 char *s; 29#if EMULATE_BASENAME
30 register char *ptr = path; 30 static const char null_or_empty[] = ".";
31 register char *prev = 0; 31#endif
32 char *first = path;
33 char *last;
34
35#if EMULATE_BASENAME
36 if (!path || !*path) {
37 return (char *) null_or_empty;
38 }
39#endif
32 40
33 while (*ptr) 41 last = path - 1;
34 ptr++;
35 s = ptr - 1;
36 42
37 /* strip trailing slashes */ 43 while (*path) {
38 while (s != path && *s == '/') { 44 if ((*path != '/') && (path > ++last)) {
39 *s-- = '\0'; 45 last = first = path;
46 }
47 ++path;
40 } 48 }
41 49
42 /* find last component */ 50 if (*first == '/') {
43 ptr = path; 51 last = first;
44 while (*ptr != '\0') {
45 if (*ptr == '/')
46 prev = ptr;
47 ptr++;
48 } 52 }
49 s = prev; 53 last[1] = 0;
50 54
51 if (s == NULL || s[1] == '\0') 55 return first;
52 return path;
53 else
54 return s+1;
55} 56}
56
57
58/* END CODE */
59/*
60Local Variables:
61c-file-style: "linux"
62c-basic-offset: 4
63tab-width: 4
64End:
65*/