#include    <stdio.h>
#include    <string.h>
#include    <ctype.h>

	char	*optarg = "";
	int	optind = 1;
static  char	*optstr = NULL;

char	*progname(char *prog)
{
    char    *p;

    if ( (p = strrchr(prog, ':')) != NULL )
	prog = p + 1;
    if ( (p = strrchr(prog, '\\')) != NULL )
	prog = p + 1;
    if ( (p = strrchr(prog, '/')) != NULL )
	prog = p + 1;
    for ( p = prog ; *p != '\0' ; p++ )
	*p = tolower(*p);
    if ( (p = strrchr(prog, '.')) != NULL && strcmp(p, ".exe") == 0 )
	*p = '\0';
    return prog;
}
int	getopt(ac, av, opt)
int	ac;
char	*av[];
char	*opt;
{
    int     n, c;
    char    *p;
    char    *s;

    for ( ; ; ) {
	if ( optstr != NULL && *optstr != '\0' ) {
	    s = opt;
	    while ( *s != '\0' ) {
		if ( *(s++) == *optstr ) {
		    c = *(optstr++);
		    if ( *s == ':' ) {
			s++;

			if ( *optstr == '=' )
			    optstr++;

			if ( *optstr != '\0' )
			    optarg = optstr;
			else if ( optind < ac && *av[optind] != '-' )
			    optarg = av[optind++];
			else {
			    fprintf(stderr,
				"%s: option `-%c' requires an argument\n",
				progname(av[0]), c);
			    optstr = NULL;
			    return '\0';
			}

			optstr = NULL;
		    }
		    return c;
		}
		if ( *s == ':' )
		    s++;
	    }

	    fprintf(stderr, "%s: unrecognized option `-%c'\n",
				progname(av[0]), *optstr);
	    return *(optstr++);
	}

	if ( optind >= ac || *av[optind] != '-' )
	    return EOF;

	optstr = av[optind++] + 1;
    }
}
