]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Use portable implementation for basename API
authorKhem Raj <raj.khem@gmail.com>
Sun, 10 Dec 2023 01:35:59 +0000 (17:35 -0800)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Mon, 10 Jun 2024 23:15:39 +0000 (18:15 -0500)
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>
configure.ac
shared/missing.h
shared/util.c
shared/util.h
tools/kmod.c

index a80780e370f9a67f418adaa8496d9c7fd563461d..9527aa2a418bfd8e8b9fd0b6ecfddc0e7ccb7d76 100644 (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])
index 262944419efb79579e8cbf9ff045686114d6529c..7df62356f4866a427eec5234c3c4c6b9715db808 100644 (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>
index e2bab83a23105693a1b12d9fb59a00de65d90e1c..437bc69716e29ed10988e64a40e0cbdaf17769b0 100644 (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')
index c4a3916bd85e8cfe2f86709b030a677de81eca77..2a377dd649ba2201f7fc62d569d6adb7505a9f4d 100644 (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>
index 1015575f7d127bf794f6a09f35a633c44856c6b5..e1a73bea794bccb2a5e6a65e5d140ce4dc587dc7 100644 (file)
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include <shared/util.h>
+#include <shared/missing.h>
 
 #include <libkmod/libkmod.h>