From cf1c745c2a777e95ca4219019847426fec947738 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 17 Jan 2005 12:56:47 +0000 Subject: [PATCH] The fwriteerror() function now needs to fclose() the stream, otherwise it cannot detect write failure reliably on NFS. --- lib/ChangeLog | 7 +++++++ lib/fwriteerror.c | 29 +++++++++++++++++++---------- lib/fwriteerror.h | 16 ++++++++-------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 6b4623ecaa..3fe9f20874 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2005-01-06 Bruno Haible + + * fwriteerror.h (fwriteerror): Change specification to include fclose. + * fwriteerror.c: Include . + (fwriteerror): At the end, close the file stream. Record whether + stdout was already closed. + 2004-05-27 Bruno Haible * execute.c (environ): Declare if needed. diff --git a/lib/fwriteerror.c b/lib/fwriteerror.c index 364db86093..97fbed2617 100644 --- a/lib/fwriteerror.c +++ b/lib/fwriteerror.c @@ -1,5 +1,5 @@ /* Detect write error on a stream. - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003-2005 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify @@ -24,24 +24,30 @@ #include "fwriteerror.h" #include +#include int fwriteerror (FILE *fp) { + /* State to allow multiple calls to fwriteerror (stdout). */ + static bool stdout_closed = false; + + if (fp == stdout && stdout_closed) + return 0; + /* Need to 1. test the error indicator of the stream, - 2. flush the buffers (what fclose() would do), testing for error again. - We can equally well swap these steps; this leads to smaller code. */ + 2. flush the buffers both in userland and in the kernel, through fclose, + testing for error again. */ /* Clear errno, so that on non-POSIX systems the caller doesn't see a wrong value of errno when we return -1. */ errno = 0; - if (fflush (fp)) - return -1; /* errno is set here */ - if (ferror (fp)) { + if (fflush (fp)) + return -1; /* errno is set here */ /* The stream had an error earlier, but its errno was lost. If the error was not temporary, we can get the same errno by writing and flushing one more byte. We can do so because at this point the @@ -55,6 +61,13 @@ fwriteerror (FILE *fp) return -1; } + /* If we are closing stdout, don't attempt to do it later again. */ + if (fp == stdout) + stdout_closed = true; + + if (fclose (fp)) + return -1; /* errno is set here */ + return 0; } @@ -109,10 +122,6 @@ main () else fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n", i, j); - - if (fclose (stream)) - fprintf (stderr, "Test %u:%u: fclose failed, errno = %d\n", - i, j, errno); } } diff --git a/lib/fwriteerror.h b/lib/fwriteerror.h index 82cf5f18e3..5700237788 100644 --- a/lib/fwriteerror.h +++ b/lib/fwriteerror.h @@ -1,5 +1,5 @@ /* Detect write error on a stream. - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2005 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software; you can redistribute it and/or modify @@ -22,7 +22,7 @@ (a) Test the return value of every fwrite() or fprintf() call, and react immediately. (b) Just before fclose(), test the error indicator in the stream and - the return value of the final fflush() or fclose() call. + the return value of the final fclose() call. The benefit of (a) is that non file related errors (such that ENOMEM during fprintf) and temporary error conditions can be diagnosed accurately. @@ -40,12 +40,12 @@ #include -/* Write out the not yet written buffered contents of the stream FP, and then - test whether some error occurred on the stream FP. FP must be a stream - opened for writing. - Return 0 if no error occurred. In this case it can be assumed that - fclose (fp) will succeed. +/* Write out the not yet written buffered contents of the stream FP, close + the stream FP, and test whether some error occurred on the stream FP. + FP must be a stream opened for writing. + Return 0 if no error occurred and fclose (fp) succeeded. Return -1 and set errno if there was an error. The errno value will be 0 if the cause of the error cannot be determined. - */ + For any given stream FP other than stdout, fwriteerror (FP) may only be + called once. */ extern int fwriteerror (FILE *fp); -- 2.30.2