Home | History | Annotate | Line # | Download | only in getopt
getopt.c revision 1.7
      1  1.7      wiz /*	$NetBSD: getopt.c,v 1.7 2006/07/09 21:38:47 wiz Exp $	*/
      2  1.7      wiz 
      3  1.7      wiz /*
      4  1.7      wiz  * This material, written by Henry Spencer, was released by him
      5  1.7      wiz  * into the public domain and is thus not subject to any copyright.
      6  1.7      wiz  */
      7  1.3      tls 
      8  1.4    lukem #include <sys/cdefs.h>
      9  1.2  mycroft #ifndef lint
     10  1.7      wiz __RCSID("$NetBSD: getopt.c,v 1.7 2006/07/09 21:38:47 wiz Exp $");
     11  1.2  mycroft #endif /* not lint */
     12  1.2  mycroft 
     13  1.4    lukem #include <errno.h>
     14  1.1   brezak #include <stdio.h>
     15  1.6     matt #include <stdlib.h>
     16  1.5    perry #include <unistd.h>
     17  1.1   brezak 
     18  1.4    lukem int	main __P((int, char **));
     19  1.4    lukem 
     20  1.4    lukem int
     21  1.1   brezak main(argc, argv)
     22  1.4    lukem 	int argc;
     23  1.4    lukem 	char *argv[];
     24  1.1   brezak {
     25  1.1   brezak 	int c;
     26  1.1   brezak 	int status = 0;
     27  1.1   brezak 
     28  1.1   brezak 	optind = 2;	/* Past the program name and the option letters. */
     29  1.4    lukem 	while ((c = getopt(argc, argv, argv[1])) != -1)
     30  1.1   brezak 		switch (c) {
     31  1.1   brezak 		case '?':
     32  1.1   brezak 			status = 1;	/* getopt routine gave message */
     33  1.1   brezak 			break;
     34  1.1   brezak 		default:
     35  1.1   brezak 			if (optarg != NULL)
     36  1.1   brezak 				printf(" -%c %s", c, optarg);
     37  1.1   brezak 			else
     38  1.1   brezak 				printf(" -%c", c);
     39  1.1   brezak 			break;
     40  1.1   brezak 		}
     41  1.1   brezak 	printf(" --");
     42  1.1   brezak 	for (; optind < argc; optind++)
     43  1.1   brezak 		printf(" %s", argv[optind]);
     44  1.1   brezak 	printf("\n");
     45  1.1   brezak 	exit(status);
     46  1.1   brezak }
     47