1 /* basename.c - Return non-directory portion of a pathname
3 * Copyright 2012 Tryn Mirell <tryn@mirell.org>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html
8 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))
14 usage: basename string [suffix]
16 Return non-directory portion of a pathname removing suffix
21 void basename_main(void)
23 char *base = basename(*toys.optargs), *suffix = toys.optargs[1];
25 // chop off the suffix if provided
26 if (suffix && *suffix) {
27 long bl = strlen(base), sl = strlen(suffix);
28 char *s = base + bl - sl;
30 if (bl > sl && !strcmp(s, suffix)) *s = 0;