summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ttyname_r.c47
-rw-r--r--lib/unistd.in.h19
2 files changed, 66 insertions, 0 deletions
diff --git a/lib/ttyname_r.c b/lib/ttyname_r.c
new file mode 100644
index 0000000000..dc8b923f1b
--- /dev/null
+++ b/lib/ttyname_r.c
@@ -0,0 +1,47 @@
+/* Determine name of a terminal.
+
+ Copyright (C) 2010 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2010. */
+
+#include <config.h>
+
+#include <unistd.h>
+
+#include <errno.h>
+#include <string.h>
+
+int
+ttyname_r (int fd, char *buf, size_t buflen)
+{
+#if HAVE_TTYNAME
+ /* Note: This is not multithread-safe. */
+ char *name;
+ size_t namelen;
+
+ name = ttyname (fd);
+ if (name == NULL)
+ return errno;
+ namelen = strlen (name) + 1;
+ if (namelen > buflen)
+ return ERANGE;
+ memcpy (buf, name, namelen);
+ return 0;
+#else
+ /* Platforms like mingw: no ttys exist at all. */
+ return ENOTTY;
+#endif
+}
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index b74484a883..c1732b8426 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -1099,6 +1099,25 @@ _GL_WARN_ON_USE (symlinkat, "symlinkat is not portable - "
#endif
+#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@
+_GL_FUNCDECL_SYS (ttyname_r, int,
+ (int fd, char *buf, size_t buflen) _GL_ARG_NONNULL ((2)));
+# endif
+_GL_CXXALIAS_SYS (ttyname_r, int,
+ (int fd, char *buf, size_t buflen));
+_GL_CXXALIASWARN (ttyname_r);
+#elif defined GNULIB_POSIXCHECK
+# undef ttyname_r
+# if HAVE_RAW_DECL_TTYNAME_R
+_GL_WARN_ON_USE (ttyname_r, "ttyname_r is not portable - "
+ "use gnulib module ttyname_r for portability");
+# endif
+#endif
+
+
#if @GNULIB_UNLINK@
# if @REPLACE_UNLINK@
# if !(defined __cplusplus && defined GNULIB_NAMESPACE)