xref: /original-bsd/usr.bin/basename/basename.c (revision 3f73ce2f)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)basename.c	4.7 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 
20 main(argc, argv)
21 	int argc;
22 	char **argv;
23 {
24 	register char *p, *t;
25 	char *base;
26 
27 	if (argc < 2 || argc > 3) {
28 		fprintf(stderr, "usage: basename string [suffix]\n");
29 		exit(1);
30 	}
31 	for (p = base = *++argv; *p;)
32 		if (*p++ == '/')
33 			base = p;
34 	if (argc == 3) {
35 		for (t = *++argv; *t; ++t);
36 		do {
37 			if (t == *argv) {
38 				*p = '\0';
39 				break;
40 			}
41 		} while (p >= base && *--t == *--p);
42 	}
43 	printf("%s\n", base);
44 	exit(0);
45 }
46