aboutsummaryrefslogtreecommitdiff
path: root/libbb/get_last_path_component.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/get_last_path_component.c')
-rw-r--r--libbb/get_last_path_component.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c
index b7bc0e626..0f602157d 100644
--- a/libbb/get_last_path_component.c
+++ b/libbb/get_last_path_component.c
@@ -8,25 +8,35 @@
8 */ 8 */
9 9
10#include "libbb.h" 10#include "libbb.h"
11 11/*
12char *bb_get_last_path_component(char *path) 12 * "/" -> "/"
13 * "abc" -> "abc"
14 * "abc/def" -> "def"
15 * "abc/def/" -> ""
16 */
17char *bb_get_last_path_component_nostrip(const char *path)
13{ 18{
14 char *first = path; 19 char *slash = strrchr(path, '/');
15 char *last; 20
21 if (!slash || (slash == path && !slash[1]))
22 return (char*)path;
16 23
17 last = path - 1; 24 return slash + 1;
25}
18 26
19 while (*path) { 27/*
20 if ((*path != '/') && (path > ++last)) { 28 * "/" -> "/"
21 last = first = path; 29 * "abc" -> "abc"
22 } 30 * "abc/def" -> "def"
23 ++path; 31 * "abc/def/" -> "def" !!
24 } 32 */
33char *bb_get_last_path_component_strip(char *path)
34{
35 char *slash = last_char_is(path, '/');
25 36
26 if (*first == '/') { 37 if (slash)
27 last = first; 38 while (*slash == '/' && slash != path)
28 } 39 *slash-- = '\0';
29 last[1] = '\0';
30 40
31 return first; 41 return bb_get_last_path_component_nostrip(path);
32} 42}