0002-Use-portable-implementation-for-basename-API.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. From 721ed6040c7aa47070faf6378c433089e178bd43 Mon Sep 17 00:00:00 2001
  2. From: Khem Raj <raj.khem@gmail.com>
  3. Date: Sat, 9 Dec 2023 17:35:59 -0800
  4. Subject: [PATCH] Use portable implementation for basename API
  5. musl has removed the non-prototype declaration of basename from
  6. string.h [1] which now results in build errors with clang-17+ compiler
  7. Implement GNU basename behavior using strchr which is portable across libcs
  8. Fixes
  9. ../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]
  10. 71 | "Commands:\n", basename(argv[0]));
  11. | ^
  12. [1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
  13. Suggested-by: Rich Felker
  14. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  15. Upstream: https://github.com/kmod-project/kmod/pull/32
  16. Signed-off-by: Fiona Klute <fiona.klute+wiwa@gmx.de>
  17. ---
  18. libkmod/libkmod-config.c | 2 +-
  19. shared/util.c | 4 ++--
  20. shared/util.h | 7 +++++++
  21. testsuite/testsuite.c | 2 +-
  22. tools/depmod.c | 2 +-
  23. tools/kmod.c | 4 ++--
  24. 6 files changed, 14 insertions(+), 7 deletions(-)
  25. diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c
  26. index e83621b3..8aa555a4 100644
  27. --- a/libkmod/libkmod-config.c
  28. +++ b/libkmod/libkmod-config.c
  29. @@ -794,7 +794,7 @@ static int conf_files_insert_sorted(struct kmod_ctx *ctx,
  30. bool is_single = false;
  31. if (name == NULL) {
  32. - name = basename(path);
  33. + name = gnu_basename(path);
  34. is_single = true;
  35. }
  36. diff --git a/shared/util.c b/shared/util.c
  37. index e2bab83a..0e16670e 100644
  38. --- a/shared/util.c
  39. +++ b/shared/util.c
  40. @@ -172,9 +172,9 @@ char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *
  41. char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len)
  42. {
  43. - char *modname;
  44. + const char *modname;
  45. - modname = basename(path);
  46. + modname = gnu_basename(path);
  47. if (modname == NULL || modname[0] == '\0')
  48. return NULL;
  49. diff --git a/shared/util.h b/shared/util.h
  50. index c4a3916b..073dc5a7 100644
  51. --- a/shared/util.h
  52. +++ b/shared/util.h
  53. @@ -5,6 +5,7 @@
  54. #include <stdbool.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. +#include <string.h>
  58. #include <sys/types.h>
  59. #include <sys/stat.h>
  60. #include <time.h>
  61. @@ -76,6 +77,12 @@ do { \
  62. __p->__v = (val); \
  63. } while(0)
  64. +static _always_inline_ const char *gnu_basename(const char *s)
  65. +{
  66. + const char *p = strrchr(s, '/');
  67. + return p ? p+1 : s;
  68. +}
  69. +
  70. static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
  71. {
  72. return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
  73. diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c
  74. index 318343ac..aafc9873 100644
  75. --- a/testsuite/testsuite.c
  76. +++ b/testsuite/testsuite.c
  77. @@ -70,7 +70,7 @@ static void help(void)
  78. printf("Usage:\n"
  79. "\t%s [options] <test>\n"
  80. - "Options:\n", basename(progname));
  81. + "Options:\n", gnu_basename(progname));
  82. for (itr = options, itr_short = options_short;
  83. itr->name != NULL; itr++, itr_short++)
  84. diff --git a/tools/depmod.c b/tools/depmod.c
  85. index 43fc354a..cfb15b11 100644
  86. --- a/tools/depmod.c
  87. +++ b/tools/depmod.c
  88. @@ -762,7 +762,7 @@ static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files
  89. if (name != NULL)
  90. namelen = strlen(name);
  91. else {
  92. - name = basename(dir);
  93. + name = gnu_basename(dir);
  94. namelen = strlen(name);
  95. dirlen -= namelen + 1;
  96. }
  97. diff --git a/tools/kmod.c b/tools/kmod.c
  98. index 55689c07..df91e5c6 100644
  99. --- a/tools/kmod.c
  100. +++ b/tools/kmod.c
  101. @@ -68,7 +68,7 @@ static int kmod_help(int argc, char *argv[])
  102. "Options:\n"
  103. "\t-V, --version show version\n"
  104. "\t-h, --help show this help\n\n"
  105. - "Commands:\n", basename(argv[0]));
  106. + "Commands:\n", gnu_basename(argv[0]));
  107. for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) {
  108. if (kmod_cmds[i]->help != NULL) {
  109. @@ -156,7 +156,7 @@ static int handle_kmod_compat_commands(int argc, char *argv[])
  110. const char *cmd;
  111. size_t i;
  112. - cmd = basename(argv[0]);
  113. + cmd = gnu_basename(argv[0]);
  114. for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) {
  115. if (streq(kmod_compat_cmds[i]->name, cmd))