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