diff options
author | Fred Drake <fdrake@acm.org> | 2000-06-28 16:40:38 +0000 |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-06-28 16:40:38 +0000 |
commit | eb75421c5bf6be195478218eaab2b680b8fd3683 (patch) | |
tree | 4d161243729fb19185c46f6c5e0c55949fe26a25 /Modules | |
parent | 3daedbfb878e55bb07caef9758ef25a7fc07c2a6 (diff) | |
download | cpython-eb75421c5bf6be195478218eaab2b680b8fd3683.tar.gz |
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')
-rw-r--r-- | Modules/posixmodule.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 67096c3157..809c24aa2b 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 */ |