summaryrefslogtreecommitdiff
path: root/src/misc/forkpty.c
blob: 0bbf2de2746bba58d38b258428700320415706f8 (plain) (blame)
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
#include <pty.h>
#include <unistd.h>
#include <sys/ioctl.h>

int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
{
	int s;
	pid_t pid;

	if (openpty(m, &s, name, tio, ws) < 0) return -1;
	pid = fork();
	if (!pid) {
		close(*m);
		setsid();
		ioctl(s, TIOCSCTTY, (char *)0);
		dup2(s, 0);
		dup2(s, 1);
		dup2(s, 2);
		if (s>2) close(s);
		return 0;
	}
	close(s);
	if (pid < 0) close(*m);
	return pid;
}