1 /* Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #include <sys/statfs.h>
27 extern __typeof(statfs) __libc_statfs;
30 #if !defined __ASSUME_DEVPTS__
32 /* Constant that identifies the `devpts' filesystem. */
33 # define DEVPTS_SUPER_MAGIC 0x1cd1
34 /* Constant that identifies the `devfs' filesystem. */
35 # define DEVFS_SUPER_MAGIC 0x1373
38 /* Path to the master pseudo terminal cloning device. */
39 #define _PATH_DEVPTMX _PATH_DEV "ptmx"
40 /* Directory containing the UNIX98 pseudo terminals. */
41 #define _PATH_DEVPTS _PATH_DEV "pts"
43 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
44 /* Prototype for function that opens BSD-style master pseudo-terminals. */
45 static __inline__ int __bsd_getpt (void);
48 /* Open a master pseudo terminal and return its file descriptor. */
50 __posix_openpt (int flags)
52 #define have_no_dev_ptmx (1<<0)
53 #define devpts_mounted (1<<1)
54 #if !defined __UNIX98PTY_ONLY__
55 static smallint _state;
59 #if !defined __UNIX98PTY_ONLY__
60 if (!(_state & have_no_dev_ptmx))
63 fd = open (_PATH_DEVPTMX, flags);
66 #if defined __ASSUME_DEVPTS__
71 /* Check that the /dev/pts filesystem is mounted
72 or if /dev is a devfs filesystem (this implies /dev/pts). */
74 #if !defined __UNIX98PTY_ONLY__
75 (_state & devpts_mounted) ||
77 (__libc_statfs (_PATH_DEVPTS, &fsbuf) == 0
78 && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
79 || (__libc_statfs (_PATH_DEV, &fsbuf) == 0
80 && fsbuf.f_type == DEVFS_SUPER_MAGIC))
82 /* Everything is ok. */
83 #if !defined __UNIX98PTY_ONLY__
84 _state |= devpts_mounted;
89 /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
92 #if !defined __UNIX98PTY_ONLY__
93 _state |= have_no_dev_ptmx;
99 #if !defined __UNIX98PTY_ONLY__
100 if (errno == ENOENT || errno == ENODEV)
101 _state |= have_no_dev_ptmx;
107 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
108 /* If we have no ptmx then ignore flags and use the fallback. */
109 if (_state & have_no_dev_ptmx)
110 return __bsd_getpt();
114 strong_alias(__posix_openpt,posix_openpt)
115 #undef have_no_dev_ptmx
116 #undef devpts_mounted
118 #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
121 return __posix_openpt(O_RDWR);
124 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
125 # define PTYNAME1 "pqrstuvwxyzabcde";
126 # define PTYNAME2 "0123456789abcdef";
128 # define __getpt __bsd_getpt
129 # include "bsd_getpt.c"
131 #endif /* GNU && __UCLIBC_HAS_GETPT__ */