openpty, posix_openpt: Remove code duplication.
authorBruno Haible <bruno@clisp.org>
Fri, 21 Oct 2011 00:45:21 +0000 (02:45 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 21 Oct 2011 00:45:21 +0000 (02:45 +0200)
* lib/posix_openpt.c: Add comments about platforms, from lib/openpty.c.
* lib/openpty.c: Include <stdlib.h>.
(openpty): Use posix_openpt on all platforms except IRIX.
* modules/openpty (Depends-on): Add posix_openpt. Add conditions.

ChangeLog
lib/openpty.c
lib/posix_openpt.c
modules/openpty

index c670c9c..f44038b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2011-10-20  Bruno Haible  <bruno@clisp.org>
 
+       openpty, posix_openpt: Remove code duplication.
+       * lib/posix_openpt.c: Add comments about platforms, from lib/openpty.c.
+       * lib/openpty.c: Include <stdlib.h>.
+       (openpty): Use posix_openpt on all platforms except IRIX.
+       * modules/openpty (Depends-on): Add posix_openpt. Add conditions.
+
+2011-10-20  Bruno Haible  <bruno@clisp.org>
+
        unlockpt: Detect invalid argument.
        * lib/unlockpt.c: Include <fcntl.h>.
        (unlockpt): Check whether fd is valid, using fcntl().
index c6e75c6..c398db5 100644 (file)
@@ -35,6 +35,7 @@ rpl_openpty (int *amaster, int *aslave, char *name,
 #else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10, mingw */
 
 # include <fcntl.h>
+# include <stdlib.h>
 # include <string.h>
 # include <sys/ioctl.h>
 # include <termios.h>
@@ -59,45 +60,11 @@ openpty (int *amaster, int *aslave, char *name,
 
 # else /* AIX 5.1, HP-UX 11, Solaris 10, mingw */
 
-#  if HAVE_POSIX_OPENPT /* Solaris 10 */
-
+  /* This call uses the 'posix_openpt' module.  */
   master = posix_openpt (O_RDWR | O_NOCTTY);
   if (master < 0)
     return -1;
 
-#  else /* AIX 5.1, HP-UX 11, Solaris 9, mingw */
-
-#   ifdef _AIX /* AIX */
-
-  master = open ("/dev/ptc", O_RDWR | O_NOCTTY);
-  if (master < 0)
-    return -1;
-
-#   else /* HP-UX 11, Solaris 9, mingw */
-
-  /* HP-UX, Solaris have /dev/ptmx.
-     HP-UX also has /dev/ptym/clone, but this should not be needed.
-     Linux also has /dev/ptmx, but Linux already has openpty().
-     MacOS X also has /dev/ptmx, but MacOS X already has openpty().
-     OSF/1 also has /dev/ptmx and /dev/ptmx_bsd, but OSF/1 already has
-     openpty().  */
-  master = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
-  if (master < 0)
-    return -1;
-
-#   endif
-
-#  endif
-
-  /* If all this does not work, we could try to open, one by one:
-     - On MacOS X: /dev/pty[p-w][0-9a-f]
-     - On *BSD:    /dev/pty[p-sP-S][0-9a-v]
-     - On Minix:   /dev/pty[p-q][0-9a-f]
-     - On AIX:     /dev/ptyp[0-9a-f]
-     - On HP-UX:   /dev/pty[p-r][0-9a-f]
-     - On OSF/1:   /dev/pty[p-q][0-9a-f]
-     - On Solaris: /dev/pty[p-r][0-9a-f]
-   */
 # endif
 
   /* This call does not require a dependency to the 'grantpt' module,
index d2e585a..137db71 100644 (file)
 # include <sys/tty.h>
 #endif
 
+/* posix_openpt() is already provided on
+     glibc >= 2.2.1 (but is a stub on GNU/Hurd),
+     MacOS X >= 10.4,
+     FreeBSD >= 5.1 (lived in src/lib/libc/stdlib/grantpt.c before 8.0),
+     NetBSD >= 3.0,
+     AIX >= 5.2, HP-UX >= 11.31, Solaris >= 10, Cygwin >= 1.7.
+   Thus, this replacement function is compiled on
+     MacOS X 10.3, OpenBSD 4.9, Minix 3.1.8,
+     AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris 9,
+     Cygwin 1.5.x, mingw, MSVC 9, Interix 3.5, BeOS.
+   Among these:
+     - AIX has /dev/ptc.
+     - HP-UX 10..11, IRIX 6.5, OSF/1 5.1, Solaris 2.6..9, Cygwin 1.5
+       have /dev/ptmx.
+     - HP-UX 10..11 also has /dev/ptym/clone, but this should not be needed.
+     - OpenBSD 4.9 has /dev/ptm and the PTMGET ioctl.
+     - Minix 3.1.8 have a list of pseudo-terminal devices in /dev.
+     - On native Windows, there are no ttys at all.  */
+
 int
 posix_openpt (int flags)
 {
@@ -73,6 +92,16 @@ posix_openpt (int flags)
   /* Most systems that lack posix_openpt() have /dev/ptmx.  */
   master = open ("/dev/ptmx", flags);
 
+  /* If all this does not work, we could try to open, one by one:
+     - On MacOS X: /dev/pty[p-w][0-9a-f]
+     - On *BSD:    /dev/pty[p-sP-S][0-9a-v]
+     - On Minix:   /dev/pty[p-q][0-9a-f]
+     - On AIX:     /dev/ptyp[0-9a-f]
+     - On HP-UX:   /dev/pty[p-r][0-9a-f]
+     - On OSF/1:   /dev/pty[p-q][0-9a-f]
+     - On Solaris: /dev/pty[p-r][0-9a-f]
+   */
+
 #endif
 
   return master;
index bf51b7f..8d174da 100644 (file)
@@ -8,8 +8,9 @@ m4/pty.m4
 Depends-on:
 pty
 extensions
-fcntl-h
-ioctl
+fcntl-h         [test $HAVE_OPENPTY = 0 || test $REPLACE_OPENPTY = 1]
+posix_openpt    [test $HAVE_OPENPTY = 0 || test $REPLACE_OPENPTY = 1]
+ioctl           [test $HAVE_OPENPTY = 0 || test $REPLACE_OPENPTY = 1]
 
 configure.ac:
 gl_FUNC_OPENPTY