From 412da0a4799489f9e41ba2b3c24d4d3f56c70963 Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Mon, 15 Jan 2007 05:04:59 -0600 Subject: [PATCH] _vte_pty_open_pt() --- moo/mooterm/pty.c | 79 ++++++++++++++++++++++++++++++++++++++++++++--- moo/mooterm/pty.h | 3 ++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/moo/mooterm/pty.c b/moo/mooterm/pty.c index 9adbd03d..a14e4b08 100644 --- a/moo/mooterm/pty.c +++ b/moo/mooterm/pty.c @@ -89,7 +89,8 @@ #include #endif #include -#include "mooterm/pty.h" +#include "pty.h" +#include #ifdef MSG_NOSIGNAL #define PTY_RECVMSG_FLAGS MSG_NOSIGNAL @@ -649,7 +650,7 @@ _vte_pty_ptsname(int master) char *buf = NULL; int i; do { - buf = (char*)calloc (sizeof(char), len); + buf = g_new0 (char, len); i = ptsname_r(master, buf, len - 1); switch (i) { case 0: @@ -657,7 +658,7 @@ _vte_pty_ptsname(int master) return buf; break; default: - free(buf); + g_free(buf); buf = NULL; break; } @@ -666,7 +667,7 @@ _vte_pty_ptsname(int master) #elif defined(HAVE_PTSNAME) char *p; if ((p = ptsname(master)) != NULL) { - return strdup (p); + return g_strdup (p); } #elif defined(TIOCGPTN) int pty = 0; @@ -735,6 +736,35 @@ _vte_pty_unlockpt(int fd) #ifndef MOO_OS_BSD +static int +_vte_pty_open_pt_unix98 (char **name) +{ + int fd; + char *buf; + + /* Attempt to open the master. */ + fd = _vte_pty_getpt(); + *name = NULL; + buf = NULL; + + if (fd != -1) { + /* Read the slave number and unlock it. */ + if (((buf = _vte_pty_ptsname(fd)) == NULL) || + (_vte_pty_grantpt(fd) != 0) || + (_vte_pty_unlockpt(fd) != 0)) { + close(fd); + fd = -1; + } + } + + if (fd != -1 && name != NULL) + *name = buf; + else + g_free (buf); + + return fd; +} + static int _vte_pty_open_unix98(pid_t *child, char **env_add, const char *command, char **argv, @@ -761,7 +791,7 @@ _vte_pty_open_unix98(pid_t *child, char **env_add, close(fd); fd = -1; } - free(buf); + g_free(buf); } } return fd; @@ -769,6 +799,27 @@ _vte_pty_open_unix98(pid_t *child, char **env_add, #else /* MOO_OS_BSD */ +static int +_vte_pty_open_pt_bsd (char **name) +{ + int master, slave; + char slave_name[80]; + + if (openpty (&master, &slave, slave_name, NULL, NULL) != -1) { + close (slave); + /* Read the slave number and unlock it. */ + if ((_vte_pty_grantpt(master) != 0) || + (_vte_pty_unlockpt(master) != 0)) { + close (master); + master = -1; + } else { + if (name != NULL) + *name = g_strdup (slave_name); + } + } + return master; +} + static int _vte_pty_open_bsd (pid_t *child, char **env_add, const char *command, char **argv, @@ -869,6 +920,24 @@ _vte_pty_open_bsd (pid_t *child, char **env_add, #endif +/** + * _vte_pty_open_pt: + * @name: location to store filename or NULL, must be freed with g_free() + * + * Creates new pseudo-terminal. + * + * Returns: file descriptor of new pseudo-terminal or -1 on error + */ +int +_vte_pty_open_pt(char **name) +{ +#ifndef MOO_OS_BSD + return _vte_pty_open_pt_unix98(name); +#else /* MOO_OS_BSD */ + return _vte_pty_open_pt_bsd(name); +#endif /* MOO_OS_BSD */ +} + /** * _vte_pty_open: * @child: location to store the new process's ID diff --git a/moo/mooterm/pty.h b/moo/mooterm/pty.h index 028816e9..71dcc162 100644 --- a/moo/mooterm/pty.h +++ b/moo/mooterm/pty.h @@ -35,6 +35,7 @@ extern "C" { #define _vte_pty_close _moo_vte_pty_close #define _vte_pty_n_read _moo_vte_pty_n_read #define _vte_pty_n_write _moo_vte_pty_n_write +#define _vte_pty_open_pt _moo_vte_pty_open_pt /* Start up the given binary (exact path, not interpreted at all) in a @@ -46,6 +47,8 @@ int _vte_pty_open(pid_t *child, char **env_add, int columns, int rows, int lastlog, int utmp, int wtmp); +int _vte_pty_open_pt(char **name); + /* Set or read the size of a terminal. Returns 0 on success, -1 on failure, * with errno set to defined return codes from ioctl(). */ int _vte_pty_get_size(int master, int *columns, int *rows);