Mercurial > hg > toybox
comparison toys/basename.c @ 455:6c62d5b6675a
Add test for basename, fix issue where suffix is wrongfully applied if it appears in the middle of the filename
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sun, 12 Feb 2012 14:14:58 +0100 |
parents | eda61bcf575a |
children | dd24f86d5ad5 |
comparison
equal
deleted
inserted
replaced
454:8f5780dd6da4 | 455:6c62d5b6675a |
---|---|
13 bool "basename" | 13 bool "basename" |
14 default y | 14 default y |
15 help | 15 help |
16 usage: basename string [suffix] | 16 usage: basename string [suffix] |
17 | 17 |
18 Return non-directory portion of a pathname | 18 Return non-directory portion of a pathname removing suffix |
19 */ | 19 */ |
20 | 20 |
21 #include "toys.h" | 21 #include "toys.h" |
22 | 22 |
23 void basename_main(void) | 23 void basename_main(void) |
35 | 35 |
36 if (!base) base = arg; | 36 if (!base) base = arg; |
37 | 37 |
38 // chop off the suffix if provided | 38 // chop off the suffix if provided |
39 if (suffix) { | 39 if (suffix) { |
40 char *s = strstr(base, suffix); | 40 int suflen = strlen(suffix); |
41 if (s && s != base) *s = 0; | 41 int reslen = strlen(base); |
42 if (suflen < reslen && !strcmp( base+reslen-suflen, suffix)) | |
43 base[reslen-suflen] = 0; | |
42 } | 44 } |
43 | 45 |
44 puts(base); | 46 puts(base); |
45 } | 47 } |