2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / grantpt.c
blobb894b8b63138e2ed6071116f675518ca72df7485
1 /* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <sys/statfs.h>
25 #include "linux_fsinfo.h"
27 /* Prototype for function that changes ownership and access permission
28 for slave pseudo terminals that do not live on a `devpts'
29 filesystem. */
30 static int __unix_grantpt (int fd);
32 /* Prototype for private function that gets the name of the slave
33 pseudo terminal in a safe way. */
34 static int pts_name (int fd, char **pts, size_t buf_len);
36 /* Change the ownership and access permission of the slave pseudo
37 terminal associated with the master pseudo terminal specified
38 by FD. */
39 int
40 grantpt (int fd)
42 struct statfs fsbuf;
43 #ifdef PATH_MAX
44 char _buf[PATH_MAX];
45 #else
46 char _buf[512];
47 #endif
48 char *buf = _buf;
50 if (__builtin_expect (pts_name (fd, &buf, sizeof (_buf)), 0))
52 int save_errno = errno;
54 /* Check, if the file descriptor is valid. pts_name returns the
55 wrong errno number, so we cannot use that. */
56 if (__libc_fcntl (fd, F_GETFD) == -1 && errno == EBADF)
57 return -1;
59 /* If the filedescriptor is no TTY, grantpt has to set errno
60 to EINVAL. */
61 if (save_errno == ENOTTY)
62 __set_errno (EINVAL);
63 else
64 __set_errno (save_errno);
66 return -1;
69 if (__statfs (buf, &fsbuf) < 0)
70 return -1;
72 /* If the slave pseudo terminal lives on a `devpts' filesystem, the
73 ownership and access permission are already set. */
74 if (fsbuf.f_type == DEVPTS_SUPER_MAGIC || fsbuf.f_type == DEVFS_SUPER_MAGIC)
75 return 0;
77 return __unix_grantpt (fd);
80 #define grantpt static __unix_grantpt
81 #include <sysdeps/unix/grantpt.c>