blob: 04fdf2a3e5700bc24184ed2fc0e9e47962988712 [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 */
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00009#include "libbb.h"
Denys Vlasenkob24ef032011-05-23 00:40:54 +020010
11const 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 Vlasenko818322b2007-09-24 18:27:04 +000019/*
20 * "/" -> "/"
21 * "abc" -> "abc"
22 * "abc/def" -> "def"
23 * "abc/def/" -> ""
24 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000025char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
Eric Andersenaad1a882001-03-16 22:47:14 +000026{
Denis Vlasenko818322b2007-09-24 18:27:04 +000027 char *slash = strrchr(path, '/');
Eric Andersen32574a42001-06-15 20:10:39 +000028
Denis Vlasenko818322b2007-09-24 18:27:04 +000029 if (!slash || (slash == path && !slash[1]))
30 return (char*)path;
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000031
Denis Vlasenko818322b2007-09-24 18:27:04 +000032 return slash + 1;
33}
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Denis Vlasenko818322b2007-09-24 18:27:04 +000035/*
36 * "/" -> "/"
37 * "abc" -> "abc"
38 * "abc/def" -> "def"
39 * "abc/def/" -> "def" !!
40 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000041char* FAST_FUNC bb_get_last_path_component_strip(char *path)
Denis Vlasenko818322b2007-09-24 18:27:04 +000042{
43 char *slash = last_char_is(path, '/');
Eric Andersen32574a42001-06-15 20:10:39 +000044
Denis Vlasenko818322b2007-09-24 18:27:04 +000045 if (slash)
46 while (*slash == '/' && slash != path)
47 *slash-- = '\0';
48
49 return bb_get_last_path_component_nostrip(path);
Eric Andersenaad1a882001-03-16 22:47:14 +000050}