blob: b7bc0e626ae1b6a8175ad83a9bd842b68d2ecdaf [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"
11
Manuel Novoa III cad53642003-03-19 09:13:01 +000012char *bb_get_last_path_component(char *path)
Eric Andersenaad1a882001-03-16 22:47:14 +000013{
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000014 char *first = path;
15 char *last;
Eric Andersen32574a42001-06-15 20:10:39 +000016
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000017 last = path - 1;
18
19 while (*path) {
20 if ((*path != '/') && (path > ++last)) {
21 last = first = path;
22 }
23 ++path;
Eric Andersenaad1a882001-03-16 22:47:14 +000024 }
25
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000026 if (*first == '/') {
27 last = first;
Eric Andersen32574a42001-06-15 20:10:39 +000028 }
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000029 last[1] = '\0';
Eric Andersen32574a42001-06-15 20:10:39 +000030
Manuel Novoa III 3280f9a2001-12-05 04:35:32 +000031 return first;
Eric Andersenaad1a882001-03-16 22:47:14 +000032}