xref: /freebsd/usr.bin/getopt/getopt.c (revision b37b9a6d2d8f98fdefaae729547cc6681bdbfcc7)
1b37b9a6dSNate Williams #include <stdio.h>
2b37b9a6dSNate Williams 
3b37b9a6dSNate Williams main(argc, argv)
4b37b9a6dSNate Williams int argc;
5b37b9a6dSNate Williams char *argv[];
6b37b9a6dSNate Williams {
7b37b9a6dSNate Williams 	extern int optind;
8b37b9a6dSNate Williams 	extern char *optarg;
9b37b9a6dSNate Williams 	int c;
10b37b9a6dSNate Williams 	int status = 0;
11b37b9a6dSNate Williams 
12b37b9a6dSNate Williams 	optind = 2;	/* Past the program name and the option letters. */
13b37b9a6dSNate Williams 	while ((c = getopt(argc, argv, argv[1])) != EOF)
14b37b9a6dSNate Williams 		switch (c) {
15b37b9a6dSNate Williams 		case '?':
16b37b9a6dSNate Williams 			status = 1;	/* getopt routine gave message */
17b37b9a6dSNate Williams 			break;
18b37b9a6dSNate Williams 		default:
19b37b9a6dSNate Williams 			if (optarg != NULL)
20b37b9a6dSNate Williams 				printf(" -%c %s", c, optarg);
21b37b9a6dSNate Williams 			else
22b37b9a6dSNate Williams 				printf(" -%c", c);
23b37b9a6dSNate Williams 			break;
24b37b9a6dSNate Williams 		}
25b37b9a6dSNate Williams 	printf(" --");
26b37b9a6dSNate Williams 	for (; optind < argc; optind++)
27b37b9a6dSNate Williams 		printf(" %s", argv[optind]);
28b37b9a6dSNate Williams 	printf("\n");
29b37b9a6dSNate Williams 	exit(status);
30b37b9a6dSNate Williams }
31