Home | History | Annotate | Line # | Download | only in getopt
getopt.c revision 1.7
      1 /*	$NetBSD: getopt.c,v 1.7 2006/07/09 21:38:47 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.7 2006/07/09 21:38:47 wiz Exp $");
     11 #endif /* not lint */
     12 
     13 #include <errno.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <unistd.h>
     17 
     18 int	main __P((int, char **));
     19 
     20 int
     21 main(argc, argv)
     22 	int argc;
     23 	char *argv[];
     24 {
     25 	int c;
     26 	int status = 0;
     27 
     28 	optind = 2;	/* Past the program name and the option letters. */
     29 	while ((c = getopt(argc, argv, argv[1])) != -1)
     30 		switch (c) {
     31 		case '?':
     32 			status = 1;	/* getopt routine gave message */
     33 			break;
     34 		default:
     35 			if (optarg != NULL)
     36 				printf(" -%c %s", c, optarg);
     37 			else
     38 				printf(" -%c", c);
     39 			break;
     40 		}
     41 	printf(" --");
     42 	for (; optind < argc; optind++)
     43 		printf(" %s", argv[optind]);
     44 	printf("\n");
     45 	exit(status);
     46 }
     47