powerpc-sf longjmp clobbering of val argument
[musl.git] / src / misc / openpty.c
blobc10740606111778c16b1d6c3dd3adb5a791240b6
1 #include <stdlib.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <pty.h>
5 #include <stdio.h>
6 #include <pthread.h>
8 /* Nonstandard, but vastly superior to the standard functions */
10 int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws)
12 int m, s, n=0, cs;
13 char buf[20];
15 m = open("/dev/ptmx", O_RDWR|O_NOCTTY);
16 if (m < 0) return -1;
18 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
20 if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n))
21 goto fail;
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;
28 if (tio) tcsetattr(s, TCSANOW, tio);
29 if (ws) ioctl(s, TIOCSWINSZ, ws);
31 *pm = m;
32 *ps = s;
34 pthread_setcancelstate(cs, 0);
35 return 0;
36 fail:
37 close(m);
38 pthread_setcancelstate(cs, 0);
39 return -1;