blob: 72598d22e6bd37780aeb948a5822556420ffa790 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * bb_get_last_path_component implementation for busybox
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000010#include "libbb.h"
Denis Vlasenko818322b2007-09-24 18:27:04 +000011/*
12 * "/" -> "/"
13 * "abc" -> "abc"
14 * "abc/def" -> "def"
15 * "abc/def/" -> ""
16 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000017char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
Eric Andersenaad1a882001-03-16 22:47:14 +000018{
Denis Vlasenko818322b2007-09-24 18:27:04 +000019 char *slash = strrchr(path, '/');
Eric Andersen32574a42001-06-15 20:10:39 +000020
Denis Vlasenko818322b2007-09-24 18:27:04 +000021 if (!slash || (slash == path && !slash[1]))
22 return (char*)path;
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000023
Denis Vlasenko818322b2007-09-24 18:27:04 +000024 return slash + 1;
25}
Eric Andersenaad1a882001-03-16 22:47:14 +000026
Denis Vlasenko818322b2007-09-24 18:27:04 +000027/*
28 * "/" -> "/"
29 * "abc" -> "abc"
30 * "abc/def" -> "def"
31 * "abc/def/" -> "def" !!
32 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000033char* FAST_FUNC bb_get_last_path_component_strip(char *path)
Denis Vlasenko818322b2007-09-24 18:27:04 +000034{
35 char *slash = last_char_is(path, '/');
Eric Andersen32574a42001-06-15 20:10:39 +000036
Denis Vlasenko818322b2007-09-24 18:27:04 +000037 if (slash)
38 while (*slash == '/' && slash != path)
39 *slash-- = '\0';
40
41 return bb_get_last_path_component_nostrip(path);
Eric Andersenaad1a882001-03-16 22:47:14 +000042}