am b9d674c8: Merge "Implement the GNU basename(3) in addition to the POSIX one."

* commit 'b9d674c8ada9c1e9c22b41bc7c89e35f11f4eb1e':
  Implement the GNU basename(3) in addition to the POSIX one.
This commit is contained in:
Elliott Hughes
2014-08-19 22:19:07 +00:00
committed by Android Git Automerger
6 changed files with 88 additions and 9 deletions

View File

@@ -121,6 +121,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 */