Use portable implementation for basename API

musl has removed the non-prototype declaration of basename from
string.h [1] which now results in build errors with clang-17+ compiler

Implement GNU basename behavior using strchr which is portable across libcs

Fixes
../git/tools/kmod.c:71:19: error: call to undeclared function 'basename'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
71 | "Commands:\n", basename(argv[0]));
| ^

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

Suggested-by: Rich Felker

Signed-off-by: Khem Raj <raj.khem@gmail.com>
[ Implement a basename() function in missing.h and ensure we always use
  the right include rather than having a separate gnu_basename() ]
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
This commit is contained in:
Khem Raj 2023-12-09 17:35:59 -08:00 committed by Lucas De Marchi
parent 05828b4a6e
commit 11eb9bc67c
5 changed files with 16 additions and 2 deletions

View File

@ -53,7 +53,9 @@ CC_CHECK_FUNC_BUILTIN([__builtin_uaddll_overflow], [ ], [ ])
AC_CHECK_MEMBERS([struct stat.st_mtim], [], [], [#include <sys/stat.h>])
# musl 1.0 and bionic 4.4 don't have strndupa
AC_CHECK_DECLS_ONCE([strndupa])
# basename may be only available in libgen.h with the POSIX behavior,
# not desired here
AC_CHECK_DECLS_ONCE([[strndupa], [basename]], [], [], [[#include <string.h>]])
# RHEL 5 and older do not have be32toh
AC_CHECK_DECLS_ONCE([be32toh])

View File

@ -38,6 +38,7 @@ static inline int finit_module(int fd, const char *uargs, int flags)
#endif
#if !HAVE_DECL_STRNDUPA
#include <string.h>
#define strndupa(s, n) \
({ \
const char *__old = (s); \
@ -48,6 +49,15 @@ static inline int finit_module(int fd, const char *uargs, int flags)
})
#endif
#if !HAVE_DECL_BASENAME
#include <string.h>
static inline const char *basename(const char *s)
{
const char *p = strrchr(s, '/');
return p ? p + 1 : s;
}
#endif
#if !HAVE_DECL_BE32TOH
#include <endian.h>
#include <byteswap.h>

View File

@ -172,7 +172,7 @@ char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *
char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len)
{
char *modname;
const char *modname;
modname = basename(path);
if (modname == NULL || modname[0] == '\0')

View File

@ -5,6 +5,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

View File

@ -24,6 +24,7 @@
#include <string.h>
#include <shared/util.h>
#include <shared/missing.h>
#include <libkmod/libkmod.h>