From 39065e3b61f9c359b6c01739be9bccea9643ea7c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 3 Jan 2012 13:52:36 +0100 Subject: [PATCH] isatty: Support for MSVC 9. * doc/posix-functions/isatty.texi: Mention the MSVC problem. * lib/isatty.c: Include , 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. --- ChangeLog | 15 +++++++++++++++ doc/posix-functions/isatty.texi | 3 +++ lib/isatty.c | 31 ++++++++++++++++++++++++++++++- lib/poll.c | 2 ++ lib/select.c | 2 ++ m4/isatty.m4 | 11 ++++++++--- modules/isatty | 2 ++ 7 files changed, 62 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index a105fc8f5..2e35ff71b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2012-01-03 Bruno Haible + + isatty: Support for MSVC 9. + * doc/posix-functions/isatty.texi: Mention the MSVC problem. + * lib/isatty.c: Include , 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 maint.mk: remove temporary transition aid from over 1.5 years ago diff --git a/doc/posix-functions/isatty.texi b/doc/posix-functions/isatty.texi index 4a5a0759b..2def9640b 100644 --- a/doc/posix-functions/isatty.texi +++ b/doc/posix-functions/isatty.texi @@ -11,6 +11,9 @@ Portability problems fixed by Gnulib: @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: diff --git a/lib/isatty.c b/lib/isatty.c index ed8be82ef..fa70001ee 100644 --- a/lib/isatty.c +++ b/lib/isatty.c @@ -21,20 +21,49 @@ /* This replacement is enabled on native Windows. */ +#include + /* Get declarations of the Win32 API functions. */ #define WIN32_LEAN_AND_MEAN #include +#include "msvc-inval.h" + /* Get _get_osfhandle(). */ #include "msvc-nothrow.h" +/* Optimized test whether a HANDLE refers to a console. + See . */ #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); diff --git a/lib/poll.c b/lib/poll.c index d795eaca3..bd19eaee0 100644 --- 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 . */ #define IsConsoleHandle(h) (((long) (h) & 3) == 3) static BOOL diff --git a/lib/select.c b/lib/select.c index 30a246660..390b6c221 100644 --- a/lib/select.c +++ b/lib/select.c @@ -78,6 +78,8 @@ typedef DWORD (WINAPI *PNtQueryInformationFile) #define PIPE_BUF 512 #endif +/* Optimized test whether a HANDLE refers to a console. + See . */ #define IsConsoleHandle(h) (((long) (h) & 3) == 3) static BOOL diff --git a/m4/isatty.m4 b/m4/isatty.m4 index 5d866ff61..6f81d18e3 100644 --- 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_DEFUN([gl_FUNC_ISATTY], [ 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]) +]) diff --git a/modules/isatty b/modules/isatty index 8d596eed7..e9aa0372d 100644 --- a/modules/isatty +++ b/modules/isatty @@ -7,12 +7,14 @@ m4/isatty.m4 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]) -- 2.11.0