about summary refs log tree commit diff
path: root/login/openpty.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>1998-09-18 17:59:03 +0000
committerUlrich Drepper <drepper@redhat.com>1998-09-18 17:59:03 +0000
commit00bc5db059212a20afb42da40b38d7f145a46dfd (patch)
tree090b419ffc6087f15fe9cba759f3055cb5d315ca /login/openpty.c
parent88263bc9ac99b5c99659173239a5a6e1bef25df6 (diff)
downloadglibc-00bc5db059212a20afb42da40b38d7f145a46dfd.tar.gz
glibc-00bc5db059212a20afb42da40b38d7f145a46dfd.tar.xz
glibc-00bc5db059212a20afb42da40b38d7f145a46dfd.zip
Update.
1998-09-18 17:41  Ulrich Drepper  <drepper@cygnus.com>

	* libio/fileops.c (_IO_new_file_underflow): Before allocating
	buffer make sure the pushback buffer is destroyed.
	(_IO_new_file_seekoff): Likewise.
	If mode==0 quit early with the result.
	Clear OEF flag after successful fseek.
	* libio/libio.h (_IO_FILE_complete): Add _IO_save_ptr.
	* libio/ftello.c (ftello): Add offset from original buffer if
	stream has pushed back characters.
	* libio/ftello64.c (ftello64): Likewise.
	* libio/iofgetpos.c (_IO_fgetpos): Likewise.
	* libio/iofgetpos64.c (_IO_fgetpos64): Likewise.
	* libio/ioftell.c (_IO_ftell): Likewise.
	* libio/genops.c (_IO_switch_to_main_get_area): Swap _IO_read_ptr
	and _IO_save_ptr.
	(_IO_switch_to_backup_area): Save _IO_read_ptr in _IO_save_ptr.
	(_IO_default_pbackfail): Only stored push back character in original
	buffer if it is the same as the one in the file at this position.
	* libio/iofclose.c: Free backup buffer if one is available.
	* libio/ioseekoff.c (_IO_seekoff): Only remove pushback buffer if
	mode!=0.

	* strdlib/strtol.c (strtol): Handle 0x... string for base!=0 correctly.

	* time/strftime.c [_LIBC] (ampm): Use tp->tm_hour not hour12.

1998-09-18  Mark Kettenis  <kettenis@phys.uva.nl>

	* login/programs/pt_chown.c (more_help): Correct message that
	describes the purpose of the program.

	* login/openpty.c: Do not include pty-private.h.
	(pts_name): New function.  Return name of slave pseudo terminal in
	an allocated buffer if necessary.
	(openpty): Use pts_name to get name of the slave end of the pseudo
	terminal pair.

	* sysdeps/unix/grantpt.c (grantpt): Free buffer allocated by
	pts_name before return.

1998-09-18 11:15  Ulrich Drepper  <drepper@cygnus.com>

	* math/math.h: Define __NO_MATH_INLINES if __STRICT_ANSI__.
Diffstat (limited to 'login/openpty.c')
-rw-r--r--login/openpty.c129
1 files changed, 97 insertions, 32 deletions
diff --git a/login/openpty.c b/login/openpty.c
index 396ca09ca6..630061d8da 100644
--- a/login/openpty.c
+++ b/login/openpty.c
@@ -17,7 +17,9 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
+#include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <pty.h>
 #include <stdlib.h>
 #include <string.h>
@@ -25,49 +27,112 @@
 #include <unistd.h>
 #include <sys/types.h>
 
-#include "pty-private.h"
 
+/* Return the result of ptsname_r in the buffer pointed to by PTS,
+   which should be of length BUF_LEN.  If it is too long to fit in
+   this buffer, a sufficiently long buffer is allocated using malloc,
+   and returned in PTS.  0 is returned upon success, -1 otherwise.  */
+static int
+pts_name (int fd, char **pts, size_t buf_len)
+{
+  int rv;
+  char *buf = *pts;
+
+  for (;;)
+    {
+      char *new_buf;
+
+      if (buf_len)
+	{
+	  rv = ptsname_r (fd, buf, buf_len);
+
+	  if (rv != 0 || memchr (buf, '\0', buf_len))
+	    /* We either got an error, or we succeeded and the
+	       returned name fit in the buffer.  */
+	    break;
+
+	  /* Try again with a longer buffer.  */
+	  buf_len += buf_len;	/* Double it */
+	}
+      else
+	/* No initial buffer; start out by mallocing one.  */
+	buf_len = 128;		/* First time guess.  */
+
+      if (buf != *pts)
+	/* We've already malloced another buffer at least once.  */
+	new_buf = realloc (buf, buf_len);
+      else
+	new_buf = malloc (buf_len);
+      if (! new_buf)
+	{
+	  rv = -1;
+	  __set_errno (ENOMEM);
+	  break;
+	}
+      buf = new_buf;
+    }
+
+  if (rv == 0)
+    *pts = buf;		/* Return buffer to the user.  */
+  else if (buf != *pts)
+    free (buf);		/* Free what we malloced when returning an error.  */
+
+  return rv;
+}
+
+/* Create pseudo tty master slave pair and set terminal attributes
+   according to TERMP and WINP.  Return handles for both ends in
+   AMASTER and ASLAVE, and return the name of the slave end in NAME.  */
 int
-openpty (pmast, pslave, pname, tio, wins)
-     int *pmast;
-     int *pslave;
-     char *pname;
-     struct termios *tio;
-     struct winsize *wins;
+openpty (int *amaster, int *aslave, char *name, struct termios *termp,
+	 struct winsize *winp)
 {
-  int pfd, tfd;
-  char name[PTYNAMELEN];
+#ifdef PATH_MAX
+  char _buf[PATH_MAX];
+#else
+  char _buf[512];
+#endif
+  char *buf = _buf;
+  int master, slave;
 
-  pfd = getpt ();
-  if (pfd == -1)
+  master = getpt ();
+  if (master == -1)
     return -1;
 
-  if (grantpt (pfd))
-    goto bail;
+  if (grantpt (master))
+    goto fail;
+
+  if (unlockpt (master))
+    goto fail;
 
-  if (unlockpt (pfd))
-    goto bail;
+  if (pts_name (master, &buf, sizeof (_buf)))
+    goto fail;
 
-  if (ptsname_r (pfd, name, PTYNAMELEN) != 0)
-    goto bail;
+  slave = open (buf, O_RDWR);
+  if (slave == -1)
+    {
+      if (buf != _buf)
+	free (buf);
 
-  tfd = open (name, O_RDWR);
-  if (tfd == -1)
-    goto bail;
+      goto fail;
+    }
 
   /* XXX Should we ignore errors here?  */
-  if(tio)
-    tcsetattr (tfd, TCSAFLUSH, tio);
-  if (wins)
-    ioctl (tfd, TIOCSWINSZ, wins);
-
-  *pmast = pfd;
-  *pslave = tfd;
-  if (pname != NULL)
-    strcpy (pname, name);
-  return 0;
+  if(termp)
+    tcsetattr (slave, TCSAFLUSH, termp);
+  if (winp)
+    ioctl (slave, TIOCSWINSZ, winp);
+
+  *amaster = master;
+  *aslave = slave;
+  if (name != NULL)
+    strcpy (name, buf);
 
-bail:
-  close (pfd);
+  if (buf != _buf)
+    free (buf);
+  return 0;
+  
+ fail:
+  close (master);
   return -1;
 }