maint: update almost all copyright ranges to include 2011
[gnulib.git] / lib / ptsname.c
1 /* Determine name of the slave side of a pseudo-terminal.
2    Copyright (C) 1998, 2002, 2010-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include <errno.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #ifdef _LIBC
27 # include <paths.h>
28 #else
29 # ifndef _PATH_TTY
30 #  define _PATH_TTY "/dev/tty"
31 # endif
32 # ifndef _PATH_DEV
33 #  define _PATH_DEV "/dev/"
34 # endif
35
36 # define __set_errno(e) errno = (e)
37 # define __isatty isatty
38 # define __stat stat
39 # define __ttyname_r ttyname_r
40
41 static int __ptsname_r (int fd, char *buf, size_t buflen);
42 #endif
43
44
45 /* Static buffer for `ptsname'.  */
46 static char buffer[64];
47
48
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.  */
52 char *
53 ptsname (int fd)
54 {
55   return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
56 }
57
58
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.  */
62 static int
63 __ptsname_r (int fd, char *buf, size_t buflen)
64 {
65   int save_errno = errno;
66   int err;
67   struct stat st;
68
69   if (buf == NULL)
70     {
71       __set_errno (EINVAL);
72       return EINVAL;
73     }
74
75   if (!__isatty (fd))
76     /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY).  */
77     return errno;
78
79   if (buflen < strlen (_PATH_TTY) + 3)
80     {
81       __set_errno (ERANGE);
82       return ERANGE;
83     }
84
85   err = __ttyname_r (fd, buf, buflen);
86   if (err != 0)
87     {
88       __set_errno (err);
89       return errno;
90     }
91
92   buf[sizeof (_PATH_DEV) - 1] = 't';
93
94   if (__stat (buf, &st) < 0)
95     return errno;
96
97   __set_errno (save_errno);
98   return 0;
99 }