blob: db70a5ec6d1a6bf05c0d127f4525c4a862fa6209 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/* vi: set sw=4 ts=4:
*
* basename.c
USE_BASENAME(NEWTOY(basename, NULL, TOYFLAG_USR|TOYFLAG_BIN))
config BASENAME
bool "basename"
default n
help
usage: basename string [suffix]
Return non-directory portion of a pathname
*/
#include "toys.h"
void basename_main(void)
{
char *arg, *suffix, *base;
int arglen;
arg = toys.optargs[0];
suffix = toys.optargs[1];
// return null string if nothing provided
if (!arg) return;
arglen = strlen(arg);
// handle the case where we only have single slash
if (arglen == 1 && arg[0] == '/') {
puts("/");
return;
}
// remove trailing slash
if (arg[arglen - 1] == '/') {
arg[arglen - 1] = 0;
}
// get everything past the last /
base = strrchr(arg, '/');
if (!base) base = arg;
else base++;
// handle the case where we have all slashes
if (base[0] == 0) base = "/";
// chop off the suffix if provided
if (suffix) {
strstr(base, suffix)[0] = 0;
}
puts(base);
}
|