X-Git-Url: https://git.saurik.com/apple/shell_cmds.git/blobdiff_plain/44bd5ea795281151bc7b81a65d2dd42c6b8914d8..HEAD:/basename/basename.c diff --git a/basename/basename.c b/basename/basename.c index ac02aa1..6bde858 100644 --- a/basename/basename.c +++ b/basename/basename.c @@ -1,5 +1,3 @@ -/* $NetBSD: basename.c,v 1.10 1997/10/18 12:18:20 lukem Exp $ */ - /*- * Copyright (c) 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. @@ -33,40 +31,49 @@ * SUCH DAMAGE. */ -#include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\ - The Regents of the University of California. All rights reserved.\n"); -#endif /* not lint */ +static const char copyright[] = +"@(#) Copyright (c) 1991, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif -#ifndef lint #if 0 +#ifndef lint static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95"; -#endif -__RCSID("$NetBSD: basename.c,v 1.10 1997/10/18 12:18:20 lukem Exp $"); #endif /* not lint */ +#endif +#include +__RCSID("$FreeBSD: src/usr.bin/basename/basename.c,v 1.14 2002/09/04 23:28:52 dwmalone Exp $"); + +#include +#include #include #include #include -#include #include -int main __P((int, char **)); -void usage __P((void)); +void usage(void); int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { - char *p; - int ch; + char *p, *q, *suffix; + size_t suffixlen; + int aflag, ch; - setlocale(LC_ALL, ""); + aflag = 0; + suffix = NULL; + suffixlen = 0; - while ((ch = getopt(argc, argv, "")) != -1) + while ((ch = getopt(argc, argv, "as:")) != -1) switch(ch) { + case 'a': + aflag = 1; + break; + case 's': + suffix = optarg; + break; case '?': default: usage(); @@ -74,75 +81,39 @@ main(argc, argv) argc -= optind; argv += optind; - if (argc != 1 && argc != 2) + if (argc < 1) usage(); - /* - * (1) If string is // it is implementation defined whether steps (2) - * through (5) are skipped or processed. - * - * (2) If string consists entirely of slash characters, string shall - * be set to a single slash character. In this case, skip steps - * (3) through (5). - */ - for (p = *argv;; ++p) { - if (!*p) { - if (p > *argv) - (void)printf("/\n"); - else - (void)printf("\n"); - exit(0); - } - if (*p != '/') - break; + if (!*argv[0]) { + printf("\n"); + exit(0); } - - /* - * (3) If there are any trailing slash characters in string, they - * shall be removed. - */ - for (; *p; ++p) - continue; - while (*--p == '/') - continue; - *++p = '\0'; - - /* - * (4) If there are any slash characters remaining in string, the - * prefix of string up to an including the last slash character - * in string shall be removed. - */ - while (--p >= *argv) - if (*p == '/') - break; - ++p; - - /* - * (5) If the suffix operand is present, is not identical to the - * characters remaining in string, and is identical to a suffix - * of the characters remaining in string, the suffix suffix - * shall be removed from string. - */ - if (*++argv) { - int suffixlen, stringlen, off; - - suffixlen = strlen(*argv); - stringlen = strlen(p); - - if (suffixlen < stringlen) { - off = stringlen - suffixlen; - if (!strcmp(p + off, *argv)) - p[off] = '\0'; - } + if ((p = basename(argv[0])) == NULL) + err(1, "%s", argv[0]); + if ((suffix == NULL && !aflag) && argc == 2) { + suffix = argv[1]; + argc--; + } + if (suffix != NULL) + suffixlen = strlen(suffix); + while (argc--) { + if ((p = basename(*argv)) == NULL) + err(1, "%s", argv[0]); + if (suffixlen && (q = strchr(p, '\0') - suffixlen) > p && + strcmp(suffix, q) == 0) + *q = '\0'; + argv++; + (void)printf("%s\n", p); } - (void)printf("%s\n", p); exit(0); } void -usage() +usage(void) { - (void)fprintf(stderr, "usage: basename string [suffix]\n"); + (void)fprintf(stderr, +"usage: basename string [suffix]\n" +" basename [-a] [-s suffix] string [...]\n"); exit(1); }