xref: /openbsd-src/usr.bin/basename/basename.c (revision 84d7e2a2af167f4532a374a5c68688ced4322391)
1*84d7e2a2Sschwarze /*	$OpenBSD: basename.c,v 1.14 2016/10/28 07:22:59 schwarze Exp $	*/
2df930be7Sderaadt /*	$NetBSD: basename.c,v 1.9 1995/09/02 05:29:46 jtc Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1991, 1993, 1994
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33ab2efd0eSotto #include <err.h>
34ab2efd0eSotto #include <libgen.h>
35df930be7Sderaadt #include <stdio.h>
36df930be7Sderaadt #include <stdlib.h>
37df930be7Sderaadt #include <string.h>
38df930be7Sderaadt #include <unistd.h>
39df930be7Sderaadt 
40*84d7e2a2Sschwarze static void __dead usage(void);
41df930be7Sderaadt 
42df930be7Sderaadt int
main(int argc,char * argv[])431837a5caSderaadt main(int argc, char *argv[])
44df930be7Sderaadt {
45ab2efd0eSotto 	int ch;
46df930be7Sderaadt 	char *p;
47df930be7Sderaadt 
480bd1216cSderaadt 	if (pledge("stdio", NULL) == -1)
490bd1216cSderaadt 		err(1, "pledge");
509d896fa1Sderaadt 
51ab2efd0eSotto 	while ((ch = getopt(argc, argv, "")) != -1) {
52ab2efd0eSotto 		switch (ch) {
53ab2efd0eSotto 		default:
54df930be7Sderaadt 			usage();
55df930be7Sderaadt 		}
56df930be7Sderaadt 	}
57ab2efd0eSotto 	argc -= optind;
58ab2efd0eSotto 	argv += optind;
59df930be7Sderaadt 
60ab2efd0eSotto 	if (argc != 1 && argc != 2)
61ab2efd0eSotto 		usage();
62df930be7Sderaadt 
63e21804c7Sotto 	if (**argv == '\0') {
64e21804c7Sotto 		(void)puts("");
65*84d7e2a2Sschwarze 		return 0;
66e21804c7Sotto 	}
67ab2efd0eSotto 	p = basename(*argv);
68ab2efd0eSotto 	if (p == NULL)
69ab2efd0eSotto 		err(1, "%s", *argv);
70df930be7Sderaadt 	/*
71ab2efd0eSotto 	 * If the suffix operand is present, is not identical to the
72df930be7Sderaadt 	 * characters remaining in string, and is identical to a suffix
73df930be7Sderaadt 	 * of the characters remaining in string, the suffix suffix
74df930be7Sderaadt 	 * shall be removed from string.
75df930be7Sderaadt 	 */
76df930be7Sderaadt 	if (*++argv) {
77ab2efd0eSotto 		size_t suffixlen, stringlen, off;
78df930be7Sderaadt 
79df930be7Sderaadt 		suffixlen = strlen(*argv);
80df930be7Sderaadt 		stringlen = strlen(p);
81df930be7Sderaadt 
82df930be7Sderaadt 		if (suffixlen < stringlen) {
83df930be7Sderaadt 			off = stringlen - suffixlen;
84df930be7Sderaadt 			if (!strcmp(p + off, *argv))
85df930be7Sderaadt 				p[off] = '\0';
86df930be7Sderaadt 		}
87df930be7Sderaadt 	}
882797883aSmillert 	(void)puts(p);
89665eb065Smmcc 	return 0;
90df930be7Sderaadt }
91df930be7Sderaadt 
92ab2efd0eSotto extern char *__progname;
93*84d7e2a2Sschwarze 
94*84d7e2a2Sschwarze static void __dead
usage(void)951837a5caSderaadt usage(void)
96df930be7Sderaadt {
97df930be7Sderaadt 
98ab2efd0eSotto 	(void)fprintf(stderr, "usage: %s string [suffix]\n", __progname);
99df930be7Sderaadt 	exit(1);
100df930be7Sderaadt }
101