2 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
3 * Copyright (c) 2012 Ross Palmer Mohn <rpmohn@waxandwane.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
14 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
15 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
29 #define debug(format, args...)
33 #define fatal(msg) debug("%s: %s", __func__, msg);
36 forkpty(int *master
, char *name
, struct termios
*tio
, struct winsize
*ws
)
43 if ((*master
= open("/dev/ptc", O_RDWR
|O_NOCTTY
)) == -1)
46 if ((path
= ttyname(*master
)) == NULL
)
48 if ((slave
= open(path
, O_RDWR
|O_NOCTTY
)) == -1)
51 switch (pid
= fork()) {
57 fd
= open(_PATH_TTY
, O_RDWR
|O_NOCTTY
);
59 ioctl(fd
, TIOCNOTTY
, NULL
);
66 fd
= open(_PATH_TTY
, O_RDWR
|O_NOCTTY
);
68 fatal("open succeeded (failed to disconnect)");
70 fd
= open(path
, O_RDWR
);
75 fd
= open("/dev/tty", O_WRONLY
);
80 if (tcgetattr(slave
, &tio2
) != 0)
81 fatal("tcgetattr failed");
83 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
84 tio2
.c_cc
[VERASE
] = '\177';
85 if (tcsetattr(slave
, TCSAFLUSH
, &tio2
) == -1)
86 fatal("tcsetattr failed");
87 if (ioctl(slave
, TIOCSWINSZ
, ws
) == -1)
88 fatal("ioctl failed");