summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ttyname_r.c20
-rw-r--r--lib/unistd.in.h15
2 files changed, 32 insertions, 3 deletions
diff --git a/lib/ttyname_r.c b/lib/ttyname_r.c
index dc8b923f1b..d3e58eb21d 100644
--- a/lib/ttyname_r.c
+++ b/lib/ttyname_r.c
@@ -22,12 +22,30 @@
#include <unistd.h>
#include <errno.h>
+#include <limits.h>
#include <string.h>
int
ttyname_r (int fd, char *buf, size_t buflen)
+#undef ttyname_r
{
-#if HAVE_TTYNAME
+ /* When ttyname_r exists and works, use it.
+ But on Solaris 10, ttyname_r is broken: it returns NULL in situations
+ when ttyname finds the result. */
+#if HAVE_TTYNAME_R && !defined __sun
+ /* This code is multithread-safe. */
+ char *name = ttyname_r (fd, buf, buflen <= INT_MAX ? buflen : INT_MAX);
+ if (name == NULL)
+ return errno;
+ if (name != buf)
+ {
+ size_t namelen = strlen (name) + 1;
+ if (namelen > buflen)
+ return ERANGE;
+ memmove (buf, name, namelen);
+ }
+ return 0;
+#elif HAVE_TTYNAME
/* Note: This is not multithread-safe. */
char *name;
size_t namelen;
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 7302d6d3e1..b1f07434ff 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -1164,12 +1164,23 @@ _GL_WARN_ON_USE (symlinkat, "symlinkat is not portable - "
#if @GNULIB_TTYNAME_R@
/* Store at most BUFLEN characters of the pathname of the terminal FD is
open on in BUF. Return 0 on success, otherwise an error number. */
-# if !@HAVE_TTYNAME_R@
+# if @REPLACE_TTYNAME_R@
+# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+# undef ttyname_r
+# define ttyname_r rpl_ttyname_r
+# endif
+_GL_FUNCDECL_RPL (ttyname_r, int,
+ (int fd, char *buf, size_t buflen) _GL_ARG_NONNULL ((2)));
+_GL_CXXALIAS_RPL (ttyname_r, int,
+ (int fd, char *buf, size_t buflen));
+# else
+# if !@HAVE_TTYNAME_R@
_GL_FUNCDECL_SYS (ttyname_r, int,
(int fd, char *buf, size_t buflen) _GL_ARG_NONNULL ((2)));
-# endif
+# endif
_GL_CXXALIAS_SYS (ttyname_r, int,
(int fd, char *buf, size_t buflen));
+# endif
_GL_CXXALIASWARN (ttyname_r);
#elif defined GNULIB_POSIXCHECK
# undef ttyname_r