Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * bb_get_last_path_component implementation for busybox |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 10 | |
| 11 | const char* FAST_FUNC bb_basename(const char *name) |
| 12 | { |
| 13 | const char *cp = strrchr(name, '/'); |
| 14 | if (cp) |
| 15 | return cp + 1; |
| 16 | return name; |
| 17 | } |
| 18 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 19 | /* |
| 20 | * "/" -> "/" |
| 21 | * "abc" -> "abc" |
| 22 | * "abc/def" -> "def" |
| 23 | * "abc/def/" -> "" |
| 24 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 25 | char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 27 | char *slash = strrchr(path, '/'); |
Eric Andersen | 32574a4 | 2001-06-15 20:10:39 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 29 | if (!slash || (slash == path && !slash[1])) |
| 30 | return (char*)path; |
Manuel Novoa III | 3280f9a | 2001-12-05 04:35:32 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 32 | return slash + 1; |
| 33 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 35 | /* |
| 36 | * "/" -> "/" |
| 37 | * "abc" -> "abc" |
| 38 | * "abc/def" -> "def" |
| 39 | * "abc/def/" -> "def" !! |
| 40 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 41 | char* FAST_FUNC bb_get_last_path_component_strip(char *path) |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 42 | { |
| 43 | char *slash = last_char_is(path, '/'); |
Eric Andersen | 32574a4 | 2001-06-15 20:10:39 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 45 | if (slash) |
| 46 | while (*slash == '/' && slash != path) |
| 47 | *slash-- = '\0'; |
| 48 | |
| 49 | return bb_get_last_path_component_nostrip(path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | } |