closein.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Close standard input, rewinding seekable stdin if necessary.
  2. Copyright (C) 2007, 2009-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 "closein.h"
  15. #include <errno.h>
  16. #include <stdbool.h>
  17. #include "stdio--.h"
  18. #include <unistd.h>
  19. #include "gettext.h"
  20. #define _(msgid) gettext (msgid)
  21. #include "close-stream.h"
  22. #include "closeout.h"
  23. #include "error.h"
  24. #include "exitfail.h"
  25. #include "freadahead.h"
  26. #include "quotearg.h"
  27. static const char *file_name;
  28. /* Set the file name to be reported in the event an error is detected
  29. on stdin by close_stdin. See also close_stdout_set_file_name, if
  30. an error is detected when closing stdout. */
  31. void
  32. close_stdin_set_file_name (const char *file)
  33. {
  34. file_name = file;
  35. }
  36. /* Close standard input, rewinding any unused input if stdin is
  37. seekable. On error, issue a diagnostic and _exit with status
  38. 'exit_failure'. Then call close_stdout.
  39. Most programs can get by with close_stdout. close_stdin is only
  40. needed when a program wants to guarantee that partially read input
  41. from seekable stdin is not consumed, for any subsequent clients.
  42. For example, POSIX requires that these two commands behave alike:
  43. (sed -ne 1q; cat) < file
  44. tail -n +2 file
  45. Since close_stdin is commonly registered via 'atexit', POSIX
  46. and the C standard both say that it should not call 'exit',
  47. because the behavior is undefined if 'exit' is called more than
  48. once. So it calls '_exit' instead of 'exit'. If close_stdin
  49. is registered via atexit before other functions are registered,
  50. the other functions can act before this _exit is invoked.
  51. Applications that use close_stdout should flush any streams other
  52. than stdin, stdout, and stderr before exiting, since the call to
  53. _exit will bypass other buffer flushing. Applications should be
  54. flushing and closing other streams anyway, to check for I/O errors.
  55. Also, applications should not use tmpfile, since _exit can bypass
  56. the removal of these files.
  57. It's important to detect such failures and exit nonzero because many
  58. tools (most notably 'make' and other build-management systems) depend
  59. on being able to detect failure in other tools via their exit status. */
  60. void
  61. close_stdin (void)
  62. {
  63. bool fail = false;
  64. /* There is no need to flush stdin if we can determine quickly that stdin's
  65. input buffer is empty; in this case we know that if stdin is seekable,
  66. (fseeko (stdin, 0, SEEK_CUR), ftello (stdin))
  67. == lseek (0, 0, SEEK_CUR). */
  68. if (freadahead (stdin) > 0)
  69. {
  70. /* Only attempt flush if stdin is seekable, as fflush is entitled to
  71. fail on non-seekable streams. */
  72. if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
  73. fail = true;
  74. }
  75. if (close_stream (stdin) != 0)
  76. fail = true;
  77. if (fail)
  78. {
  79. /* Report failure, but defer exit until after closing stdout,
  80. since the failure report should still be flushed. */
  81. char const *close_error = _("error closing file");
  82. if (file_name)
  83. error (0, errno, "%s: %s", quotearg_colon (file_name),
  84. close_error);
  85. else
  86. error (0, errno, "%s", close_error);
  87. }
  88. close_stdout ();
  89. if (fail)
  90. _exit (exit_failure);
  91. }