summaryrefslogtreecommitdiff
blob: f4839dad2474abc544027dd177e67760b4f3b6bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
https://github.com/kmod-project/kmod/pull/32

From 721ed6040c7aa47070faf6378c433089e178bd43 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sat, 9 Dec 2023 17:35:59 -0800
Subject: [PATCH] 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>
--- a/libkmod/libkmod-config.c
+++ b/libkmod/libkmod-config.c
@@ -794,7 +794,7 @@ static int conf_files_insert_sorted(struct kmod_ctx *ctx,
 	bool is_single = false;
 
 	if (name == NULL) {
-		name = basename(path);
+		name = gnu_basename(path);
 		is_single = true;
 	}
 
--- a/shared/util.c
+++ b/shared/util.c
@@ -172,9 +172,9 @@ 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);
+	modname = gnu_basename(path);
 	if (modname == NULL || modname[0] == '\0')
 		return NULL;
 
--- a/shared/util.h
+++ b/shared/util.h
@@ -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>
@@ -76,6 +77,12 @@ do {						\
 	__p->__v = (val);			\
 } while(0)
 
+static _always_inline_ const char *gnu_basename(const char *s)
+{
+  const char *p = strrchr(s, '/');
+  return p ? p+1 : s;
+}
+
 static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
 {
 	return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
--- a/testsuite/testsuite.c
+++ b/testsuite/testsuite.c
@@ -70,7 +70,7 @@ static void help(void)
 
 	printf("Usage:\n"
 	       "\t%s [options] <test>\n"
-	       "Options:\n", basename(progname));
+	       "Options:\n", gnu_basename(progname));
 
 	for (itr = options, itr_short = options_short;
 				itr->name != NULL; itr++, itr_short++)
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -762,7 +762,7 @@ static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files
 	if (name != NULL)
 		namelen = strlen(name);
 	else {
-		name = basename(dir);
+		name = gnu_basename(dir);
 		namelen = strlen(name);
 		dirlen -= namelen + 1;
 	}
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -68,7 +68,7 @@ static int kmod_help(int argc, char *argv[])
 			"Options:\n"
 			"\t-V, --version     show version\n"
 			"\t-h, --help        show this help\n\n"
-			"Commands:\n", basename(argv[0]));
+			"Commands:\n", gnu_basename(argv[0]));
 
 	for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) {
 		if (kmod_cmds[i]->help != NULL) {
@@ -156,7 +156,7 @@ static int handle_kmod_compat_commands(int argc, char *argv[])
 	const char *cmd;
 	size_t i;
 
-	cmd = basename(argv[0]);
+	cmd = gnu_basename(argv[0]);
 
 	for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) {
 		if (streq(kmod_compat_cmds[i]->name, cmd))