This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH] string: add GNU basename(3)
- From: Yaakov Selkowitz <yselkowi at redhat dot com>
- To: newlib at sourceware dot org
- Date: Fri, 13 Mar 2015 11:25:13 -0500
- Subject: [PATCH] string: add GNU basename(3)
- Authentication-results: sourceware.org; auth=none
* libc/include/libgen.h (_BASENAME_DEFINED): Define.
* libc/include/string.h (basename): Declare.
* libc/string/Makefile.am (ELIX_4_SOURCES): Add gnu_basename.c.
* libc/string/Makefile.in: Regenerate.
* libc/string/gnu_basename.c: New file.
---
Patch for Cygwin to follow.
newlib/libc/include/libgen.h | 1 +
newlib/libc/include/string.h | 14 ++++++++++++++
newlib/libc/string/Makefile.am | 1 +
newlib/libc/string/Makefile.in | 13 +++++++++++--
newlib/libc/string/gnu_basename.c | 32 ++++++++++++++++++++++++++++++++
5 files changed, 59 insertions(+), 2 deletions(-)
create mode 100644 newlib/libc/string/gnu_basename.c
diff --git a/newlib/libc/include/libgen.h b/newlib/libc/include/libgen.h
index abfab0e..8360a22 100644
--- a/newlib/libc/include/libgen.h
+++ b/newlib/libc/include/libgen.h
@@ -13,6 +13,7 @@ extern "C" {
#endif
char *_EXFUN(basename, (char *));
+#define _BASENAME_DEFINED
char *_EXFUN(dirname, (char *));
#ifdef __cplusplus
diff --git a/newlib/libc/include/string.h b/newlib/libc/include/string.h
index 43b3626..58cc0c6 100644
--- a/newlib/libc/include/string.h
+++ b/newlib/libc/include/string.h
@@ -163,6 +163,20 @@ int _EXFUN(strtosigno, (const char *__name));
(char *) memcpy (__out, __in, __len-1);}))
#endif /* _GNU_SOURCE && __GNUC__ */
+/* There are two common basename variants. If you #include <libgen.h>
+ first, you get the POSIX version; otherwise you get the GNU version.
+ POSIX requires that #undef basename will still let you
+ invoke the underlying function, but that requires gcc support. */
+#if __GNU_VISIBLE && !defined(_BASENAME_DEFINED)
+# ifdef __GNUC__
+char *_EXFUN(basename,(const char *))
+ __asm__ (__ASMNAME ("__gnu_basename"));
+# else
+char *_EXFUN(__gnu_basename,(const char *));
+# define basename __gnu_basename
+# endif
+#endif
+
#include <sys/string.h>
_END_STD_C
diff --git a/newlib/libc/string/Makefile.am b/newlib/libc/string/Makefile.am
index e9adbe1..e73bfdc 100644
--- a/newlib/libc/string/Makefile.am
+++ b/newlib/libc/string/Makefile.am
@@ -102,6 +102,7 @@ if ELIX_LEVEL_3
ELIX_4_SOURCES =
else
ELIX_4_SOURCES = \
+ gnu_basename.c \
memmem.c \
memrchr.c \
rawmemchr.c \
diff --git a/newlib/libc/string/Makefile.in b/newlib/libc/string/Makefile.in
index 255ee07..bcebcbd 100644
--- a/newlib/libc/string/Makefile.in
+++ b/newlib/libc/string/Makefile.in
@@ -118,7 +118,8 @@ am__objects_1 = lib_a-bcopy.$(OBJEXT) lib_a-bzero.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-wcpcpy.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-wcpncpy.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@ lib_a-wcsdup.$(OBJEXT)
-@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@am__objects_3 = lib_a-memmem.$(OBJEXT) \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@am__objects_3 = lib_a-gnu_basename.$(OBJEXT) \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ lib_a-memmem.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ lib_a-memrchr.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ lib_a-rawmemchr.$(OBJEXT) \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ lib_a-wcscasecmp.$(OBJEXT) \
@@ -145,7 +146,8 @@ am__objects_4 = bcopy.lo bzero.lo index.lo memchr.lo memcmp.lo \
@ELIX_LEVEL_1_FALSE@ stpcpy.lo stpncpy.lo strndup.lo \
@ELIX_LEVEL_1_FALSE@ strcasestr.lo strchrnul.lo strndup_r.lo \
@ELIX_LEVEL_1_FALSE@ wcpcpy.lo wcpncpy.lo wcsdup.lo
-@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@am__objects_6 = memmem.lo \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@am__objects_6 = gnu_basename.lo \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ memmem.lo \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ memrchr.lo \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ rawmemchr.lo \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ wcscasecmp.lo \
@@ -410,6 +412,7 @@ GENERAL_SOURCES = \
@ELIX_LEVEL_1_TRUE@ELIX_2_SOURCES =
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ELIX_4_SOURCES = \
+@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ gnu_basename.c \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ memmem.c \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ memrchr.c \
@ELIX_LEVEL_1_FALSE@@ELIX_LEVEL_2_FALSE@@ELIX_LEVEL_3_FALSE@ rawmemchr.c \
@@ -998,6 +1001,12 @@ lib_a-wcsdup.o: wcsdup.c
lib_a-wcsdup.obj: wcsdup.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-wcsdup.obj `if test -f 'wcsdup.c'; then $(CYGPATH_W) 'wcsdup.c'; else $(CYGPATH_W) '$(srcdir)/wcsdup.c'; fi`
+lib_a-gnu_basename.o: gnu_basename.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-gnu_basename.o `test -f 'gnu_basename.c' || echo '$(srcdir)/'`gnu_basename.c
+
+lib_a-gnu_basename.obj: gnu_basename.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-gnu_basename.obj `if test -f 'gnu_basename.c'; then $(CYGPATH_W) 'gnu_basename.c'; else $(CYGPATH_W) '$(srcdir)/gnu_basename.c'; fi`
+
lib_a-memmem.o: memmem.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-memmem.o `test -f 'memmem.c' || echo '$(srcdir)/'`memmem.c
diff --git a/newlib/libc/string/gnu_basename.c b/newlib/libc/string/gnu_basename.c
new file mode 100644
index 0000000..a13c6d9
--- /dev/null
+++ b/newlib/libc/string/gnu_basename.c
@@ -0,0 +1,32 @@
+#ifndef _NO_BASENAME
+/* Copyright 2005 Shaun Jackman
+ * Copyright 2015 Red Hat, Inc.
+ * Permission to use, copy, modify, and distribute this software
+ * is freely granted, provided that this notice is preserved.
+ */
+
+/* The differences with the POSIX version (unix/basename.c):
+ * - declared in <string.h> (instead of <libgen.h>);
+ * - the argument is never modified, and therefore is marked const;
+ * - the empty string is returned if path is an empty string, "/", or ends
+ * with a trailing slash.
+ */
+
+#include <string.h>
+
+char*
+_DEFUN (__gnu_basename, (path),
+ const char *path)
+{
+ char *p;
+ if( path == NULL || *path == '\0' )
+ return "";
+ p = (char *)path + strlen(path) - 1;
+ if( *p == '/' )
+ return "";
+ while( p >= path && *p != '/' )
+ p--;
+ return p + 1;
+}
+
+#endif /* !_NO_BASENAME */
--
2.1.4