xref: /freebsd/usr.bin/getopt/getopt.c (revision 59f31bb2d0e08df819b633af3af91433072c552b)
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 <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 int
14 main(int argc, char *argv[])
15 {
16 	int c;
17 	int status = 0;
18 
19 	optind = 2;	/* Past the program name and the option letters. */
20 	while ((c = getopt(argc, argv, argv[1])) != -1)
21 		switch (c) {
22 		case '?':
23 			status = 1;	/* getopt routine gave message */
24 			break;
25 		default:
26 			if (optarg != NULL)
27 				printf(" -%c %s", c, optarg);
28 			else
29 				printf(" -%c", c);
30 			break;
31 		}
32 	printf(" --");
33 	for (; optind < argc; optind++)
34 		printf(" %s", argv[optind]);
35 	printf("\n");
36 	return status;
37 }
38