xref: /freebsd/usr.bin/basename/basename.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <capsicum_helpers.h>
33 #include <err.h>
34 #include <libgen.h>
35 #include <limits.h>
36 #include <locale.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <wchar.h>
42 
43 void stripsuffix(char *, const char *, size_t);
44 void usage(void);
45 
46 int
47 main(int argc, char **argv)
48 {
49 	char *p, *suffix;
50 	size_t suffixlen;
51 	int aflag, ch;
52 
53 	setlocale(LC_ALL, "");
54 
55 	if (caph_limit_stdio() < 0 || caph_enter() < 0)
56 		err(1, "capsicum");
57 
58 	aflag = 0;
59 	suffix = NULL;
60 	suffixlen = 0;
61 
62 	while ((ch = getopt(argc, argv, "as:")) != -1)
63 		switch(ch) {
64 		case 'a':
65 			aflag = 1;
66 			break;
67 		case 's':
68 			suffix = optarg;
69 			break;
70 		case '?':
71 		default:
72 			usage();
73 		}
74 	argc -= optind;
75 	argv += optind;
76 
77 	if (argc < 1)
78 		usage();
79 
80 	if (!*argv[0]) {
81 		printf("\n");
82 		exit(0);
83 	}
84 	if ((p = basename(argv[0])) == NULL)
85 		err(1, "%s", argv[0]);
86 	if ((suffix == NULL && !aflag) && argc == 2) {
87 		suffix = argv[1];
88 		argc--;
89 	}
90 	if (suffix != NULL)
91 		suffixlen = strlen(suffix);
92 	while (argc--) {
93 		if ((p = basename(*argv)) == NULL)
94 			err(1, "%s", argv[0]);
95 		stripsuffix(p, suffix, suffixlen);
96 		argv++;
97 		(void)printf("%s\n", p);
98 	}
99 	exit(0);
100 }
101 
102 void
103 stripsuffix(char *p, const char *suffix, size_t suffixlen)
104 {
105 	char *q, *r;
106 	mbstate_t mbs;
107 	size_t n;
108 
109 	if (suffixlen && (q = strchr(p, '\0') - suffixlen) > p &&
110 	    strcmp(suffix, q) == 0) {
111 		/* Ensure that the match occurred on a character boundary. */
112 		memset(&mbs, 0, sizeof(mbs));
113 		for (r = p; r < q; r += n) {
114 			n = mbrlen(r, MB_LEN_MAX, &mbs);
115 			if (n == (size_t)-1 || n == (size_t)-2) {
116 				memset(&mbs, 0, sizeof(mbs));
117 				n = 1;
118 			}
119 		}
120 		/* Chop off the suffix. */
121 		if (q == r)
122 			*q = '\0';
123 	}
124 }
125 
126 void
127 usage(void)
128 {
129 
130 	(void)fprintf(stderr,
131 "usage: basename string [suffix]\n"
132 "       basename [-a] [-s suffix] string [...]\n");
133 	exit(1);
134 }
135