Include <config.h> unconditionally.
[gnulib.git] / lib / fwriteerror.c
1 /* Detect write error on a stream.
2    Copyright (C) 2003-2006 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 #include <config.h>
20
21 /* Specification.  */
22 #include "fwriteerror.h"
23
24 #include <errno.h>
25 #include <stdbool.h>
26
27 int
28 fwriteerror (FILE *fp)
29 {
30   /* State to allow multiple calls to fwriteerror (stdout).  */
31   static bool stdout_closed = false;
32
33   if (fp == stdout && stdout_closed)
34     return 0;
35
36   /* Need to
37      1. test the error indicator of the stream,
38      2. flush the buffers both in userland and in the kernel, through fclose,
39         testing for error again.  */
40
41   /* Clear errno, so that on non-POSIX systems the caller doesn't see a
42      wrong value of errno when we return -1.  */
43   errno = 0;
44
45   if (ferror (fp))
46     {
47       if (fflush (fp))
48         goto close_preserving_errno; /* errno is set here */
49       /* The stream had an error earlier, but its errno was lost.  If the
50          error was not temporary, we can get the same errno by writing and
51          flushing one more byte.  We can do so because at this point the
52          stream's contents is garbage anyway.  */
53       if (fputc ('\0', fp) == EOF)
54         goto close_preserving_errno; /* errno is set here */
55       if (fflush (fp))
56         goto close_preserving_errno; /* errno is set here */
57       /* Give up on errno.  */
58       errno = 0;
59      close_preserving_errno:
60       /* There's an error.  Nevertheless call fclose(fp), for consistency
61          with the other cases.  */
62       {
63         int saved_errno = errno;
64         fclose (fp);
65         errno = saved_errno;
66         return -1;
67       }
68     }
69
70   /* If we are closing stdout, don't attempt to do it later again.  */
71   if (fp == stdout)
72     stdout_closed = true;
73
74   if (fclose (fp))
75     return -1; /* errno is set here */
76
77   return 0;
78 }
79
80
81 #if TEST
82
83 /* Name of a file on which writing fails.  On systems without /dev/full,
84    you can choose a filename on a full filesystem.  */
85 #define UNWRITABLE_FILE "/dev/full"
86
87 int
88 main ()
89 {
90   static int sizes[] =
91     {
92        511,  512,  513,
93       1023, 1024, 1025,
94       2047, 2048, 2049,
95       4095, 4096, 4097,
96       8191, 8192, 8193
97     };
98   static char dummy[8193];
99   unsigned int i, j;
100
101   for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
102     {
103       size_t size = sizes[i];
104
105       for (j = 0; j < 2; j++)
106         {
107           /* Run a test depending on i and j:
108              Write size bytes and then calls fflush if j==1.  */
109           FILE *stream = fopen (UNWRITABLE_FILE, "w");
110
111           if (stream == NULL)
112             {
113               fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
114               continue;
115             }
116
117           fwrite (dummy, 347, 1, stream);
118           fwrite (dummy, size - 347, 1, stream);
119           if (j)
120             fflush (stream);
121
122           if (fwriteerror (stream) == -1)
123             {
124               if (errno != ENOSPC)
125                 fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
126                          i, j, errno);
127             }
128           else
129             fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
130                      i, j);
131         }
132     }
133
134   return 0;
135 }
136
137 #endif