options/posix: Implement basename()
This commit is contained in:
parent
204bc206b3
commit
0e2293e8fc
|
@ -1,10 +1,24 @@
|
|||
|
||||
#include <libgen.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
|
||||
char *basename(char *) {
|
||||
__ensure(!"Not implemented");
|
||||
__builtin_unreachable();
|
||||
// Adopted from musl's code.
|
||||
char *basename(char *s) {
|
||||
// This empty string behavior is specified by POSIX.
|
||||
if (!s || !*s)
|
||||
return const_cast<char *>(".");
|
||||
|
||||
// Delete trailing slashes.
|
||||
// Note that we do not delete the slash at index zero.
|
||||
auto i = strlen(s) - 1;
|
||||
for(; i && s[i] == '/'; i--)
|
||||
s[i] = 0;
|
||||
|
||||
// Find the last non-trailing slash.
|
||||
for(; i && s[i - 1] != '/'; i--)
|
||||
;
|
||||
return s + i;
|
||||
}
|
||||
|
||||
char *dirname(char *) {
|
||||
|
|
Loading…
Reference in a new issue