1 /* Copyright (C) 1998, 2000, 2001, 2002, 2009 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 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.
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.
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
24 #include <sys/ioctl.h>
26 #include <sys/sysmacros.h>
30 #include <stdio-common/_itoa.h>
32 /* Check if DEV corresponds to a master pseudo terminal device. */
33 #define MASTER_P(Dev) \
35 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
36 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
38 /* Check if DEV corresponds to a slave pseudo terminal device. */
39 #define SLAVE_P(Dev) \
41 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
42 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
44 /* Note that major number 4 corresponds to the old BSD style pseudo
45 terminal devices. As of Linux 2.1.115 these are no longer
46 supported. They have been replaced by major numbers 2 (masters)
49 /* Directory where we can find the slave pty nodes. */
50 #define _PATH_DEVPTS "/dev/pts/"
52 /* The are declared in getpt.c. */
53 extern const char __libc_ptyname1
[] attribute_hidden
;
54 extern const char __libc_ptyname2
[] attribute_hidden
;
56 /* Static buffer for `ptsname'. */
57 static char buffer
[sizeof (_PATH_DEVPTS
) + 20];
60 /* Return the pathname of the pseudo terminal slave assoicated with
61 the master FD is open on, or NULL on errors.
62 The returned storage is good until the next call to this function. */
66 return __ptsname_r (fd
, buffer
, sizeof (buffer
)) != 0 ? NULL
: buffer
;
71 __ptsname_internal (int fd
, char *buf
, size_t buflen
, struct stat64
*stp
)
73 int save_errno
= errno
;
89 if (__ioctl (fd
, TIOCGPTN
, &ptyno
) == 0)
91 /* Buffer we use to print the number in. For a maximum size for
92 `int' of 8 bytes we never need more than 20 digits. */
94 const char *devpts
= _PATH_DEVPTS
;
95 const size_t devptslen
= strlen (_PATH_DEVPTS
);
98 numbuf
[sizeof (numbuf
) - 1] = '\0';
99 p
= _itoa_word (ptyno
, &numbuf
[sizeof (numbuf
) - 1], 10, 0);
101 if (buflen
< devptslen
+ (&numbuf
[sizeof (numbuf
)] - p
))
103 __set_errno (ERANGE
);
107 memcpy (__stpcpy (buf
, devpts
), p
, &numbuf
[sizeof (numbuf
)] - p
);
109 else if (errno
== EINVAL
)
114 if (buflen
< strlen (_PATH_TTY
) + 3)
116 __set_errno (ERANGE
);
120 if (__fxstat64 (_STAT_VER
, fd
, stp
) < 0)
123 /* Check if FD really is a master pseudo terminal. */
124 if (! MASTER_P (stp
->st_rdev
))
126 __set_errno (ENOTTY
);
130 ptyno
= minor (stp
->st_rdev
);
131 #if __LINUX_KERNEL_VERSION < 131443
132 /* This is for the old BSD pseudo terminals. As of Linux
133 2.1.115 these are no longer supported. */
134 if (major (stp
->st_rdev
) == 4)
138 if (ptyno
/ 16 >= strlen (__libc_ptyname1
))
140 __set_errno (ENOTTY
);
144 p
= __stpcpy (buf
, _PATH_TTY
);
145 p
[0] = __libc_ptyname1
[ptyno
/ 16];
146 p
[1] = __libc_ptyname2
[ptyno
% 16];
150 if (__xstat64 (_STAT_VER
, buf
, stp
) < 0)
153 /* Check if the name we're about to return really corresponds to a
154 slave pseudo terminal. */
155 if (! S_ISCHR (stp
->st_mode
) || ! SLAVE_P (stp
->st_rdev
))
157 /* This really is a configuration problem. */
158 __set_errno (ENOTTY
);
162 __set_errno (save_errno
);
167 /* Store at most BUFLEN characters of the pathname of the slave pseudo
168 terminal associated with the master FD is open on in BUF.
169 Return 0 on success, otherwise an error number. */
171 __ptsname_r (int fd
, char *buf
, size_t buflen
)
174 return __ptsname_internal (fd
, buf
, buflen
, &st
);
176 weak_alias (__ptsname_r
, ptsname_r
)