Skip to content

Commit

Permalink
fixup! fixup! Use portable implementation for basename API
Browse files Browse the repository at this point in the history
Move basename to the missing.h header, relying on the build system to
check for differences between musl and glibc, like done for other
functions.

with glibc:
checking whether strndupa is declared... yes
checking whether basename is declared... yes
checking whether be32toh is declared... yes

with musl 1.2.5:
checking whether strndupa is declared... no
checking whether basename is declared... no
checking whether be32toh is declared... yes

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
  • Loading branch information
lucasdemarchi committed Apr 9, 2024
1 parent 3325c9d commit 877280d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
10 changes: 10 additions & 0 deletions shared/missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand All @@ -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>
Expand Down
6 changes: 0 additions & 6 deletions shared/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ do { \
__p->__v = (val); \
} while(0)

static _always_inline_ const char *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
1 change: 1 addition & 0 deletions tools/kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string.h>

#include <shared/util.h>
#include <shared/missing.h>

#include <libkmod/libkmod.h>

Expand Down

0 comments on commit 877280d

Please sign in to comment.