xref: /openbsd-src/usr.bin/getopt/getopt.c (revision 0bd1216cbfac4316d823b5f086842b66ddea2018)
1 /*	$OpenBSD: getopt.c,v 1.10 2015/10/09 01:37:07 deraadt Exp $	*/
2 
3 /*
4  * This material, written by Henry Spencer, was released by him
5  * into the public domain and is thus not subject to any copyright.
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <err.h>
12 
13 int
main(int argc,char * argv[])14 main(int argc, char *argv[])
15 {
16 	extern int optind;
17 	extern char *optarg;
18 	int c;
19 	int status = 0;
20 
21 	if (pledge("stdio", NULL) == -1)
22 		err(1, "pledge");
23 
24 	optind = 2;	/* Past the program name and the option letters. */
25 	while ((c = getopt(argc, argv, argv[1])) != -1)
26 		switch (c) {
27 		case '?':
28 			status = 1;	/* getopt routine gave message */
29 			break;
30 		default:
31 			if (optarg != NULL)
32 				printf(" -%c %s", c, optarg);
33 			else
34 				printf(" -%c", c);
35 			break;
36 		}
37 	printf(" --");
38 	for (; optind < argc; optind++)
39 		printf(" %s", argv[optind]);
40 	printf("\n");
41 	exit(status);
42 }
43