Skip to content

Commit 924c4bd

Browse files
haukekrallin
authored andcommitted
Support POSIX basename() from musl libc
Musl libc 1.2.5 removed the definition of the basename() function from string.h and only provides it in libgen.h as the POSIX standard defines it. This change fixes compilation with musl libc 1.2.5. ```` build_dir/target-mips_24kc_musl/tini-0.19.0/src/tini.c:227:36: error: implicit declaration of function 'basename' [-Wimplicit-function-declaration] 227 | fprintf(file, "%s (%s)\n", basename(name), TINI_VERSION_STRING); build_dir/target-mips_24kc_musl/tini-0.19.0/src/tini.c:227:25: error: format '%s' expects argument of type 'char *', but argument 3 has type 'int' [-Werror=format=] 227 | fprintf(file, "%s (%s)\n", basename(name), TINI_VERSION_STRING); | ~^ ~~~~~~~~~~~~~~ | | | | char * int | %d ```` basename() modifies the input string, copy it first with strdup(), If strdup() returns NULL the code will handle it. Signed-off-by: Hauke Mehrtens <[email protected]>
1 parent 7724cbe commit 924c4bd

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎src/tini.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdlib.h>
1515
#include <unistd.h>
1616
#include <stdbool.h>
17+
#include <libgen.h>
1718

1819
#include "tiniConfig.h"
1920
#include "tiniLicense.h"
@@ -224,14 +225,19 @@ int spawn(const signal_configuration_t* const sigconf_ptr, char* const argv[], i
224225
}
225226

226227
void print_usage(char* const name, FILE* const file) {
227-
fprintf(file, "%s (%s)\n", basename(name), TINI_VERSION_STRING);
228+
char *dirc, *bname;
229+
230+
dirc = strdup(name);
231+
bname = basename(dirc);
232+
233+
fprintf(file, "%s (%s)\n", bname, TINI_VERSION_STRING);
228234

229235
#if TINI_MINIMAL
230-
fprintf(file, "Usage: %s PROGRAM [ARGS] | --version\n\n", basename(name));
236+
fprintf(file, "Usage: %s PROGRAM [ARGS] | --version\n\n", bname);
231237
#else
232-
fprintf(file, "Usage: %s [OPTIONS] PROGRAM -- [ARGS] | --version\n\n", basename(name));
238+
fprintf(file, "Usage: %s [OPTIONS] PROGRAM -- [ARGS] | --version\n\n", bname);
233239
#endif
234-
fprintf(file, "Execute a program under the supervision of a valid init process (%s)\n\n", basename(name));
240+
fprintf(file, "Execute a program under the supervision of a valid init process (%s)\n\n", bname);
235241

236242
fprintf(file, "Command line options:\n\n");
237243

@@ -261,6 +267,7 @@ void print_usage(char* const name, FILE* const file) {
261267
fprintf(file, " %s: Send signals to the child's process group.\n", KILL_PROCESS_GROUP_GROUP_ENV_VAR);
262268

263269
fprintf(file, "\n");
270+
free(dirc);
264271
}
265272

266273
void print_license(FILE* const file) {

0 commit comments

Comments
 (0)