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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/statfs.h>
28 extern __typeof(statfs) __libc_statfs;
31 #if !defined __ASSUME_DEVPTS__
33 /* Constant that identifies the `devpts' filesystem. */
34 # define DEVPTS_SUPER_MAGIC 0x1cd1
35 /* Constant that identifies the `devfs' filesystem. */
36 # define DEVFS_SUPER_MAGIC 0x1373
39 /* Path to the master pseudo terminal cloning device. */
40 #define _PATH_DEVPTMX _PATH_DEV "ptmx"
41 /* Directory containing the UNIX98 pseudo terminals. */
42 #define _PATH_DEVPTS _PATH_DEV "pts"
44 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
45 /* Prototype for function that opens BSD-style master pseudo-terminals. */
46 extern int __bsd_getpt (void) attribute_hidden;
49 /* Open a master pseudo terminal and return its file descriptor. */
51 posix_openpt (int flags)
53 #define have_no_dev_ptmx (1<<0)
54 #define devpts_mounted (1<<1)
55 #if !defined __UNIX98PTY_ONLY__
56 static smallint _state;
60 #if !defined __UNIX98PTY_ONLY__
61 if (!(_state & have_no_dev_ptmx))
64 fd = open (_PATH_DEVPTMX, flags);
67 #if defined __ASSUME_DEVPTS__
72 /* Check that the /dev/pts filesystem is mounted
73 or if /dev is a devfs filesystem (this implies /dev/pts). */
75 #if !defined __UNIX98PTY_ONLY__
76 (_state & devpts_mounted) ||
78 (__libc_statfs (_PATH_DEVPTS, &fsbuf) == 0
79 && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
80 || (__libc_statfs (_PATH_DEV, &fsbuf) == 0
81 && fsbuf.f_type == DEVFS_SUPER_MAGIC))
83 /* Everything is ok. */
84 #if !defined __UNIX98PTY_ONLY__
85 _state |= devpts_mounted;
90 /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
93 #if !defined __UNIX98PTY_ONLY__
94 _state |= have_no_dev_ptmx;
100 #if !defined __UNIX98PTY_ONLY__
101 if (errno == ENOENT || errno == ENODEV)
102 _state |= have_no_dev_ptmx;
108 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
109 /* If we have no ptmx then ignore flags and use the fallback. */
110 if (_state & have_no_dev_ptmx)
111 return __bsd_getpt();
115 libc_hidden_def(posix_openpt)
116 #undef have_no_dev_ptmx
117 #undef devpts_mounted
119 #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
122 return posix_openpt(O_RDWR);
125 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
126 # define PTYNAME1 "pqrstuvwxyzabcde";
127 # define PTYNAME2 "0123456789abcdef";
129 # define __getpt __bsd_getpt
130 # include "bsd_getpt.c"
132 #endif /* GNU && __UCLIBC_HAS_GETPT__ */