| 1 |
/* Open the master side of a pseudo-terminal. |
| 2 |
Copyright (C) 2010-2026 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 <stdlib.h> |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <fcntl.h> |
| 24 |
#if defined __OpenBSD__ |
| 25 |
# include <sys/ioctl.h> |
| 26 |
# include <sys/tty.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
/* posix_openpt() is already provided on |
| 30 |
glibc >= 2.2.1 (but is a stub on GNU/Hurd), |
| 31 |
Mac OS X >= 10.4, |
| 32 |
FreeBSD >= 5.1 (lived in src/lib/libc/stdlib/grantpt.c before 8.0), |
| 33 |
NetBSD >= 3.0, |
| 34 |
AIX >= 5.2, HP-UX >= 11.31, Solaris >= 10, Cygwin >= 1.7. |
| 35 |
Thus, this replacement function is compiled on |
| 36 |
Mac OS X 10.3, OpenBSD 4.9, Minix 3.1.8, |
| 37 |
AIX 5.1, HP-UX 11.23, Solaris 9, |
| 38 |
Cygwin 1.5.x, mingw, MSVC 9, Interix 3.5, BeOS. |
| 39 |
Among these: |
| 40 |
- AIX has /dev/ptc. |
| 41 |
- HP-UX 10..11, Solaris 2.6..9, Cygwin 1.5 have /dev/ptmx. |
| 42 |
- HP-UX 10..11 also has /dev/ptym/clone, but this should not be needed. |
| 43 |
- OpenBSD 4.9 has /dev/ptm and the PTMGET ioctl. |
| 44 |
- Minix 3.1.8 have a list of pseudo-terminal devices in /dev. |
| 45 |
- On native Windows, there are no ttys at all. */ |
| 46 |
|
| 47 |
int |
| 48 |
posix_openpt (int flags) |
| 49 |
{ |
| 50 |
int master; |
| 51 |
|
| 52 |
#ifdef _AIX /* AIX */ |
| 53 |
|
| 54 |
master = open ("/dev/ptc", flags); |
| 55 |
|
| 56 |
#elif defined _WIN32 && !defined __CYGWIN__ /* mingw */ |
| 57 |
|
| 58 |
/* Mingw lacks pseudo-terminals altogether. */ |
| 59 |
master = -1; |
| 60 |
errno = ENOSYS; |
| 61 |
|
| 62 |
#elif defined __OpenBSD__ |
| 63 |
|
| 64 |
/* On OpenBSD, master and slave of a pseudo-terminal are allocated together, |
| 65 |
by opening /dev/ptm and applying the PTMGET ioctl to it. */ |
| 66 |
int fd = open (PATH_PTMDEV, O_RDWR); |
| 67 |
if (fd >= 0) |
| 68 |
{ |
| 69 |
struct ptmget data; |
| 70 |
if (ioctl (fd, PTMGET, &data) >= 0) |
| 71 |
{ |
| 72 |
master = data.cfd; |
| 73 |
close (data.sfd); |
| 74 |
close (fd); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
int saved_errno = errno; |
| 79 |
close (fd); |
| 80 |
errno = saved_errno; |
| 81 |
master = -1; |
| 82 |
} |
| 83 |
} |
| 84 |
else |
| 85 |
master = -1; |
| 86 |
|
| 87 |
#else /* Mac OS X, Minix, HP-UX, Solaris 9, Cygwin 1.5 */ |
| 88 |
|
| 89 |
/* Most systems that lack posix_openpt() have /dev/ptmx. */ |
| 90 |
master = open ("/dev/ptmx", flags); |
| 91 |
|
| 92 |
/* If all this does not work, we could try to open, one by one: |
| 93 |
- On Mac OS X: /dev/pty[p-w][0-9a-f] |
| 94 |
- On *BSD: /dev/pty[p-sP-S][0-9a-v] |
| 95 |
- On Minix: /dev/pty[p-q][0-9a-f] |
| 96 |
- On AIX: /dev/ptyp[0-9a-f] |
| 97 |
- On HP-UX: /dev/pty[p-r][0-9a-f] |
| 98 |
- On Solaris: /dev/pty[p-r][0-9a-f] |
| 99 |
*/ |
| 100 |
|
| 101 |
#endif |
| 102 |
|
| 103 |
return master; |
| 104 |
} |