xref: /openbsd-src/usr.bin/dirname/dirname.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: dirname.c,v 1.11 2005/04/07 07:16:21 otto Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef lint
20 static char rcsid[] = "$OpenBSD: dirname.c,v 1.11 2005/04/07 07:16:21 otto Exp $";
21 #endif /* not lint */
22 
23 #include <err.h>
24 #include <libgen.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 
30 void usage(void);
31 
32 int
33 main(int argc, char *argv[])
34 {
35 	int ch;
36 	char *dir;
37 
38 	setlocale(LC_ALL, "");
39 
40 	while ((ch = getopt(argc, argv, "")) != -1) {
41 		switch (ch) {
42 		default:
43 			usage();
44 		}
45 	}
46 	argc -= optind;
47 	argv += optind;
48 
49 	if (argc != 1)
50 		usage();
51 
52 	if ((dir = dirname(argv[0])) == NULL)
53 		err(1, "%s", argv[0]);
54 	puts(dir);
55 	exit(0);
56 }
57 
58 extern char *__progname;
59 
60 void
61 usage(void)
62 {
63 	(void)fprintf(stderr, "Usage: %s pathname\n", __progname);
64 	exit(1);
65 }
66