diff options
author | Rob Landley <rob@landley.net> | 2017-05-08 23:01:06 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2017-05-08 23:01:06 -0500 |
commit | d74b562f508475192b6783b0b2d4dae4e5c2fa93 (patch) | |
tree | fd654c847b9b50924b26b37ed47af16b28e63a64 | |
parent | 71921dc74d051fb352bbb65dc92c3416e219dbb9 (diff) | |
download | toybox-d74b562f508475192b6783b0b2d4dae4e5c2fa93.tar.gz |
Move strend() to lib/lib.c
-rw-r--r-- | lib/lib.c | 9 | ||||
-rw-r--r-- | lib/lib.h | 1 | ||||
-rw-r--r-- | toys/posix/basename.c | 7 |
3 files changed, 11 insertions, 6 deletions
@@ -392,6 +392,15 @@ int unescape(char c) return (idx == -1) ? 0 : to[idx]; } +char *strend(char *str, char *suffix) +{ + long a = strlen(str), b = strlen(suffix); + + if (a>b && !strcmp(str += a-b, suffix)) return str; + + return 0; +} + // If *a starts with b, advance *a past it and return 1, else return 0; int strstart(char **a, char *b) { @@ -206,6 +206,7 @@ char *strlower(char *s); char *strafter(char *haystack, char *needle); char *chomp(char *s); int unescape(char c); +char *strend(char *str, char *suffix); int strstart(char **a, char *b); off_t fdlength(int fd); void loopfiles_rw(char **argv, int flags, int permissions, diff --git a/toys/posix/basename.c b/toys/posix/basename.c index c123cc79..0436bfe7 100644 --- a/toys/posix/basename.c +++ b/toys/posix/basename.c @@ -23,12 +23,7 @@ void basename_main(void) char *base = basename(*toys.optargs), *suffix = toys.optargs[1]; // chop off the suffix if provided - if (suffix && *suffix) { - long bl = strlen(base), sl = strlen(suffix); - char *s = base + bl - sl; - - if (bl > sl && !strcmp(s, suffix)) *s = 0; - } + if (suffix && *suffix && (suffix = strend(base, suffix))) *suffix = 0; puts(base); } |