X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fopenpty.c;h=c396a962cd6176ac095f39953d72f1355c5ec154;hb=9fc81090f6c5590bd1b0e0fa5087577a2ee43a3e;hp=e7eb46d46432a448ceff257a46937aa0abac4de8;hpb=8e7bc4d9d435c2d18be77134896aa823733816a0;p=gnulib.git diff --git a/lib/openpty.c b/lib/openpty.c index e7eb46d46..c396a962c 100644 --- a/lib/openpty.c +++ b/lib/openpty.c @@ -1,5 +1,5 @@ -/* Open a pseudo-terminal descriptor. - Copyright (C) 2010 Free Software Foundation, Inc. +/* Open a pseudo-terminal. + Copyright (C) 2010-2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,17 +19,118 @@ /* Specification. */ #include -#if HAVE_DECL_OPENPTY +#if HAVE_OPENPTY + +/* Provide a wrapper with the prototype of glibc-2.8 and newer. */ # undef openpty int -rpl_openpty (int *amaster, int *aslave, char *name, struct termios const *termp, - struct winsize const *winp) +rpl_openpty (int *amaster, int *aslave, char *name, + struct termios const *termp, struct winsize const *winp) { /* Cast away const, for implementations with weaker prototypes. */ return openpty (amaster, aslave, name, (struct termios *) termp, (struct winsize *) winp); } -#else -# error openpty has not been ported to your system; \ - report this to bug-gnulib@gnu.org for help + +#elif (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ /* mingw */ + +# include + +int +openpty (int *amaster _GL_UNUSED, int *aslave _GL_UNUSED, + char *name _GL_UNUSED, + struct termios const *termp _GL_UNUSED, + struct winsize const *winp _GL_UNUSED) +{ + /* Mingw lacks pseudo-terminals altogether. */ + errno = ENOSYS; + return -1; +} + +#else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10 */ + +# include +# include +# include +# include +# include +# include +# if defined __sun || defined __hpux /* Solaris, HP-UX */ +# include +# endif + +int +openpty (int *amaster, int *aslave, char *name, + struct termios const *termp, struct winsize const *winp) +{ + int master; + char *slave_name; + int slave; + +# if HAVE__GETPTY /* IRIX */ + + slave_name = _getpty (&master, O_RDWR, 0622, 0); + if (slave_name == NULL) + return -1; + +# else /* AIX 5.1, HP-UX 11, Solaris 10, mingw */ + + /* This call uses the 'posix_openpt' module. */ + master = posix_openpt (O_RDWR | O_NOCTTY); + if (master < 0) + return -1; + +# endif + + /* This call does not require a dependency to the 'grantpt' module, + because AIX, HP-UX, IRIX, Solaris all have the grantpt() function. */ + if (grantpt (master)) + goto fail; + + /* This call does not require a dependency to the 'unlockpt' module, + because AIX, HP-UX, IRIX, Solaris all have the unlockpt() function. */ + if (unlockpt (master)) + goto fail; + +# if !HAVE__GETPTY /* !IRIX */ + slave_name = ptsname (master); + if (slave_name == NULL) + goto fail; +# endif + + slave = open (slave_name, O_RDWR | O_NOCTTY); + if (slave == -1) + goto fail; + +# if defined __sun || defined __hpux /* Solaris, HP-UX */ + if (ioctl (slave, I_PUSH, "ptem") < 0 + || ioctl (slave, I_PUSH, "ldterm") < 0 +# if defined __sun + || ioctl (slave, I_PUSH, "ttcompat") < 0 +# endif + ) + { + close (slave); + goto fail; + } +# endif + + /* XXX Should we ignore errors here? */ + if (termp) + tcsetattr (slave, TCSAFLUSH, termp); + if (winp) + ioctl (slave, TIOCSWINSZ, winp); + + *amaster = master; + *aslave = slave; + if (name != NULL) + strcpy (name, slave_name); + + return 0; + + fail: + close (master); + return -1; +} + #endif