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