From bd749957dc8e1129c765445de6cc98aa5a8b8a74 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 22 Jul 2006 22:25:09 +0000 Subject: [PATCH] Initial revision. --- lib/close-stream.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ lib/close-stream.h | 2 ++ m4/close-stream.m4 | 13 ++++++++ 3 files changed, 93 insertions(+) create mode 100644 lib/close-stream.c create mode 100644 lib/close-stream.h create mode 100644 m4/close-stream.m4 diff --git a/lib/close-stream.c b/lib/close-stream.c new file mode 100644 index 0000000000..d7551d67ff --- /dev/null +++ b/lib/close-stream.c @@ -0,0 +1,78 @@ +/* Close a stream, with nicer error checking than fclose's. + + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free + Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "close-stream.h" + +#include +#include + +#include "__fpending.h" + +#if USE_UNLOCKED_IO +# include "unlocked-io.h" +#endif + +/* Close STREAM. Return 0 if successful, EOF (setting errno) + otherwise. A failure might set errno to 0 if the error number + cannot be determined. + + If a program writes *anything* to STREAM, that program should close + STREAM and make sure that it succeeds before exiting. Otherwise, + suppose that you go to the extreme of checking the return status + of every function that does an explicit write to STREAM. The last + printf can succeed in writing to the internal stream buffer, and yet + the fclose(STREAM) could still fail (due e.g., to a disk full error) + when it tries to write out that buffered data. Thus, you would be + left with an incomplete output file and the offending program would + exit successfully. Even calling fflush is not always sufficient, + since some file systems (NFS and CODA) buffer written/flushed data + until an actual close call. + + Besides, it's wasteful to check the return value from every call + that writes to STREAM -- just let the internal stream state record + the failure. That's what the ferror test is checking below. */ + +int +close_stream (FILE *stream) +{ + bool some_pending = (__fpending (stream) != 0); + bool prev_fail = (ferror (stream) != 0); + bool fclose_fail = (fclose (stream) != 0); + + /* Return an error indication if there was a previous failure or if + fclose failed, with one exception: ignore an fclose failure if + there was no previous error, no data remains to be flushed, and + fclose failed with EBADF. That can happen when a program like cp + is invoked like this `cp a b >&-' (i.e., with standard output + closed) and doesn't generate any output (hence no previous error + and nothing to be flushed). */ + + if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) + { + if (! fclose_fail) + errno = 0; + return EOF; + } + + return 0; +} diff --git a/lib/close-stream.h b/lib/close-stream.h new file mode 100644 index 0000000000..be3d4196b0 --- /dev/null +++ b/lib/close-stream.h @@ -0,0 +1,2 @@ +#include +int close_stream (FILE *stream); diff --git a/m4/close-stream.m4 b/m4/close-stream.m4 new file mode 100644 index 0000000000..93f4d9a916 --- /dev/null +++ b/m4/close-stream.m4 @@ -0,0 +1,13 @@ +dnl Copyright (C) 2006 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_CLOSE_STREAM], +[ + AC_LIBSOURCES([close-stream.c, close-stream.h]) + AC_LIBOBJ([close-stream]) + + dnl Prerequisites of lib/close-stream.c. + : +]) -- 2.30.2