Use a consistent style for including <config.h>.
[gnulib.git] / lib / fwriteerror.c
1 /* Detect write error on a stream.
2    Copyright (C) 2003-2005 Free Software Foundation, Inc.
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,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "fwriteerror.h"
25
26 #include <errno.h>
27 #include <stdbool.h>
28
29 int
30 fwriteerror (FILE *fp)
31 {
32   /* State to allow multiple calls to fwriteerror (stdout).  */
33   static bool stdout_closed = false;
34
35   if (fp == stdout && stdout_closed)
36     return 0;
37
38   /* Need to
39      1. test the error indicator of the stream,
40      2. flush the buffers both in userland and in the kernel, through fclose,
41         testing for error again.  */
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     {
49       if (fflush (fp))
50         return -1; /* errno is set here */
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)
56         return -1; /* errno is set here */
57       if (fflush (fp))
58         return -1; /* errno is set here */
59       /* Give up on errno.  */
60       errno = 0;
61       return -1;
62     }
63
64   /* If we are closing stdout, don't attempt to do it later again.  */
65   if (fp == stdout)
66     stdout_closed = true;
67
68   if (fclose (fp))
69     return -1; /* errno is set here */
70
71   return 0;
72 }
73
74
75 #if TEST
76
77 /* Name of a file on which writing fails.  On systems without /dev/full,
78    you can choose a filename on a full filesystem.  */
79 #define UNWRITABLE_FILE "/dev/full"
80
81 int
82 main ()
83 {
84   static int sizes[] =
85     {
86        511,  512,  513,
87       1023, 1024, 1025,
88       2047, 2048, 2049,
89       4095, 4096, 4097,
90       8191, 8192, 8193
91     };
92   static char dummy[8193];
93   unsigned int i, j;
94
95   for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
96     {
97       size_t size = sizes[i];
98
99       for (j = 0; j < 2; j++)
100         {
101           /* Run a test depending on i and j:
102              Write size bytes and then calls fflush if j==1.  */
103           FILE *stream = fopen (UNWRITABLE_FILE, "w");
104
105           if (stream == NULL)
106             {
107               fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
108               continue;
109             }
110
111           fwrite (dummy, 347, 1, stream);
112           fwrite (dummy, size - 347, 1, stream);
113           if (j)
114             fflush (stream);
115
116           if (fwriteerror (stream) == -1)
117             {
118               if (errno != ENOSPC)
119                 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
120                          i, j, errno);
121             }
122           else
123             fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
124                      i, j);
125         }
126     }
127
128   return 0;
129 }
130
131 #endif