1 /* Detect write error on a stream.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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)
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* There are two approaches for detecting a write error on a stream opened
22 (a) Test the return value of every fwrite() or fprintf() call, and react
24 (b) Just before fclose(), test the error indicator in the stream and
25 the return value of the final fflush() or fclose() call.
27 The benefit of (a) is that non file related errors (such that ENOMEM during
28 fprintf) and temporary error conditions can be diagnosed accurately.
30 A theoretical benefit of (a) is also that, on POSIX systems, in the case of
31 an ENOSPC error, errno is set and can be used by error() to provide a more
32 accurate error message. But in practice, this benefit is not big because
33 users can easily figure out by themselves why a file cannot be written to,
34 and furthermore the function fwriteerror() can provide errno as well.
36 The big drawback of (a) is extensive error checking code: Every function
37 which does stream output must return an error indicator.
39 This file provides support for (b). */
43 /* Write out the not yet written buffered contents of the stream FP, and then
44 test whether some error occurred on the stream FP. FP must be a stream
46 Return 0 if no error occurred. In this case it can be assumed that
47 fclose (fp) will succeed.
48 Return -1 and set errno if there was an error. The errno value will be 0
49 if the cause of the error cannot be determined.
51 extern int fwriteerror (FILE *fp);