#define EOF -1
#define NULL ((char *)0)

char *optarg;
int optind=1;

getopt(argc, argv, opt)
char **argv;
char *opt;
{
	static int optoff=1, finished=0;
	char *arg, *scan;

	if (finished)
		return EOF;

	if (optind == argc) {
		finished=1;
		return EOF;
	}

	arg=argv[optind];
	if (*arg != '-') {
		finished=1;
		return EOF;
	}

	arg += optoff;
	if (*arg == '\0') {
		finished=1;
		return EOF;
	}

	if (*arg == '-') {
		optind++;
		finished=1;
		return EOF;
	}

	for (scan=opt; *scan; scan++)
		if (*scan == *arg)
			break;
	if (*scan == '\0') {
		finished=1;
		return '?';
	}

	arg++;
	if (scan[1] != ':') {
		optoff++;
		optarg=NULL;
		if (*arg == '\0') {
			optind++;
			optoff=1;
		}
	} else {
		optoff=1;
		optind++;
		if (*arg != '\0')
			optarg=arg;
		else
			optarg=argv[optind++];
	}

	return *scan;
}
