Skip to contentSkip to conversation

xfsdump: Mimic GNU basename() API for non-glibc library e.g. musl

PATCH In review Brahmajit Das 2 participants 2 messages Sep 3, 2024 – Sep 10, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsdump: Mimic GNU basename() API for non-glibc library e.g. musl

musl only provides POSIX version of basename and it has also removed providing it via string.h header [1] which now results in compile errors with newer compilers e.g. clang-18

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

Please also reffer: https://bugs.gentoo.org/937495

---
 common/main.c    | 3 +++
 invutil/invidx.c | 4 ++++
 2 files changed, 7 insertions(+)
common/main.c +3 -0
index 6141ffb..107d335 100644
@@ -77,6 +77,9 @@
 #define MINSTACKSZ	0x02000000
 #define MAXSTACKSZ	0x08000000
 
+#if !defined(__GLIBC__)
+#define basename(src) (strrchr(src, '/') ? strrchr(src, '/') + 1 : src)
+#endif
 
 /* declarations of externally defined global symbols *************************/
 
invutil/invidx.c +4 -0
index 5874e8d..942f16f 100644
@@ -41,6 +41,10 @@
 #include "stobj.h"
 #include "timeutil.h"
 
+#if !defined(__GLIBC__)
+#define basename(src) (strrchr(src, '/') ? strrchr(src, '/') + 1 : src)
+#endif
+
 invidx_fileinfo_t *invidx_file;
 int invidx_numfiles;
 
Signature · 1 line
2.46.0
#1 lore raw

common/main.c line 82

Quoted 3 lines · Brahmajit Das
common/main.c +3 -0
+#if !defined(__GLIBC__)
+#define basename(src) (strrchr(src, '/') ? strrchr(src, '/') + 1 : src)
+#endif

A hacky macro without any explanation is really not a good idea. Now this basically open codes the glibc implementation of basename, so maybe just turn it into a proper function always used and write a comment explaining why it exists.