summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2011-11-07 15:26:08 -0700
committerEric Blake <eblake@redhat.com>2011-11-09 15:53:07 -0700
commit78232883c8c01a22307bb38793928f2b9a5a6159 (patch)
treeeb53e0effab97294bfc73767a34cc5270f5b99c0
parentc025e2c9516ad949e0b8c97aacc2dd811909d877 (diff)
downloadgnulib-78232883c8c01a22307bb38793928f2b9a5a6159.tar.gz
ptsname_r: new module
For now, this replacement focuses solely on compilation compatibility, and assumes that isatty() and ttyname_r() work on a master side pty; if this assumption fails, or if thread-safety is also required, then a later patch can follow the lead of strerror_r.c in wrapping the system ptsname() with a lock. * modules/ptsname_r: New module. * m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): New file. * lib/ptsname.c (__ptsname_r): Split... * lib/ptsname_r.c: ...into new file. * m4/stdlib_h.m4 (gl_STDLIB_H): Check for decl. (gl_STDLIB_H_DEFAULTS): Set witness defaults. * modules/stdlib (Makefile.am): Substitute witnesses. * lib/stdlib.in.h (ptsname_r): Declare it. * doc/glibc-functions/ptsname_r.texi (ptsname_r): Document it. * MODULES.html.sh (Misc): Likewise. * modules/ptsname (Depends-on): Alter dependency. * doc/posix-functions/ptsname.texi (ptsname): Mention new module. Signed-off-by: Eric Blake <eblake@redhat.com>
-rw-r--r--ChangeLog16
-rwxr-xr-xMODULES.html.sh1
-rw-r--r--doc/glibc-functions/ptsname_r.texi10
-rw-r--r--doc/posix-functions/ptsname.texi7
-rw-r--r--lib/ptsname.c69
-rw-r--r--lib/ptsname_r.c85
-rw-r--r--lib/stdlib.in.h17
-rw-r--r--m4/ptsname_r.m423
-rw-r--r--m4/stdlib_h.m48
-rw-r--r--modules/ptsname2
-rw-r--r--modules/ptsname_r30
-rw-r--r--modules/stdlib2
12 files changed, 194 insertions, 76 deletions
diff --git a/ChangeLog b/ChangeLog
index 83c2d12480..8aacfdaf4a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2011-11-09 Eric Blake <eblake@redhat.com>
+
+ ptsname_r: new module
+ * modules/ptsname_r: New module.
+ * m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): New file.
+ * lib/ptsname.c (__ptsname_r): Split...
+ * lib/ptsname_r.c: ...into new file.
+ * m4/stdlib_h.m4 (gl_STDLIB_H): Check for decl.
+ (gl_STDLIB_H_DEFAULTS): Set witness defaults.
+ * modules/stdlib (Makefile.am): Substitute witnesses.
+ * lib/stdlib.in.h (ptsname_r): Declare it.
+ * doc/glibc-functions/ptsname_r.texi (ptsname_r): Document it.
+ * MODULES.html.sh (Misc): Likewise.
+ * modules/ptsname (Depends-on): Alter dependency.
+ * doc/posix-functions/ptsname.texi (ptsname): Mention new module.
+
2011-11-09 Jim Meyering <meyering@redhat.com>
announce-gen: be more concise when there's only one URL+tarball
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 34ca5cc3d7..bc8a1a6a68 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -3447,6 +3447,7 @@ func_all_modules ()
func_module physmem
func_module posixver
func_module progname
+ func_module ptsname_r
func_module pty
func_module quotearg
func_module quote
diff --git a/doc/glibc-functions/ptsname_r.texi b/doc/glibc-functions/ptsname_r.texi
index cbaa2d31a7..986e4910f4 100644
--- a/doc/glibc-functions/ptsname_r.texi
+++ b/doc/glibc-functions/ptsname_r.texi
@@ -2,15 +2,17 @@
@subsection @code{ptsname_r}
@findex ptsname_r
-Gnulib module: ---
+Gnulib module: ptsname_r
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8,
+AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 11 2010-11, Cygwin 1.7.9, mingw,
+MSVC 9, BeOS.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
-@item
-This function is missing on some platforms:
-MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 11 2010-11, Cygwin, mingw, MSVC 9, BeOS.
@end itemize
diff --git a/doc/posix-functions/ptsname.texi b/doc/posix-functions/ptsname.texi
index e585390c43..2584af860e 100644
--- a/doc/posix-functions/ptsname.texi
+++ b/doc/posix-functions/ptsname.texi
@@ -21,4 +21,11 @@ IRIX 5.3.
@item
On Solaris 11 2010-11, this function fails on all BSD-style @file{/dev/pty*}
device files.
+@item
+This function is not thread-safe on some platforms:
+Cygwin 1.7.9.
+Likewise, the gnulib replacement is not thread-safe.
@end itemize
+
+Note that the Gnulib module @code{ptsname_r} is a version of this
+function that is more likely to be thread-safe.
diff --git a/lib/ptsname.c b/lib/ptsname.c
index cd2cc5ee97..102a65c90a 100644
--- a/lib/ptsname.c
+++ b/lib/ptsname.c
@@ -18,30 +18,6 @@
#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[64];
@@ -52,48 +28,5 @@ static char buffer[64];
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;
- int err;
- 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;
- }
-
- err = __ttyname_r (fd, buf, buflen);
- if (err != 0)
- {
- __set_errno (err);
- return errno;
- }
-
- buf[sizeof (_PATH_DEV) - 1] = 't';
-
- if (__stat (buf, &st) < 0)
- return errno;
-
- __set_errno (save_errno);
- return 0;
+ return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
}
diff --git a/lib/ptsname_r.c b/lib/ptsname_r.c
new file mode 100644
index 0000000000..e7f2d5b9ef
--- /dev/null
+++ b/lib/ptsname_r.c
@@ -0,0 +1,85 @@
+/* Determine name of the slave side of a pseudo-terminal.
+ Copyright (C) 1998, 2002, 2010-2011 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
+# define __ptsname_r ptsname_r
+
+#endif
+
+
+/* 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. */
+int
+__ptsname_r (int fd, char *buf, size_t buflen)
+{
+ int save_errno = errno;
+ int err;
+ 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;
+ }
+
+ err = __ttyname_r (fd, buf, buflen);
+ if (err != 0)
+ {
+ __set_errno (err);
+ 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 1a8df492c9..009b180cf3 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -455,6 +455,23 @@ _GL_WARN_ON_USE (ptsname, "ptsname is not portable - "
# endif
#endif
+#if @GNULIB_PTSNAME_R@
+/* Set the pathname of the pseudo-terminal slave associated with
+ the master FD is open on and return 0, or set errno and return
+ non-zero on errors. */
+# if !@HAVE_PTSNAME_R@
+_GL_FUNCDECL_SYS (ptsname_r, int, (int fd, char *buf, size_t len));
+# endif
+_GL_CXXALIAS_SYS (ptsname_r, int, (int fd, char *buf, size_t len));
+_GL_CXXALIASWARN (ptsname_r);
+#elif defined GNULIB_POSIXCHECK
+# undef ptsname_r
+# if HAVE_RAW_DECL_PTSNAME_R
+_GL_WARN_ON_USE (ptsname_r, "ptsname_r is not portable - "
+ "use gnulib module ptsname_r for portability");
+# endif
+#endif
+
#if @GNULIB_PUTENV@
# if @REPLACE_PUTENV@
# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/m4/ptsname_r.m4 b/m4/ptsname_r.m4
new file mode 100644
index 0000000000..fb070998ed
--- /dev/null
+++ b/m4/ptsname_r.m4
@@ -0,0 +1,23 @@
+# ptsname_r.m4 serial 1
+dnl Copyright (C) 2010-2011 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_R],
+[
+ AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+
+ dnl Persuade glibc <stdlib.h> to declare ptsname_r().
+ AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+ AC_CHECK_FUNCS_ONCE([ptsname_r])
+ if test $ac_cv_func_ptsname_r = no; then
+ HAVE_PTSNAME_R=0
+ fi
+])
+
+# Prerequisites of lib/ptsname.c.
+AC_DEFUN([gl_PREREQ_PTSNAME_R], [
+ :
+])
diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4
index fbdba980eb..ebf7fb52a9 100644
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 37
+# stdlib_h.m4 serial 38
dnl Copyright (C) 2007-2011 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -21,8 +21,8 @@ AC_DEFUN([gl_STDLIB_H],
#endif
]], [_Exit atoll canonicalize_file_name getloadavg getsubopt grantpt
initstate_r mkdtemp mkostemp mkostemps mkstemp mkstemps posix_openpt
- ptsname random_r realpath rpmatch setenv setstate_r srandom_r strtod
- strtoll strtoull unlockpt unsetenv])
+ ptsname ptsname_r random_r realpath rpmatch setenv setstate_r srandom_r
+ strtod strtoll strtoull unlockpt unsetenv])
])
AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
@@ -52,6 +52,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
GNULIB_MKSTEMPS=0; AC_SUBST([GNULIB_MKSTEMPS])
GNULIB_POSIX_OPENPT=0; AC_SUBST([GNULIB_POSIX_OPENPT])
GNULIB_PTSNAME=0; AC_SUBST([GNULIB_PTSNAME])
+ GNULIB_PTSNAME_R=0; AC_SUBST([GNULIB_PTSNAME_R])
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])
@@ -79,6 +80,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
HAVE_MKSTEMPS=1; AC_SUBST([HAVE_MKSTEMPS])
HAVE_POSIX_OPENPT=1; AC_SUBST([HAVE_POSIX_OPENPT])
HAVE_PTSNAME=1; AC_SUBST([HAVE_PTSNAME])
+ HAVE_PTSNAME_R=1; AC_SUBST([HAVE_PTSNAME_R])
HAVE_RANDOM_H=1; AC_SUBST([HAVE_RANDOM_H])
HAVE_RANDOM_R=1; AC_SUBST([HAVE_RANDOM_R])
HAVE_REALPATH=1; AC_SUBST([HAVE_REALPATH])
diff --git a/modules/ptsname b/modules/ptsname
index 58560d63e5..f0ce25600d 100644
--- a/modules/ptsname
+++ b/modules/ptsname
@@ -8,7 +8,7 @@ m4/ptsname.m4
Depends-on:
stdlib
extensions
-ttyname_r [test $HAVE_PTSNAME = 0]
+ptsname_r [test $HAVE_PTSNAME = 0]
configure.ac:
gl_FUNC_PTSNAME
diff --git a/modules/ptsname_r b/modules/ptsname_r
new file mode 100644
index 0000000000..daf1d98125
--- /dev/null
+++ b/modules/ptsname_r
@@ -0,0 +1,30 @@
+Description:
+ptsname_r() function: Determine name of the slave side of a pseudo-terminal.
+
+Files:
+lib/ptsname_r.c
+m4/ptsname_r.m4
+
+Depends-on:
+stdlib
+extensions
+ttyname_r [test $HAVE_PTSNAME_R = 0]
+
+configure.ac:
+gl_FUNC_PTSNAME_R
+if test $HAVE_PTSNAME_R = 0; then
+ AC_LIBOBJ([ptsname_r])
+ gl_PREREQ_PTSNAME_R
+fi
+gl_STDLIB_MODULE_INDICATOR([ptsname_r])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Bruno Haible, Eric Blake
diff --git a/modules/stdlib b/modules/stdlib
index 707d5e5d7d..d46f66412a 100644
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -47,6 +47,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
-e 's/@''GNULIB_MKSTEMPS''@/$(GNULIB_MKSTEMPS)/g' \
-e 's/@''GNULIB_POSIX_OPENPT''@/$(GNULIB_POSIX_OPENPT)/g' \
-e 's/@''GNULIB_PTSNAME''@/$(GNULIB_PTSNAME)/g' \
+ -e 's/@''GNULIB_PTSNAME_R''@/$(GNULIB_PTSNAME_R)/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' \
@@ -74,6 +75,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
-e 's|@''HAVE_MKSTEMPS''@|$(HAVE_MKSTEMPS)|g' \
-e 's|@''HAVE_POSIX_OPENPT''@|$(HAVE_POSIX_OPENPT)|g' \
-e 's|@''HAVE_PTSNAME''@|$(HAVE_PTSNAME)|g' \
+ -e 's|@''HAVE_PTSNAME_R''@|$(HAVE_PTSNAME_R)|g' \
-e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \
-e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \
-e 's|@''HAVE_REALPATH''@|$(HAVE_REALPATH)|g' \