Skip to content

Commit 41bb2ba

Browse files
committed
util: Use strrchr instead of GNU basename
The GNU basename behavior can be replicated easily with strrchr, so just use that, since some platforms (like musl libc) don't have GNU basename.
1 parent 5a6d67a commit 41bb2ba

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/util.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <ctype.h>
44
#include <fcntl.h>
5+
#include <libgen.h>
56
#include <poll.h>
67
#include <stdarg.h>
78
#include <sys/stat.h>
@@ -15,12 +16,6 @@
1516
# include <sys/random.h>
1617
#endif
1718

18-
/* When we include libgen.h because we need dirname() we immediately
19-
* undefine basename() since libgen.h defines it as a macro to the
20-
* POSIX version which is really broken. We prefer GNU basename(). */
21-
#include <libgen.h>
22-
#undef basename
23-
2419
#include "def.h"
2520
#include "time-util.h"
2621
#include "util.h"
@@ -482,7 +477,8 @@ int tempfn_random(const char *p, char **ret) {
482477
* /foo/bar/.#waldobaa2a261115984a9
483478
*/
484479

485-
fn = basename(p);
480+
fn = strrchr(p, '/');
481+
fn = fn ? fn + 1 : p;
486482
if (!filename_is_valid(fn))
487483
return -EINVAL;
488484

0 commit comments

Comments
 (0)