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>
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])
#endif
#if !HAVE_DECL_STRNDUPA
+#include <string.h>
#define strndupa(s, n) \
({ \
const char *__old = (s); \
})
#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>
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')
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <shared/util.h>
+#include <shared/missing.h>
#include <libkmod/libkmod.h>