sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 026e63c005683c0004e1a4307b08b4ce3177ccea
parent be6e177f6e9b525e0c922b4a074550d81905331d
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Tue, 24 May 2011 12:56:38 +0100

simplify basename
Diffstat:
Mbasename.c | 23+++++++++--------------
1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/basename.c b/basename.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <libgen.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -7,24 +8,18 @@ int main(int argc, char *argv[]) { - char *str = argv[1]; - size_t n, i = 0; + char *s; + size_t n; if(argc < 2) eprintf("usage: %s string [suffix]\n", argv[0]); - if(str[0] != '\0') - for(i = strlen(str)-1; i > 0 && str[i] == '/'; i--) - str[i] = '\0'; - if(i == 0 || !(str = strrchr(argv[1], '/'))) - str = argv[1]; - else - str++; - if(argc > 2 && strlen(str) > strlen(argv[2])) { - n = strlen(str) - strlen(argv[2]); - if(!strcmp(&str[n], argv[2])) - str[n] = '\0'; + s = basename(argv[1]); + if(argc > 2 && strlen(s) > strlen(argv[2])) { + n = strlen(s) - strlen(argv[2]); + if(!strcmp(&s[n], argv[2])) + s[n] = '\0'; } - puts(str); + puts(s); return EXIT_SUCCESS; }