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

Ulf Hermann ulf.hermann@qt.io
Wed Feb 22 13:41:00 GMT 2017


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 <ulf.hermann@qt.io>
---
 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  <ulf.hermann@qt.io>
 
+	* configure.ac: Add check GNU-style basename.
+
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* configure.ac: Add checks for asprintf and vasprintf.
 
 2017-02-20  Ulf Hermann  <ulf.hermann@qt.io>
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 <stdio.h>])
 AM_CONDITIONAL(HAVE_VASPRINTF, [test "x$ac_cv_have_decl_vasprintf" = "xyes"])
 
+AC_CHECK_DECLS([basename],[],[],
+               [#define _GNU_SOURCE
+                #include <string.h>])
+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  <ulf.hermann@qt.io>
 
+	* 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  <ulf.hermann@qt.io>
+
 	* 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 0000000..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 <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "system.h"
+#include <string.h>
+
+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  <ulf.hermann@qt.io>
+
+	* dwarf_getsrc_file.c: Include system.h.
+
 2017-02-17  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* Makefile.am: Add libdw_so_LIBS to specify the archives libdw is is
diff --git a/libdw/dwarf_getsrc_file.c b/libdw/dwarf_getsrc_file.c
index 5289c7d..d3538a3 100644
--- a/libdw/dwarf_getsrc_file.c
+++ b/libdw/dwarf_getsrc_file.c
@@ -36,6 +36,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <system.h>
+
 #include "libdwP.h"
 
 
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 75358dc..f5921dc 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,9 @@
 2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* dwfl_module_getsrc_file.c: Include system.h.
+
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* offline.c: Include system.h.
 
 2017-02-20  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/libdwfl/dwfl_module_getsrc_file.c b/libdwfl/dwfl_module_getsrc_file.c
index 4eaaeaf..dad19d3 100644
--- a/libdwfl/dwfl_module_getsrc_file.c
+++ b/libdwfl/dwfl_module_getsrc_file.c
@@ -28,7 +28,7 @@
 
 #include "libdwflP.h"
 #include "../libdw/libdwP.h"
-
+#include <system.h>
 
 static inline const char *
 dwfl_dwarf_line_file (const Dwarf_Line *line)
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 0e7e28c..dad2909 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,12 @@
 2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am (show_die_info_LDADD): Link against libeu.a.
+	(varlocs_LDADD): Likewise.
+	* show-die-info.c: Include system.h.
+	* varlocs.c: Likewise.
+
+2017-02-22  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am (backtrace_LDADD): Link against libeu.a.
 	(backtrace_data_LDADD): Likewise.
 	* backtrace.c: Include system.h.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5826703..0c4d472 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -418,7 +418,7 @@ update1_LDADD = $(libelf)
 update2_LDADD = $(libelf)
 update3_LDADD = $(libdw) $(libelf)
 update4_LDADD = $(libdw) $(libelf)
-show_die_info_LDADD = $(libdw) $(libelf)
+show_die_info_LDADD = $(libdw) $(libelf) $(libeu)
 get_pubnames_LDADD = $(libdw) $(libelf)
 show_abbrev_LDADD = $(libdw) $(libelf)
 get_lines_LDADD = $(libdw) $(libelf)
@@ -464,7 +464,7 @@ test_elf_cntl_gelf_getshdr_LDADD = $(libelf)
 dwflsyms_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 dwfllines_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 dwfl_report_elf_align_LDADD = $(libdw)
-varlocs_LDADD = $(libdw) $(libelf) $(argp_LDADD)
+varlocs_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD)
 backtrace_LDADD = $(libdw) $(libelf) $(libeu) $(argp_LDADD)
 # backtrace-child-biarch also uses those *_CFLAGS and *_LDLAGS variables:
 backtrace_child_CFLAGS = -fPIE
diff --git a/tests/show-die-info.c b/tests/show-die-info.c
index 34e27a3..c5ae5c7 100644
--- a/tests/show-die-info.c
+++ b/tests/show-die-info.c
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <system.h>
 #include "../libdw/known-dwarf.h"
 
 static const char *
diff --git a/tests/varlocs.c b/tests/varlocs.c
index c3fba89..69d3eaf 100644
--- a/tests/varlocs.c
+++ b/tests/varlocs.c
@@ -32,6 +32,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <system.h>
 #include "../libdw/known-dwarf.h"
 
 // The Dwarf, Dwarf_CFIs and address bias of
-- 
2.1.4



More information about the Elfutils-devel mailing list