1 /* Detect write error on a stream.
2 Copyright (C) 2003-2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "fwriteerror.h"
28 do_fwriteerror (FILE *fp, bool ignore_ebadf)
30 /* State to allow multiple calls to fwriteerror (stdout). */
31 static bool stdout_closed = false;
38 /* If we are closing stdout, don't attempt to do it later again. */
43 1. test the error indicator of the stream,
44 2. flush the buffers both in userland and in the kernel, through fclose,
45 testing for error again. */
47 /* Clear errno, so that on non-POSIX systems the caller doesn't see a
48 wrong value of errno when we return -1. */
54 goto close_preserving_errno; /* errno is set here */
55 /* The stream had an error earlier, but its errno was lost. If the
56 error was not temporary, we can get the same errno by writing and
57 flushing one more byte. We can do so because at this point the
58 stream's contents is garbage anyway. */
59 if (fputc ('\0', fp) == EOF)
60 goto close_preserving_errno; /* errno is set here */
62 goto close_preserving_errno; /* errno is set here */
63 /* Give up on errno. */
65 goto close_preserving_errno;
70 /* We need an explicit fflush to tell whether some output was already
73 goto close_preserving_errno; /* errno is set here */
74 if (fclose (fp) && errno != EBADF)
75 return -1; /* errno is set here */
80 return -1; /* errno is set here */
85 close_preserving_errno:
86 /* There's an error. Nevertheless call fclose(fp), for consistency
87 with the other cases. */
89 int saved_errno = errno;
97 fwriteerror (FILE *fp)
99 return do_fwriteerror (fp, false);
103 fwriteerror_no_ebadf (FILE *fp)
105 return do_fwriteerror (fp, true);
111 /* Name of a file on which writing fails. On systems without /dev/full,
112 you can choose a filename on a full filesystem. */
113 #define UNWRITABLE_FILE "/dev/full"
126 static char dummy[8193];
129 for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
131 size_t size = sizes[i];
133 for (j = 0; j < 2; j++)
135 /* Run a test depending on i and j:
136 Write size bytes and then calls fflush if j==1. */
137 FILE *stream = fopen (UNWRITABLE_FILE, "w");
141 fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
145 fwrite (dummy, 347, 1, stream);
146 fwrite (dummy, size - 347, 1, stream);
150 if (fwriteerror (stream) == -1)
153 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
157 fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",