public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Check for existence of GNU-style basename()
@ 2017-05-04  9:50 Ulf Hermann
  0 siblings, 0 replies; 2+ messages in thread
From: Ulf Hermann @ 2017-05-04  9:50 UTC (permalink / raw)
  To: elfutils-devel

If it doesn't exist, add an implementation to libgnu.a and config.h.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 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  <ulf.hermann@qt.io>
+
+	* configure.ac: Add check for GNU-style basename.
+
 2017-02-15  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* 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 <sys/param.h>])
 
+AC_CHECK_DECLS([basename],[],[],
+               [#define _GNU_SOURCE
+                #include <string.h>])
+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 0000000..3394de6
--- /dev/null
+++ b/libgnu/ChangeLog
@@ -0,0 +1,6 @@
+2017-05-04  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* 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 0000000..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 <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "dosname.h"
+#include <string.h>
+
+/* 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] Check for existence of GNU-style basename()
@ 2017-02-22 13:41 Ulf Hermann
  0 siblings, 0 replies; 2+ messages in thread
From: Ulf Hermann @ 2017-02-22 13:41 UTC (permalink / raw)
  To: elfutils-devel

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-05-04  9:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-04  9:50 [PATCH] Check for existence of GNU-style basename() Ulf Hermann
  -- strict thread matches above, loose matches on Subject: below --
2017-02-22 13:41 Ulf Hermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).