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