xref: /netbsd-src/usr.bin/getopt/getopt.c (revision 504d664ef911b7b7b2adff7bde54af404df61cca)
1*504d664eSwiz /*	$NetBSD: getopt.c,v 1.8 2006/07/09 21:39:48 wiz Exp $	*/
21f359fc5Swiz 
31f359fc5Swiz /*
41f359fc5Swiz  * This material, written by Henry Spencer, was released by him
51f359fc5Swiz  * into the public domain and is thus not subject to any copyright.
61f359fc5Swiz  */
79d225a17Stls 
88c012c72Slukem #include <sys/cdefs.h>
9aee4b07bSmycroft #ifndef lint
10*504d664eSwiz __RCSID("$NetBSD: getopt.c,v 1.8 2006/07/09 21:39:48 wiz Exp $");
11aee4b07bSmycroft #endif /* not lint */
12aee4b07bSmycroft 
13a7ee7f88Sbrezak #include <stdio.h>
14fcd0fb11Smatt #include <stdlib.h>
153a2316b4Sperry #include <unistd.h>
16a7ee7f88Sbrezak 
178c012c72Slukem int
main(int argc,char * argv[])18*504d664eSwiz main(int argc, char *argv[])
19a7ee7f88Sbrezak {
20a7ee7f88Sbrezak 	int c;
21a7ee7f88Sbrezak 	int status = 0;
22a7ee7f88Sbrezak 
23a7ee7f88Sbrezak 	optind = 2;	/* Past the program name and the option letters. */
248c012c72Slukem 	while ((c = getopt(argc, argv, argv[1])) != -1)
25a7ee7f88Sbrezak 		switch (c) {
26a7ee7f88Sbrezak 		case '?':
27a7ee7f88Sbrezak 			status = 1;	/* getopt routine gave message */
28a7ee7f88Sbrezak 			break;
29a7ee7f88Sbrezak 		default:
30a7ee7f88Sbrezak 			if (optarg != NULL)
31a7ee7f88Sbrezak 				printf(" -%c %s", c, optarg);
32a7ee7f88Sbrezak 			else
33a7ee7f88Sbrezak 				printf(" -%c", c);
34a7ee7f88Sbrezak 			break;
35a7ee7f88Sbrezak 		}
36a7ee7f88Sbrezak 	printf(" --");
37a7ee7f88Sbrezak 	for (; optind < argc; optind++)
38a7ee7f88Sbrezak 		printf(" %s", argv[optind]);
39a7ee7f88Sbrezak 	printf("\n");
40a7ee7f88Sbrezak 	exit(status);
41a7ee7f88Sbrezak }
42