2004-12-11 15:48:00 +00:00
|
|
|
|
/* ttyname_r.c - A ttyname_r() replacement.
|
2012-01-19 17:43:10 +00:00
|
|
|
|
Copyright (C) 2003, 2004, 2012 g10 Code GmbH
|
2004-12-11 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
This file is part of GPGME.
|
2012-01-19 17:43:10 +00:00
|
|
|
|
|
2004-12-11 15:48:00 +00:00
|
|
|
|
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.
|
2012-01-19 17:43:10 +00:00
|
|
|
|
|
2004-12-11 15:48:00 +00:00
|
|
|
|
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.
|
2012-01-19 17:43:10 +00:00
|
|
|
|
|
2004-12-11 15:48:00 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2016-11-16 12:27:00 +00:00
|
|
|
|
License along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2012-01-19 17:43:10 +00:00
|
|
|
|
*/
|
2004-12-11 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2010-11-02 16:27:46 +00:00
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
2004-12-11 15:48:00 +00:00
|
|
|
|
|
|
|
|
|
|
2012-01-19 17:43:10 +00:00
|
|
|
|
#if !HAVE_TTYNAME_R && defined(__GNUC__)
|
2010-11-02 16:27:46 +00:00
|
|
|
|
# warning ttyname is not thread-safe, and ttyname_r is missing
|
|
|
|
|
#endif
|
2004-12-11 15:48:00 +00:00
|
|
|
|
|
2012-10-24 14:44:34 +00:00
|
|
|
|
/* For Android we force the use of our replacement code. */
|
|
|
|
|
#if HAVE_ANDROID_SYSTEM
|
|
|
|
|
# undef HAVE_TTYNAME_R
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2004-12-11 15:48:34 +00:00
|
|
|
|
int
|
2012-01-19 17:43:10 +00:00
|
|
|
|
_gpgme_ttyname_r (int fd, char *buf, size_t buflen)
|
2004-12-11 15:48:00 +00:00
|
|
|
|
{
|
2012-01-19 17:43:10 +00:00
|
|
|
|
#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*/
|
2004-12-11 15:48:00 +00:00
|
|
|
|
char *tty;
|
|
|
|
|
|
2012-10-24 14:44:34 +00:00
|
|
|
|
# if HAVE_W32_SYSTEM || HAVE_ANDROID_SYSTEM
|
2007-01-10 10:18:05 +00:00
|
|
|
|
/* 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
|
2012-10-24 14:44:34 +00:00
|
|
|
|
is needed because elsewhere we bail out on error or Android
|
|
|
|
|
provided ttyname_r prints an error message if used. */
|
2012-01-19 17:43:10 +00:00
|
|
|
|
tty = "/dev/tty";
|
|
|
|
|
# else
|
2004-12-11 15:48:00 +00:00
|
|
|
|
tty = ttyname (fd);
|
|
|
|
|
if (!tty)
|
2012-01-19 17:43:10 +00:00
|
|
|
|
return errno? errno : -1;
|
|
|
|
|
# endif
|
|
|
|
|
|
2004-12-11 15:48:00 +00:00
|
|
|
|
strncpy (buf, tty, buflen);
|
|
|
|
|
buf[buflen - 1] = '\0';
|
|
|
|
|
return (strlen (tty) >= buflen) ? ERANGE : 0;
|
2012-01-19 17:43:10 +00:00
|
|
|
|
#endif /*!HAVE_TTYNAME_R*/
|
2004-12-11 15:48:00 +00:00
|
|
|
|
}
|