]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/getpt.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / getpt.c
CommitLineData
b168057a 1/* Copyright (C) 1998-2015 Free Software Foundation, Inc.
6591c335
UD
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
41bdb6e2
AJ
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.
6591c335
UD
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
41bdb6e2 13 Lesser General Public License for more details.
6591c335 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
6591c335 18
6591c335 19#include <errno.h>
9b3c7c3c 20#include <fcntl.h>
6591c335 21#include <stdlib.h>
d7807bfa
UD
22#include <unistd.h>
23#include <paths.h>
24#include <sys/statfs.h>
25
1503837c 26#include "linux_fsinfo.h"
9c9f3b17 27
9b3c7c3c 28/* Path to the master pseudo terminal cloning device. */
d7807bfa
UD
29#define _PATH_DEVPTMX _PATH_DEV "ptmx"
30/* Directory containing the UNIX98 pseudo terminals. */
31#define _PATH_DEVPTS _PATH_DEV "pts"
6591c335 32
9b3c7c3c
UD
33/* Prototype for function that opens BSD-style master pseudo-terminals. */
34int __bsd_getpt (void);
6591c335 35
9b3c7c3c 36/* Open a master pseudo terminal and return its file descriptor. */
6591c335 37int
0101a56f
UD
38__posix_openpt (oflag)
39 int oflag;
6591c335 40{
c4563d2d 41 static int have_no_dev_ptmx;
9b3c7c3c 42 int fd;
6591c335 43
c4563d2d 44 if (!have_no_dev_ptmx)
6591c335 45 {
0101a56f 46 fd = __open (_PATH_DEVPTMX, oflag);
6591c335 47 if (fd != -1)
d7807bfa
UD
48 {
49 struct statfs fsbuf;
50 static int devpts_mounted;
51
9c9f3b17
UD
52 /* Check that the /dev/pts filesystem is mounted
53 or if /dev is a devfs filesystem (this implies /dev/pts). */
d7807bfa
UD
54 if (devpts_mounted
55 || (__statfs (_PATH_DEVPTS, &fsbuf) == 0
9c9f3b17 56 && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
0101a56f 57 || (__statfs (_PATH_DEV, &fsbuf) == 0
9c9f3b17 58 && fsbuf.f_type == DEVFS_SUPER_MAGIC))
d7807bfa
UD
59 {
60 /* Everything is ok. */
61 devpts_mounted = 1;
62 return fd;
63 }
64
65 /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
d79eccd6 66 are not usable. */
d7807bfa
UD
67 __close (fd);
68 have_no_dev_ptmx = 1;
d79eccd6 69 __set_errno (ENOENT);
d7807bfa 70 }
6591c335
UD
71 else
72 {
73 if (errno == ENOENT || errno == ENODEV)
c4563d2d 74 have_no_dev_ptmx = 1;
6591c335
UD
75 else
76 return -1;
77 }
78 }
d79eccd6
AS
79 else
80 __set_errno (ENOENT);
6591c335 81
0101a56f
UD
82 return -1;
83}
84weak_alias (__posix_openpt, posix_openpt)
85
86
87int
88__getpt (void)
89{
90 int fd = __posix_openpt (O_RDWR);
91 if (fd == -1)
92 fd = __bsd_getpt ();
93 return fd;
6591c335 94}
9b3c7c3c 95
0101a56f 96
9b3c7c3c
UD
97#define PTYNAME1 "pqrstuvwxyzabcde";
98#define PTYNAME2 "0123456789abcdef";
99
100#define __getpt __bsd_getpt
0101a56f 101#define HAVE_POSIX_OPENPT
9b3c7c3c 102#include <sysdeps/unix/bsd/getpt.c>