xref: /openbsd-src/usr.bin/getopt/getopt.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: getopt.c,v 1.7 2006/06/07 07:33:23 jmc 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 #ifndef lint
9 static char rcsid[] = "$OpenBSD: getopt.c,v 1.7 2006/06/07 07:33:23 jmc Exp $";
10 #endif /* not lint */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 
16 int
17 main(int argc, char *argv[])
18 {
19 	extern int optind;
20 	extern char *optarg;
21 	int c;
22 	int status = 0;
23 
24 	optind = 2;	/* Past the program name and the option letters. */
25 	while ((c = getopt(argc, argv, argv[1])) != -1)
26 		switch (c) {
27 		case '?':
28 			status = 1;	/* getopt routine gave message */
29 			break;
30 		default:
31 			if (optarg != NULL)
32 				printf(" -%c %s", c, optarg);
33 			else
34 				printf(" -%c", c);
35 			break;
36 		}
37 	printf(" --");
38 	for (; optind < argc; optind++)
39 		printf(" %s", argv[optind]);
40 	printf("\n");
41 	exit(status);
42 }
43