xref: /freebsd/usr.bin/getopt/getopt.c (revision 821df508e882ca532bec415852399c6558f22f7a)
1e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
2e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
362f882d6SWarner Losh 
459f31bb2SBrooks Davis /*
559f31bb2SBrooks Davis  * This material, written by Henry Spencer, was released by him
659f31bb2SBrooks Davis  * into the public domain and is thus not subject to any copyright.
759f31bb2SBrooks Davis  */
859f31bb2SBrooks Davis 
9b37b9a6dSNate Williams #include <stdio.h>
10821df508SXin LI #include <stdlib.h>
1162f882d6SWarner Losh #include <unistd.h>
12b37b9a6dSNate Williams 
13b2688868SDavid Malone int
14f4ac32deSDavid Malone main(int argc, char *argv[])
15b37b9a6dSNate Williams {
16b37b9a6dSNate Williams 	int c;
17b37b9a6dSNate Williams 	int status = 0;
18b37b9a6dSNate Williams 
19b37b9a6dSNate Williams 	optind = 2;	/* Past the program name and the option letters. */
201c8af878SWarner Losh 	while ((c = getopt(argc, argv, argv[1])) != -1)
21b37b9a6dSNate Williams 		switch (c) {
22b37b9a6dSNate Williams 		case '?':
23b37b9a6dSNate Williams 			status = 1;	/* getopt routine gave message */
24b37b9a6dSNate Williams 			break;
25b37b9a6dSNate Williams 		default:
26b37b9a6dSNate Williams 			if (optarg != NULL)
270ab2a7aeSMartin Cracauer 				printf(" -%c %s", c, optarg);
28b37b9a6dSNate Williams 			else
29b37b9a6dSNate Williams 				printf(" -%c", c);
30b37b9a6dSNate Williams 			break;
31b37b9a6dSNate Williams 		}
32b37b9a6dSNate Williams 	printf(" --");
33b37b9a6dSNate Williams 	for (; optind < argc; optind++)
34b37b9a6dSNate Williams 		printf(" %s", argv[optind]);
35b37b9a6dSNate Williams 	printf("\n");
366c28b67bSMark Murray 	return status;
37b37b9a6dSNate Williams }
38