re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / gnu / posix_openpt.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Open the master side of a pseudo-terminal.
4    Copyright (C) 2010-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdlib.h>
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #if defined __OpenBSD__
27 # include <sys/ioctl.h>
28 # include <sys/tty.h>
29 #endif
30
31 /* posix_openpt() is already provided on
32      glibc >= 2.2.1 (but is a stub on GNU/Hurd),
33      Mac OS X >= 10.4,
34      FreeBSD >= 5.1 (lived in src/lib/libc/stdlib/grantpt.c before 8.0),
35      NetBSD >= 3.0,
36      AIX >= 5.2, HP-UX >= 11.31, Solaris >= 10, Cygwin >= 1.7.
37    Thus, this replacement function is compiled on
38      Mac OS X 10.3, OpenBSD 4.9, Minix 3.1.8,
39      AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris 9,
40      Cygwin 1.5.x, mingw, MSVC 9, Interix 3.5, BeOS.
41    Among these:
42      - AIX has /dev/ptc.
43      - HP-UX 10..11, IRIX 6.5, OSF/1 5.1, Solaris 2.6..9, Cygwin 1.5
44        have /dev/ptmx.
45      - HP-UX 10..11 also has /dev/ptym/clone, but this should not be needed.
46      - OpenBSD 4.9 has /dev/ptm and the PTMGET ioctl.
47      - Minix 3.1.8 have a list of pseudo-terminal devices in /dev.
48      - On native Windows, there are no ttys at all.  */
49
50 int
51 posix_openpt (int flags)
52 {
53   int master;
54
55 #ifdef _AIX /* AIX */
56
57   master = open ("/dev/ptc", flags);
58
59 #elif (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ /* mingw */
60
61   /* Mingw lacks pseudo-terminals altogether.  */
62   master = -1;
63   errno = ENOSYS;
64
65 #elif defined __OpenBSD__
66
67   /* On OpenBSD, master and slave of a pseudo-terminal are allocated together,
68      by opening /dev/ptm and applying the PTMGET ioctl to it.  */
69   int fd;
70   struct ptmget data;
71
72   fd = open (PATH_PTMDEV, O_RDWR);
73   if (fd >= 0)
74     {
75       if (ioctl (fd, PTMGET, &data) >= 0)
76         {
77           master = data.cfd;
78           close (data.sfd);
79           close (fd);
80         }
81       else
82         {
83           int saved_errno = errno;
84           close (fd);
85           errno = saved_errno;
86           master = -1;
87         }
88     }
89   else
90     master = -1;
91
92 #else /* Mac OS X, Minix, HP-UX, IRIX, OSF/1, Solaris 9, Cygwin 1.5 */
93
94   /* Most systems that lack posix_openpt() have /dev/ptmx.  */
95   master = open ("/dev/ptmx", flags);
96
97   /* If all this does not work, we could try to open, one by one:
98      - On Mac OS X: /dev/pty[p-w][0-9a-f]
99      - On *BSD:     /dev/pty[p-sP-S][0-9a-v]
100      - On Minix:    /dev/pty[p-q][0-9a-f]
101      - On AIX:      /dev/ptyp[0-9a-f]
102      - On HP-UX:    /dev/pty[p-r][0-9a-f]
103      - On OSF/1:    /dev/pty[p-q][0-9a-f]
104      - On Solaris:  /dev/pty[p-r][0-9a-f]
105    */
106
107 #endif
108
109   return master;
110 }