#include <stdio.h>
#include <signal.h>

int tty;
char *argp[] = { "-sh",0};
char *envp[] = { 0 };

_main()
{

    close(0);
    close(1);
    close(2);

    signal(SIGINT,SIG_IGN);
   
    spawn("/dev/tty1");
/*    spawn("/dev/tty2"); */
    wait(0);
}




spawn(ttyname)
char *ttyname;
{
    int pid;
    char ubuf[120];
    int tty;

    if (fork() == 0)
    {
	tty = open(ttyname,2);
	if (tty < 0)
		_exit(-1);

	dup(tty);
	dup(tty);

        while(1)
        {
	    pid = fork();
	    if (pid == -1)
	    {
	        perror("init: cant fork");
		continue;
     	    }

            if (pid)
	    {
	        while (wait(0) != pid && wait(0) != -1) {}
	    }
            else
            {
	        write(1,"user: ",6);
		read(0, ubuf, sizeof(ubuf));
	        setuid(atoi(ubuf));
	        signal(SIGINT,SIG_DFL);
 	        execve("/bin/sh",argp,envp);
	        execve("/sh",argp,envp);
	        perror("fork: cant exec sh");
            }
	}
    }
}
