0bbf2de2746bba58d38b258428700320415706f8
[musl] / src / misc / forkpty.c
1 #include <pty.h>
2 #include <unistd.h>
3 #include <sys/ioctl.h>
4
5 int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
6 {
7         int s;
8         pid_t pid;
9
10         if (openpty(m, &s, name, tio, ws) < 0) return -1;
11         pid = fork();
12         if (!pid) {
13                 close(*m);
14                 setsid();
15                 ioctl(s, TIOCSCTTY, (char *)0);
16                 dup2(s, 0);
17                 dup2(s, 1);
18                 dup2(s, 2);
19                 if (s>2) close(s);
20                 return 0;
21         }
22         close(s);
23         if (pid < 0) close(*m);
24         return pid;
25 }