1 /* Close a stream, with nicer error checking than fclose's. 2 3 Copyright (C) 1998-2002, 2004, 2006-2023 Free Software Foundation, Inc. 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 3 of the License, or 8 (at your option) 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, see <https://www.gnu.org/licenses/>. */ 17 18 #include <config.h> 19 20 #include "close-stream.h" 21 22 #include <errno.h> 23 24 #include "fpending.h" 25 26 #if USE_UNLOCKED_IO 27 # include "unlocked-io.h" 28 #endif 29 30 /* Close STREAM. Return 0 if successful, EOF (setting errno) 31 otherwise. A failure might set errno to 0 if the error number 32 cannot be determined. 33 34 A failure with errno set to EPIPE may or may not indicate an error 35 situation worth signaling to the user. See the documentation of the 36 close_stdout_set_ignore_EPIPE function for details. 37 38 If a program writes *anything* to STREAM, that program should close 39 STREAM and make sure that it succeeds before exiting. Otherwise, 40 suppose that you go to the extreme of checking the return status 41 of every function that does an explicit write to STREAM. The last 42 printf can succeed in writing to the internal stream buffer, and yet 43 the fclose(STREAM) could still fail (due e.g., to a disk full error) 44 when it tries to write out that buffered data. Thus, you would be 45 left with an incomplete output file and the offending program would 46 exit successfully. Even calling fflush is not always sufficient, 47 since some file systems (NFS and CODA) buffer written/flushed data 48 until an actual close call. 49 50 Besides, it's wasteful to check the return value from every call 51 that writes to STREAM -- just let the internal stream state record 52 the failure. That's what the ferror test is checking below. */ 53 54 int 55 close_stream (FILE *stream) 56 { 57 const bool some_pending = (__fpending (stream) != 0); 58 const bool prev_fail = (ferror (stream) != 0); 59 const bool fclose_fail = (fclose (stream) != 0); 60 61 /* Return an error indication if there was a previous failure or if 62 fclose failed, with one exception: ignore an fclose failure if 63 there was no previous error, no data remains to be flushed, and 64 fclose failed with EBADF. That can happen when a program like cp 65 is invoked like this 'cp a b >&-' (i.e., with standard output 66 closed) and doesn't generate any output (hence no previous error 67 and nothing to be flushed). */ 68 69 if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) 70 { 71 if (! fclose_fail) 72 errno = 0; 73 return EOF; 74 } 75 76 return 0; 77 }