/*
 * Micro-X -- an X server for DOS
 * Copyright (C) 1995 StarNet Communications Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * StarNet Communications Corp.
 * 550 Lakeside Dr. #3
 * Sunnyvale CA 94086 US
 * http://www.starnet.com
 * x-dos@starnet.com
 */

#include "pcdefs.h"
#include "funcdef.h"

typedef unsigned char u_char;

char _far * _fastcall
getenv(var)
char *var;
{
    _segment base;
    char _based(base) * cp, _based(base) *env;
    int len = strlen(var);

    /* Get PSP */
    _asm	mov	ah,0x62
    _asm	int	0x21
    _asm	mov	base,bx
    cp = 0;
    env = 0;

    /* Get the segment of the environment and use that as our base */
    base = *(short _based(base) *)(&cp[0x2c]);

    while (*cp) {
	env = cp;
	while (*env && *env != '=')
	    env++;
	if (*env && env - cp == len && memcmp(var, cp, len) == 0)
	    break;
	cp = env;
	while (*cp)
	    cp++;
	cp++;
    }
    if (*cp)
	return (char _far *)(env + 1);
    else
	return 0;
}

static char tolower[128] = {
	'\0',	'\1',	'\2',	'\3',	'\4',	'\5',	'\6',	'\7',
	'\010',	'\011',	'\012',	'\013',	'\014',	'\015',	'\016',	'\017',
	'\020',	'\021',	'\022',	'\023',	'\024',	'\025',	'\026',	'\027',
	'\030',	'\031',	'\032',	'\033',	'\034',	'\035',	'\036',	'\037',
	' ',	'!',	'\"',	'#',	'$',	'%',	'&',	'\'',
	'(',	')',	'*',	'+',	',',	'-',	'.',	'/',
	'0',	'1',	'2',	'3',	'4',	'5',	'6',	'7',
	'8',	'9',	':',	';',	'<',	'=',	'>',	'?',
	'@',	'a',	'b',	'c',	'd',	'e',	'f',	'g',
	'h',	'i',	'j',	'k',	'l',	'm',	'n',	'o',
	'p',	'q',	'r',	's',	't',	'u',	'v',	'w',
	'x',	'y',	'z',	'[',	'\\',	']',	'^',	'_',
	'`',	'a',	'b',	'c',	'd',	'e',	'f',	'g',
	'h',	'i',	'j',	'k',	'l',	'm',	'n',	'o',
	'p',	'q',	'r',	's',	't',	'u',	'v',	'w',
	'x',	'y',	'z',	'{',	'|',	'}',	'~',	'\177'
};

int
stricmp(s1, s2)
char *s1, *s2;
{
    while (tolower[*s1] == tolower[*s2]) {
	if (!*s1)
	    return 0;
	s1++;
	s2++;
    }
    return tolower[*s1] - tolower[*s2];
}

unsigned long _fastcall
inet_addr(name)
char *name;
{
	unsigned char addr[4];
	unsigned char *ap;

	*(unsigned long *)addr = 0;
	for (ap = addr; ap < &addr[4]; ap++) {
		while (*name >= '0' && *name <= '9')
		    *ap = (u_char)((u_char)10 * *ap + (u_char)*name++ - '0');
		if (*name++ != '.' && ap < &addr[3])
		    return (unsigned long)-1;
	}
	return *(unsigned long *)addr;
}

int _fastcall
atoi(a)
char *a;
{
    int i = 0;
    char neg;

    if (*a == '-') {
	neg = 1;
	a++;
    } else
	neg = 0;
    while (*a <= '9' && *a >= '0')
	i = i * 10 + (*a++ - '0');
    return i;
}

int _fastcall
xtoi(a)
char *a;
{
    int i = 0;

    while (*a == '0')
	a++;
    if (*a == 'x' || *a == 'X')
	a++;
    while (*a) {
	if (*a <= '9' && *a >= '0')
	    i = (i << 4) + (*a++ - '0');
	else if (*a <= 'F' && *a >= 'A')
	    i = (i << 4) + (*a++ - 'A' + 10);
	else if (*a <= 'f' && *a >= 'a')
	    i = (i << 4) + (*a++ - 'a' + 10);
	else
	    break;
    }
    return i;
}
