New module 'ptsname'.
authorBruno Haible <bruno@clisp.org>
Sun, 21 Mar 2010 15:17:22 +0000 (16:17 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Mar 2010 15:17:22 +0000 (16:17 +0100)
ChangeLog
config/srclist.txt
doc/posix-functions/ptsname.texi
lib/ptsname.c [new file with mode: 0644]
lib/stdlib.in.h
m4/ptsname.m4 [new file with mode: 0644]
m4/stdlib_h.m4
modules/ptsname [new file with mode: 0644]
modules/stdlib
tests/test-stdlib-c++.cc

index 87398a8..2a87f09 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 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'.
        * modules/ttyname_r-tests: New file.
        * tests/test-ttyname_r.c: New file.
index f46158b..a91eb0f 100644 (file)
@@ -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
index 54f1ec3..5e1a15a 100644 (file)
@@ -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 (file)
index 0000000..a62ef21
--- /dev/null
@@ -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;
+}
index cb55a52..60c10bd 100644 (file)
@@ -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 (file)
index 0000000..7771763
--- /dev/null
@@ -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], [
+  :
+])
index 77344bd..85efb6e 100644 (file)
@@ -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 <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 @@ 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 (file)
index 0000000..d6ae046
--- /dev/null
@@ -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
index 6f9959f..663fc72 100644 (file)
@@ -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' \
index ef82fdd..d1e36ab 100644 (file)
@@ -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