|  | #include <stdio.h>
#include <string.h>
#define tell(S) \
if (opterr) { \
	(void) fprintf(stderr, "%s%s%c\n", *nargv, S, optopt); \
} \
return ('?'); 
/*
 * statics
 */
static char *place = "";	/* option letter processing */
static int opterr, optopt;
int optind;
char *optarg;
void getopt_init()
{
	opterr = 1;
	optind = 0;
	optopt = 0;
	optarg = NULL;
	place = "";
}
/*
 * get command line arguments. 
 */
char *getarg(nargc, nargv)
	int nargc;
	char *nargv[];
{
	if (optind >= nargc)
		return (NULL);
	else
		return (nargv[optind++]);
}
int getopt(nargc, nargv, ostr)
	int nargc;
	char *nargv[];
	char *ostr;
{
	register char *oli;		/* option letter list index */
	if (!*place) {			/* update scanning pointer */
		if (optind >= nargc) {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (*(place = nargv[optind]) != '-') {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (!*++place) {
			place = "";	/* if there's a next time */
			return (EOF);
		}
		if (*place == '-') {	/* found "--" */
			++optind;
			place = "";	/* if there's a next time */
			return (EOF);
		}
	}				/* option letter okay? */
	optopt = (int)*place++;
	oli = strchr(ostr, optopt);
	if ((optopt == (int)':') || !oli) {
		if (!*place)
			++optind;
		tell(": illegal option -- ");
	}
	if (*++oli != ':') {		/* don't need argument */
		optarg = NULL;
		if (!*place)
			++optind;
	} else {				/* need an argument */
		if (*place) {
			optarg = place;		/* no white space */
		} else if (nargc <= ++optind) {	/* no arg */
			place = "";
			tell(": option requires an argument -- ");
		} else {
			optarg = nargv[optind];	/* white space */
		}
		place = "";
		++optind;
	}
	return (optopt);			/* dump back option letter */
}
 |