vt: respect relative position mode when clamping cursor
[dvtm.git] / forkpty-aix.c
blob430f14b77805a5521af3b50d0dc53a173263e63c
1 /*
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>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stropts.h>
24 #include <unistd.h>
25 #include <paths.h>
27 /* Fatal errors. */
28 #ifdef NDEBUG
29 #define debug(format, args...)
30 #else
31 #define debug eprint
32 #endif
33 #define fatal(msg) debug("%s: %s", __func__, msg);
35 pid_t
36 forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
38 int slave, fd;
39 char *path;
40 pid_t pid;
42 if ((*master = open("/dev/ptc", O_RDWR|O_NOCTTY)) == -1)
43 return (-1);
45 if ((path = ttyname(*master)) == NULL)
46 goto out;
47 if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
48 goto out;
50 switch (pid = fork()) {
51 case -1:
52 goto out;
53 case 0:
54 close(*master);
56 fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
57 if (fd >= 0) {
58 ioctl(fd, TIOCNOTTY, NULL);
59 close(fd);
62 if (setsid() < 0)
63 fatal("setsid");
65 fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
66 if (fd >= 0)
67 fatal("open succeeded (failed to disconnect)");
69 fd = open(path, O_RDWR);
70 if (fd < 0)
71 fatal("open failed");
72 close(fd);
74 fd = open("/dev/tty", O_WRONLY);
75 if (fd < 0)
76 fatal("open failed");
77 close(fd);
79 if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1)
80 fatal("tcsetattr failed");
81 if (ioctl(slave, TIOCSWINSZ, ws) == -1)
82 fatal("ioctl failed");
84 dup2(slave, 0);
85 dup2(slave, 1);
86 dup2(slave, 2);
87 if (slave > 2)
88 close(slave);
89 return (0);
92 close(slave);
93 return (pid);
95 out:
96 if (*master != -1)
97 close(*master);
98 if (slave != -1)
99 close(slave);
100 return (-1);