bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

error: more reliable output on mingw


From: Bruno Haible
Subject: error: more reliable output on mingw
Date: Sun, 9 May 2010 12:03:06 +0200
User-agent: KMail/1.9.9

The 'error' module is careful not to call fflush (stdout) when it is not
safe - but not on mingw. This extends the code to work on mingw as well.


2010-05-09  Bruno Haible  <address@hidden>

        error: More reliable output on mingw.
        * lib/error.c: Include <windows.h>.
        (is_open): New function.
        (flush_stdout): Call it instead of fcntl, also if F_GETFL is not
        defined.

--- lib/error.c.orig    Sun May  9 11:56:07 2010
+++ lib/error.c Sun May  9 11:50:53 2010
@@ -88,6 +88,12 @@
 # include <fcntl.h>
 # include <unistd.h>
 
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+/* Get declarations of the Win32 API functions.  */
+#  define WIN32_LEAN_AND_MEAN
+#  include <windows.h>
+# endif
+
 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
 #  ifndef HAVE_DECL_STRERROR_R
 "this configure-time declaration test was not run"
@@ -104,10 +110,29 @@
 # endif /* HAVE_STRERROR_R || defined strerror_r */
 #endif  /* not _LIBC */
 
+#if !_LIBC
+/* Return non-zero if FD is open.  */
+static inline int
+is_open (int fd)
+{
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+  /* On Win32: The initial state of unassigned standard file descriptors is
+     that they are open but point to an INVALID_HANDLE_VALUE.  There is no
+     fcntl, and the gnulib replacement fcntl does not support F_GETFL.  */
+  return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
+# else
+#  ifndef F_GETFL
+#   error Please port fcntl to your platform
+#  endif
+  return 0 <= fcntl (fd, F_GETFL);
+# endif
+}
+#endif
+
 static inline void
 flush_stdout (void)
 {
-#if !_LIBC && defined F_GETFL
+#if !_LIBC
   int stdout_fd;
 
 # if GNULIB_FREOPEN_SAFER
@@ -124,7 +149,7 @@
   /* POSIX states that fflush (stdout) after fclose is unspecified; it
      is safe in glibc, but not on all other platforms.  fflush (NULL)
      is always defined, but too draconian.  */
-  if (0 <= stdout_fd && 0 <= fcntl (stdout_fd, F_GETFL))
+  if (0 <= stdout_fd && is_open (stdout_fd))
 #endif
     fflush (stdout);
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]