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