147176Sbostic /*-
266647Spendry * Copyright (c) 1991, 1993, 1994
361920Sbostic * The Regents of the University of California. All rights reserved.
432779Sbostic *
542719Sbostic * %sccs.include.redist.c%
632742Sbostic */
7958Sbill
832742Sbostic #ifndef lint
961920Sbostic static char copyright[] =
1066647Spendry "@(#) Copyright (c) 1991, 1993, 1994\n\
1161920Sbostic The Regents of the University of California. All rights reserved.\n";
1232742Sbostic #endif /* not lint */
138727Smckusick
1432742Sbostic #ifndef lint
15*69227Sbostic static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 05/04/95";
1632742Sbostic #endif /* not lint */
1732742Sbostic
1836273Sbostic #include <stdio.h>
1947176Sbostic #include <stdlib.h>
2047176Sbostic #include <string.h>
21*69227Sbostic #include <unistd.h>
2236273Sbostic
2366569Spendry void usage __P((void));
2466569Spendry
2566569Spendry int
main(argc,argv)26958Sbill main(argc, argv)
2732779Sbostic int argc;
2832779Sbostic char **argv;
29958Sbill {
3066569Spendry char *p;
3147176Sbostic int ch;
32958Sbill
3347176Sbostic while ((ch = getopt(argc, argv, "")) != EOF)
3447176Sbostic switch(ch) {
3547176Sbostic case '?':
3647176Sbostic default:
3747176Sbostic usage();
3847176Sbostic }
3947176Sbostic argc -= optind;
4047176Sbostic argv += optind;
4147176Sbostic
4247176Sbostic if (argc != 1 && argc != 2)
4347176Sbostic usage();
4447176Sbostic
4547176Sbostic /*
4647176Sbostic * (1) If string is // it is implementation defined whether steps (2)
4747176Sbostic * through (5) are skipped or processed.
4847176Sbostic *
4947176Sbostic * (2) If string consists entirely of slash characters, string shall
5047176Sbostic * be set to a single slash character. In this case, skip steps
5147176Sbostic * (3) through (5).
5247176Sbostic */
5347176Sbostic for (p = *argv;; ++p) {
5447176Sbostic if (!*p) {
5547176Sbostic if (p > *argv)
5647176Sbostic (void)printf("/\n");
5747176Sbostic else
5847176Sbostic (void)printf("\n");
5947176Sbostic exit(0);
6047176Sbostic }
6147176Sbostic if (*p != '/')
6247176Sbostic break;
63958Sbill }
6447176Sbostic
6547176Sbostic /*
6647176Sbostic * (3) If there are any trailing slash characters in string, they
6747176Sbostic * shall be removed.
6847176Sbostic */
6966569Spendry for (; *p; ++p)
7066569Spendry continue;
7166569Spendry while (*--p == '/')
7266569Spendry continue;
7347176Sbostic *++p = '\0';
7447176Sbostic
7547176Sbostic /*
7647176Sbostic * (4) If there are any slash characters remaining in string, the
7747176Sbostic * prefix of string up to an including the last slash character
7847176Sbostic * in string shall be removed.
7947176Sbostic */
8047176Sbostic while (--p >= *argv)
8147176Sbostic if (*p == '/')
8247176Sbostic break;
8347176Sbostic ++p;
8447176Sbostic
8547176Sbostic /*
8647176Sbostic * (5) If the suffix operand is present, is not identical to the
8747176Sbostic * characters remaining in string, and is identical to a suffix
8847176Sbostic * of the characters remaining in string, the suffix suffix
8947176Sbostic * shall be removed from string.
9047176Sbostic */
9147176Sbostic if (*++argv) {
9247176Sbostic int suffixlen, stringlen, off;
9347176Sbostic
9447176Sbostic suffixlen = strlen(*argv);
9547176Sbostic stringlen = strlen(p);
9647176Sbostic
9747176Sbostic if (suffixlen < stringlen) {
9847176Sbostic off = stringlen - suffixlen;
9947176Sbostic if (!strcmp(p + off, *argv))
10047176Sbostic p[off] = '\0';
10147176Sbostic }
102958Sbill }
10347176Sbostic (void)printf("%s\n", p);
104958Sbill exit(0);
105958Sbill }
10647176Sbostic
10766569Spendry void
usage()10847176Sbostic usage()
10947176Sbostic {
11066569Spendry
11147176Sbostic (void)fprintf(stderr, "usage: basename string [suffix]\n");
11247176Sbostic exit(1);
11347176Sbostic }
114