implement (nonstandard) forkpty
authorRich Felker <dalias@aerifal.cx>
Thu, 21 Apr 2011 00:55:13 +0000 (20:55 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 21 Apr 2011 00:55:13 +0000 (20:55 -0400)
include/pty.h
src/misc/forkpty.c [new file with mode: 0644]

index 9444e5ecb2915082397f8785518efbf3e9d07932..46a99f47e363831599374dfa9de3a7438262859e 100644 (file)
@@ -9,6 +9,7 @@ extern "C" {
 #include <sys/ioctl.h>
 
 int openpty(int *, int *, char *, const struct termios *, const struct winsize *);
+int forkpty(int *, char *, const struct termios *, const struct winsize *);
 
 #ifdef __cplusplus
 extern }
diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c
new file mode 100644 (file)
index 0000000..2d1b0ae
--- /dev/null
@@ -0,0 +1,22 @@
+#include <pty.h>
+#include <unistd.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);
+               dup2(s, 0);
+               dup2(s, 1);
+               dup2(s, 2);
+               close(s);
+               return 0;
+       }
+       close(s);
+       if (pid < 0) close(*m);
+       return pid;
+}