+2003-09-14 Bruno Haible <bruno@clisp.org>
+
+ * Makefile.am (libgrt_a_SOURCES): Add fwriteerror.h, fwriteerror.c.
+ * Makefile.msvc (OBJECTS): Add fwriteerror.obj.
+ (fwriteerror.obj): New rule.
+ * Makefile.vms (OBJECTS): Add fwriteerror.obj.
+ (fwriteerror.obj): New rule.
+
2003-09-13 Bruno Haible <bruno@clisp.org>
* Makefile.am (libgrt_a_SOURCES): Add closeout.h, closeout.c.
../../gettext-tools/lib/closeout.h ../../gettext-tools/lib/closeout.c \
../../gettext-tools/lib/error.h ../../gettext-tools/lib/error.c \
../../gettext-tools/lib/exit.h \
+ ../../gettext-tools/lib/fwriteerror.h ../../gettext-tools/lib/fwriteerror.c \
../../gettext-tools/lib/getopt.h ../../gettext-tools/lib/getopt.c ../../gettext-tools/lib/getopt1.c \
../../gettext-tools/lib/pathmax.h \
../../gettext-tools/lib/progname.h ../../gettext-tools/lib/progname.c ../../gettext-tools/lib/progreloc.c \
SHELL = /bin/sh
-OBJECTS = basename.obj closeout.obj error.obj getopt.obj getopt1.obj progname.obj progreloc.obj relocatable.obj xmalloc.obj xstrdup.obj
+OBJECTS = basename.obj closeout.obj error.obj fwriteerror.obj getopt.obj getopt1.obj progname.obj progreloc.obj relocatable.obj xmalloc.obj xstrdup.obj
all : grt.lib
error.obj : ..\..\gettext-tools\lib\error.c
$(CC) $(INCLUDES) $(CFLAGS) -c ..\..\gettext-tools\lib\error.c
+fwriteerror.obj : ..\..\gettext-tools\lib\fwriteerror.c
+ $(CC) $(INCLUDES) $(CFLAGS) -c ..\..\gettext-tools\lib\fwriteerror.c
+
getopt.obj : ..\..\gettext-tools\lib\getopt.c
$(CC) $(INCLUDES) $(CFLAGS) -c ..\..\gettext-tools\lib\getopt.c
#### End of system configuration section. ####
-OBJECTS = basename.obj,closeout.obj,error.obj,getopt.obj,getopt1.obj,progname.obj,progreloc.obj,relocatable.obj,xmalloc.obj,xstrdup.obj
+OBJECTS = basename.obj,closeout.obj,error.obj,fwriteerror.obj,getopt.obj,getopt1.obj,progname.obj,progreloc.obj,relocatable.obj,xmalloc.obj,xstrdup.obj
all : grt.olb
write sys$output "Nothing else to be done for 'all'."
error.obj : [-.-.gettext-tools.lib]error.c
$(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) [-.-.gettext-tools.lib]error.c
+fwriteerror.obj : [-.-.gettext-tools.lib]fwriteerror.c
+ $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) [-.-.gettext-tools.lib]fwriteerror.c
+
getopt.obj : [-.-.gettext-tools.lib]getopt.c
$(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) [-.-.gettext-tools.lib]getopt.c
+2003-09-14 Bruno Haible <bruno@clisp.org>
+
+ * windows/gettextlib.def: Export fwriteerror.
+
2003-09-13 Bruno Haible <bruno@clisp.org>
* windows/gettextlib.def: Export close_stdout.
+2003-09-14 Bruno Haible <bruno@clisp.org>
+
+ * fwriteerror.h: New file.
+ * fwriteerror.c: New file.
+ * Makefile.am (libgettextlib_la_SOURCES): Add fwriteerror.h,
+ fwriteerror.c.
+ * Makefile.msvc (OBJECTS): Add fwriteerror.obj.
+ (fwriteerror.obj): New rule.
+ * Makefile.vms (OBJECTS): Add fwriteerror.obj.
+ (fwriteerror.obj): New rule.
+
2003-09-13 Bruno Haible <bruno@clisp.org>
* closeout.h: New file, from gnulib with modifications.
findprog.h findprog.c \
fstrcmp.h fstrcmp.c \
full-write.h full-write.c \
+ fwriteerror.h fwriteerror.c \
gcd.h gcd.c \
getopt.h getopt.c getopt1.c \
hash.h hash.c \
findprog.obj \
fstrcmp.obj \
full-write.obj \
+ fwriteerror.obj \
gcd.obj \
getopt.obj getopt1.obj \
hash.obj \
full-write.obj : full-write.c
$(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c full-write.c
+fwriteerror.obj : fwriteerror.c
+ $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c fwriteerror.c
+
gcd.obj : gcd.c
$(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c gcd.c
findprog.obj, \
fstrcmp.obj, \
full-write.obj, \
+ fwriteerror.obj, \
gcd.obj, \
getopt.obj, getopt1.obj, \
hash.obj, \
full-write.obj : full-write.c
$(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) full-write.c
+fwriteerror.obj : fwriteerror.c
+ $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) fwriteerror.c
+
gcd.obj : gcd.c
$(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) gcd.c
--- /dev/null
+/* Detect write error on a stream.
+ Copyright (C) 2003 Free Software Foundation, Inc.
+ Written by Bruno Haible <bruno@clisp.org>, 2003.
+
+ 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
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification. */
+#include "fwriteerror.h"
+
+#include <errno.h>
+
+int
+fwriteerror (FILE *fp)
+{
+ /* 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. */
+
+ /* 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))
+ {
+ /* 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
+ stream's contents is garbage anyway. */
+ if (fputc ('\0', fp) == EOF)
+ return -1; /* errno is set here */
+ if (fflush (fp))
+ return -1; /* errno is set here */
+ /* Give up on errno. */
+ errno = 0;
+ return -1;
+ }
+
+ return 0;
+}
+
+
+#if TEST
+
+/* Name of a file on which writing fails. On systems without /dev/full,
+ you can choose a filename on a full filesystem. */
+#define UNWRITABLE_FILE "/dev/full"
+
+int
+main ()
+{
+ static int sizes[] =
+ {
+ 511, 512, 513,
+ 1023, 1024, 1025,
+ 2047, 2048, 2049,
+ 4095, 4096, 4097,
+ 8191, 8192, 8193
+ };
+ static char dummy[8193];
+ unsigned int i, j;
+
+ for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
+ {
+ size_t size = sizes[i];
+
+ for (j = 0; j < 2; j++)
+ {
+ /* Run a test depending on i and j:
+ Write size bytes and then calls fflush if j==1. */
+ FILE *stream = fopen (UNWRITABLE_FILE, "w");
+
+ if (stream == NULL)
+ {
+ fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
+ continue;
+ }
+
+ fwrite (dummy, 347, 1, stream);
+ fwrite (dummy, size - 347, 1, stream);
+ if (j)
+ fflush (stream);
+
+ if (fwriteerror (stream) == -1)
+ {
+ if (errno != ENOSPC)
+ fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
+ i, j, errno);
+ }
+ 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);
+ }
+ }
+
+ return 0;
+}
+
+#endif
--- /dev/null
+/* Detect write error on a stream.
+ Copyright (C) 2003 Free Software Foundation, Inc.
+ Written by Bruno Haible <bruno@clisp.org>, 2003.
+
+ 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
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+/* There are two approaches for detecting a write error on a stream opened
+ for writing:
+
+ (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 benefit of (a) is that non file related errors (such that ENOMEM during
+ fprintf) and temporary error conditions can be diagnosed accurately.
+
+ A theoretical benefit of (a) is also that, on POSIX systems, in the case of
+ an ENOSPC error, errno is set and can be used by error() to provide a more
+ accurate error message. But in practice, this benefit is not big because
+ users can easily figure out by themselves why a file cannot be written to,
+ and furthermore the function fwriteerror() can provide errno as well.
+
+ The big drawback of (a) is extensive error checking code: Every function
+ which does stream output must return an error indicator.
+
+ This file provides support for (b). */
+
+#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.
+ 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.
+ */
+extern int fwriteerror (FILE *fp);
+2003-09-14 Bruno Haible <bruno@clisp.org>
+
+ * write-mo.c: Include fwriterror.h.
+ (msgdomain_write_mo): Use fwriteerror, to get right errno value in
+ error message about write error.
+ * write-po.c: Include fwriterror.h.
+ (msgdomain_list_print): Use fwriteerror, to get right errno value in
+ error message about write error.
+ * write-java.c: Include fwriterror.h.
+ (msgdomain_write_java): Use fwriteerror, to get right errno value in
+ error message about write error.
+ * write-tcl.c: Include fwriterror.h.
+ (msgdomain_write_tcl): Use fwriteerror, to get right errno value in
+ error message about write error.
+
2003-09-18 Bruno Haible <bruno@clisp.org>
* xgettext.c (from_current_source_encoding): Use multiline_error,
#include "po-charset.h"
#include "xmalloc.h"
#include "pathname.h"
+#include "fwriteerror.h"
#include "tmpdir.h"
#include "utf8-ucs4.h"
#include "gettext.h"
write_java_code (java_file, class_name, mlp, assume_java2);
- if (fflush (java_file) || ferror (java_file))
+ if (fwriteerror (java_file))
{
error (0, errno, _("error while writing \"%s\" file"), java_file_name);
fclose (java_file);
#include "format.h"
#include "xmalloc.h"
#include "binary-io.h"
+#include "fwriteerror.h"
#include "exit.h"
#include "gettext.h"
write_table (output_file, mlp);
/* Make sure nothing went wrong. */
- if (fflush (output_file) || ferror (output_file))
+ if (fwriteerror (output_file))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
#include "write-properties.h"
#include "xmalloc.h"
#include "strstr.h"
+#include "fwriteerror.h"
#include "exit.h"
#include "error-progname.h"
#include "error.h"
msgdomain_list_print_po (mdlp, fp, debug);
/* Make sure nothing went wrong. */
- if (fflush (fp) || ferror (fp))
+ if (fwriteerror (fp))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
filename);
#include "po-charset.h"
#include "xmalloc.h"
#include "pathname.h"
+#include "fwriteerror.h"
#include "exit.h"
#include "utf8-ucs4.h"
#include "gettext.h"
write_msg (output_file, mlp, frobbed_locale_name);
/* Make sure nothing went wrong. */
- if (fflush (output_file) || ferror (output_file))
+ if (fwriteerror (output_file))
error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
file_name);
find_in_path
fstrcmp
full_write
+fwriteerror
gcd
getopt_long
get_version