annotate lib/posix_openpt.c @ 16229:daac1ca4c436

inttypes: Modernize. * lib/inttypes.in.h (strtoimax, strtoumax): Use the C++ safe idioms. * modules/inttypes-incomplete (Depends-on): Add snippet/c++defs. (Makefile.am): Update inttypes.h rule.
author Bruno Haible <bruno@clisp.org>
date Thu, 05 Jan 2012 18:42:08 +0100
parents 8250f2777afc
children 498a2211d839
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
1 /* Open the master side of a pseudo-terminal.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15980
diff changeset
2 Copyright (C) 2010-2012 Free Software Foundation, Inc.
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
3
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
7 (at your option) any later version.
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
8
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
12 GNU General Public License for more details.
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
13
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
16
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
17 #include <config.h>
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
18
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
19 /* Specification. */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
20 #include <stdlib.h>
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
21
15976
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
22 #include <errno.h>
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
23 #include <fcntl.h>
15976
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
24 #if defined __OpenBSD__
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
25 # include <sys/ioctl.h>
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
26 # include <sys/tty.h>
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
27 #endif
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
28
15980
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
29 /* posix_openpt() is already provided on
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
30 glibc >= 2.2.1 (but is a stub on GNU/Hurd),
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
31 MacOS X >= 10.4,
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
32 FreeBSD >= 5.1 (lived in src/lib/libc/stdlib/grantpt.c before 8.0),
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
33 NetBSD >= 3.0,
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
34 AIX >= 5.2, HP-UX >= 11.31, Solaris >= 10, Cygwin >= 1.7.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
35 Thus, this replacement function is compiled on
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
36 MacOS X 10.3, OpenBSD 4.9, Minix 3.1.8,
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
37 AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris 9,
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
38 Cygwin 1.5.x, mingw, MSVC 9, Interix 3.5, BeOS.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
39 Among these:
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
40 - AIX has /dev/ptc.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
41 - HP-UX 10..11, IRIX 6.5, OSF/1 5.1, Solaris 2.6..9, Cygwin 1.5
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
42 have /dev/ptmx.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
43 - HP-UX 10..11 also has /dev/ptym/clone, but this should not be needed.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
44 - OpenBSD 4.9 has /dev/ptm and the PTMGET ioctl.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
45 - Minix 3.1.8 have a list of pseudo-terminal devices in /dev.
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
46 - On native Windows, there are no ttys at all. */
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
47
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
48 int
15965
d646eaed80e3 posix_openpt: Fix compilation error.
Bruno Haible <bruno@clisp.org>
parents: 15961
diff changeset
49 posix_openpt (int flags)
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
50 {
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
51 int master;
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
52
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
53 #ifdef _AIX /* AIX */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
54
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
55 master = open ("/dev/ptc", flags);
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
56
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
57 #elif (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ /* mingw */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
58
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
59 /* Mingw lacks pseudo-terminals altogether. */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
60 master = -1;
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
61 errno = ENOSYS;
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
62
15976
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
63 #elif defined __OpenBSD__
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
64
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
65 /* On OpenBSD, master and slave of a pseudo-terminal are allocated together,
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
66 by opening /dev/ptm and applying the PTMGET ioctl to it. */
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
67 int fd;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
68 struct ptmget data;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
69
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
70 fd = open (PATH_PTMDEV, O_RDWR);
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
71 if (fd >= 0)
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
72 {
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
73 if (ioctl (fd, PTMGET, &data) >= 0)
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
74 {
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
75 master = data.cfd;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
76 close (data.sfd);
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
77 close (fd);
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
78 }
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
79 else
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
80 {
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
81 int saved_errno = errno;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
82 close (fd);
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
83 errno = saved_errno;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
84 master = -1;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
85 }
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
86 }
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
87 else
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
88 master = -1;
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
89
e4144dcb6f45 posix_openpt: Support for OpenBSD.
Bruno Haible <bruno@clisp.org>
parents: 15967
diff changeset
90 #else /* MacOS X, Minix, HP-UX, IRIX, OSF/1, Solaris 9, Cygwin 1.5 */
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
91
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
92 /* Most systems that lack posix_openpt() have /dev/ptmx. */
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
93 master = open ("/dev/ptmx", flags);
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
94
15980
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
95 /* If all this does not work, we could try to open, one by one:
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
96 - On MacOS X: /dev/pty[p-w][0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
97 - On *BSD: /dev/pty[p-sP-S][0-9a-v]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
98 - On Minix: /dev/pty[p-q][0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
99 - On AIX: /dev/ptyp[0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
100 - On HP-UX: /dev/pty[p-r][0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
101 - On OSF/1: /dev/pty[p-q][0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
102 - On Solaris: /dev/pty[p-r][0-9a-f]
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
103 */
ca99be30d00b openpty, posix_openpt: Remove code duplication.
Bruno Haible <bruno@clisp.org>
parents: 15976
diff changeset
104
15961
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
105 #endif
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
106
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
107 return master;
27cc5b08d442 posix_openpt: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
108 }