Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/fwriteerror.c @ 9309:bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 07 Oct 2007 19:14:58 +0200 (2007-10-07) |
parents | de85bc61c2dc |
children | 080eba3c1d32 |
rev | line source |
---|---|
4709 | 1 /* Detect write error on a stream. |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
2 Copyright (C) 2003-2006 Free Software Foundation, Inc. |
4709 | 3 Written by Bruno Haible <bruno@clisp.org>, 2003. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7388
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
4709 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7388
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7388
diff
changeset
|
8 (at your option) any later version. |
4709 | 9 |
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. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7388
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4709 | 17 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6747
diff
changeset
|
18 #include <config.h> |
4709 | 19 |
20 /* Specification. */ | |
21 #include "fwriteerror.h" | |
22 | |
23 #include <errno.h> | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
24 #include <stdbool.h> |
4709 | 25 |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
26 static int |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
27 do_fwriteerror (FILE *fp, bool ignore_ebadf) |
4709 | 28 { |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
29 /* State to allow multiple calls to fwriteerror (stdout). */ |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
30 static bool stdout_closed = false; |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
31 |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
32 if (fp == stdout) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
33 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
34 if (stdout_closed) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
35 return 0; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
36 |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
37 /* If we are closing stdout, don't attempt to do it later again. */ |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
38 stdout_closed = true; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
39 } |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
40 |
4709 | 41 /* Need to |
42 1. test the error indicator of the stream, | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
43 2. flush the buffers both in userland and in the kernel, through fclose, |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
44 testing for error again. */ |
4709 | 45 |
46 /* Clear errno, so that on non-POSIX systems the caller doesn't see a | |
47 wrong value of errno when we return -1. */ | |
48 errno = 0; | |
49 | |
50 if (ferror (fp)) | |
51 { | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
52 if (fflush (fp)) |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
53 goto close_preserving_errno; /* errno is set here */ |
4709 | 54 /* The stream had an error earlier, but its errno was lost. If the |
55 error was not temporary, we can get the same errno by writing and | |
56 flushing one more byte. We can do so because at this point the | |
57 stream's contents is garbage anyway. */ | |
58 if (fputc ('\0', fp) == EOF) | |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
59 goto close_preserving_errno; /* errno is set here */ |
4709 | 60 if (fflush (fp)) |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
61 goto close_preserving_errno; /* errno is set here */ |
4709 | 62 /* Give up on errno. */ |
63 errno = 0; | |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
64 goto close_preserving_errno; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
65 } |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
66 |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
67 if (ignore_ebadf) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
68 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
69 /* We need an explicit fflush to tell whether some output was already |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
70 done on FP. */ |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
71 if (fflush (fp)) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
72 goto close_preserving_errno; /* errno is set here */ |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
73 if (fclose (fp) && errno != EBADF) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
74 return -1; /* errno is set here */ |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
75 } |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
76 else |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
77 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
78 if (fclose (fp)) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
79 return -1; /* errno is set here */ |
4709 | 80 } |
81 | |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
82 return 0; |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
83 |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
84 close_preserving_errno: |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
85 /* There's an error. Nevertheless call fclose(fp), for consistency |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
86 with the other cases. */ |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
87 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
88 int saved_errno = errno; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
89 fclose (fp); |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
90 errno = saved_errno; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
91 return -1; |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
92 } |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
93 } |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
94 |
7388
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
95 int |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
96 fwriteerror (FILE *fp) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
97 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
98 return do_fwriteerror (fp, false); |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
99 } |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
100 |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
101 int |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
102 fwriteerror_no_ebadf (FILE *fp) |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
103 { |
de85bc61c2dc
New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
104 return do_fwriteerror (fp, true); |
4709 | 105 } |
106 | |
107 | |
108 #if TEST | |
109 | |
110 /* Name of a file on which writing fails. On systems without /dev/full, | |
111 you can choose a filename on a full filesystem. */ | |
112 #define UNWRITABLE_FILE "/dev/full" | |
113 | |
114 int | |
115 main () | |
116 { | |
117 static int sizes[] = | |
118 { | |
119 511, 512, 513, | |
120 1023, 1024, 1025, | |
121 2047, 2048, 2049, | |
122 4095, 4096, 4097, | |
123 8191, 8192, 8193 | |
124 }; | |
125 static char dummy[8193]; | |
126 unsigned int i, j; | |
127 | |
128 for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) | |
129 { | |
130 size_t size = sizes[i]; | |
131 | |
132 for (j = 0; j < 2; j++) | |
133 { | |
134 /* Run a test depending on i and j: | |
135 Write size bytes and then calls fflush if j==1. */ | |
136 FILE *stream = fopen (UNWRITABLE_FILE, "w"); | |
137 | |
138 if (stream == NULL) | |
139 { | |
140 fprintf (stderr, "Test %u:%u: could not open file\n", i, j); | |
141 continue; | |
142 } | |
143 | |
144 fwrite (dummy, 347, 1, stream); | |
145 fwrite (dummy, size - 347, 1, stream); | |
146 if (j) | |
147 fflush (stream); | |
148 | |
149 if (fwriteerror (stream) == -1) | |
150 { | |
151 if (errno != ENOSPC) | |
152 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n", | |
153 i, j, errno); | |
154 } | |
155 else | |
156 fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n", | |
157 i, j); | |
158 } | |
159 } | |
160 | |
161 return 0; | |
162 } | |
163 | |
164 #endif |