/* vi: set sw=4 ts=4: */
/*
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include <defs.h>

#if defined(__Minix__)

#ifdef HAVE_SYS_MEMIO_H   /* For Minix-vmd */

static	struct mio_map mio_map;
static	int vgaFd;

int MinixMmapVideoRAM(unsigned char **gMem)
{
#define VIDEO_SIZE 0x20000  /* 128KB */

	unsigned char *vidbase;
    uid_t real_uid;

	*gMem = (unsigned char *)MAP_FAILED; /* error flag for gramMem */

	real_uid = getuid();
	setuid(0);
	vgaFd = open("/dev/vga", O_RDWR); /* Minix-vmd 1.7.0 for  */
	if (vgaFd == -1)
		return FAILURE;
	setuid(real_uid);

	vidbase = (unsigned char *)malloc(VIDEO_SIZE);
	if (!vidbase) return FAILURE;

	mio_map.mm_base = vidbase;
	mio_map.mm_size = VIDEO_SIZE;
	
	if (ioctl(vgaFd, MIOCMAP, &mio_map) == -1)
		return FAILURE;

	*gMem = vidbase;

	return SUCCESS;
}

int MinixMunmapVideoRAM(void)
{
	if (vgaFd >= 0)
	{
		close(vgaFd);
		vgaFd = -1;
	}
}

#endif /* HAVE_SYS_MEMIO_H  */

#ifndef HAVE_STRDUP

char * strdup(const char *str)
{
    size_t len;
    char *copy;

    len = strlen(str) + 1;
    if (!(copy = malloc((u_int)len)))
        return (NULL);
    memcpy(copy, str, len);
    return (copy);
}
                                                              
#endif  /* HAVE_STRDUP */

#ifndef HAVE_MKSTEMP 

static int _gettemp(char *path, register int *doopen)
{
	extern int errno;
	register char *start, *trv;
	struct stat sbuf;
	u_int pid;

	pid = getpid();
	for (trv = path; *trv; ++trv);		/* extra X's get set to 0's */
	while (*--trv == 'X') 
	{
		*trv = (pid % 10) + '0';
		pid /= 10;
	}

	for (start = trv + 1;; --trv) 
	{
		if (trv <= path)
			break;
		if (*trv == '/') {
			*trv = '\0';
			if (stat(path, &sbuf))
				return(0);
			if (!S_ISDIR(sbuf.st_mode)) 
			{
				errno = ENOTDIR;
				return(0);
			}
			*trv = '/';
			break;
		}
	}

	for (;;) 
	{
		if (doopen) {
			if ((*doopen =
			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
				return(1);
			if (errno != EEXIST)
				return(0);
		}
		else if (stat(path, &sbuf))
			return(errno == ENOENT ? 1 : 0);

		for (trv = start;;) {
			if (!*trv)
				return(0);
			if (*trv == 'z')
				*trv++ = 'a';
			else {
				if (isdigit(*trv))
					*trv = 'a';
				else
					++*trv;
				break;
			}
		}
	}

}

int mkstemp(char *template)
{
	int fd;

	return (_gettemp(template, &fd) ? fd : -1);
}

#endif  /* HAVE_MKSTEMP */

#endif  /* __Minix__ */
