Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/fwriteerror.c @ 7292:17785d5bede0
Fix docstrings
author | Sergey Poznyakoff <gray@gnu.org.ua> |
---|---|
date | Sun, 10 Sep 2006 11:52:44 +0000 |
parents | bd5c81f4c585 |
children | 1c4ed7637c24 |
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 | |
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) | |
8 any later version. | |
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 | |
16 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5581
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4709 | 18 |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
19 #ifdef HAVE_CONFIG_H |
4709 | 20 # include <config.h> |
21 #endif | |
22 | |
23 /* Specification. */ | |
24 #include "fwriteerror.h" | |
25 | |
26 #include <errno.h> | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
27 #include <stdbool.h> |
4709 | 28 |
29 int | |
30 fwriteerror (FILE *fp) | |
31 { | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
32 /* 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
|
33 static bool stdout_closed = false; |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
34 |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
35 if (fp == stdout && stdout_closed) |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
36 return 0; |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
37 |
4709 | 38 /* Need to |
39 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
|
40 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
|
41 testing for error again. */ |
4709 | 42 |
43 /* Clear errno, so that on non-POSIX systems the caller doesn't see a | |
44 wrong value of errno when we return -1. */ | |
45 errno = 0; | |
46 | |
47 if (ferror (fp)) | |
48 { | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
49 if (fflush (fp)) |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
50 goto close_preserving_errno; /* errno is set here */ |
4709 | 51 /* The stream had an error earlier, but its errno was lost. If the |
52 error was not temporary, we can get the same errno by writing and | |
53 flushing one more byte. We can do so because at this point the | |
54 stream's contents is garbage anyway. */ | |
55 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
|
56 goto close_preserving_errno; /* errno is set here */ |
4709 | 57 if (fflush (fp)) |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
58 goto close_preserving_errno; /* errno is set here */ |
4709 | 59 /* Give up on errno. */ |
60 errno = 0; | |
6747
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
61 close_preserving_errno: |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
62 /* There's an error. Nevertheless call fclose(fp), for consistency |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
63 with the other cases. */ |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
64 { |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
65 int saved_errno = errno; |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
66 fclose (fp); |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
67 errno = saved_errno; |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
68 return -1; |
bd5c81f4c585
Call fclose() in all cases, even in the failure case.
Bruno Haible <bruno@clisp.org>
parents:
6259
diff
changeset
|
69 } |
4709 | 70 } |
71 | |
5581
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
72 /* If we are closing stdout, don't attempt to do it later again. */ |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
73 if (fp == stdout) |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
74 stdout_closed = true; |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
75 |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
76 if (fclose (fp)) |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
77 return -1; /* errno is set here */ |
c8676e66b5da
The fwriteerror() function now needs to fclose() the stream,
Bruno Haible <bruno@clisp.org>
parents:
4709
diff
changeset
|
78 |
4709 | 79 return 0; |
80 } | |
81 | |
82 | |
83 #if TEST | |
84 | |
85 /* Name of a file on which writing fails. On systems without /dev/full, | |
86 you can choose a filename on a full filesystem. */ | |
87 #define UNWRITABLE_FILE "/dev/full" | |
88 | |
89 int | |
90 main () | |
91 { | |
92 static int sizes[] = | |
93 { | |
94 511, 512, 513, | |
95 1023, 1024, 1025, | |
96 2047, 2048, 2049, | |
97 4095, 4096, 4097, | |
98 8191, 8192, 8193 | |
99 }; | |
100 static char dummy[8193]; | |
101 unsigned int i, j; | |
102 | |
103 for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++) | |
104 { | |
105 size_t size = sizes[i]; | |
106 | |
107 for (j = 0; j < 2; j++) | |
108 { | |
109 /* Run a test depending on i and j: | |
110 Write size bytes and then calls fflush if j==1. */ | |
111 FILE *stream = fopen (UNWRITABLE_FILE, "w"); | |
112 | |
113 if (stream == NULL) | |
114 { | |
115 fprintf (stderr, "Test %u:%u: could not open file\n", i, j); | |
116 continue; | |
117 } | |
118 | |
119 fwrite (dummy, 347, 1, stream); | |
120 fwrite (dummy, size - 347, 1, stream); | |
121 if (j) | |
122 fflush (stream); | |
123 | |
124 if (fwriteerror (stream) == -1) | |
125 { | |
126 if (errno != ENOSPC) | |
127 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n", | |
128 i, j, errno); | |
129 } | |
130 else | |
131 fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n", | |
132 i, j); | |
133 } | |
134 } | |
135 | |
136 return 0; | |
137 } | |
138 | |
139 #endif |