Eric Blake wrote: > That is ONLY possible if both: > stdin was closed > the user is not using freopen_safer > ... > I'd rather make the patch conditional on whether freopen_safer is also in use.
I see. How about this? 2009-11-22 Bruno Haible <br...@clisp.org> error: account for the possibility of freopen (stdout). * lib/error.c (error, error_at_line): Use fileno (stdout) instead of 1. *** lib/error.c.orig 2009-11-22 17:02:20.000000000 +0100 --- lib/error.c 2009-11-22 17:02:10.000000000 +0100 *************** *** 238,250 **** 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); #ifdef _LIBC _IO_flockfile (stderr); #endif --- 238,265 ---- 0); #endif + { #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 always. */ ! stdout_fd = 1; ! # 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); ! } ! #ifdef _LIBC _IO_flockfile (stderr); #endif *************** *** 303,315 **** 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); #ifdef _LIBC _IO_flockfile (stderr); #endif --- 318,345 ---- 0); #endif + { #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 always. */ ! stdout_fd = 1; ! # 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); ! } ! #ifdef _LIBC _IO_flockfile (stderr); #endif