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