/* 20070425 bkw:
   Public Domain implementation of getopt(),
   downloaded from http://www.zdomain.com/a56/src/getopt.c
   Following anonymous comment was in the original version:
*/

/*
   I got this off net.sources from Henry Spencer.
   It is a public domain getopt(3) like in System V.
   I have made the following modifications:

   index(s,c) was added because too many people could
   not compile getopt without it.

   A test main program was added, ifdeffed by GETOPT.
   This main program is a public domain implementation
   of the getopt(1) program like in System V.  The getopt
   program can be used to standardize shell option handling.
      e.g.  cc -DGETOPT getopt.c -o getopt
*/

/* 20070425 bkw:
   Altered slightly for cassio (use fatal() for error messages,
   get rid of main(), add return type to getopt()...)
*/

/* 20241214 bkw: modified slightly for use with uxd. */

extern char *self;

#include <stdio.h>

#define ARGCH    (int)':'
#define BADCH	 (int)'?'
#define EMSG	 ""
#define ENDARGS  "--"

/* this is included because index is not on some UNIX systems */
static char *index (char *s, int c) {
	while (*s)
		if (c == *s) return (s);
		else s++;
	return (NULL);
}

/*
 * get option letter from argument vector
 */
int my_optind = 1,   /* index into parent argv vector */
    my_optopt;       /* character checked for validity */
char *my_optarg;     /* argument associated with option */

#define tell(s) fprintf(stderr, "%s: %s", self, s); \
		fputc(my_optopt,stderr);fputs(" (use -h for help)\n",stderr);return(BADCH);

int my_getopt(int nargc, char **nargv, char *ostr) {
	static char	*place = EMSG;	/* option letter processing */
	char	*oli;		/* option letter list index */

	if(!*place) {			/* update scanning pointer */
		if(my_optind >= nargc || *(place = nargv[my_optind]) != '-' || !*++place) return(EOF);
		if (*place == '-') {	/* found "--" */
			++my_optind;
			return(EOF);
		}
	}				/* option letter okay? */
	if ((my_optopt = (int)*place++) == ARGCH || !(oli = index(ostr,my_optopt))) {
		if(!*place) ++my_optind;
		tell("illegal option: -");
	}
	if (*++oli != ARGCH) {		/* don't need argument */
		my_optarg = NULL;
		if (!*place) ++my_optind;
	}
	else {				/* need an argument */
		if (*place) my_optarg = place;	/* no white space */
		else if (nargc <= ++my_optind) {	/* no arg */
			place = EMSG;
			tell("option requires an argument: -");
		}
	 	else my_optarg = nargv[my_optind];	/* white space */
		place = EMSG;
		++my_optind;
	}
	return(my_optopt);			/* dump back option letter */
}

