+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.
+ * closeout.c: Update comments.
+
2005-01-06 Bruno Haible <bruno@clisp.org>
* strerror.c: Update from gnulib, with HAVE_STRERROR modifications.
/* closeout.c - close standard output
- Copyright (C) 1998-2003 Free Software Foundation, Inc.
+ Copyright (C) 1998-2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#define _(msgid) gettext (msgid)
/* Close standard output, exiting with status STATUS on failure.
- If a program writes *anything* to stdout, that program should `fflush'
+ If a program writes *anything* to stdout, that program should close
stdout and make sure that it succeeds before exiting. Otherwise,
suppose that you go to the extreme of checking the return status
of every function that does an explicit write to stdout. The last
the fclose(stdout) could still fail (due e.g., to a disk full error)
when it tries to write out that buffered data. Thus, you would be
left with an incomplete output file and the offending program would
- exit successfully.
-
- FIXME: note the fflush suggested above is implicit in the fclose
- we actually do below. Consider doing only the fflush and/or using
- setvbuf to inhibit buffering.
+ exit successfully. Even calling fflush is not always sufficient,
+ since some file systems (NFS and CODA) buffer written/flushed data
+ until an actual close call.
Besides, it's wasteful to check the return value from every call
that writes to stdout -- just let the internal stream state record
{
if (fwriteerror (stdout))
error (status, errno, "%s", _("write error"));
- /* We don't need to fclose (stdout). fwriteerror (stdout) == 0 guarantees
- that the implicit fclose (stdout) at program exit will succeed.
- This avoids a useless close(1) system call in the frequent case
- that no error occurred. */
}
/* Close standard output, exiting with status EXIT_FAILURE on failure. */
/* 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
#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
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;
}
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);
}
}
/* 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
(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.
#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);
+2005-01-06 Bruno Haible <bruno@clisp.org>
+
+ * write-csharp.c (msgdomain_write_csharp): Don't call fclose after
+ fwriteerror.
+ * write-java.c (msgdomain_write_java): Likewise.
+ * write-mo.c (msgdomain_write_mo): Likewise.
+ * write-po.c (msgdomain_list_print): Likewise.
+ * write-qt.c (msgdomain_write_qt): Likewise.
+ * write-resources.c (execute_writing_input): Likewise.
+ * write-tcl.c (msgdomain_write_tcl): Likewise.
+
2005-01-05 Bruno Haible <bruno@clisp.org>
* msgattrib.c (main): Update year in --version output.
/* Writing C# satellite assemblies.
- Copyright (C) 2003-2004 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
fclose (csharp_file);
goto quit5;
}
- fclose (csharp_file);
/* Make it possible to override the .dll location. This is
necessary for running the testsuite before "make install". */
/* Writing Java ResourceBundles.
- Copyright (C) 2001-2003 Free Software Foundation, Inc.
+ Copyright (C) 2001-2003, 2005 Free Software Foundation, Inc.
Written by Bruno Haible <haible@clisp.cons.org>, 2001.
This program is free software; you can redistribute it and/or modify
fclose (java_file);
goto quit5;
}
- fclose (java_file);
/* Compile the Java file to a .class file.
directory must be non-NULL, because when the -d option is omitted, the
/* Writing binary .mo files.
- Copyright (C) 1995-1998, 2000-2004 Free Software Foundation, Inc.
+ Copyright (C) 1995-1998, 2000-2005 Free Software Foundation, Inc.
Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, April 1995.
This program is free software; you can redistribute it and/or modify
if (fwriteerror (output_file))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
-
- if (output_file != stdout)
- fclose (output_file);
}
}
/* GNU gettext - internationalization aids
- Copyright (C) 1995-1998, 2000-2004 Free Software Foundation, Inc.
+ Copyright (C) 1995-1998, 2000-2005 Free Software Foundation, Inc.
This file was written by Peter Miller <millerp@canb.auug.org.au>
if (fwriteerror (fp))
po_error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
filename);
-
- if (fp != stdout)
- fclose (fp);
}
/* Writing Qt .qm files.
- 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
if (fwriteerror (output_file))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
-
- if (output_file != stdout)
- fclose (output_file);
}
}
/* Writing C# .resources files.
- 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
if (fwriteerror (fp))
error (EXIT_FAILURE, 0, _("error while writing to %s subprocess"),
progname);
- fclose (fp);
/* Remove zombie process from process list, and retrieve exit status. */
/* He we can ignore SIGPIPE because WriteResource either writes to a file
/* Writing tcl/msgcat .msg files.
- Copyright (C) 2002-2003 Free Software Foundation, Inc.
+ Copyright (C) 2002-2003, 2005 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2002.
This program is free software; you can redistribute it and/or modify
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
- fclose (output_file);
freesa (frobbed_locale_name);
}