Update.
[glibc.git] / sysdeps / unix / sysv / linux / getpt.c
blob3b01ef561e87a72d84f24260daf35486b3729e91
1 /* Copyright (C) 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <paths.h>
25 #include <sys/statfs.h>
27 #include "linux_fsinfo.h"
29 /* Path to the master pseudo terminal cloning device. */
30 #define _PATH_DEVPTMX _PATH_DEV "ptmx"
31 /* Directory containing the UNIX98 pseudo terminals. */
32 #define _PATH_DEVPTS _PATH_DEV "pts"
34 /* Prototype for function that opens BSD-style master pseudo-terminals. */
35 int __bsd_getpt (void);
37 /* Open a master pseudo terminal and return its file descriptor. */
38 int
39 __getpt (void)
41 static int have_no_dev_ptmx;
42 int fd;
44 if (!have_no_dev_ptmx)
46 fd = __open (_PATH_DEVPTMX, O_RDWR);
47 if (fd != -1)
49 struct statfs fsbuf;
50 static int devpts_mounted;
52 /* Check that the /dev/pts filesystem is mounted
53 or if /dev is a devfs filesystem (this implies /dev/pts). */
54 if (devpts_mounted
55 || (__statfs (_PATH_DEVPTS, &fsbuf) == 0
56 && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
57 || (__statfs (_PATH_DEV, &fsbuf) == 0
58 && fsbuf.f_type == DEVFS_SUPER_MAGIC))
60 /* Everything is ok. */
61 devpts_mounted = 1;
62 return fd;
65 /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
66 are not usable. */
67 __close (fd);
68 have_no_dev_ptmx = 1;
70 else
72 if (errno == ENOENT || errno == ENODEV)
73 have_no_dev_ptmx = 1;
74 else
75 return -1;
79 return __bsd_getpt ();
82 #define PTYNAME1 "pqrstuvwxyzabcde";
83 #define PTYNAME2 "0123456789abcdef";
85 #define __getpt __bsd_getpt
86 #include <sysdeps/unix/bsd/getpt.c>