| 1 |
#include <stdlib.h> |
| 2 |
#include <fcntl.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <pty.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <pthread.h> |
| 7 |
|
| 8 |
/* Nonstandard, but vastly superior to the standard functions */ |
| 9 |
|
| 10 |
int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws) |
| 11 |
{ |
| 12 |
int m, s, n=0, cs; |
| 13 |
char buf[20]; |
| 14 |
|
| 15 |
m = open("/dev/ptmx", O_RDWR|O_NOCTTY); |
| 16 |
if (m < 0) return -1; |
| 17 |
|
| 18 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
| 19 |
|
| 20 |
if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n)) |
| 21 |
goto fail; |
| 22 |
|
| 23 |
if (!name) name = buf; |
| 24 |
snprintf(name, sizeof buf, "/dev/pts/%d", n); |
| 25 |
if ((s = open(name, O_RDWR|O_NOCTTY)) < 0) |
| 26 |
goto fail; |
| 27 |
|
| 28 |
if (tio) tcsetattr(s, TCSANOW, tio); |
| 29 |
if (ws) ioctl(s, TIOCSWINSZ, ws); |
| 30 |
|
| 31 |
*pm = m; |
| 32 |
*ps = s; |
| 33 |
|
| 34 |
pthread_setcancelstate(cs, 0); |
| 35 |
return 0; |
| 36 |
fail: |
| 37 |
close(m); |
| 38 |
pthread_setcancelstate(cs, 0); |
| 39 |
return -1; |
| 40 |
} |