diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2017-05-04 11:21:56 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2017-05-04 12:24:02 +0000 |
commit | 0d2c0bfbe284d715abc0885796436f1bf8e790db (patch) | |
tree | f21e4f885a77f5e88130401b0ed7f4a8641d0655 /libgnu | |
parent | 04c4853ab9cedc5d14bf7f08936ee4e26878f395 (diff) |
Check for existence of GNU-style basename()
If it doesn't exist, add an implementation to libgnu.a and config.h.
Change-Id: Ice0356030dd666d61f8a582ad09a74c843b19add
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'libgnu')
-rw-r--r-- | libgnu/ChangeLog | 6 | ||||
-rw-r--r-- | libgnu/Makefile.am | 6 | ||||
-rw-r--r-- | libgnu/basename-gnu.c | 54 |
3 files changed, 65 insertions, 1 deletions
diff --git a/libgnu/ChangeLog b/libgnu/ChangeLog new file mode 100644 index 00000000..3394de6d --- /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 1c8e6b8f..32c9aa71 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 00000000..7feee81a --- /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; +} |