summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2000-06-28 16:40:38 (GMT)
committerFred Drake <fdrake@acm.org>2000-06-28 16:40:38 (GMT)
commit8cef4cf737878bd8eec648426eebe9b9019b18be (patch)
tree2fe93f0a2d3c9e22ad836a45bc7a526416c11c61 /Modules/posixmodule.c
parent57823862683db7a463a134dbdf259d2e027619f6 (diff)
downloadcpython-8cef4cf737878bd8eec648426eebe9b9019b18be.zip
cpython-8cef4cf737878bd8eec648426eebe9b9019b18be.tar.gz
cpython-8cef4cf737878bd8eec648426eebe9b9019b18be.tar.bz2
Thomas Wouters <thomas@xs4all.net>:
This patch adds the openpty() and forkpty() library calls to posixmodule.c, when they are available on the target system. (glibc-2.1-based Linux systems, FreeBSD and BSDI at least, probably the other BSD-based systems as well.) Lib/pty.py is also rewritten to use openpty when available, but falls back to the old SGI method or the "manual" BSD open-a-pty code. Openpty() is necessary to use the Unix98 ptys under Linux 2.2, or when using non-standard tty names under (at least) BSDI, which is why I needed it, myself ;-) forkpty() is included for symmetry.
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 67096c3..809c24a 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1731,6 +1731,64 @@ posix_fork(self, args)
}
#endif
+#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
+#ifdef HAVE_PTY_H
+#include <pty.h>
+#else
+#ifdef HAVE_LIBUTIL_H
+#include <libutil.h>
+#else
+/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
+ functions, eventhough they are included in libutil. */
+#include <termios.h>
+extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
+extern int forkpty(int *, char *, struct termios *, struct winsize *);
+#endif /* HAVE_LIBUTIL_H */
+#endif /* HAVE_PTY_H */
+#endif /* defined(HAVE_OPENPTY) or defined(HAVE_FORKPTY) */
+
+#ifdef HAVE_OPENPTY
+static char posix_openpty__doc__[] =
+"openpty() -> (master_fd, slave_fd)\n\
+Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
+
+static PyObject *
+posix_openpty(self, args)
+ PyObject *self;
+ PyObject *args;
+{
+ int master_fd, slave_fd;
+ if (!PyArg_ParseTuple(args, ":openpty"))
+ return NULL;
+ if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
+ return posix_error();
+ return Py_BuildValue("(ii)", master_fd, slave_fd);
+}
+#endif
+
+#ifdef HAVE_FORKPTY
+static char posix_forkpty__doc__[] =
+"forkpty() -> (pid, master_fd)\n\
+Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
+Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
+To both, return fd of newly opened pseudo-terminal.\n";
+
+static PyObject *
+posix_forkpty(self, args)
+ PyObject *self;
+ PyObject *args;
+{
+ int master_fd, pid;
+
+ if (!PyArg_ParseTuple(args, ":forkpty"))
+ return NULL;
+ pid = forkpty(&master_fd, NULL, NULL, NULL);
+ if (pid == -1)
+ return posix_error();
+ PyOS_AfterFork();
+ return Py_BuildValue("(ii)", pid, master_fd);
+}
+#endif
#ifdef HAVE_GETEGID
static char posix_getegid__doc__[] =
@@ -4514,6 +4572,12 @@ static PyMethodDef posix_methods[] = {
#ifdef HAVE_FORK
{"fork", posix_fork, METH_VARARGS, posix_fork__doc__},
#endif /* HAVE_FORK */
+#ifdef HAVE_OPENPTY
+ {"openpty", posix_openpty, METH_VARARGS, posix_openpty__doc__},
+#endif /* HAVE_OPENPTY */
+#ifdef HAVE_FORKPTY
+ {"forkpty", posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
+#endif /* HAVE_FORKPTY */
#ifdef HAVE_GETEGID
{"getegid", posix_getegid, METH_VARARGS, posix_getegid__doc__},
#endif /* HAVE_GETEGID */