/*
	GETOPT
*/

#include <stdio.h>

#define ARGCH (int)':'
#define BADCH (int)'?'
#define EMSG ""
#define ENDARGS "--"

static char *index( s, c ) 
register char *s ;
register int c ;
{
	while(*s)
		if(c == *s) return(s) ;
		else s++ ;
	return(NULL ) ;
}

int opterr = 1 ;
int optind = 1 ;
int optopt ;
char *optarg ;

#define tell(s) fputs(*nargv,stderr);fputs(s,stderr);fputc(optopt,stderr);fputc('\n',stderr);return(BADCH) ;

getopt(nargc,nargv,ostr)
int nargc ;
char **nargv, *ostr ;
{
	static char *place = EMSG ;
	char *oli ;

	if(!*place) {
		if(optind >= nargc || 
			*(place = nargv[optind]) != '-' ||
			!*++place ) return(EOF); 
		if(*place == '-') {
			++optind ;
			return(EOF) ;
		}
	}
	if((optopt =(int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
		if(!*place) ++optind ;
		tell(": illegal option -- ") ;
	}
	if(*++oli != ARGCH) {
		optarg = NULL ;
		if( !*place) ++optind ;
	}else {
		if( *place) optarg = place ;
		else if ( nargc <= ++optind ) {
			place = EMSG ;
			tell( ":option requires an argument -- " ) ;
		}
		else optarg = nargv[optind] ;
		place = EMSG ;
		++optind ;
	}
	return( optopt) ;
}


