-
Notifications
You must be signed in to change notification settings - Fork 23
/
forkpty.c
39 lines (35 loc) · 1.15 KB
/
forkpty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Process server for soft ioc
// Ralph Lange <[email protected]> 03/25/2010
* GNU Public License (GPLv3) applies - see www.gnu.org */
#include <unistd.h>
#include <stropts.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int forkpty(int* fdm, char* name, void* x1, void* x2)
{
/* From the Solaris manpage for pts(7D) */
int ptm, pts;
pid_t pid;
char* c;
ptm = open("/dev/ptmx", O_RDWR); /* open master */
grantpt(ptm); /* change permission of slave */
unlockpt(ptm); /* unlock slave */
c = ptsname(ptm); /* get name of slave */
if (c) strcpy(name, c);
pts = open(name, O_RDWR); /* open slave */
ioctl(pts, I_PUSH, "ptem"); /* push ptem */
ioctl(pts, I_PUSH, "ldterm"); /* push ldterm */
/* From forums.sun.com */
pid = fork();
if (!pid) { /* child */
close(ptm);
dup2(pts, 0); /* connect to slave fd */
dup2(pts, 1);
dup2(pts, 2);
} else {
*fdm = ptm; /* return fd to parent */
}
close(pts);
return pid;
}