Skip to content

Commit

Permalink
musl: basename: 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 which
now results in build errors with newer clang compilers.

Implement GNU basename behavior using strchr which is portable across libcs.

Fixes:
| ../../git/tools/mountcomposefs.c:43:20:
| error: call to undeclared function 'basename'; ISO C99 and later do not
| support implicit function declarations [-Wimplicit-function-declaration]
|    43 |         const char *bin = basename(argv0);
|       |                           ^
| ../../git/tools/mountcomposefs.c:43:14:
| error: incompatible integer to pointer conversion initializing 'const char *'
| with an expression of type 'int' [-Wint-conversion]
|    43 |         const char *bin = basename(argv0);
|       |                     ^     ~~~~~~~~~~~~~~~

For reference:
https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

Closes: #272

Signed-off-by: Fathi Boudra <[email protected]>
  • Loading branch information
fboudra committed Apr 9, 2024
1 parent af69922 commit 053cf9a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions libcomposefs/lcfs-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,10 @@ static inline void *steal_pointer(void *pp)
/* type safety */
#define steal_pointer(pp) (0 ? (*(pp)) : (steal_pointer)(pp))

static inline const char *gnu_basename(const char *filename)
{
const char *p = strrchr(filename, '/');
return p ? p+1 : filename;
}

#endif
2 changes: 1 addition & 1 deletion tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ static void digest_to_string(const uint8_t *csum, char *buf)

static void usage(const char *argv0)
{
const char *bin = basename(argv0);
const char *bin = gnu_basename(argv0);
fprintf(stderr,
"Usage: %s [OPTIONS] SOURCE IMAGE\n"
"Options:\n"
Expand Down
3 changes: 2 additions & 1 deletion tools/mountcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
#include <linux/fsverity.h>

#include "libcomposefs/lcfs-mount.h"
#include "libcomposefs/lcfs-utils.h"

static void usage(const char *argv0)
{
const char *bin = basename(argv0);
const char *bin = gnu_basename(argv0);
fprintf(stderr,
"usage: %s [-t type] [-o opt[,opts..]] IMAGE MOUNTPOINT\n"
"Example:\n"
Expand Down

0 comments on commit 053cf9a

Please sign in to comment.