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