Mercurial > hg > octave-kai > gnulib-hg
changeset 5581:c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
otherwise it cannot detect write failure reliably on NFS.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 17 Jan 2005 12:56:47 +0000 |
parents | 19f7545b3c2b |
children | f5538a5d1cc4 |
files | lib/ChangeLog lib/fwriteerror.c lib/fwriteerror.h |
diffstat | 3 files changed, 34 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,10 @@ +2005-01-06 Bruno Haible <bruno@clisp.org> + + * fwriteerror.h (fwriteerror): Change specification to include fclose. + * fwriteerror.c: Include <stdbool.h>. + (fwriteerror): At the end, close the file stream. Record whether + stdout was already closed. + 2004-05-27 Bruno Haible <bruno@clisp.org> * execute.c (environ): Declare if needed.
--- 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 <bruno@clisp.org>, 2003. This program is free software; you can redistribute it and/or modify @@ -24,24 +24,30 @@ #include "fwriteerror.h" #include <errno.h> +#include <stdbool.h> 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 @@ 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 @@ 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); } }
--- 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 <bruno@clisp.org>, 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 <stdio.h> -/* 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);