1 /* basename.c - Return non-directory portion of a pathname
2  *
3  * Copyright 2012 Tryn Mirell <tryn@mirell.org>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html
6 
7 
8 USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN))
9 
10 config BASENAME
11   bool "basename"
12   default y
13   help
14     usage: basename string [suffix]
15 
16     Return non-directory portion of a pathname removing suffix
17 */
18 
19 #include "toys.h"
20 
basename_main(void)21 void basename_main(void)
22 {
23   char *base = basename(*toys.optargs), *suffix = toys.optargs[1];
24 
25   // chop off the suffix if provided
26   if (suffix) {
27     char *s = base + strlen(base) - strlen(suffix);
28     if (s > base && !strcmp(s, suffix)) *s = 0;
29   }
30 
31   puts(base);
32 }
33