From 41fa461fe157d9977967dcb96d9f0803d8aebc5b Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Wed, 10 Feb 2021 14:26:26 -0700 Subject: [PATCH] Add a GNU-compatible version of basename(3). Unlike POSIX basename(3), the GNU variant does not modify its argument. Note that basename of a path ending in "/" returns an empty string. --- MANIFEST | 1 + include/sudo_util.h | 4 ++++ lib/util/Makefile.in | 12 ++++++++-- lib/util/basename.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ lib/util/util.exp.in | 1 + 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 lib/util/basename.c diff --git a/MANIFEST b/MANIFEST index 3d5bb0392..9149239e8 100644 --- a/MANIFEST +++ b/MANIFEST @@ -158,6 +158,7 @@ lib/util/aix.c lib/util/arc4random.c lib/util/arc4random_buf.c lib/util/arc4random_uniform.c +lib/util/basename.c lib/util/cfmakeraw.c lib/util/chacha_private.h lib/util/closefrom.c diff --git a/include/sudo_util.h b/include/sudo_util.h index eb4c81c60..b9991dd1c 100644 --- a/include/sudo_util.h +++ b/include/sudo_util.h @@ -188,6 +188,10 @@ sudo_dso_public int aix_setauthdb_v1(char *user); sudo_dso_public int aix_setauthdb_v2(char *user, char *registry); #define aix_setauthdb(_a, _b) aix_setauthdb_v2((_a), (_b)) +/* basename.c */ +sudo_dso_public char *sudo_basename_v1(const char *filename); +#define sudo_basename(_a) sudo_basename_v1(_a) + /* gethostname.c */ sudo_dso_public char *sudo_gethostname_v1(void); #define sudo_gethostname() sudo_gethostname_v1() diff --git a/lib/util/Makefile.in b/lib/util/Makefile.in index 9241f61f1..a928a39f5 100644 --- a/lib/util/Makefile.in +++ b/lib/util/Makefile.in @@ -120,8 +120,8 @@ DEVEL = @DEVEL@ SHELL = @SHELL@ -LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo gettime.lo \ - getgrouplist.lo gidlist.lo json.lo lbuf.lo locking.lo \ +LTOBJS = basename.lo @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \ + gettime.lo getgrouplist.lo gidlist.lo json.lo lbuf.lo locking.lo \ logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \ roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \ strtoid.lo strtomode.lo strtonum.lo sudo_conf.lo \ @@ -459,6 +459,14 @@ arc4random_uniform.i: $(srcdir)/arc4random_uniform.c $(incdir)/sudo_compat.h \ $(CC) -E -o $@ $(CPPFLAGS) $< arc4random_uniform.plog: arc4random_uniform.i rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/arc4random_uniform.c --i-file $< --output-file $@ +basename.lo: $(srcdir)/basename.c $(incdir)/sudo_compat.h \ + $(top_builddir)/config.h + $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/basename.c +basename.i: $(srcdir)/basename.c $(incdir)/sudo_compat.h \ + $(top_builddir)/config.h + $(CC) -E -o $@ $(CPPFLAGS) $< +basename.plog: basename.i + rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/basename.c --i-file $< --output-file $@ cfmakeraw.lo: $(srcdir)/cfmakeraw.c $(incdir)/sudo_compat.h \ $(top_builddir)/config.h $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/cfmakeraw.c diff --git a/lib/util/basename.c b/lib/util/basename.c new file mode 100644 index 000000000..b757b36fe --- /dev/null +++ b/lib/util/basename.c @@ -0,0 +1,52 @@ +/* + * SPDX-License-Identifier: ISC + * + * Copyright (c) 2021 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This is an open source non-commercial project. Dear PVS-Studio, please check it. + * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "sudo_compat.h" +#include "sudo_util.h" + +/* + * GNU-compatible basename(3) + * Unlike the POSIX version, this version never modifies its argument + * and returns an empty string if filename ends in a slash. + */ +char * +sudo_basename_v1(const char *filename) +{ + char *base; + + if ((base = strrchr(filename, '/')) != NULL) + base++; + else + base = (char *)filename; + + return base; +} diff --git a/lib/util/util.exp.in b/lib/util/util.exp.in index b0b56f09b..de7c140b0 100644 --- a/lib/util/util.exp.in +++ b/lib/util/util.exp.in @@ -1,5 +1,6 @@ @COMPAT_EXP@initprogname initprogname2 +sudo_basename sudo_conf_askpass_path_v1 sudo_conf_clear_paths_v1 sudo_conf_debug_files_v1