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