72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#include <fcntl.h>
|
|
#include <pty.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
#include <utmp.h>
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
extern "C"
|
|
{
|
|
int openpty(int* master, int* slave, char* name, const struct termios* term, const struct winsize* win)
|
|
{
|
|
*master = posix_openpt(O_RDWR);
|
|
if (*master < 0) return -1;
|
|
|
|
// No-ops on Luna, and since this is the Luna libc, we can skip error handling.
|
|
grantpt(*master);
|
|
unlockpt(*master);
|
|
|
|
char* pname = ptsname(*master);
|
|
if (!pname)
|
|
{
|
|
close(*master);
|
|
return -1;
|
|
}
|
|
|
|
if (name)
|
|
{
|
|
fputs("warning: openpty() called with non-null name, this is insecure\n", stderr);
|
|
strcpy(name, pname);
|
|
}
|
|
|
|
*slave = open(pname, O_RDWR);
|
|
if (*slave < 0)
|
|
{
|
|
close(*master);
|
|
return -1;
|
|
}
|
|
|
|
if (term) tcsetattr(*slave, TCSANOW, term);
|
|
if (win) fputs("warning: openpty() called with non-null window size, this is not supported yet\n", stderr);
|
|
|
|
return 0;
|
|
}
|
|
|
|
pid_t forkpty(int* master, char* name, const struct termios* term, const struct winsize* win)
|
|
{
|
|
int slave;
|
|
if (openpty(master, &slave, name, term, win) < 0) return -1;
|
|
|
|
pid_t child = fork();
|
|
if (child < 0)
|
|
{
|
|
close(*master);
|
|
close(slave);
|
|
return -1;
|
|
}
|
|
if (!child)
|
|
{
|
|
close(*master);
|
|
login_tty(slave);
|
|
}
|
|
else
|
|
close(slave);
|
|
|
|
return child;
|
|
}
|
|
}
|