Implement the GNU basename(3) in addition to the POSIX one.

Code like perf(1) needs this.

Bug: 11860789
Change-Id: I907eb448052a7b165e4012d74303330d32328cb2
This commit is contained in:
Elliott Hughes 2014-08-19 14:30:30 -07:00
parent c5d6df6f2f
commit 09c39d6df0
6 changed files with 88 additions and 9 deletions

View File

@ -122,6 +122,7 @@ libc_bionic_src_files := \
bionic/getpgrp.cpp \
bionic/getpid.cpp \
bionic/gettid.cpp \
bionic/__gnu_basename.cpp \
bionic/inotify_init.cpp \
bionic/lchown.cpp \
bionic/lfs64_support.cpp \

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2014 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define _GNU_SOURCE 1
#include <string.h>
extern "C" const char* __gnu_basename(const char* path) {
const char* last_slash = strrchr(path, '/');
return (last_slash != NULL) ? last_slash + 1 : path;
}

View File

@ -25,6 +25,7 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _LIBGEN_H
#define _LIBGEN_H
@ -33,9 +34,18 @@
__BEGIN_DECLS
#if !defined(__bionic_using_gnu_basename)
/*
* <string.h> gets you the GNU basename.
* <libgen.h> the POSIX one.
* Note that our "POSIX" one has the wrong argument cv-qualifiers.
*/
extern char* basename(const char*);
#define __bionic_using_posix_basename
#endif
/* our version of dirname/basename don't modify the input path */
extern char* dirname (const char* path);
extern char* basename(const char* path);
/* special thread-safe Bionic versions
*

View File

@ -25,8 +25,9 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STRING_H_
#define _STRING_H_
#ifndef _STRING_H
#define _STRING_H
#include <sys/cdefs.h>
#include <stddef.h>
@ -92,6 +93,20 @@ extern size_t strxfrm(char* __restrict, const char* __restrict, size_t);
extern int strcoll_l(const char *, const char *, locale_t) __purefunc;
extern size_t strxfrm_l(char* __restrict, const char* __restrict, size_t, locale_t);
#if defined(__USE_GNU) && !defined(__bionic_using_posix_basename)
/*
* glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
* It doesn't modify its argument, and in C++ it's const-correct.
*/
#if defined(__cplusplus)
extern "C++" char* basename(char*) __RENAME(__gnu_basename) __nonnull((1));
extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
#else
extern char* basename(const char*) __RENAME(__gnu_basename) __nonnull((1));
#endif
#define __bionic_using_gnu_basename
#endif
#if defined(__BIONIC_FORTIFY)
__BIONIC_FORTIFY_INLINE
@ -289,4 +304,4 @@ char* strrchr(const char *s, int c) {
__END_DECLS
#endif /* _STRING_H_ */
#endif /* _STRING_H */

View File

@ -14,11 +14,10 @@
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <libgen.h>
#include <errno.h>
#include <gtest/gtest.h>
static void TestBasename(const char* in, const char* expected_out) {
char* writable_in = (in != NULL) ? strdup(in) : NULL;
@ -40,7 +39,7 @@ static void TestDirname(const char* in, const char* expected_out) {
// Do not use basename as the test name, it's defined to another value in glibc
// so leads to a differently named test on host versus target architectures.
TEST(libgen, basename_smoke) {
TEST(libgen, posix_basename) {
TestBasename(NULL, ".");
TestBasename("", ".");
TestBasename("/usr/lib", "lib");

View File

@ -14,12 +14,12 @@
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <string.h>
#include <errno.h>
#include <gtest/gtest.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include "buffer_tests.h"
@ -1287,3 +1287,22 @@ TEST(string, strchr_align) {
TEST(string, strchr_overread) {
RunSingleBufferOverreadTest(DoStrchrTest);
}
static void TestBasename(const char* in, const char* expected_out) {
errno = 0;
const char* out = basename(in);
ASSERT_STREQ(expected_out, out) << in;
ASSERT_EQ(0, errno) << in;
}
TEST(string, __gnu_basename) {
TestBasename("", "");
TestBasename("/usr/lib", "lib");
TestBasename("/usr/", "");
TestBasename("usr", "usr");
TestBasename("/", "");
TestBasename(".", ".");
TestBasename("..", "..");
TestBasename("///", "");
TestBasename("//usr//lib//", "");
}