1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/* ttyname_r.c - A ttyname_r() replacement.
* Copyright (C) 2003, 2004, 2012 g10 Code GmbH
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* GPGME is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <https://gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if !HAVE_TTYNAME_R && defined(__GNUC__)
# warning ttyname is not thread-safe, and ttyname_r is missing
#endif
/* For Android we force the use of our replacement code. */
#if HAVE_ANDROID_SYSTEM
# undef HAVE_TTYNAME_R
#endif
int
_gpgme_ttyname_r (int fd, char *buf, size_t buflen)
{
#if HAVE_TTYNAME_R
# if HAVE_BROKEN_TTYNAME_R
/* Solaris fails if BUFLEN is less than 128. OSF/1 5.1 completely
ignores BUFLEN. We use a large buffer to woraround this. */
{
char largebuf[512];
size_t namelen;
int rc;
# if HAVE_POSIXDECL_TTYNAME_R
if (buflen < sizeof (largebuf))
{
rc = ttyname_r (fd, largebuf, (int)sizeof (largebuf));
if (!rc)
{
namelen = strlen (largebuf) + 1;
if (namelen > buflen)
rc = ERANGE;
else
memcpy (buf, largebuf, namelen);
}
}
else
rc = ttyname_r (fd, buf, (int)buflen);
# else /*!HAVE_POSIXDECL_TTYNAME_R*/
char *name;
if (buflen < sizeof (largebuf))
name = ttyname_r (fd, largebuf, (int)sizeof (largebuf));
else
name = ttyname_r (fd, buf, (int)buflen);
rc = name? 0 : (errno? errno : -1);
if (!rc && buf != name)
{
namelen = strlen (name) + 1;
if (namelen > buflen)
rc = ERANGE;
else
memmove (buf, name, namelen);
}
# endif
return rc;
}
# else /*!HAVE_BROKEN_TTYNAME_R*/
{
int rc;
# if HAVE_POSIXDECL_TTYNAME_R
rc = ttyname_r (fd, buf, buflen);
# else /*!HAVE_POSIXDECL_TTYNAME_R*/
char *name;
size_t namelen;
name = ttyname_r (fd, buf, (int)buflen);
rc = name? 0 : (errno? errno : -1);
if (!rc && buf != name)
{
namelen = strlen (name) + 1;
if (namelen > buflen)
rc = ERANGE;
else
memmove (buf, name, namelen);
}
# endif
return rc;
}
# endif /*!HAVE_BROKEN_TTYNAME_R*/
#else /*!HAVE_TTYNAME_R*/
char *tty;
# if HAVE_W32_SYSTEM || HAVE_ANDROID_SYSTEM
/* We use this default one for now. AFAICS we only need it to be
passed to gpg and in turn to pinentry. Providing a replacement
is needed because elsewhere we bail out on error or Android
provided ttyname_r prints an error message if used. */
tty = "/dev/tty";
# else
tty = ttyname (fd);
if (!tty)
return errno? errno : -1;
# endif
strncpy (buf, tty, buflen);
buf[buflen - 1] = '\0';
return (strlen (tty) >= buflen) ? ERANGE : 0;
#endif /*!HAVE_TTYNAME_R*/
}
|