]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/stdlib/getpt.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / stdlib / getpt.c
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.
4
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.
9
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.
14
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/>.  */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <unistd.h>
24 #include <paths.h>
25 #include <sys/statfs.h>
26
27 extern __typeof(statfs) __libc_statfs;
28
29
30 #if !defined __ASSUME_DEVPTS__
31
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
36 #endif
37
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"
42
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);
46 #endif
47
48 /* Open a master pseudo terminal and return its file descriptor.  */
49 static int
50 __posix_openpt (int flags)
51 {
52 #define have_no_dev_ptmx (1<<0)
53 #define devpts_mounted   (1<<1)
54 #if !defined __UNIX98PTY_ONLY__
55   static smallint _state;
56 #endif
57   int fd;
58
59 #if !defined __UNIX98PTY_ONLY__
60   if (!(_state & have_no_dev_ptmx))
61 #endif
62     {
63       fd = open (_PATH_DEVPTMX, flags);
64       if (fd != -1)
65         {
66 #if defined __ASSUME_DEVPTS__
67           return fd;
68 #else
69           struct statfs fsbuf;
70
71           /* Check that the /dev/pts filesystem is mounted
72              or if /dev is a devfs filesystem (this implies /dev/pts).  */
73           if (
74 #if !defined __UNIX98PTY_ONLY__
75               (_state & devpts_mounted) ||
76 #endif
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))
81             {
82               /* Everything is ok.  */
83 #if !defined __UNIX98PTY_ONLY__
84               _state |= devpts_mounted;
85 #endif
86               return fd;
87             }
88
89           /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
90              are not usable.  */
91           close (fd);
92 #if !defined __UNIX98PTY_ONLY__
93           _state |= have_no_dev_ptmx;
94 #endif
95 #endif
96         }
97       else
98         {
99 #if !defined __UNIX98PTY_ONLY__
100           if (errno == ENOENT || errno == ENODEV)
101             _state |= have_no_dev_ptmx;
102           else
103 #endif
104             return -1;
105         }
106     }
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();
111 #endif
112   return -1;
113 }
114 strong_alias(__posix_openpt,posix_openpt)
115 #undef have_no_dev_ptmx
116 #undef devpts_mounted
117
118 #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
119 int getpt (void)
120 {
121         return __posix_openpt(O_RDWR);
122 }
123
124 #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
125 # define PTYNAME1 "pqrstuvwxyzabcde";
126 # define PTYNAME2 "0123456789abcdef";
127
128 # define __getpt __bsd_getpt
129 # include "bsd_getpt.c"
130 #endif
131 #endif /* GNU && __UCLIBC_HAS_GETPT__ */