xref: /netbsd-src/external/gpl2/gettext/dist/gettext-runtime/gnulib-lib/fwriteerror.h (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Detect write error on a stream.
2*946379e7Schristos    Copyright (C) 2003, 2005-2006 Free Software Foundation, Inc.
3*946379e7Schristos    Written by Bruno Haible <bruno@clisp.org>, 2003.
4*946379e7Schristos 
5*946379e7Schristos    This program is free software; you can redistribute it and/or modify
6*946379e7Schristos    it under the terms of the GNU General Public License as published by
7*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos    any later version.
9*946379e7Schristos 
10*946379e7Schristos    This program is distributed in the hope that it will be useful,
11*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*946379e7Schristos    GNU General Public License for more details.
14*946379e7Schristos 
15*946379e7Schristos    You should have received a copy of the GNU General Public License
16*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18*946379e7Schristos 
19*946379e7Schristos /* There are two approaches for detecting a write error on a stream opened
20*946379e7Schristos    for writing:
21*946379e7Schristos 
22*946379e7Schristos      (a) Test the return value of every fwrite() or fprintf() call, and react
23*946379e7Schristos          immediately.
24*946379e7Schristos      (b) Just before fclose(), test the error indicator in the stream and
25*946379e7Schristos          the return value of the final fclose() call.
26*946379e7Schristos 
27*946379e7Schristos    The benefit of (a) is that non file related errors (such that ENOMEM during
28*946379e7Schristos    fprintf) and temporary error conditions can be diagnosed accurately.
29*946379e7Schristos 
30*946379e7Schristos    A theoretical benefit of (a) is also that, on POSIX systems, in the case of
31*946379e7Schristos    an ENOSPC error, errno is set and can be used by error() to provide a more
32*946379e7Schristos    accurate error message. But in practice, this benefit is not big because
33*946379e7Schristos    users can easily figure out by themselves why a file cannot be written to,
34*946379e7Schristos    and furthermore the function fwriteerror() can provide errno as well.
35*946379e7Schristos 
36*946379e7Schristos    The big drawback of (a) is extensive error checking code: Every function
37*946379e7Schristos    which does stream output must return an error indicator.
38*946379e7Schristos 
39*946379e7Schristos    This file provides support for (b).  */
40*946379e7Schristos 
41*946379e7Schristos #include <stdio.h>
42*946379e7Schristos 
43*946379e7Schristos /* Write out the not yet written buffered contents of the stream FP, close
44*946379e7Schristos    the stream FP, and test whether some error occurred on the stream FP.
45*946379e7Schristos    FP must be a stream opened for writing.
46*946379e7Schristos    Return 0 if no error occurred and fclose (fp) succeeded.
47*946379e7Schristos    Return -1 and set errno if there was an error.  The errno value will be 0
48*946379e7Schristos    if the cause of the error cannot be determined.
49*946379e7Schristos    For any given stream FP other than stdout, fwriteerror (FP) may only be
50*946379e7Schristos    called once.  */
51*946379e7Schristos extern int fwriteerror (FILE *fp);
52*946379e7Schristos 
53*946379e7Schristos /* Likewise, but don't consider it an error if FP has an invalid file
54*946379e7Schristos    descriptor and no output was done to FP.  */
55*946379e7Schristos extern int fwriteerror_no_ebadf (FILE *fp);
56