changeset 16213:64bc9fe46ccd

isatty: Support for MSVC 9. * doc/posix-functions/isatty.texi: Mention the MSVC problem. * lib/isatty.c: Include <errno.h>, msvc-inval.h. (_isatty_nothrow): New function. (isatty): Use it instead of _isatty. (IsConsoleHandle): Add comment, from Paolo Bonzini. * lib/poll.c (IsConsoleHandle): Likewise. * lib/select.c (IsConsoleHandle): Likewise. * m4/isatty.m4 (gl_FUNC_ISATTY): Fix comment. Reported by Eli Zaretskii. (gl_PREREQ_ISATTY): New macro. * modules/isatty (Depends-on): Add msvc-inval. (configure.ac): Invoke gl_PREREQ_ISATTY.
author Bruno Haible <bruno@clisp.org>
date Tue, 03 Jan 2012 13:52:36 +0100
parents 41ea1c7c240a
children ec738d6aeef5
files ChangeLog doc/posix-functions/isatty.texi lib/isatty.c lib/poll.c lib/select.c m4/isatty.m4 modules/isatty
diffstat 7 files changed, 62 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2012-01-03  Bruno Haible  <bruno@clisp.org>
+
+	isatty: Support for MSVC 9.
+	* doc/posix-functions/isatty.texi: Mention the MSVC problem.
+	* lib/isatty.c: Include <errno.h>, msvc-inval.h.
+	(_isatty_nothrow): New function.
+	(isatty): Use it instead of _isatty.
+	(IsConsoleHandle): Add comment, from Paolo Bonzini.
+	* lib/poll.c (IsConsoleHandle): Likewise.
+	* lib/select.c (IsConsoleHandle): Likewise.
+	* m4/isatty.m4 (gl_FUNC_ISATTY): Fix comment. Reported by Eli Zaretskii.
+	(gl_PREREQ_ISATTY): New macro.
+	* modules/isatty (Depends-on): Add msvc-inval.
+	(configure.ac): Invoke gl_PREREQ_ISATTY.
+
 2012-01-03  Jim Meyering  <meyering@redhat.com>
 
 	maint.mk: remove temporary transition aid from over 1.5 years ago
--- a/doc/posix-functions/isatty.texi
+++ b/doc/posix-functions/isatty.texi
@@ -11,6 +11,9 @@
 @item
 On native Windows, this function also returns true for character devices such
 as @file{NUL}.
+@item
+This function crashes when invoked with invalid arguments on some platforms:
+MSVC 9.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -21,20 +21,49 @@
 
 /* This replacement is enabled on native Windows.  */
 
+#include <errno.h>
+
 /* Get declarations of the Win32 API functions.  */
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+#include "msvc-inval.h"
+
 /* Get _get_osfhandle().  */
 #include "msvc-nothrow.h"
 
+/* Optimized test whether a HANDLE refers to a console.
+   See <http://lists.gnu.org/archive/html/bug-gnulib/2009-08/msg00065.html>.  */
 #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
 
+#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static inline int
+_isatty_nothrow (int fd)
+{
+  int result;
+
+  TRY_MSVC_INVAL
+    {
+      result = _isatty (fd);
+    }
+  CATCH_MSVC_INVAL
+    {
+      result = -1;
+      errno = EBADF;
+    }
+  DONE_MSVC_INVAL;
+
+  return result;
+}
+#else
+# define _isatty_nothrow _isatty
+#endif
+
 int
 isatty (int fd)
 {
   /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.  */
-  if (_isatty (fd))
+  if (_isatty_nothrow (fd))
     {
       HANDLE h = (HANDLE) _get_osfhandle (fd);
       return IsConsoleHandle (h);
--- a/lib/poll.c
+++ b/lib/poll.c
@@ -71,6 +71,8 @@
 
 #ifdef WIN32_NATIVE
 
+/* Optimized test whether a HANDLE refers to a console.
+   See <http://lists.gnu.org/archive/html/bug-gnulib/2009-08/msg00065.html>.  */
 #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
 
 static BOOL
--- a/lib/select.c
+++ b/lib/select.c
@@ -78,6 +78,8 @@
 #define PIPE_BUF        512
 #endif
 
+/* Optimized test whether a HANDLE refers to a console.
+   See <http://lists.gnu.org/archive/html/bug-gnulib/2009-08/msg00065.html>.  */
 #define IsConsoleHandle(h) (((long) (h) & 3) == 3)
 
 static BOOL
--- a/m4/isatty.m4
+++ b/m4/isatty.m4
@@ -1,4 +1,4 @@
-# isatty.m4 serial 1
+# isatty.m4 serial 2
 dnl Copyright (C) 2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,9 +8,14 @@
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
-  dnl On native Windows, the system's isatty() returns true for pipes and
-  dnl for the NUL device.
+  dnl On native Windows, the system's isatty(), defined as an alias of _isatty()
+  dnl in the "oldnames" library, returns true for the NUL device.
   case $host_os in
     mingw*) REPLACE_ISATTY=1 ;;
   esac
 ])
+
+# Prerequisites of lib/isatty.c.
+AC_DEFUN([gl_PREREQ_ISATTY], [
+  AC_REQUIRE([AC_C_INLINE])
+])
--- a/modules/isatty
+++ b/modules/isatty
@@ -7,12 +7,14 @@
 
 Depends-on:
 unistd
+msvc-inval      [test $REPLACE_ISATTY = 1]
 msvc-nothrow    [test $REPLACE_ISATTY = 1]
 
 configure.ac:
 gl_FUNC_ISATTY
 if test $REPLACE_ISATTY = 1; then
   AC_LIBOBJ([isatty])
+  gl_PREREQ_ISATTY
 fi
 gl_UNISTD_MODULE_INDICATOR([isatty])