OSDN Git Service

187f993331549143d18109cba7778304ab41f45f
[uclinux-h8/uClibc.git] / libc / stdlib / grantpt.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    see <http://www.gnu.org/licenses/>.  */
17
18 #include <limits.h>
19 #include <stdlib.h>
20
21 /* If __ASSUME_DEVPTS__ is defined, grantpt() reduces to a stub since we
22    assume that the devfs/devpts filesystem automatically manages the
23    permissions. */
24 #if !defined __ASSUME_DEVPTS__
25 #include <sys/statfs.h>
26
27 /* Constant that identifies the `devpts' filesystem.  */
28 #define DEVPTS_SUPER_MAGIC      0x1cd1
29 /* Constant that identifies the `devfs' filesystem.  */
30 #define DEVFS_SUPER_MAGIC       0x1373
31
32 /* Prototype for function that changes ownership and access permission
33    for slave pseudo terminals that do not live on a `devpts'
34    filesystem.  */
35 int __unix_grantpt (int fd);
36
37 /* Prototype for private function that gets the name of the slave
38    pseudo terminal in a safe way.  */
39 static int pts_name (int fd, char **pts, size_t buf_len);
40 extern __typeof(statfs) __libc_statfs;
41 #endif
42
43 /* Change the ownership and access permission of the slave pseudo
44    terminal associated with the master pseudo terminal specified
45    by FD.  */
46 int
47 #if !defined __ASSUME_DEVPTS__
48 grantpt (int fd)
49 #else
50 grantpt (attribute_unused int fd)
51 #endif
52 {
53 #if !defined __ASSUME_DEVPTS__
54   struct statfs fsbuf;
55   char _buf[PATH_MAX];
56   char *buf = _buf;
57
58   if (pts_name (fd, &buf, sizeof (_buf)))
59     return -1;
60
61   if (__libc_statfs (buf, &fsbuf) < 0)
62     return -1;
63
64   /* If the slave pseudo terminal lives on a `devpts' filesystem, the
65      ownership and access permission are already set.  */
66   if (fsbuf.f_type != DEVPTS_SUPER_MAGIC && fsbuf.f_type != DEVFS_SUPER_MAGIC)
67   return __unix_grantpt (fd);
68 #endif
69   return 0;
70 }
71
72 #if !defined __ASSUME_DEVPTS__
73 # define grantpt __unix_grantpt
74 # include "unix_grantpt.c"
75 #endif