xref: /titanic_51/usr/src/ucbcmd/basename/basename.c (revision 43a291055ab3951f6372241323fd4e2486098fff)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 
10 /*
11  * Copyright (c) 1980 Regents of the University of California.
12  * All rights reserved. The Berkeley software License Agreement
13  * specifies the terms and conditions for redistribution.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #include	<stdio.h>
19 
20 int
21 main(int argc, char **argv)
22 {
23 	char *p1, *p2, *p3;
24 
25 	if (argc < 2) {
26 		(void)putchar('\n');
27 		exit(1);
28 	}
29 	p1 = argv[1];
30 	p2 = p1;
31 	while (*p1) {
32 		if (*p1++ == '/')
33 			p2 = p1;
34 	}
35 	if (argc>2) {
36 		for(p3=argv[2]; *p3; p3++)
37 			;
38 		while(p3>argv[2])
39 			if(p1 <= p2 || *--p3 != *--p1)
40 				goto output;
41 		*p1 = '\0';
42 	}
43 output:
44 	fputs(p2, stdout);
45 	putc('\n', stdout);
46 	return (0);
47 }
48