blob: 0f602157d970ea177d32f8843b995027da4c1532 [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 *
Rob Landley35709152006-03-26 21:49:42 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 */
17char *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 */
33char *bb_get_last_path_component_strip(char *path)
34{
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}