dirent: Fix compilation error in C++ mode on OS/2 kLIBC.
[gnulib.git] / lib / fwriteerror.c
blobe01793a7ea4477e49fabedb2062cdf49dbf8b114
1 /* Detect write error on a stream.
2 Copyright (C) 2003-2006, 2008-2021 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "fwriteerror.h"
23 #include <errno.h>
24 #include <stdbool.h>
26 static int
27 do_fwriteerror (FILE *fp, bool ignore_ebadf)
29 /* State to allow multiple calls to fwriteerror (stdout). */
30 static bool stdout_closed = false;
32 if (fp == stdout)
34 if (stdout_closed)
35 return 0;
37 /* If we are closing stdout, don't attempt to do it later again. */
38 stdout_closed = true;
41 /* This function returns an error indication if there was a previous failure
42 or if fclose failed, with two exceptions:
43 - Ignore an fclose failure if there was no previous error, no data
44 remains to be flushed, and fclose failed with EBADF. That can
45 happen when a program like cp is invoked like this 'cp a b >&-'
46 (i.e., with standard output closed) and doesn't generate any
47 output (hence no previous error and nothing to be flushed).
48 - Ignore an fclose failure due to EPIPE. That can happen when a
49 program blocks or ignores SIGPIPE, and the output pipe or socket
50 has no readers now. The EPIPE tells us that we should stop writing
51 to this output. That's what we are doing anyway here.
53 Need to
54 1. test the error indicator of the stream,
55 2. flush the buffers both in userland and in the kernel, through fclose,
56 testing for error again. */
58 /* Clear errno, so that on non-POSIX systems the caller doesn't see a
59 wrong value of errno when we return -1. */
60 errno = 0;
62 if (ferror (fp))
64 if (fflush (fp))
65 goto close_preserving_errno; /* errno is set here */
66 /* The stream had an error earlier, but its errno was lost. If the
67 error was not temporary, we can get the same errno by writing and
68 flushing one more byte. We can do so because at this point the
69 stream's contents is garbage anyway. */
70 if (fputc ('\0', fp) == EOF)
71 goto close_preserving_errno; /* errno is set here */
72 if (fflush (fp))
73 goto close_preserving_errno; /* errno is set here */
74 /* Give up on errno. */
75 errno = 0;
76 goto close_preserving_errno;
79 if (ignore_ebadf)
81 /* We need an explicit fflush to tell whether some output was already
82 done on FP. */
83 if (fflush (fp))
84 goto close_preserving_errno; /* errno is set here */
85 if (fclose (fp) && errno != EBADF)
86 goto got_errno; /* errno is set here */
88 else
90 if (fclose (fp))
91 goto got_errno; /* errno is set here */
94 return 0;
96 close_preserving_errno:
97 /* There's an error. Nevertheless call fclose(fp), for consistency
98 with the other cases. */
100 int saved_errno = errno;
101 fclose (fp);
102 errno = saved_errno;
104 got_errno:
105 /* There's an error. Ignore EPIPE. */
106 if (errno == EPIPE)
107 return 0;
108 else
109 return -1;
113 fwriteerror (FILE *fp)
115 return do_fwriteerror (fp, false);
119 fwriteerror_no_ebadf (FILE *fp)
121 return do_fwriteerror (fp, true);
125 #if TEST
127 /* Name of a file on which writing fails. On systems without /dev/full,
128 you can choose a filename on a full file system. */
129 #define UNWRITABLE_FILE "/dev/full"
132 main ()
134 static int sizes[] =
136 511, 512, 513,
137 1023, 1024, 1025,
138 2047, 2048, 2049,
139 4095, 4096, 4097,
140 8191, 8192, 8193
142 static char dummy[8193];
143 unsigned int i, j;
145 for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
147 size_t size = sizes[i];
149 for (j = 0; j < 2; j++)
151 /* Run a test depending on i and j:
152 Write size bytes and then calls fflush if j==1. */
153 FILE *stream = fopen (UNWRITABLE_FILE, "w");
155 if (stream == NULL)
157 fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
158 continue;
161 fwrite (dummy, 347, 1, stream);
162 fwrite (dummy, size - 347, 1, stream);
163 if (j)
164 fflush (stream);
166 if (fwriteerror (stream) == -1)
168 if (errno != ENOSPC)
169 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
170 i, j, errno);
172 else
173 fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
174 i, j);
178 return 0;
181 #endif