1 
2 /* jgaa oct 9th 2000: Found this on www.
3  * No copyright information given.
4  * Slightly modidied.
5  *
6  * Origin: http://www.winsite.com/info/pc/win3/winsock/sossntr4.zip/SOSSNT/SRC/GETOPT.C.html
7  */
8 
9  /* got this off net.sources  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "getopt.h"
16 
17 
18 /*
19 * get option letter from argument vector
20 */
21 int	opterr = 1,		/* useless, never set or used */
22 optind = 1,		/* index into parent argv vector */
23 optopt;			/* character checked for validity */
24 char* optarg;		/* argument associated with option */
25 
26 #define BADCH	(int)'?'
27 #define EMSG	""
28 #define tell(s)	fputs(*argv,stderr);fputs(s,stderr); \
29 	fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
30 
31 int getopt(int argc, char * const *argv, const char *optstring)
32 {
33 	static char	*place = EMSG;	/* option letter processing */
34 	register char	*oli;		/* option letter list index */
35 
36 	if(!*place) {			/* update scanning pointer */
37 		if(optind >= argc || *(place = argv[optind]) != '-' || !*++place)
38 			return(EOF);
39 		if (*place == '-') {	/* found "--" */
40 			++optind;
41 			return(EOF);
42 		}
43 	}				/* option letter okay? */
44 	if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(optstring,optopt)))
45 	{
46 		if(!*place) ++optind;
47 		tell(": illegal option -- ");
48 	}
49 	if (*++oli != ':') {		/* don't need argument */
50 		optarg = NULL;
51 		if (!*place) ++optind;
52 	}
53 	else {				/* need an argument */
54 		if (*place) optarg = place;	/* no white space */
55 		else if (argc <= ++optind) {	/* no arg */
56 			place = EMSG;
57 			tell(": option requires an argument -- ");
58 		}
59 		else optarg = argv[optind];	/* white space */
60 		place = EMSG;
61 		++optind;
62 	}
63 	return(optopt);			/* dump back option letter */
64 }
65