# HG changeset patch # User Bruno Haible # Date 1258914639 -3600 # Node ID 9d131fdf39036453c4cd0875d9bb9117011273dd # Parent 90584cfd31a4077a9eda25cfb5f9a3b43e1c0de2 error: account for the possibility of freopen (stdout). diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-11-22 Bruno Haible + + error: account for the possibility of freopen (stdout). + * lib/error.c: Include . + (flush_stdout): New function, extracted from error and error_at_line. + Determine stdout's fd dynamically. + (error, error_at_line): Invoke flush_stdout. + * m4/error.m4 (gl_PREREQ_ERROR): Require AC_C_INLINE. + * modules/error (Depends-on): Add unistd. + 2009-11-22 Bruno Haible diffseq: Add comment. diff --git a/lib/error.c b/lib/error.c --- a/lib/error.c +++ b/lib/error.c @@ -27,6 +27,7 @@ #include #include #include +#include #if !_LIBC && ENABLE_NLS # include "gettext.h" @@ -103,6 +104,31 @@ # endif /* HAVE_STRERROR_R || defined strerror_r */ #endif /* not _LIBC */ +static inline void +flush_stdout (void) +{ +#if !_LIBC && defined F_GETFL + int stdout_fd; + +# if GNULIB_FREOPEN_SAFER + /* Use of gnulib's freopen-safer module normally ensures that + fileno (stdout) == 1 + whenever stdout is open. */ + stdout_fd = STDOUT_FILENO; +# else + /* POSIX states that fileno (stdout) after fclose is unspecified. But in + practice it is not a problem, because stdout is statically allocated and + the fd of a FILE stream is stored as a field in its allocated memory. */ + stdout_fd = fileno (stdout); +# endif + /* 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)) +#endif + fflush (stdout); +} + static void print_errno_message (int errnum) { @@ -238,13 +264,7 @@ 0); #endif -#if !_LIBC && defined F_GETFL - /* 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 <= fcntl (1, F_GETFL)) -#endif - fflush (stdout); + flush_stdout (); #ifdef _LIBC _IO_flockfile (stderr); #endif @@ -303,13 +323,7 @@ 0); #endif -#if !_LIBC && defined F_GETFL - /* 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 <= fcntl (1, F_GETFL)) -#endif - fflush (stdout); + flush_stdout (); #ifdef _LIBC _IO_flockfile (stderr); #endif diff --git a/m4/error.m4 b/m4/error.m4 --- a/m4/error.m4 +++ b/m4/error.m4 @@ -1,6 +1,6 @@ -#serial 11 +#serial 12 -# Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004 Free Software +# Copyright (C) 1996-1998, 2001-2004, 2009 Free Software # Foundation, Inc. # # This file is free software; the Free Software Foundation @@ -18,5 +18,6 @@ AC_DEFUN([gl_PREREQ_ERROR], [ AC_REQUIRE([AC_FUNC_STRERROR_R]) + AC_REQUIRE([AC_C_INLINE]) : ]) diff --git a/modules/error b/modules/error --- a/modules/error +++ b/modules/error @@ -13,6 +13,7 @@ Depends-on: strerror +unistd configure.ac: gl_ERROR