1*0b8a9e80SLionel Sambuc /* $NetBSD: basename.c,v 1.15 2011/08/29 14:24:03 joerg Exp $ */
2*0b8a9e80SLionel Sambuc
3*0b8a9e80SLionel Sambuc /*-
4*0b8a9e80SLionel Sambuc * Copyright (c) 1991, 1993, 1994
5*0b8a9e80SLionel Sambuc * The Regents of the University of California. All rights reserved.
6*0b8a9e80SLionel Sambuc *
7*0b8a9e80SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0b8a9e80SLionel Sambuc * modification, are permitted provided that the following conditions
9*0b8a9e80SLionel Sambuc * are met:
10*0b8a9e80SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0b8a9e80SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0b8a9e80SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0b8a9e80SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0b8a9e80SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0b8a9e80SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
16*0b8a9e80SLionel Sambuc * may be used to endorse or promote products derived from this software
17*0b8a9e80SLionel Sambuc * without specific prior written permission.
18*0b8a9e80SLionel Sambuc *
19*0b8a9e80SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0b8a9e80SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0b8a9e80SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0b8a9e80SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0b8a9e80SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0b8a9e80SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0b8a9e80SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0b8a9e80SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0b8a9e80SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0b8a9e80SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0b8a9e80SLionel Sambuc * SUCH DAMAGE.
30*0b8a9e80SLionel Sambuc */
31*0b8a9e80SLionel Sambuc
32*0b8a9e80SLionel Sambuc #include <sys/cdefs.h>
33*0b8a9e80SLionel Sambuc #ifndef lint
34*0b8a9e80SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
35*0b8a9e80SLionel Sambuc The Regents of the University of California. All rights reserved.");
36*0b8a9e80SLionel Sambuc #endif /* not lint */
37*0b8a9e80SLionel Sambuc
38*0b8a9e80SLionel Sambuc #ifndef lint
39*0b8a9e80SLionel Sambuc #if 0
40*0b8a9e80SLionel Sambuc static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95";
41*0b8a9e80SLionel Sambuc #endif
42*0b8a9e80SLionel Sambuc __RCSID("$NetBSD: basename.c,v 1.15 2011/08/29 14:24:03 joerg Exp $");
43*0b8a9e80SLionel Sambuc #endif /* not lint */
44*0b8a9e80SLionel Sambuc
45*0b8a9e80SLionel Sambuc #include <err.h>
46*0b8a9e80SLionel Sambuc #include <libgen.h>
47*0b8a9e80SLionel Sambuc #include <locale.h>
48*0b8a9e80SLionel Sambuc #include <stdio.h>
49*0b8a9e80SLionel Sambuc #include <stdlib.h>
50*0b8a9e80SLionel Sambuc #include <string.h>
51*0b8a9e80SLionel Sambuc #include <unistd.h>
52*0b8a9e80SLionel Sambuc
53*0b8a9e80SLionel Sambuc __dead static void usage(void);
54*0b8a9e80SLionel Sambuc
55*0b8a9e80SLionel Sambuc int
main(int argc,char ** argv)56*0b8a9e80SLionel Sambuc main(int argc, char **argv)
57*0b8a9e80SLionel Sambuc {
58*0b8a9e80SLionel Sambuc char *p;
59*0b8a9e80SLionel Sambuc int ch;
60*0b8a9e80SLionel Sambuc
61*0b8a9e80SLionel Sambuc setlocale(LC_ALL, "");
62*0b8a9e80SLionel Sambuc
63*0b8a9e80SLionel Sambuc while ((ch = getopt(argc, argv, "")) != -1)
64*0b8a9e80SLionel Sambuc switch (ch) {
65*0b8a9e80SLionel Sambuc case '?':
66*0b8a9e80SLionel Sambuc default:
67*0b8a9e80SLionel Sambuc usage();
68*0b8a9e80SLionel Sambuc }
69*0b8a9e80SLionel Sambuc argc -= optind;
70*0b8a9e80SLionel Sambuc argv += optind;
71*0b8a9e80SLionel Sambuc
72*0b8a9e80SLionel Sambuc if (argc != 1 && argc != 2)
73*0b8a9e80SLionel Sambuc usage();
74*0b8a9e80SLionel Sambuc
75*0b8a9e80SLionel Sambuc if (**argv == '\0') {
76*0b8a9e80SLionel Sambuc (void)printf("\n");
77*0b8a9e80SLionel Sambuc exit(0);
78*0b8a9e80SLionel Sambuc }
79*0b8a9e80SLionel Sambuc if ((p = basename(*argv)) == NULL)
80*0b8a9e80SLionel Sambuc err(1, "%s", *argv);
81*0b8a9e80SLionel Sambuc if (*++argv != '\0') {
82*0b8a9e80SLionel Sambuc int suffixlen, stringlen, off;
83*0b8a9e80SLionel Sambuc
84*0b8a9e80SLionel Sambuc suffixlen = strlen(*argv);
85*0b8a9e80SLionel Sambuc stringlen = strlen(p);
86*0b8a9e80SLionel Sambuc
87*0b8a9e80SLionel Sambuc if (suffixlen < stringlen) {
88*0b8a9e80SLionel Sambuc off = stringlen - suffixlen;
89*0b8a9e80SLionel Sambuc if (strcmp(p + off, *argv) == 0)
90*0b8a9e80SLionel Sambuc p[off] = '\0';
91*0b8a9e80SLionel Sambuc }
92*0b8a9e80SLionel Sambuc }
93*0b8a9e80SLionel Sambuc (void)printf("%s\n", p);
94*0b8a9e80SLionel Sambuc exit(0);
95*0b8a9e80SLionel Sambuc }
96*0b8a9e80SLionel Sambuc
97*0b8a9e80SLionel Sambuc static void
usage(void)98*0b8a9e80SLionel Sambuc usage(void)
99*0b8a9e80SLionel Sambuc {
100*0b8a9e80SLionel Sambuc
101*0b8a9e80SLionel Sambuc (void)fprintf(stderr, "usage: basename string [suffix]\n");
102*0b8a9e80SLionel Sambuc exit(1);
103*0b8a9e80SLionel Sambuc }
104