1 /* xexit.c -- exit with attention to return values and closing stdout.
2 $Id: xexit.c,v 1.5 2004/04/11 17:56:46 karl Exp $
4 Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* SunOS 4.1.1 gets STDC_HEADERS defined, but it doesn't provide
23 EXIT_FAILURE. So far no system has defined one of EXIT_FAILURE and
24 EXIT_SUCCESS without the other. */
26 /* The following test is to work around the gross typo in
27 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
28 is defined to 0, not 1. */
31 # define EXIT_FAILURE 1
33 #else /* not EXIT_SUCCESS */
34 # ifdef VMS /* these values suppress some messages; from gnuplot */
35 # define EXIT_SUCCESS 1
36 # define EXIT_FAILURE 0x10000002
38 # define EXIT_SUCCESS 0
39 # define EXIT_FAILURE 1
41 #endif /* not EXIT_SUCCESS */
44 /* Flush stdout first, exit if failure (therefore, xexit should be
45 called to exit every program, not just `return' from main).
46 Otherwise, if EXIT_STATUS is zero, exit successfully, else
50 xexit (int exit_status
)
54 fputs (_("ferror on stdout\n"), stderr
);
57 else if (fflush (stdout
) != 0)
59 fputs (_("fflush error on stdout\n"), stderr
);
63 exit_status
= exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
69 /* Why do we care about stdout you may ask? Here's why, from Jim
70 Meyering in the lib/closeout.c file. */
72 /* If a program writes *anything* to stdout, that program should close
73 stdout and make sure that the close succeeds. Otherwise, suppose that
74 you go to the extreme of checking the return status of every function
75 that does an explicit write to stdout. The last printf can succeed in
76 writing to the internal stream buffer, and yet the fclose(stdout) could
77 still fail (due e.g., to a disk full error) when it tries to write
78 out that buffered data. Thus, you would be left with an incomplete
79 output file and the offending program would exit successfully.
81 Besides, it's wasteful to check the return value from every call
82 that writes to stdout -- just let the internal stream state record
83 the failure. That's what the ferror test is checking below.
85 It's important to detect such failures and exit nonzero because many
86 tools (most notably `make' and other build-management systems) depend
87 on being able to detect failure in other tools via their exit status. */