public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Ulf Hermann <ulf.hermann@qt.io>
To: <elfutils-devel@sourceware.org>
Subject: [PATCH] Check for existence of GNU-style basename()
Date: Thu, 04 May 2017 09:50:00 -0000	[thread overview]
Message-ID: <4e14efa4-7eb7-0a5e-cf3c-36c18592cccf@qt.io> (raw)

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

             reply	other threads:[~2017-05-04  9:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-04  9:50 Ulf Hermann [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-02-22 13:41 Ulf Hermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4e14efa4-7eb7-0a5e-cf3c-36c18592cccf@qt.io \
    --to=ulf.hermann@qt.io \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).