X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fbasename.c;h=e98197dd420241c3c356b4a47efaac4e1937e102;hb=5f265b755a92e7efdbd0d18694913209dfd9e055;hp=06e27663f813e34de059a3a3cb09750cd9e66e31;hpb=61677feff7f549a48267c2c0c50a7420de6e2599;p=oweals%2Fbusybox.git diff --git a/coreutils/basename.c b/coreutils/basename.c index 06e27663f..e98197dd4 100644 --- a/coreutils/basename.c +++ b/coreutils/basename.c @@ -2,7 +2,7 @@ /* * Mini basename implementation for busybox * - * Copyright (C) 1999,2000 by Lineo, inc. + * Copyright (C) 1999,2000,2001 by Lineo, inc. * Written by Erik Andersen , * * This program is free software; you can redistribute it and/or modify @@ -21,23 +21,32 @@ * */ -#include "internal.h" -#include +/* getopt not needed */ + +#include +#include "busybox.h" +#include extern int basename_main(int argc, char **argv) { - char* s, *s1; + int m, n; + char *s; if ((argc < 2) || (**(argv + 1) == '-')) { - usage("basename [file ...]\n"); + show_usage(); } + argv++; - s1=*argv+strlen(*argv)-1; - if (*s1 == '/') - *s1 = '\0'; - s = strrchr(*argv, '/'); - printf("%s\n", (s)? s + 1 : *argv); - exit(TRUE); -} + s = get_last_path_component(*argv); + if (argc>2) { + argv++; + n = strlen(*argv); + m = strlen(s); + if (m>=n && strncmp(s+m-n, *argv, n)==0) + s[m-n] = '\0'; + } + printf("%s\n", s); + return EXIT_SUCCESS; +}