changeset 13025:89efc5fdcef3

New module 'ptsname'.
author Bruno Haible <bruno@clisp.org>
date Sun, 21 Mar 2010 16:17:22 +0100
parents 6270472b0df6
children 565eb3ed1c33
files ChangeLog config/srclist.txt doc/posix-functions/ptsname.texi lib/ptsname.c lib/stdlib.in.h m4/ptsname.m4 m4/stdlib_h.m4 modules/ptsname modules/stdlib tests/test-stdlib-c++.cc
diffstat 10 files changed, 192 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2010-03-21  Bruno Haible  <bruno@clisp.org>
+
+	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  <bruno@clisp.org>
 
 	Tests for module 'ttyname_r'.
--- a/config/srclist.txt
+++ b/config/srclist.txt
@@ -213,6 +213,7 @@
 #$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
--- 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
-@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
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
new file mode 100644
--- /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 <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <stdlib.h>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#ifdef _LIBC
+# include <paths.h>
+#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;
+}
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -349,6 +349,22 @@
 # 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)
new file mode 100644
--- /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 <stdlib.h> 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], [
+  :
+])
--- 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 @@
 # include <random.h>
 #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 @@
   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 @@
   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])
new file mode 100644
--- /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:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -39,6 +39,7 @@
 	      -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 @@
 	      -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' \
--- a/tests/test-stdlib-c++.cc
+++ b/tests/test-stdlib-c++.cc
@@ -72,6 +72,10 @@
 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