From 3b7b97813fbb7f37f07f2acb8e0b0106efa3bf93 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 21 Mar 2010 16:17:22 +0100 Subject: [PATCH] New module 'ptsname'. --- ChangeLog | 15 +++++++ config/srclist.txt | 1 + doc/posix-functions/ptsname.texi | 8 ++-- lib/ptsname.c | 94 ++++++++++++++++++++++++++++++++++++++++ lib/stdlib.in.h | 16 +++++++ m4/ptsname.m4 | 25 +++++++++++ m4/stdlib_h.m4 | 6 ++- modules/ptsname | 26 +++++++++++ modules/stdlib | 2 + tests/test-stdlib-c++.cc | 4 ++ 10 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 lib/ptsname.c create mode 100644 m4/ptsname.m4 create mode 100644 modules/ptsname diff --git a/ChangeLog b/ChangeLog index 87398a8b7..2a87f0916 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2010-03-21 Bruno Haible + New module 'ptsname'. + * lib/ptsname.c: New file, from glibc with modifications. + * m4/ptsname.m4: New file. + * modules/ptsname: New file. + * lib/stdlib.in.h (ptsname): New declaration. + * m4/stdlib_h.m4 (gl_STDLIB_H): Check whether ptsname is declared. + (gl_STDLIB_H_DEFAULTS): Initialize GNULIB_PTSNAME, HAVE_PTSNAME. + * modules/stdlib (Makefile.am): Substitute GNULIB_PTSNAME, + HAVE_PTSNAME. + * doc/posix-functions/ptsname.texi: Mention the new module. + * tests/test-stdlib-c++.cc: Check GNULIB_NAMESPACE::ptsname. + * config/srclist.txt: Add ptsname.c (commented). + +2010-03-21 Bruno Haible + Tests for module 'ttyname_r'. * modules/ttyname_r-tests: New file. * tests/test-ttyname_r.c: New file. diff --git a/config/srclist.txt b/config/srclist.txt index f46158b68..a91eb0f57 100644 --- a/config/srclist.txt +++ b/config/srclist.txt @@ -213,6 +213,7 @@ $LIBCSRC/stdlib/strtoul.c lib gpl #$LIBCSRC/sysdeps/posix/euidaccess.c lib gpl #$LIBCSRC/sysdeps/posix/tempname.c lib gpl #$LIBCSRC/sysdeps/unix/bsd/poll.c lib gpl +#$LIBCSRC/sysdeps/unix/bsd/ptsname.c lib gpl #$LIBCSRC/sysdeps/unix/dirfd.c lib gpl #$LIBCSRC/sysdeps/unix/rmdir.c lib gpl #$LIBCSRC/time/strftime.c lib gpl diff --git a/doc/posix-functions/ptsname.texi b/doc/posix-functions/ptsname.texi index 54f1ec33a..5e1a15a76 100644 --- a/doc/posix-functions/ptsname.texi +++ b/doc/posix-functions/ptsname.texi @@ -4,15 +4,15 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/ptsname.html} -Gnulib module: --- +Gnulib module: ptsname Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +MacOS X 10.3, OpenBSD 3.8, mingw, BeOS. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -MacOS X 10.3, OpenBSD 3.8, mingw, BeOS. @end itemize diff --git a/lib/ptsname.c b/lib/ptsname.c new file mode 100644 index 000000000..a62ef2104 --- /dev/null +++ b/lib/ptsname.c @@ -0,0 +1,94 @@ +/* Determine name of the slave side of a pseudo-terminal. + Copyright (C) 1998, 2002, 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 . */ + +#include + +#include + +#include +#include +#include +#include + +#ifdef _LIBC +# include +#else +# ifndef _PATH_TTY +# define _PATH_TTY "/dev/tty" +# endif +# ifndef _PATH_DEV +# define _PATH_DEV "/dev/" +# endif + +# define __set_errno(e) errno = (e) +# define __isatty isatty +# define __stat stat +# define __ttyname_r ttyname_r + +static int __ptsname_r (int fd, char *buf, size_t buflen); +#endif + + +/* Static buffer for `ptsname'. */ +static char buffer[sizeof (_PATH_TTY) + 2]; + + +/* Return the pathname of the pseudo terminal slave associated with + the master FD is open on, or NULL on errors. + The returned storage is good until the next call to this function. */ +char * +ptsname (int fd) +{ + return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer; +} + + +/* Store at most BUFLEN characters of the pathname of the slave pseudo + terminal associated with the master FD is open on in BUF. + Return 0 on success, otherwise an error number. */ +static int +__ptsname_r (int fd, char *buf, size_t buflen) +{ + int save_errno = errno; + struct stat st; + + if (buf == NULL) + { + __set_errno (EINVAL); + return EINVAL; + } + + if (!__isatty (fd)) + /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */ + return errno; + + if (buflen < strlen (_PATH_TTY) + 3) + { + __set_errno (ERANGE); + return ERANGE; + } + + if (__ttyname_r (fd, buf, buflen) != 0) + return errno; + + buf[sizeof (_PATH_DEV) - 1] = 't'; + + if (__stat (buf, &st) < 0) + return errno; + + __set_errno (save_errno); + return 0; +} diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h index cb55a5257..60c10bd03 100644 --- a/lib/stdlib.in.h +++ b/lib/stdlib.in.h @@ -349,6 +349,22 @@ _GL_WARN_ON_USE (mkstemps, "mkstemps is unportable - " # endif #endif +#if @GNULIB_PTSNAME@ +/* Return the pathname of the pseudo-terminal slave associated with + the master FD is open on, or NULL on errors. */ +# if !@HAVE_PTSNAME@ +_GL_FUNCDECL_SYS (ptsname, char *, (int fd)); +# endif +_GL_CXXALIAS_SYS (ptsname, char *, (int fd)); +_GL_CXXALIASWARN (ptsname); +#elif defined GNULIB_POSIXCHECK +# undef ptsname +# if HAVE_RAW_DECL_PTSNAME +_GL_WARN_ON_USE (ptsname, "ptsname is not portable - " + "use gnulib module ptsname for portability"); +# endif +#endif + #if @GNULIB_PUTENV@ # if @REPLACE_PUTENV@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/m4/ptsname.m4 b/m4/ptsname.m4 new file mode 100644 index 000000000..7771763f4 --- /dev/null +++ b/m4/ptsname.m4 @@ -0,0 +1,25 @@ +# ptsname.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_PTSNAME], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + + dnl Persuade glibc to declare ptsname(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_CHECK_FUNCS([ptsname]) + if test $ac_cv_func_ptsname = no; then + HAVE_PTSNAME=0 + AC_LIBOBJ([ptsname]) + gl_PREREQ_PTSNAME + fi +]) + +# Prerequisites of lib/ptsname.c. +AC_DEFUN([gl_PREREQ_PTSNAME], [ + : +]) diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4 index 77344bda8..85efb6e7c 100644 --- a/m4/stdlib_h.m4 +++ b/m4/stdlib_h.m4 @@ -1,4 +1,4 @@ -# stdlib_h.m4 serial 23 +# stdlib_h.m4 serial 24 dnl Copyright (C) 2007-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, @@ -34,7 +34,7 @@ AC_DEFUN([gl_STDLIB_H], # include #endif ]], [atoll canonicalize_file_name getloadavg getsubopt mkdtemp - mkostemp mkostemps mkstemp mkstemps random_r initstat_r srandom_r + mkostemp mkostemps mkstemp mkstemps ptsname random_r initstat_r srandom_r setstate_r realpath rpmatch setenv strtod strtoll strtoull unsetenv]) ]) @@ -60,6 +60,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS], GNULIB_MKOSTEMPS=0; AC_SUBST([GNULIB_MKOSTEMPS]) GNULIB_MKSTEMP=0; AC_SUBST([GNULIB_MKSTEMP]) GNULIB_MKSTEMPS=0; AC_SUBST([GNULIB_MKSTEMPS]) + GNULIB_PTSNAME=0; AC_SUBST([GNULIB_PTSNAME]) GNULIB_PUTENV=0; AC_SUBST([GNULIB_PUTENV]) GNULIB_RANDOM_R=0; AC_SUBST([GNULIB_RANDOM_R]) GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX]) @@ -81,6 +82,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS], HAVE_MKOSTEMP=1; AC_SUBST([HAVE_MKOSTEMP]) HAVE_MKOSTEMPS=1; AC_SUBST([HAVE_MKOSTEMPS]) HAVE_MKSTEMPS=1; AC_SUBST([HAVE_MKSTEMPS]) + HAVE_PTSNAME=1; AC_SUBST([HAVE_PTSNAME]) HAVE_RANDOM_R=1; AC_SUBST([HAVE_RANDOM_R]) HAVE_REALLOC_POSIX=1; AC_SUBST([HAVE_REALLOC_POSIX]) HAVE_REALPATH=1; AC_SUBST([HAVE_REALPATH]) diff --git a/modules/ptsname b/modules/ptsname new file mode 100644 index 000000000..d6ae04683 --- /dev/null +++ b/modules/ptsname @@ -0,0 +1,26 @@ +Description: +ptsname() function: Determine name of the slave side of a pseudo-terminal. + +Files: +lib/ptsname.c +m4/ptsname.m4 + +Depends-on: +stdlib +extensions +ttyname_r + +configure.ac: +gl_FUNC_PTSNAME +gl_STDLIB_MODULE_INDICATOR([ptsname]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/stdlib b/modules/stdlib index 6f9959f96..663fc72e5 100644 --- a/modules/stdlib +++ b/modules/stdlib @@ -39,6 +39,7 @@ stdlib.h: stdlib.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''GNULIB_MKOSTEMPS''@|$(GNULIB_MKOSTEMPS)|g' \ -e 's|@''GNULIB_MKSTEMP''@|$(GNULIB_MKSTEMP)|g' \ -e 's|@''GNULIB_MKSTEMPS''@|$(GNULIB_MKSTEMPS)|g' \ + -e 's|@''GNULIB_PTSNAME''@|$(GNULIB_PTSNAME)|g' \ -e 's|@''GNULIB_PUTENV''@|$(GNULIB_PUTENV)|g' \ -e 's|@''GNULIB_RANDOM_R''@|$(GNULIB_RANDOM_R)|g' \ -e 's|@''GNULIB_REALLOC_POSIX''@|$(GNULIB_REALLOC_POSIX)|g' \ @@ -59,6 +60,7 @@ stdlib.h: stdlib.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''HAVE_MKOSTEMP''@|$(HAVE_MKOSTEMP)|g' \ -e 's|@''HAVE_MKOSTEMPS''@|$(HAVE_MKOSTEMPS)|g' \ -e 's|@''HAVE_MKSTEMPS''@|$(HAVE_MKSTEMPS)|g' \ + -e 's|@''HAVE_PTSNAME''@|$(HAVE_PTSNAME)|g' \ -e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \ -e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \ -e 's|@''HAVE_REALLOC_POSIX''@|$(HAVE_REALLOC_POSIX)|g' \ diff --git a/tests/test-stdlib-c++.cc b/tests/test-stdlib-c++.cc index ef82fdd19..d1e36aba1 100644 --- a/tests/test-stdlib-c++.cc +++ b/tests/test-stdlib-c++.cc @@ -72,6 +72,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::mkstemp, int, (char *)); SIGNATURE_CHECK (GNULIB_NAMESPACE::mkstemps, int, (char *, int)); #endif +#if GNULIB_PTSNAME +SIGNATURE_CHECK (GNULIB_NAMESPACE::ptsname, char *, (int)); +#endif + #if GNULIB_PUTENV SIGNATURE_CHECK (GNULIB_NAMESPACE::putenv, int, (char *)); #endif -- 2.11.0