48 lines
1.1 KiB
C++
48 lines
1.1 KiB
C++
|
#include <fcntl.h>
|
||
|
#include <pty.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <termios.h>
|
||
|
#include <unistd.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;
|
||
|
}
|
||
|
}
|