xref: /illumos-gate/usr/src/ucbcmd/basename/basename.c (revision d363b1b0cb9ef6d6f3febdd8d1cba46507e97098)
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 #include <stdio.h>
17 #include <stdlib.h>
18 
19 int
20 main(int argc, char **argv)
21 {
22 	char *p1, *p2, *p3;
23 
24 	if (argc < 2) {
25 		(void) putchar('\n');
26 		exit(1);
27 	}
28 	p1 = argv[1];
29 	p2 = p1;
30 	while (*p1) {
31 		if (*p1++ == '/')
32 			p2 = p1;
33 	}
34 	if (argc > 2) {
35 		p3 = argv[2];
36 		while (*p3)
37 			p3++;
38 
39 		while (p3 > argv[2])
40 			if (p1 <= p2 || *--p3 != *--p1)
41 				goto output;
42 		*p1 = '\0';
43 	}
44 output:
45 
46 	(void) fputs(p2, stdout);
47 	(void) putc('\n', stdout);
48 	return (0);
49 }
50