New module 'ttyname_r'.
authorBruno Haible <bruno@clisp.org>
Sun, 21 Mar 2010 13:38:25 +0000 (14:38 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Mar 2010 13:38:25 +0000 (14:38 +0100)
ChangeLog
doc/posix-functions/ttyname_r.texi
lib/ttyname_r.c [new file with mode: 0644]
lib/unistd.in.h
m4/ttyname_r.m4 [new file with mode: 0644]
m4/unistd_h.m4
modules/ttyname_r [new file with mode: 0644]
modules/unistd
tests/test-unistd-c++.cc

index d662425..de4cdb8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2010-03-21  Bruno Haible  <bruno@clisp.org>
+
+       New module 'ttyname_r'.
+       * lib/ttyname_r.c: New file.
+       * m4/ttyname_r.m4: New file.
+       * modules/ttyname_r: New file.
+       * lib/unistd.in.h (ttyname_r): New declaration.
+       * m4/unistd_h.m4 (gl_UNISTD_H): Check whether ttyname_r is declared.
+       (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_TTYNAME_R, HAVE_TTYNAME_R.
+       * modules/unistd (Makefile.am): Substitute GNULIB_TTYNAME_R,
+       HAVE_TTYNAME_R.
+       * tests/test-unistd-c++.cc: Check GNULIB_NAMESPACE::ttyname_r.
+       * doc/posix-functions/ttyname_r.texi: Mention the new module.
+
 2010-03-20  Bruno Haible  <bruno@clisp.org>
 
        signal: Undefine macro definitions in C++ mode.
index 16425ba..ee25ad6 100644 (file)
@@ -4,15 +4,14 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/ttyname_r.html}
 
-Gnulib module: ---
+Gnulib module: ttyname_r
 
 Portability problems fixed by Gnulib:
 @itemize
+This function is missing on some platforms:
+NetBSD 3.0, mingw, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-NetBSD 3.0, mingw, BeOS.
 @end itemize
diff --git a/lib/ttyname_r.c b/lib/ttyname_r.c
new file mode 100644 (file)
index 0000000..dc8b923
--- /dev/null
@@ -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
+}
index b74484a..c1732b8 100644 (file)
@@ -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)
diff --git a/m4/ttyname_r.m4 b/m4/ttyname_r.m4
new file mode 100644 (file)
index 0000000..54a8676
--- /dev/null
@@ -0,0 +1,22 @@
+# ttyname_r.m4 serial 1
+dnl Copyright (C) 2010 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_TTYNAME_R],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+
+  AC_CHECK_FUNCS([ttyname_r])
+  if test $ac_cv_func_ttyname_r = no; then
+    HAVE_TTYNAME_R=0
+    AC_LIBOBJ([ttyname_r])
+    gl_PREREQ_TTYNAME_R
+  fi
+])
+
+# Prerequisites of lib/ttyname_r.c.
+AC_DEFUN([gl_PREREQ_TTYNAME_R], [
+  AC_CHECK_FUNCS([ttyname])
+])
index d7c5528..61c5f33 100644 (file)
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 41
+# unistd_h.m4 serial 42
 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -39,7 +39,7 @@ AC_DEFUN([gl_UNISTD_H],
     fsync ftruncate getcwd getdomainname getdtablesize getgroups
     gethostname getlogin getlogin_r getpagesize getusershell setusershell
     endusershell lchown link linkat lseek pipe2 pread readlink readlinkat
-    rmdir sleep symlink symlinkat unlink unlinkat usleep])
+    rmdir sleep symlink symlinkat ttyname_r unlink unlinkat usleep])
 ])
 
 AC_DEFUN([gl_UNISTD_MODULE_INDICATOR],
@@ -85,6 +85,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_SLEEP=0;            AC_SUBST([GNULIB_SLEEP])
   GNULIB_SYMLINK=0;          AC_SUBST([GNULIB_SYMLINK])
   GNULIB_SYMLINKAT=0;        AC_SUBST([GNULIB_SYMLINKAT])
+  GNULIB_TTYNAME_R=0;        AC_SUBST([GNULIB_TTYNAME_R])
   GNULIB_UNISTD_H_GETOPT=0;  AC_SUBST([GNULIB_UNISTD_H_GETOPT])
   GNULIB_UNISTD_H_SIGPIPE=0; AC_SUBST([GNULIB_UNISTD_H_SIGPIPE])
   GNULIB_UNLINK=0;           AC_SUBST([GNULIB_UNLINK])
@@ -116,13 +117,14 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_SLEEP=1;           AC_SUBST([HAVE_SLEEP])
   HAVE_SYMLINK=1;         AC_SUBST([HAVE_SYMLINK])
   HAVE_SYMLINKAT=1;       AC_SUBST([HAVE_SYMLINKAT])
+  HAVE_TTYNAME_R=1;       AC_SUBST([HAVE_TTYNAME_R])
+  HAVE_UNLINKAT=1;        AC_SUBST([HAVE_UNLINKAT])
+  HAVE_USLEEP=1;          AC_SUBST([HAVE_USLEEP])
   HAVE_DECL_ENVIRON=1;    AC_SUBST([HAVE_DECL_ENVIRON])
   HAVE_DECL_GETLOGIN_R=1; AC_SUBST([HAVE_DECL_GETLOGIN_R])
   HAVE_DECL_GETUSERSHELL=1; AC_SUBST([HAVE_DECL_GETUSERSHELL])
   HAVE_OS_H=0;            AC_SUBST([HAVE_OS_H])
   HAVE_SYS_PARAM_H=0;     AC_SUBST([HAVE_SYS_PARAM_H])
-  HAVE_UNLINKAT=1;        AC_SUBST([HAVE_UNLINKAT])
-  HAVE_USLEEP=1;          AC_SUBST([HAVE_USLEEP])
   REPLACE_CHOWN=0;        AC_SUBST([REPLACE_CHOWN])
   REPLACE_CLOSE=0;        AC_SUBST([REPLACE_CLOSE])
   REPLACE_DUP=0;          AC_SUBST([REPLACE_DUP])
diff --git a/modules/ttyname_r b/modules/ttyname_r
new file mode 100644 (file)
index 0000000..1e74839
--- /dev/null
@@ -0,0 +1,24 @@
+Description:
+ttyname_r() function: Determine name of a terminal.
+
+Files:
+lib/ttyname_r.c
+m4/ttyname_r.m4
+
+Depends-on:
+unistd
+
+configure.ac:
+gl_FUNC_TTYNAME_R
+gl_UNISTD_MODULE_INDICATOR([ttyname_r])
+
+Makefile.am:
+
+Include:
+<unistd.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
index 8e2b33d..f4e16bf 100644 (file)
@@ -59,6 +59,7 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
              -e 's|@''GNULIB_SLEEP''@|$(GNULIB_SLEEP)|g' \
              -e 's|@''GNULIB_SYMLINK''@|$(GNULIB_SYMLINK)|g' \
              -e 's|@''GNULIB_SYMLINKAT''@|$(GNULIB_SYMLINKAT)|g' \
+             -e 's|@''GNULIB_TTYNAME_R''@|$(GNULIB_TTYNAME_R)|g' \
              -e 's|@''GNULIB_UNISTD_H_GETOPT''@|$(GNULIB_UNISTD_H_GETOPT)|g' \
              -e 's|@''GNULIB_UNISTD_H_SIGPIPE''@|$(GNULIB_UNISTD_H_SIGPIPE)|g' \
              -e 's|@''GNULIB_UNLINK''@|$(GNULIB_UNLINK)|g' \
@@ -90,6 +91,7 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
              -e 's|@''HAVE_SLEEP''@|$(HAVE_SLEEP)|g' \
              -e 's|@''HAVE_SYMLINK''@|$(HAVE_SYMLINK)|g' \
              -e 's|@''HAVE_SYMLINKAT''@|$(HAVE_SYMLINKAT)|g' \
+             -e 's|@''HAVE_TTYNAME_R''@|$(HAVE_TTYNAME_R)|g' \
              -e 's|@''HAVE_UNLINKAT''@|$(HAVE_UNLINKAT)|g' \
              -e 's|@''HAVE_USLEEP''@|$(HAVE_USLEEP)|g' \
              -e 's|@''HAVE_DECL_ENVIRON''@|$(HAVE_DECL_ENVIRON)|g' \
index 679ba85..f185e1b 100644 (file)
@@ -165,6 +165,11 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::symlinkat, int,
                  (char const *, int, char const *));
 #endif
 
+#if GNULIB_TTYNAME_R
+SIGNATURE_CHECK (GNULIB_NAMESPACE::ttyname_r, int,
+                 (int fd, char *buf, size_t buflen));
+#endif
+
 #if GNULIB_UNLINK
 SIGNATURE_CHECK (GNULIB_NAMESPACE::unlink, int, (char const *));
 #endif