[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: new module 'isatty'
From: |
Bruno Haible |
Subject: |
Re: new module 'isatty' |
Date: |
Tue, 03 Jan 2012 14:03:11 +0100 |
User-agent: |
KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; ) |
Eli Zaretskii wrote:
> > HANDLE h = (HANDLE) _get_osfhandle (fd);
>
> Why not use intptr_t instead of HANDLE
Because the common way to write code for the Windows API is to use the
Windows types, not the ISO C 89 types.
> and get rid of the cast, both
> here and in IsConsoleHandle above?
Because that would yield warnings when you compile on mingw with "-Wall".
> > dnl On native Windows, the system's isatty() returns true for pipes and
> > dnl for the NUL device.
>
> This could perhaps mislead, because it is TRT to return false for
> pipes on Windows.
Yup, thanks for the hint. I had written that comment before executing
the tests.
> Also, I'd replace "NUL device" with a more accurate
> "all character devices such as the null device", similar to what you
> wrote in the manual.
Well, I tested also aux, com, com1, lpt, lpt1, prn, and none of these
resulted in a successful open() call. Only nul and con could be open()ed,
and only for nul was the value of _isatty() wrong. Tested on Windows XP
(with no printer connected). YMMV.
Thanks all for your comments. I'm applying this additional patch,
- to fix a test failure on MSVC 9,
- to fix the two wrong comments.
Bruno
2012-01-03 Bruno Haible <address@hidden>
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.
--- doc/posix-functions/isatty.texi.orig Tue Jan 3 13:47:47 2012
+++ doc/posix-functions/isatty.texi Tue Jan 3 13:02:34 2012
@@ -11,6 +11,9 @@
@item
On native Windows, this function also returns true for character devices such
as @file{NUL}.
address@hidden
+This function crashes when invoked with invalid arguments on some platforms:
+MSVC 9.
@end itemize
Portability problems not fixed by Gnulib:
--- lib/isatty.c.orig Tue Jan 3 13:47:47 2012
+++ lib/isatty.c Tue Jan 3 13:00:47 2012
@@ -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);
--- lib/poll.c.orig Tue Jan 3 13:47:47 2012
+++ lib/poll.c Tue Jan 3 13:01:24 2012
@@ -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
--- lib/select.c.orig Tue Jan 3 13:47:47 2012
+++ lib/select.c Tue Jan 3 13:01:42 2012
@@ -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
--- m4/isatty.m4.orig Tue Jan 3 13:47:47 2012
+++ m4/isatty.m4 Tue Jan 3 13:07:44 2012
@@ -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])
+])
--- modules/isatty.orig Tue Jan 3 13:47:47 2012
+++ modules/isatty Tue Jan 3 12:55:32 2012
@@ -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])