[PATCH] Check for existence of GNU-style basename()

2017-02-22 Thread Ulf Hermann
If it doesn't exist, add an implementation to libeu.a. Include system.h
and link against libeu.a where it is used.

Signed-off-by: Ulf Hermann 
---
 ChangeLog |  4 
 configure.ac  |  5 +
 lib/ChangeLog |  7 +++
 lib/Makefile.am   |  4 
 lib/basename.c| 41 +++
 lib/system.h  |  4 
 libdw/ChangeLog   |  4 
 libdw/dwarf_getsrc_file.c |  2 ++
 libdwfl/ChangeLog |  4 
 libdwfl/dwfl_module_getsrc_file.c |  2 +-
 tests/ChangeLog   |  7 +++
 tests/Makefile.am |  4 ++--
 tests/show-die-info.c |  1 +
 tests/varlocs.c   |  1 +
 14 files changed, 87 insertions(+), 3 deletions(-)
 create mode 100644 lib/basename.c

diff --git a/ChangeLog b/ChangeLog
index 8e01ce2..d1e36f6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2017-02-22  Ulf Hermann  
 
+   * configure.ac: Add check GNU-style basename.
+
+2017-02-22  Ulf Hermann  
+
* configure.ac: Add checks for asprintf and vasprintf.
 
 2017-02-20  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index 3d4bb70..889f88c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -300,6 +300,11 @@ AC_CHECK_DECLS([vasprintf],[],[],
 #include ])
 AM_CONDITIONAL(HAVE_VASPRINTF, [test "x$ac_cv_have_decl_vasprintf" = "xyes"])
 
+AC_CHECK_DECLS([basename],[],[],
+   [#define _GNU_SOURCE
+#include ])
+AM_CONDITIONAL(HAVE_BASENAME, [test "x$ac_cv_have_decl_basename" = "xyes"])
+
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
 AM_CONDITIONAL(DEMANGLE, test "x$ac_cv_lib_stdcpp___cxa_demangle" = "xyes")
diff --git a/lib/ChangeLog b/lib/ChangeLog
index d0229ec..a8d9b91 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,12 @@
 2017-02-22  Ulf Hermann  
 
+   * Makefile.am (libeu_a_SOURCES): Add basname.c if no GNU-style
+   basename is available.
+   * basename.c: New file.
+   * system.h: Add declaration of basename if !HAVE_DECL_BASENAME.
+
+2017-02-22  Ulf Hermann  
+
* Makefile.am (libeu_a_SOURCES): Add asprintf.c and vasprintf.c
if !HAVE_ASPRINTF and !HAVE_VASPRINTF, respectively.
* asprintf.c: New file.
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 02e6bd9..0f3bfe7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -57,6 +57,10 @@ libeu_a_SOURCES += printversion.c color.c
 noinst_HEADERS += printversion.h color.h
 endif
 
+if !HAVE_BASENAME
+libeu_a_SOURCES += basename.c
+endif
+
 if !GPROF
 xmalloc_CFLAGS = -ffunction-sections
 endif
diff --git a/lib/basename.c b/lib/basename.c
new file mode 100644
index 000..a104db6
--- /dev/null
+++ b/lib/basename.c
@@ -0,0 +1,41 @@
+/* Implementation of GNU-style basename()
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include "system.h"
+#include 
+
+char *
+basename (const char *path)
+{
+  char *base = strrchr(path, '/');
+  return base ? base + 1 : (char *)path;
+}
diff --git a/lib/system.h b/lib/system.h
index b6f2269..7539e11 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -80,6 +80,10 @@ int asprintf (char **strp, const char *fmt, ...);
 int vasprintf (char **strp, const char *fmt, va_list ap);
 #endif
 
+#if !HAVE_DECL_BASENAME
+char *basename (const char *path);
+#endif
+
 /* A special gettext function we use if the strings are too short.  */
 #define sgettext(Str) \
   ({ const char *__res = strrchr (gettext (Str), '|');   \
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 0cc6049..694b7ce 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-22  Ulf Hermann  
+
+   * dwarf_getsrc_file.c: Include system.h.
+
 2017-02-17  Ulf Hermann  
 
* Makefile.am: Add libdw_so_LIBS to specify the archives libdw is

[PATCH] Check for existence of GNU-style basename()

2017-05-04 Thread Ulf Hermann
If it doesn't exist, add an implementation to libgnu.a and config.h.

Signed-off-by: Ulf Hermann 
---
 ChangeLog |  4 
 configure.ac  | 16 +++
 libgnu/ChangeLog  |  6 ++
 libgnu/Makefile.am|  6 +-
 libgnu/basename-gnu.c | 54 +++
 5 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 libgnu/ChangeLog
 create mode 100644 libgnu/basename-gnu.c

diff --git a/ChangeLog b/ChangeLog
index 29013e8..aa0759c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,10 @@
 
* .gitignore: Add fillfile and peel_type tests.
 
+2017-04-21  Ulf Hermann  
+
+   * configure.ac: Add check for GNU-style basename.
+
 2017-02-15  Ulf Hermann  
 
* configure.ac: Add check for mempcpy.
diff --git a/configure.ac b/configure.ac
index 0432bb1..bfdc53f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -440,6 +440,22 @@ AC_SUBST([zip_LIBS])
 
 AC_CHECK_DECLS([powerof2],[],[],[#include ])
 
+AC_CHECK_DECLS([basename],[],[],
+   [#define _GNU_SOURCE
+#include ])
+AM_CONDITIONAL(HAVE_BASENAME, [test "x$ac_cv_have_decl_basename" = "xyes"])
+
+if test "x$ac_cv_have_decl_basename" != "xyes"; then
+   AC_DEFINE([USE_REPLACEMENT_BASENAME], [1], [Use hand-rolled basename() 
replacement.])
+fi
+AH_VERBATIM([USE_REPLACEMENT_BASENAME],
+   [/* Define basename() here if it is not available from a system header. 
*/
+#undef USE_REPLACEMENT_BASENAME
+#ifdef USE_REPLACEMENT_BASENAME
+char *basename(const char *path);
+#endif
+])
+
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
 AM_CONDITIONAL(DEMANGLE, test "x$ac_cv_lib_stdcpp___cxa_demangle" = "xyes")
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog
new file mode 100644
index 000..3394de6
--- /dev/null
+++ b/libgnu/ChangeLog
@@ -0,0 +1,6 @@
+2017-05-04  Ulf Hermann  
+
+   * Makefile.am: If GNU basename is unavailable add our own
+   implementation.
+   * basename-gnu.c: New file.
+
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index 1c8e6b8..32c9aa7 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -36,7 +36,7 @@ MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
 BUILT_SOURCES =
 EXTRA_DIST = endian.in.h byteswap.in.h sys_mman.win32.h mman_win32.c 
sysconf_win32.c ar.in.h features.in.h \
- stdio_ext.in.h fts.in.h
+ stdio_ext.in.h fts.in.h basename-gnu.c
 CLEANFILES =
 SUFFIXES =
 
@@ -104,3 +104,7 @@ if USE_WIN32_SYSCONF
 libgnu_a_SOURCES += sysconf_win32.c
 endif
 endif
+
+if !HAVE_BASENAME
+libgnu_a_SOURCES += basename-gnu.c
+endif
diff --git a/libgnu/basename-gnu.c b/libgnu/basename-gnu.c
new file mode 100644
index 000..7feee81
--- /dev/null
+++ b/libgnu/basename-gnu.c
@@ -0,0 +1,54 @@
+/* Implementation of GNU-style basename()
+   Copyright (C) 2017 The Qt Company Ltd.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include "dosname.h"
+#include 
+
+/* On windows, file names with ':' in them are invalid, so we don't have to
+   add a special case for them. If we get an invalid path as input, we may
+   return a nonsensical path as output. This assumption allows us to use the
+   simple strrpos() equivalent below, without any allocation. */
+
+char *
+basename (const char *name)
+{
+  size_t prefix = FILE_SYSTEM_PREFIX_LEN(name);
+  size_t length = strlen(name);
+
+  while (length > prefix) {
+--length;
+if (ISSLASH(name[length]))
+  return (char *)name + length + 1;
+  }
+
+  return (char *)name + prefix;
+}
-- 
2.1.4