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 | * |
Rob Landley | 3570915 | 2006-03-26 21:49:42 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 11 | /* |
| 12 | * "/" -> "/" |
| 13 | * "abc" -> "abc" |
| 14 | * "abc/def" -> "def" |
| 15 | * "abc/def/" -> "" |
| 16 | */ |
| 17 | char *bb_get_last_path_component_nostrip(const char *path) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 19 | char *slash = strrchr(path, '/'); |
Eric Andersen | 32574a4 | 2001-06-15 20:10:39 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 21 | if (!slash || (slash == path && !slash[1])) |
| 22 | return (char*)path; |
Manuel Novoa III | 3280f9a | 2001-12-05 04:35:32 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 24 | return slash + 1; |
| 25 | } |
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 | /* |
| 28 | * "/" -> "/" |
| 29 | * "abc" -> "abc" |
| 30 | * "abc/def" -> "def" |
| 31 | * "abc/def/" -> "def" !! |
| 32 | */ |
| 33 | char *bb_get_last_path_component_strip(char *path) |
| 34 | { |
| 35 | char *slash = last_char_is(path, '/'); |
Eric Andersen | 32574a4 | 2001-06-15 20:10:39 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 37 | if (slash) |
| 38 | while (*slash == '/' && slash != path) |
| 39 | *slash-- = '\0'; |
| 40 | |
| 41 | return bb_get_last_path_component_nostrip(path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 42 | } |