maint: update copyright
[gnulib.git] / lib / ptsname_r.c
1 /* Determine name of the slave side of a pseudo-terminal.
2    Copyright (C) 1998, 2002, 2010-2014 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 <fcntl.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #ifdef _LIBC
28 # include <paths.h>
29 #else
30 # ifndef _PATH_TTY
31 #  define _PATH_TTY "/dev/tty"
32 # endif
33 # ifndef _PATH_DEV
34 #  define _PATH_DEV "/dev/"
35 # endif
36
37 # define __set_errno(e) errno = (e)
38 # define __isatty isatty
39 # define __stat stat
40 # define __ttyname_r ttyname_r
41 # define __ptsname_r ptsname_r
42
43 #endif
44
45 #ifdef __sun
46 /* Get ioctl() and 'struct strioctl'.  */
47 # include <stropts.h>
48 /* Get ISPTM.  */
49 # include <sys/stream.h>
50 # include <sys/ptms.h>
51 /* Get the major, minor macros.  */
52 # include <sys/sysmacros.h>
53 # include <stdio.h>
54 #endif
55
56 #if defined _AIX || defined __osf__
57 /* Get ioctl(), ISPTM.  */
58 # include <sys/ioctl.h>
59 /* Get the major, minor macros.  */
60 # include <sys/sysmacros.h>
61 # include <stdio.h>
62 #endif
63
64
65 /* Store at most BUFLEN characters of the pathname of the slave pseudo
66    terminal associated with the master FD is open on in BUF.
67    Return 0 on success, otherwise an error number.  */
68 int
69 __ptsname_r (int fd, char *buf, size_t buflen)
70 {
71   int save_errno = errno;
72   int err;
73   struct stat st;
74
75   if (buf == NULL)
76     {
77       __set_errno (EINVAL);
78       return EINVAL;
79     }
80
81 #if defined __sun /* Solaris */
82   if (fstat (fd, &st) < 0)
83     return errno;
84   if (!(S_ISCHR (st.st_mode) && major (st.st_rdev) == 0))
85     {
86       errno = ENOTTY;
87       return errno;
88     }
89   {
90     /* Master ptys can be recognized through a STREAMS ioctl.  See
91        "STREAMS-based Pseudo-Terminal Subsystem"
92        <http://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
93        and "STREAMS ioctl commands"
94        <http://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
95      */
96     struct strioctl ioctl_arg;
97     ioctl_arg.ic_cmd = ISPTM;
98     ioctl_arg.ic_timout = 0;
99     ioctl_arg.ic_len = 0;
100     ioctl_arg.ic_dp = NULL;
101
102     if (ioctl (fd, I_STR, &ioctl_arg) < 0)
103       {
104         errno = ENOTTY;
105         return errno;
106       }
107   }
108   {
109     char tmpbuf[9 + 10 + 1];
110     int n = sprintf (tmpbuf, "/dev/pts/%u", minor (st.st_rdev));
111     if (n >= buflen)
112       {
113         errno = ERANGE;
114         return errno;
115       }
116     memcpy (buf, tmpbuf, n + 1);
117   }
118 #elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
119   /* This implementation returns /dev/pts/N, like ptsname() does.
120      Whereas the generic implementation below returns /dev/ttypN.
121      Both are correct, but let's be consistent with ptsname().  */
122   if (fstat (fd, &st) < 0)
123     return errno;
124   if (!S_ISCHR (st.st_mode))
125     {
126       errno = ENOTTY;
127       return errno;
128     }
129   {
130     int ret;
131     int dev;
132     char tmpbuf[9 + 10 + 1];
133     int n;
134 # ifdef _AIX
135     ret = ioctl (fd, ISPTM, &dev);
136 # endif
137 # ifdef __osf__
138     ret = ioctl (fd, ISPTM, NULL);
139     dev = ret;
140 # endif
141     if (ret < 0)
142       {
143         errno = ENOTTY;
144         return errno;
145       }
146     n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
147     if (n >= buflen)
148       {
149         errno = ERANGE;
150         return errno;
151       }
152     memcpy (buf, tmpbuf, n + 1);
153   }
154 #else
155   if (!__isatty (fd))
156     {
157 #if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
158       /* Set errno.  */
159       if (fcntl (fd, F_GETFL) != -1)
160         errno = ENOTTY;
161 #else
162       /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY).  */
163 #endif
164       return errno;
165     }
166
167   if (buflen < strlen (_PATH_TTY) + 3)
168     {
169       __set_errno (ERANGE);
170       return ERANGE;
171     }
172
173   err = __ttyname_r (fd, buf, buflen);
174   if (err != 0)
175     {
176       __set_errno (err);
177       return errno;
178     }
179
180   if (strncmp(buf, "/dev/pts/", strlen("/dev/pts/")) != 0)
181     buf[sizeof (_PATH_DEV) - 1] = 't';
182 #endif
183
184   if (__stat (buf, &st) < 0)
185     return errno;
186
187   __set_errno (save_errno);
188   return 0;
189 }