OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <paths.h>
25
26 #if !defined __ASSUME_DEVPTS__
27 # include <sys/statfs.h>
28
29 /* Constant that identifies the `devpts' filesystem.  */
30 # define DEVPTS_SUPER_MAGIC     0x1cd1
31 /* Constant that identifies the `devfs' filesystem.  */
32 # define DEVFS_SUPER_MAGIC      0x1373
33 #endif
34
35 /* Path to the master pseudo terminal cloning device.  */
36 #define _PATH_DEVPTMX _PATH_DEV "ptmx"
37 /* Directory containing the UNIX98 pseudo terminals.  */
38 #define _PATH_DEVPTS _PATH_DEV "pts"
39
40 #if !defined __UNIX98PTY_ONLY__
41 /* Prototype for function that opens BSD-style master pseudo-terminals.  */
42 extern int __bsd_getpt (void) attribute_hidden;
43 #endif
44
45 libc_hidden_proto(open)
46 libc_hidden_proto(close)
47
48 /* Open a master pseudo terminal and return its file descriptor.  */
49 int
50 getpt (void)
51 {
52 #if !defined __UNIX98PTY_ONLY__
53   static int have_no_dev_ptmx;
54 #endif
55   int fd;
56
57 #if !defined __UNIX98PTY_ONLY__
58   if (!have_no_dev_ptmx)
59 #endif
60     {
61       fd = open (_PATH_DEVPTMX, O_RDWR);
62       if (fd != -1)
63         {
64 #if defined __ASSUME_DEVPTS__
65           return fd;
66 #else
67           struct statfs fsbuf;
68           static int devpts_mounted;
69
70           /* Check that the /dev/pts filesystem is mounted
71              or if /dev is a devfs filesystem (this implies /dev/pts).  */
72           if (devpts_mounted
73               || (statfs (_PATH_DEVPTS, &fsbuf) == 0
74                   && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
75               || (statfs (_PATH_DEV, &fsbuf) == 0       
76                   && fsbuf.f_type == DEVFS_SUPER_MAGIC))
77             {
78               /* Everything is ok.  */
79               devpts_mounted = 1;
80               return fd;
81             }
82
83           /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
84              are not usable.  */
85           close (fd);
86 #if !defined __UNIX98PTY_ONLY__
87           have_no_dev_ptmx = 1;
88 #endif
89 #endif
90         }
91       else
92         {
93 #if !defined __UNIX98PTY_ONLY__
94           if (errno == ENOENT || errno == ENODEV)
95             have_no_dev_ptmx = 1;
96           else
97 #endif
98             return -1;
99         }
100     }
101
102 #if !defined __UNIX98PTY_ONLY__
103   return __bsd_getpt ();
104 #endif
105 }
106
107 #if !defined __UNIX98PTY_ONLY__
108 # define PTYNAME1 "pqrstuvwxyzabcde";
109 # define PTYNAME2 "0123456789abcdef";
110
111 # define __getpt __bsd_getpt
112 # include "bsd_getpt.c"
113 #endif