annotate lib/ptsname_r.c @ 40140:81f075eaa990

ptsname_r: Work around bug on Android 4.3. * m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): Define HAVE_ESSENTIALLY_WORKING_PTSNAME_R. Test whether the return value is correct. * lib/ptsname_r.c (__ptsname_r): If HAVE_ESSENTIALLY_WORKING_PTSNAME_R is defined, just fix the return value. * doc/glibc-functions/ptsname_r.texi: Mention the Android bug. Reword: The behaviour of musl libc is nothing to be "fixed", since it is compliant with the next POSIX standard.
author Bruno Haible <bruno@clisp.org>
date Sat, 26 Jan 2019 15:23:19 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
1 /* Determine name of the slave side of a pseudo-terminal.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 1998, 2002, 2010-2019 Free Software Foundation, Inc.
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
3
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
7 (at your option) any later version.
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
8
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
12 GNU General Public License for more details.
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
13
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
16
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
17 #include <config.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
18
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
19 #include <stdlib.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
20
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
21 #include <errno.h>
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
22 #include <fcntl.h>
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
23 #include <string.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
24 #include <sys/stat.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
25 #include <unistd.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
26
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
27 #ifdef _LIBC
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
28 # include <paths.h>
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
29 #else
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
30 # ifndef _PATH_TTY
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
31 # define _PATH_TTY "/dev/tty"
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
32 # endif
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
33 # ifndef _PATH_DEV
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
34 # define _PATH_DEV "/dev/"
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
35 # endif
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
36
18372
d9961cf477d9 maint: preprocessor changes to support z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents: 18189
diff changeset
37 # undef __set_errno
d9961cf477d9 maint: preprocessor changes to support z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents: 18189
diff changeset
38 # undef __stat
d9961cf477d9 maint: preprocessor changes to support z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents: 18189
diff changeset
39 # undef __ttyname_r
d9961cf477d9 maint: preprocessor changes to support z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents: 18189
diff changeset
40 # undef __ptsname_r
d9961cf477d9 maint: preprocessor changes to support z/OS
Daniel Richard G <skunk@iSKUNK.ORG>
parents: 18189
diff changeset
41
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
42 # define __set_errno(e) errno = (e)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
43 # define __isatty isatty
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
44 # define __stat stat
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
45 # define __ttyname_r ttyname_r
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
46 # define __ptsname_r ptsname_r
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
47
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
48 #endif
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
49
18532
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
50 /* Get the major, minor macros. */
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
51 #if MAJOR_IN_MKDEV
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
52 # include <sys/mkdev.h>
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
53 #endif
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
54 #if MAJOR_IN_SYSMACROS
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
55 # include <sys/sysmacros.h>
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
56 #endif
93a8694eca79 ptsname_r: leverage AC_HEADER_MAJOR
Mike Frysinger <vapier@gentoo.org>
parents: 18372
diff changeset
57
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
58 #ifdef __sun
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
59 /* Get ioctl() and 'struct strioctl'. */
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
60 # include <stropts.h>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
61 /* Get ISPTM. */
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
62 # include <sys/stream.h>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
63 # include <sys/ptms.h>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
64 # include <stdio.h>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
65 #endif
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
66
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
67 #if defined _AIX || defined __osf__
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
68 /* Get ioctl(), ISPTM. */
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
69 # include <sys/ioctl.h>
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
70 # include <stdio.h>
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
71 #endif
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
72
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
73
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
74 /* Store at most BUFLEN characters of the pathname of the slave pseudo
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
75 terminal associated with the master FD is open on in BUF.
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
76 Return 0 on success, otherwise an error number. */
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
77 int
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
78 __ptsname_r (int fd, char *buf, size_t buflen)
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
79 #undef ptsname_r
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
80 {
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
81 #if HAVE_ESSENTIALLY_WORKING_PTSNAME_R
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
82 int ret = ptsname_r (fd, buf, buflen);
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
83 if (ret == 0)
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
84 return 0;
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
85 else
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
86 return errno;
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
87 #else
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
88 int save_errno = errno;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
89 int err;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
90 struct stat st;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
91
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
92 if (buf == NULL)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
93 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
94 __set_errno (EINVAL);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
95 return EINVAL;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
96 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
97
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
98 # if defined __sun /* Solaris */
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
99 if (fstat (fd, &st) < 0)
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
100 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
101 if (!(S_ISCHR (st.st_mode) && major (st.st_rdev) == 0))
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
102 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
103 errno = ENOTTY;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
104 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
105 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
106 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
107 /* Master ptys can be recognized through a STREAMS ioctl. See
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
108 "STREAMS-based Pseudo-Terminal Subsystem"
19192
d86e08b1f555 all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
109 <https://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
110 and "STREAMS ioctl commands"
19192
d86e08b1f555 all: Replace many more http URLs by https URLs. Update stale URLs.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
111 <https://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
112 */
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
113 struct strioctl ioctl_arg;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
114 ioctl_arg.ic_cmd = ISPTM;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
115 ioctl_arg.ic_timout = 0;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
116 ioctl_arg.ic_len = 0;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
117 ioctl_arg.ic_dp = NULL;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
118
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
119 if (ioctl (fd, I_STR, &ioctl_arg) < 0)
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
120 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
121 errno = ENOTTY;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
122 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
123 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
124 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
125 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
126 char tmpbuf[9 + 10 + 1];
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
127 int n = sprintf (tmpbuf, "/dev/pts/%u", minor (st.st_rdev));
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
128 if (n >= buflen)
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
129 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
130 errno = ERANGE;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
131 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
132 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
133 memcpy (buf, tmpbuf, n + 1);
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
134 }
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
135 # elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
136 /* This implementation returns /dev/pts/N, like ptsname() does.
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
137 Whereas the generic implementation below returns /dev/ttypN.
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
138 Both are correct, but let's be consistent with ptsname(). */
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
139 if (fstat (fd, &st) < 0)
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
140 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
141 if (!S_ISCHR (st.st_mode))
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
142 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
143 errno = ENOTTY;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
144 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
145 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
146 {
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
147 int ret;
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
148 int dev;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
149 char tmpbuf[9 + 10 + 1];
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
150 int n;
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
151 # ifdef _AIX
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
152 ret = ioctl (fd, ISPTM, &dev);
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
153 # endif
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
154 # ifdef __osf__
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
155 ret = ioctl (fd, ISPTM, NULL);
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
156 dev = ret;
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
157 # endif
16946
f34e11adfc84 ptsname_r: Fix typo in last commit.
Bruno Haible <bruno@clisp.org>
parents: 16945
diff changeset
158 if (ret < 0)
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
159 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
160 errno = ENOTTY;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
161 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
162 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
163 n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
164 if (n >= buflen)
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
165 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
166 errno = ERANGE;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
167 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
168 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
169 memcpy (buf, tmpbuf, n + 1);
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
170 }
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
171 # else
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
172 if (!__isatty (fd))
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
173 {
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
174 # if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
175 /* Set errno. */
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
176 if (fcntl (fd, F_GETFL) != -1)
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
177 errno = ENOTTY;
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
178 # else
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
179 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
180 # endif
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
181 return errno;
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
182 }
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
183
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
184 if (buflen < strlen (_PATH_TTY) + 3)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
185 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
186 __set_errno (ERANGE);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
187 return ERANGE;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
188 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
189
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
190 err = __ttyname_r (fd, buf, buflen);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
191 if (err != 0)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
192 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
193 __set_errno (err);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
194 return errno;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
195 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
196
17125
99a0d94bccb1 ptsname_r: support ptys returned by FreeBSD posix_openpt
Eric Blake <eblake@redhat.com>
parents: 16946
diff changeset
197 if (strncmp(buf, "/dev/pts/", strlen("/dev/pts/")) != 0)
99a0d94bccb1 ptsname_r: support ptys returned by FreeBSD posix_openpt
Eric Blake <eblake@redhat.com>
parents: 16946
diff changeset
198 buf[sizeof (_PATH_DEV) - 1] = 't';
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
199 # endif
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
200
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
201 if (__stat (buf, &st) < 0)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
202 return errno;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
203
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
204 __set_errno (save_errno);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
205 return 0;
40140
81f075eaa990 ptsname_r: Work around bug on Android 4.3.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
206 #endif
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
207 }