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)21void 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 long bl = strlen(base), sl = strlen(suffix); 28 char *s = base + bl - sl; 29 30 if (bl > sl && !strcmp(s, suffix)) *s = 0; 31 } 32 33 puts(base); 34 } 35