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.
This commit is contained in:
Todd C. Miller
2021-02-10 14:26:26 -07:00
parent bbfd430cf9
commit 41fa461fe1
5 changed files with 68 additions and 2 deletions

View File

@@ -158,6 +158,7 @@ lib/util/aix.c
lib/util/arc4random.c lib/util/arc4random.c
lib/util/arc4random_buf.c lib/util/arc4random_buf.c
lib/util/arc4random_uniform.c lib/util/arc4random_uniform.c
lib/util/basename.c
lib/util/cfmakeraw.c lib/util/cfmakeraw.c
lib/util/chacha_private.h lib/util/chacha_private.h
lib/util/closefrom.c lib/util/closefrom.c

View File

@@ -188,6 +188,10 @@ sudo_dso_public int aix_setauthdb_v1(char *user);
sudo_dso_public int aix_setauthdb_v2(char *user, char *registry); sudo_dso_public int aix_setauthdb_v2(char *user, char *registry);
#define aix_setauthdb(_a, _b) aix_setauthdb_v2((_a), (_b)) #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 */ /* gethostname.c */
sudo_dso_public char *sudo_gethostname_v1(void); sudo_dso_public char *sudo_gethostname_v1(void);
#define sudo_gethostname() sudo_gethostname_v1() #define sudo_gethostname() sudo_gethostname_v1()

View File

@@ -120,8 +120,8 @@ DEVEL = @DEVEL@
SHELL = @SHELL@ SHELL = @SHELL@
LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo gettime.lo \ LTOBJS = basename.lo @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \
getgrouplist.lo gidlist.lo json.lo lbuf.lo locking.lo \ gettime.lo getgrouplist.lo gidlist.lo json.lo lbuf.lo locking.lo \
logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \ logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \
roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \ roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \
strtoid.lo strtomode.lo strtonum.lo sudo_conf.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) $< $(CC) -E -o $@ $(CPPFLAGS) $<
arc4random_uniform.plog: arc4random_uniform.i 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 $@ 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 \ cfmakeraw.lo: $(srcdir)/cfmakeraw.c $(incdir)/sudo_compat.h \
$(top_builddir)/config.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/cfmakeraw.c $(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/cfmakeraw.c

52
lib/util/basename.c Normal file
View File

@@ -0,0 +1,52 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws>
*
* 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 <config.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#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;
}

View File

@@ -1,5 +1,6 @@
@COMPAT_EXP@initprogname @COMPAT_EXP@initprogname
initprogname2 initprogname2
sudo_basename
sudo_conf_askpass_path_v1 sudo_conf_askpass_path_v1
sudo_conf_clear_paths_v1 sudo_conf_clear_paths_v1
sudo_conf_debug_files_v1 sudo_conf_debug_files_v1