1 /* xexit.c -- exit with attention to return values and closing stdout.
2 $Id: xexit.c,v 1.1.1.2 2006/07/17 16:03:41 espie Exp $
3
4 Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
5
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)
9 any later version.
10
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.
15
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. */
19
20 #include "system.h"
21
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. */
25 #ifdef EXIT_SUCCESS
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. */
29 # if !EXIT_FAILURE
30 # undef EXIT_FAILURE
31 # define EXIT_FAILURE 1
32 # endif
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
37 # else /* not VMS */
38 # define EXIT_SUCCESS 0
39 # define EXIT_FAILURE 1
40 # endif /* not VMS */
41 #endif /* not EXIT_SUCCESS */
42
43
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
47 unsuccessfully. */
48
49 void
xexit(int exit_status)50 xexit (int exit_status)
51 {
52 if (ferror (stdout))
53 {
54 fputs (_("ferror on stdout\n"), stderr);
55 exit_status = 1;
56 }
57 else if (fflush (stdout) != 0)
58 {
59 fputs (_("fflush error on stdout\n"), stderr);
60 exit_status = 1;
61 }
62
63 exit_status = exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
64
65 exit (exit_status);
66 }
67
68
69 /* Why do we care about stdout you may ask? Here's why, from Jim
70 Meyering in the lib/closeout.c file. */
71
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.
80
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.
84
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. */
88