1 /* Determine name of the slave side of a pseudo-terminal.
2 Copyright (C) 1998, 2002, 2010-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 # define _PATH_TTY "/dev/tty"
33 # define _PATH_DEV "/dev/"
36 # define __set_errno(e) errno = (e)
37 # define __isatty isatty
39 # define __ttyname_r ttyname_r
41 static int __ptsname_r (int fd, char *buf, size_t buflen);
45 /* Static buffer for `ptsname'. */
46 static char buffer[64];
49 /* Return the pathname of the pseudo terminal slave associated with
50 the master FD is open on, or NULL on errors.
51 The returned storage is good until the next call to this function. */
55 return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
59 /* Store at most BUFLEN characters of the pathname of the slave pseudo
60 terminal associated with the master FD is open on in BUF.
61 Return 0 on success, otherwise an error number. */
63 __ptsname_r (int fd, char *buf, size_t buflen)
65 int save_errno = errno;
76 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
79 if (buflen < strlen (_PATH_TTY) + 3)
85 err = __ttyname_r (fd, buf, buflen);
92 buf[sizeof (_PATH_DEV) - 1] = 't';
94 if (__stat (buf, &st) < 0)
97 __set_errno (save_errno);