#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <ctype.h>
#ifndef	UNIX
#include    <jctype.h>
#include    <dos.h>
#endif
#include    "defs.h"

#ifndef	UNIX

#ifdef	LSIC
int	_dos_findfirst(char *file, int mod, struct find_t *dma)
{
    union REGS regs;
    struct SREGS seg;
    char far *p;

    p = (char far *)dma;
    regs.h.ah = 0x1a;
    seg.ds = FP_SEG(p);
    regs.x.dx = FP_OFF(p);
    intdosx(&regs, &regs, &seg);

    p = (char far *)file;
    regs.h.ah = 0x4e;
    regs.x.cx = mod;
    seg.ds = FP_SEG(p);
    regs.x.dx = FP_OFF(p);
    intdosx(&regs, &regs, &seg);
    return ((regs.x.flags & 1) ? ERR : FALSE);
}
int	_dos_findnext(struct find_t *dma)
{
    union REGS regs;

    regs.h.ah = 0x4F;
    intdos(&regs, &regs);
    return ((regs.x.flags & 1) ? ERR : FALSE);
}
char	*strdup(char *str)
{
    char    *p;

    if ( (p = (char *)malloc(strlen(str) + 1)) != NULL )
	strcpy(p, str);
    return p;
}
#endif

int	strlcmp(char *s, char *p)
{
    int c;

    for ( ; ; ) {
	if ( (c = tolower(*s) - tolower(*p)) != 0 )
	    return c;
	if ( *s == '\0' )
	    return 0;
	s++;
	p++;
    }
}
char	*strlcpy(char *s, char *p)
{
    char *q = s;

    while ( *p != '\0' ) {
	if ( iskanji(p[0]) && iskanji2(p[1]) ) {
	    *(q++) = *(p++);
	    *(q++) = *(p++);
	} else {
	    *(q++) = tolower(*p);
	    p++;
	}
    }
    *q = '\0';
    return s;
}

static	char	*wild_ptr = NULL;
static	char	wild_buf[STRLEN + 2];
static	struct find_t dma;

char	*wild_file(char *file)
{
    if ( wild_ptr == NULL ) {
	strcpy(wild_buf, file);
	if ( (wild_ptr = strrchr(wild_buf, '\\')) != NULL ||
	     (wild_ptr = strrchr(wild_buf, ':')) != NULL )
	    wild_ptr += 1;
	else
	    wild_ptr = wild_buf;

	if ( _dos_findfirst(file, 0x21, &dma) ) {
	    wild_ptr = NULL;
	    return NULL;
	}

    } else if ( _dos_findnext(&dma) ) {
	wild_ptr = NULL;
	return NULL;
    }

    strlcpy(wild_ptr, dma.name);
    return wild_buf;
}

int	execute(int sh, char *cmd)
{
    int n;
    int ac = 0;
    char *p;
    char *av[ARG_MAX + 1];
    static char *dos[] = {
        "cd",    "chdir", "cls",  "copy",  "ctty",
        "date",  "del",   "dir",  "echo",  "erase",
	"md",    "mkdir", "rd",   "ren",   "rename",
	"rmdir", "time",  "type", "ver",   "verify",
	"vol",   NULL
    };
    
    if ( (p = get_env("SHELL")) == NULL ||
	 (p = get_env("COMSPEC")) == NULL )
	p = "COMMAND.COM";

    av[ac++] = p;
    av[ac++] = "/c";

    p = cmd;
    while ( *p != '\0' ) {
	while ( *p == ' ' || *p == '\t' )
	    p++;
	if ( ac >= ARG_MAX ) {
	    fprintf(stderr, "arg overflow '%s'\n", p);
	    exit(2);
	}
	av[ac++] = p;
	while ( *p != ' ' && *p != '\t' && *p != '\0' ) {
	    if ( *(p++) == '"' ) {
		while ( *p != '"' && *p != '\0' )
		    p++;
	    }
	}
	if ( *p != '\0' )
	    *(p++) = '\0';
    }
    av[ac] = NULL;

    if ( sh == FALSE ) {
	for ( n = 0 ; dos[n] != NULL ; n++ ) {
	    if ( strlcmp(dos[n], av[2]) == 0 ) {
		sh = TRUE;
		break;
	    }
	}
    }

    ac = (sh ? 0 : 2);
    return spawnvp(0, av[ac], &(av[ac]));
}

	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     c;
    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;
    }
}
#endif
