xexit.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* xexit.c -- exit with attention to return values and closing stdout.
  2. $Id: xexit.c,v 1.8 2007/07/01 21:20:31 karl Exp $
  3. Copyright (C) 1999, 2003, 2004, 2007 Free Software Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "system.h"
  15. /* SunOS 4.1.1 gets STDC_HEADERS defined, but it doesn't provide
  16. EXIT_FAILURE. So far no system has defined one of EXIT_FAILURE and
  17. EXIT_SUCCESS without the other. */
  18. #ifdef EXIT_SUCCESS
  19. /* The following test is to work around the gross typo in
  20. systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
  21. is defined to 0, not 1. */
  22. # if !EXIT_FAILURE
  23. # undef EXIT_FAILURE
  24. # define EXIT_FAILURE 1
  25. # endif
  26. #else /* not EXIT_SUCCESS */
  27. # ifdef VMS /* these values suppress some messages; from gnuplot */
  28. # define EXIT_SUCCESS 1
  29. # define EXIT_FAILURE 0x10000002
  30. # else /* not VMS */
  31. # define EXIT_SUCCESS 0
  32. # define EXIT_FAILURE 1
  33. # endif /* not VMS */
  34. #endif /* not EXIT_SUCCESS */
  35. /* Flush stdout first, exit if failure (therefore, xexit should be
  36. called to exit every program, not just `return' from main).
  37. Otherwise, if EXIT_STATUS is zero, exit successfully, else
  38. unsuccessfully. */
  39. void
  40. xexit (int exit_status)
  41. {
  42. if (ferror (stdout))
  43. {
  44. fputs (_("ferror on stdout\n"), stderr);
  45. exit_status = 1;
  46. }
  47. else if (fflush (stdout) != 0)
  48. {
  49. fputs (_("fflush error on stdout\n"), stderr);
  50. exit_status = 1;
  51. }
  52. exit_status = exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  53. exit (exit_status);
  54. }
  55. /* Why do we care about stdout you may ask? Here's why, from Jim
  56. Meyering in the lib/closeout.c file. */
  57. /* If a program writes *anything* to stdout, that program should close
  58. stdout and make sure that the close succeeds. Otherwise, suppose that
  59. you go to the extreme of checking the return status of every function
  60. that does an explicit write to stdout. The last printf can succeed in
  61. writing to the internal stream buffer, and yet the fclose(stdout) could
  62. still fail (due e.g., to a disk full error) when it tries to write
  63. out that buffered data. Thus, you would be left with an incomplete
  64. output file and the offending program would exit successfully.
  65. Besides, it's wasteful to check the return value from every call
  66. that writes to stdout -- just let the internal stream state record
  67. the failure. That's what the ferror test is checking below.
  68. It's important to detect such failures and exit nonzero because many
  69. tools (most notably `make' and other build-management systems) depend
  70. on being able to detect failure in other tools via their exit status. */