/*-
 * Copyright (c) 2003 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * This software was developed for the TrustedBSD Project in part by Network
 * Associates Laboratories, the Security Research Division of Network
 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
 * as part of the DARPA CHATS research program.
 *
 * 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 AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <libutil.h>
#include <paths.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#define	PTM_PREFIX	"pty"
#define	PTS_PREFIX	"pts/"
#define	PTMX		"ptmx"

char *
ptsname(int filedes)
{
	static char slave[] = _PATH_DEV PTS_PREFIX "XYZ";
	struct stat sb;

	if (fstat(filedes, &sb) == 0) {
		(void)sprintf(slave, _PATH_DEV PTS_PREFIX "%d",
		    minor(sb.st_rdev));
		return (slave);
	}
	return (NULL);
}

int
posix_openpt(int oflag)
{
	int filedes;

	filedes = -1;
	if (oflag & ~(O_RDWR | O_NOCTTY)) 
		errno = EINVAL;
	else
		filedes = open(_PATH_DEV PTMX, oflag);
	return (filedes);
}

int
grantpt(int filedes)
{

	return (0);
}

int
unlockpt(int filedes)
{

	return (0);
}

int
openpty(int *amaster, int *aslave, char *name, struct termios *termp,
    struct winsize *winp)
{
	int master, slave;

	master = posix_openpt(O_RDWR);
	if (master == -1)
		return (-1);

	if (grantpt(master) == -1) {
		close(master);
		return (-1);
	}

	slave = open(ptsname(master), O_RDWR);
	if (slave == -1) {
		close(master);
		return (-1);
	}

	if (unlockpt(master) == -1) {
		close(master);
		close(slave);
		return (-1);
	}

	*amaster = master;
	*aslave = slave;

	if (name)
		strcpy(name, ptsname(master));
	if (termp)
		tcsetattr(slave, TCSAFLUSH, termp);
	if (winp)
		ioctl(slave, TIOCSWINSZ, (char *)winp);

	return (0);
}
