root/maint/gnulib/lib/openpty.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rpl_openpty
  2. openpty
  3. openpty

   1 /* Open a pseudo-terminal.
   2    Copyright (C) 2010-2021 Free Software Foundation, Inc.
   3 
   4    This file is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU Lesser General Public License as
   6    published by the Free Software Foundation; either version 2.1 of the
   7    License, or (at your option) any later version.
   8 
   9    This file is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU Lesser General Public License for more details.
  13 
  14    You should have received a copy of the GNU Lesser General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 #include <config.h>
  18 
  19 /* Specification.  */
  20 #include <pty.h>
  21 
  22 #if HAVE_OPENPTY
  23 
  24 /* Provide a wrapper with the prototype of glibc-2.8 and newer.  */
  25 # undef openpty
  26 int
  27 rpl_openpty (int *amaster, int *aslave, char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
  28              struct termios const *termp, struct winsize const *winp)
  29 {
  30   /* Cast away const, for implementations with weaker prototypes.  */
  31   return openpty (amaster, aslave, name, (struct termios *) termp,
  32                   (struct winsize *) winp);
  33 }
  34 
  35 #elif defined _WIN32 && !defined __CYGWIN__ /* mingw */
  36 
  37 # include <errno.h>
  38 
  39 int
  40 openpty (_GL_UNUSED int *amaster, _GL_UNUSED int *aslave,
     /* [previous][next][first][last][top][bottom][index][help] */
  41          _GL_UNUSED char *name,
  42          _GL_UNUSED struct termios const *termp,
  43          _GL_UNUSED struct winsize const *winp)
  44 {
  45   /* Mingw lacks pseudo-terminals altogether.  */
  46   errno = ENOSYS;
  47   return -1;
  48 }
  49 
  50 #else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10 */
  51 
  52 # include <fcntl.h>
  53 # include <stdlib.h>
  54 # include <string.h>
  55 # include <sys/ioctl.h>
  56 # include <termios.h>
  57 # include <unistd.h>
  58 # if defined __sun || defined __hpux /* Solaris, HP-UX */
  59 #  include <stropts.h>
  60 # endif
  61 
  62 int
  63 openpty (int *amaster, int *aslave, char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
  64          struct termios const *termp, struct winsize const *winp)
  65 {
  66   int master;
  67   char *slave_name;
  68   int slave;
  69 
  70 # if HAVE__GETPTY /* IRIX */
  71 
  72   slave_name = _getpty (&master, O_RDWR, 0622, 0);
  73   if (slave_name == NULL)
  74     return -1;
  75 
  76 # else /* AIX 5.1, HP-UX 11, Solaris 10, mingw */
  77 
  78   /* This call uses the 'posix_openpt' module.  */
  79   master = posix_openpt (O_RDWR | O_NOCTTY);
  80   if (master < 0)
  81     return -1;
  82 
  83 # endif
  84 
  85   /* This call does not require a dependency to the 'grantpt' module,
  86      because AIX, HP-UX, IRIX, Solaris all have the grantpt() function.  */
  87   if (grantpt (master))
  88     goto fail;
  89 
  90   /* This call does not require a dependency to the 'unlockpt' module,
  91      because AIX, HP-UX, IRIX, Solaris all have the unlockpt() function.  */
  92   if (unlockpt (master))
  93     goto fail;
  94 
  95 # if !HAVE__GETPTY /* !IRIX */
  96   slave_name = ptsname (master);
  97   if (slave_name == NULL)
  98     goto fail;
  99 # endif
 100 
 101   slave = open (slave_name, O_RDWR | O_NOCTTY);
 102   if (slave == -1)
 103     goto fail;
 104 
 105 # if defined __sun || defined __hpux /* Solaris, HP-UX */
 106   if (ioctl (slave, I_PUSH, "ptem") < 0
 107       || ioctl (slave, I_PUSH, "ldterm") < 0
 108 #  if defined __sun
 109       || ioctl (slave, I_PUSH, "ttcompat") < 0
 110 #  endif
 111      )
 112     {
 113       close (slave);
 114       goto fail;
 115     }
 116 # endif
 117 
 118   /* XXX Should we ignore errors here?  */
 119   if (termp)
 120     tcsetattr (slave, TCSAFLUSH, termp);
 121   if (winp)
 122     ioctl (slave, TIOCSWINSZ, winp);
 123 
 124   *amaster = master;
 125   *aslave = slave;
 126   if (name != NULL)
 127     strcpy (name, slave_name);
 128 
 129   return 0;
 130 
 131  fail:
 132   close (master);
 133   return -1;
 134 }
 135 
 136 #endif

/* [previous][next][first][last][top][bottom][index][help] */