3 * basename.c - Return non-directory portion of a pathname
5 * Copyright 2012 Tryn Mirell <tryn@mirell.org>
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html
10 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))
16 usage: basename string [suffix]
18 Return non-directory portion of a pathname removing suffix
23 void basename_main(void)
25 char *arg = toys.optargs[0], *suffix = toys.optargs[1], *base;
27 while ((base = strrchr(arg, '/'))) {
28 if (base == arg) break;
29 if (!base[1]) *base = 0;
36 if (!base) base = arg;
38 // chop off the suffix if provided
40 arg = base + strlen(base) - strlen(suffix);
41 if (arg > base && !strcmp(arg, suffix)) *arg = 0;