xref: /openbsd-src/usr.bin/getopt/getopt.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: getopt.c,v 1.6 2003/07/10 00:06:51 david Exp $	*/
2 
3 #ifndef lint
4 static char rcsid[] = "$OpenBSD: getopt.c,v 1.6 2003/07/10 00:06:51 david Exp $";
5 #endif /* not lint */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 
11 int
12 main(int argc, char *argv[])
13 {
14 	extern int optind;
15 	extern char *optarg;
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 	exit(status);
37 }
38