annotate lib/ptsname_r.c @ 19190:9759915b2aca

all: prefer https: URLs
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 13 Sep 2017 00:48:18 -0700
parents 12df2165ec1c
children d86e08b1f555
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.
18626
12df2165ec1c version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 18532
diff changeset
2 Copyright (C) 1998, 2002, 2010-2017 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)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
79 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
80 int save_errno = errno;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
81 int err;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
82 struct stat st;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
83
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
84 if (buf == NULL)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
85 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
86 __set_errno (EINVAL);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
87 return EINVAL;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
88 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
89
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
90 #if defined __sun /* Solaris */
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
91 if (fstat (fd, &st) < 0)
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
92 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
93 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
94 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
95 errno = ENOTTY;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
96 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
97 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
98 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
99 /* 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
100 "STREAMS-based Pseudo-Terminal Subsystem"
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
101 <http://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
102 and "STREAMS ioctl commands"
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
103 <http://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
104 */
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
105 struct strioctl ioctl_arg;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
106 ioctl_arg.ic_cmd = ISPTM;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
107 ioctl_arg.ic_timout = 0;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
108 ioctl_arg.ic_len = 0;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
109 ioctl_arg.ic_dp = NULL;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
110
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
111 if (ioctl (fd, I_STR, &ioctl_arg) < 0)
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 errno = ENOTTY;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
114 return errno;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
115 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
116 }
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
117 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
118 char tmpbuf[9 + 10 + 1];
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
119 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
120 if (n >= buflen)
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
121 {
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
122 errno = ERANGE;
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
123 return errno;
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 memcpy (buf, tmpbuf, n + 1);
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
126 }
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
127 #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
128 /* 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
129 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
130 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
131 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
132 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
133 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
134 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
135 errno = ENOTTY;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
136 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
137 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
138 {
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
139 int ret;
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
140 int dev;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
141 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
142 int n;
16945
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
143 # ifdef _AIX
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
144 ret = ioctl (fd, ISPTM, &dev);
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
145 # endif
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
146 # ifdef __osf__
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
147 ret = ioctl (fd, ISPTM, NULL);
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
148 dev = ret;
af6492941f0d ptsname_r: Make it consistent with ptsname on AIX.
Bruno Haible <bruno@clisp.org>
parents: 16944
diff changeset
149 # endif
16946
f34e11adfc84 ptsname_r: Fix typo in last commit.
Bruno Haible <bruno@clisp.org>
parents: 16945
diff changeset
150 if (ret < 0)
16944
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
151 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
152 errno = ENOTTY;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
153 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
154 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
155 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
156 if (n >= buflen)
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
157 {
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
158 errno = ERANGE;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
159 return errno;
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
160 }
00ae3faf06c6 ptsname_r: Make it consistent with ptsname on OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 16942
diff changeset
161 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
162 }
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
163 #else
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
164 if (!__isatty (fd))
16940
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
165 {
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
166 #if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
167 /* Set errno. */
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
168 if (fcntl (fd, F_GETFL) != -1)
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
169 errno = ENOTTY;
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
170 #else
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
171 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
172 #endif
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
173 return errno;
f0c9b2dc0596 ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
174 }
16067
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
175
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
176 if (buflen < strlen (_PATH_TTY) + 3)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
177 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
178 __set_errno (ERANGE);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
179 return ERANGE;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
180 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
181
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
182 err = __ttyname_r (fd, buf, buflen);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
183 if (err != 0)
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
184 {
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
185 __set_errno (err);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
186 return errno;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
187 }
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
188
17125
99a0d94bccb1 ptsname_r: support ptys returned by FreeBSD posix_openpt
Eric Blake <eblake@redhat.com>
parents: 16946
diff changeset
189 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
190 buf[sizeof (_PATH_DEV) - 1] = 't';
16942
17843e284264 ptsname_r: Add support for Solaris.
Bruno Haible <bruno@clisp.org>
parents: 16940
diff changeset
191 #endif
16067
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 if (__stat (buf, &st) < 0)
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 __set_errno (save_errno);
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
197 return 0;
c4b6df81c7b3 ptsname_r: new module
Eric Blake <eblake@redhat.com>
parents:
diff changeset
198 }