From 116440ae18064c1495285a42767547b3ffa418d4 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 21 Mar 2010 14:38:25 +0100 Subject: [PATCH] New module 'ttyname_r'. --- ChangeLog | 14 ++++++++++++ doc/posix-functions/ttyname_r.texi | 7 +++--- lib/ttyname_r.c | 47 ++++++++++++++++++++++++++++++++++++++ lib/unistd.in.h | 19 +++++++++++++++ m4/ttyname_r.m4 | 22 ++++++++++++++++++ m4/unistd_h.m4 | 10 ++++---- modules/ttyname_r | 24 +++++++++++++++++++ modules/unistd | 2 ++ tests/test-unistd-c++.cc | 5 ++++ 9 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 lib/ttyname_r.c create mode 100644 m4/ttyname_r.m4 create mode 100644 modules/ttyname_r diff --git a/ChangeLog b/ChangeLog index d66242510..de4cdb842 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2010-03-21 Bruno Haible + + 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 signal: Undefine macro definitions in C++ mode. diff --git a/doc/posix-functions/ttyname_r.texi b/doc/posix-functions/ttyname_r.texi index 16425ba55..ee25ad618 100644 --- a/doc/posix-functions/ttyname_r.texi +++ b/doc/posix-functions/ttyname_r.texi @@ -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 index 000000000..dc8b923f1 --- /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 . */ + +/* Written by Bruno Haible , 2010. */ + +#include + +#include + +#include +#include + +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 b74484a88..c1732b842 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) diff --git a/m4/ttyname_r.m4 b/m4/ttyname_r.m4 new file mode 100644 index 000000000..54a867688 --- /dev/null +++ b/m4/ttyname_r.m4 @@ -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]) +]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 index d7c552896..61c5f3323 100644 --- a/m4/unistd_h.m4 +++ b/m4/unistd_h.m4 @@ -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 index 000000000..1e74839ef --- /dev/null +++ b/modules/ttyname_r @@ -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: + + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/unistd b/modules/unistd index 8e2b33d97..f4e16bf8f 100644 --- a/modules/unistd +++ b/modules/unistd @@ -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' \ diff --git a/tests/test-unistd-c++.cc b/tests/test-unistd-c++.cc index 679ba85b5..f185e1b9a 100644 --- a/tests/test-unistd-c++.cc +++ b/tests/test-unistd-c++.cc @@ -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 -- 2.11.0