close-stream.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Close a stream, with nicer error checking than fclose's.
  2. Copyright (C) 1998-2002, 2004, 2006-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. #include "close-stream.h"
  15. #include <errno.h>
  16. #include <stdbool.h>
  17. #include "fpending.h"
  18. #if USE_UNLOCKED_IO
  19. # include "unlocked-io.h"
  20. #endif
  21. /* Close STREAM. Return 0 if successful, EOF (setting errno)
  22. otherwise. A failure might set errno to 0 if the error number
  23. cannot be determined.
  24. A failure with errno set to EPIPE may or may not indicate an error
  25. situation worth signaling to the user. See the documentation of the
  26. close_stdout_set_ignore_EPIPE function for details.
  27. If a program writes *anything* to STREAM, that program should close
  28. STREAM and make sure that it succeeds before exiting. Otherwise,
  29. suppose that you go to the extreme of checking the return status
  30. of every function that does an explicit write to STREAM. The last
  31. printf can succeed in writing to the internal stream buffer, and yet
  32. the fclose(STREAM) could still fail (due e.g., to a disk full error)
  33. when it tries to write out that buffered data. Thus, you would be
  34. left with an incomplete output file and the offending program would
  35. exit successfully. Even calling fflush is not always sufficient,
  36. since some file systems (NFS and CODA) buffer written/flushed data
  37. until an actual close call.
  38. Besides, it's wasteful to check the return value from every call
  39. that writes to STREAM -- just let the internal stream state record
  40. the failure. That's what the ferror test is checking below. */
  41. int
  42. close_stream (FILE *stream)
  43. {
  44. const bool some_pending = (__fpending (stream) != 0);
  45. const bool prev_fail = (ferror (stream) != 0);
  46. const bool fclose_fail = (fclose (stream) != 0);
  47. /* Return an error indication if there was a previous failure or if
  48. fclose failed, with one exception: ignore an fclose failure if
  49. there was no previous error, no data remains to be flushed, and
  50. fclose failed with EBADF. That can happen when a program like cp
  51. is invoked like this 'cp a b >&-' (i.e., with standard output
  52. closed) and doesn't generate any output (hence no previous error
  53. and nothing to be flushed). */
  54. if (prev_fail || (fclose_fail && (some_pending || errno != EBADF)))
  55. {
  56. if (! fclose_fail)
  57. errno = 0;
  58. return EOF;
  59. }
  60. return 0;
  61. }