2.9
[glibc/nacl-glibc.git] / sysdeps / unix / bsd / ptsname.c
blobfd446a4b66543e3fce41da6bdd561066be6f4e83
1 /* Copyright (C) 1998,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 <paths.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
26 /* Static buffer for `ptsname'. */
27 static char buffer[sizeof (_PATH_TTY) + 2];
30 /* Return the pathname of the pseudo terminal slave assoicated with
31 the master FD is open on, or NULL on errors.
32 The returned storage is good until the next call to this function. */
33 char *
34 ptsname (int fd)
36 return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
40 /* Store at most BUFLEN characters of the pathname of the slave pseudo
41 terminal associated with the master FD is open on in BUF.
42 Return 0 on success, otherwise an error number. */
43 int
44 __ptsname_r (int fd, char *buf, size_t buflen)
46 int save_errno = errno;
47 struct stat st;
49 if (buf == NULL)
51 __set_errno (EINVAL);
52 return EINVAL;
55 if (!__isatty (fd))
56 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
57 return errno;
59 if (buflen < strlen (_PATH_TTY) + 3)
61 __set_errno (ERANGE);
62 return ERANGE;
65 if (__ttyname_r (fd, buf, buflen) != 0)
66 return errno;
68 buf[sizeof (_PATH_DEV) - 1] = 't';
70 if (__stat (buf, &st) < 0)
71 return errno;
73 __set_errno (save_errno);
74 return 0;
76 weak_alias (__ptsname_r, ptsname_r)