Skip to content

Commit

Permalink
Use portable implementation for basename API
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
  • Loading branch information
kraj authored and lucasdemarchi committed Apr 9, 2024
1 parent e4130a4 commit 81580c1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libkmod/libkmod-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions shared/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions shared/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion testsuite/testsuite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion tools/depmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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) {
Expand Down Expand Up @@ -151,7 +151,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))
Expand Down

0 comments on commit 81580c1

Please sign in to comment.