annotate lib/fwriteerror.c @ 16201:8250f2777afc

maint: update all copyright year number ranges Run "make update-copyright".
author Jim Meyering <meyering@redhat.com>
date Sun, 01 Jan 2012 10:04:58 +0100
parents 97fc9a21a8fb
children 18a38c9615f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Detect write error on a stream.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
2 Copyright (C) 2003-2006, 2008-2012 Free Software Foundation, Inc.
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6747
diff changeset
18 #include <config.h>
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "fwriteerror.h"
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
35 return 0;
7388
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
10492
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
41 /* This function returns an error indication if there was a previous failure
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
42 or if fclose failed, with two exceptions:
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
43 - Ignore an fclose failure if there was no previous error, no data
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
44 remains to be flushed, and fclose failed with EBADF. That can
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
45 happen when a program like cp is invoked like this `cp a b >&-'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
46 (i.e., with standard output closed) and doesn't generate any
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
47 output (hence no previous error and nothing to be flushed).
10492
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
48 - Ignore an fclose failure due to EPIPE. That can happen when a
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
49 program blocks or ignores SIGPIPE, and the output pipe or socket
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
50 has no readers now. The EPIPE tells us that we should stop writing
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
51 to this output. That's what we are doing anyway here.
10492
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
52
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
53 Need to
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 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
55 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
56 testing for error again. */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 /* Clear errno, so that on non-POSIX systems the caller doesn't see a
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 wrong value of errno when we return -1. */
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 errno = 0;
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (ferror (fp))
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
5581
c8676e66b5da The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents: 4709
diff changeset
64 if (fflush (fp))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
65 goto close_preserving_errno; /* errno is set here */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 /* The stream had an error earlier, but its errno was lost. If the
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
67 error was not temporary, we can get the same errno by writing and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
68 flushing one more byte. We can do so because at this point the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
69 stream's contents is garbage anyway. */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (fputc ('\0', fp) == EOF)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
71 goto close_preserving_errno; /* errno is set here */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 if (fflush (fp))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
73 goto close_preserving_errno; /* errno is set here */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* Give up on errno. */
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 errno = 0;
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
76 goto close_preserving_errno;
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
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
79 if (ignore_ebadf)
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
80 {
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
81 /* We need an explicit fflush to tell whether some output was already
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
82 done on FP. */
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
83 if (fflush (fp))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
84 goto close_preserving_errno; /* errno is set here */
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
85 if (fclose (fp) && errno != EBADF)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
86 goto got_errno; /* errno is set here */
7388
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 else
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
89 {
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
90 if (fclose (fp))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
91 goto got_errno; /* errno is set here */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 }
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
94 return 0;
5581
c8676e66b5da The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents: 4709
diff changeset
95
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
96 close_preserving_errno:
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
97 /* 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
98 with the other cases. */
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 int saved_errno = errno;
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
101 fclose (fp);
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
102 errno = saved_errno;
10492
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
103 }
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
104 got_errno:
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
105 /* There's an error. Ignore EPIPE. */
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
106 if (errno == EPIPE)
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
107 return 0;
080eba3c1d32 Ignore error EPIPE when closing a stream.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
108 else
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
109 return -1;
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
110 }
5581
c8676e66b5da The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents: 4709
diff changeset
111
7388
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
112 int
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
113 fwriteerror (FILE *fp)
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
114 {
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
115 return do_fwriteerror (fp, false);
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
116 }
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
117
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
118 int
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
119 fwriteerror_no_ebadf (FILE *fp)
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
120 {
de85bc61c2dc New function fwriteerror_no_ebadf.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
121 return do_fwriteerror (fp, true);
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 }
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 #if TEST
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 /* Name of a file on which writing fails. On systems without /dev/full,
12146
e076a06bae48 maint: prefer 'file system' over 'filesystem'
Eric Blake <ebb9@byu.net>
parents: 10492
diff changeset
128 you can choose a filename on a full file system. */
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 #define UNWRITABLE_FILE "/dev/full"
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 int
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 main ()
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 {
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 static int sizes[] =
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 {
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 511, 512, 513,
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 1023, 1024, 1025,
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 2047, 2048, 2049,
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 4095, 4096, 4097,
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 8191, 8192, 8193
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 };
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 static char dummy[8193];
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 unsigned int i, j;
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 {
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 size_t size = sizes[i];
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 for (j = 0; j < 2; j++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
150 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
151 /* Run a test depending on i and j:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
152 Write size bytes and then calls fflush if j==1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
153 FILE *stream = fopen (UNWRITABLE_FILE, "w");
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
155 if (stream == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
156 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
157 fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
158 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
159 }
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
161 fwrite (dummy, 347, 1, stream);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
162 fwrite (dummy, size - 347, 1, stream);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
163 if (j)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
164 fflush (stream);
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
166 if (fwriteerror (stream) == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
167 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
168 if (errno != ENOSPC)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
169 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
170 i, j, errno);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
171 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
172 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
173 fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
174 i, j);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12146
diff changeset
175 }
4709
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 }
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 return 0;
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180
86c722c44f87 New module 'fwriteerror'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 #endif