Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / texinfo / lib / xexit.c
1 /* xexit.c -- exit with attention to return values and closing stdout.
2    $Id: xexit.c,v 1.5 1999/02/19 14:13:51 karl Exp $
3
4    Copyright (C) 1999 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.  Otherwise, if EXIT_STATUS is
45    zero, exit successfully, else unsuccessfully.  */
46         
47 void
48 xexit (exit_status)
49      int exit_status;
50 {
51   if (ferror (stdout))
52     {
53       fprintf (stderr, "ferror on stdout");
54       exit_status = 1;
55     }
56   else if (fflush (stdout) != 0)
57     {
58       fprintf (stderr, "fflush error on stdout");
59       exit_status = 1;
60     }
61   
62   exit_status = exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
63   
64   exit (exit_status);
65 }
66
67
68 /* Why do we care about stdout you may ask?  Here's why, from Jim
69    Meyering in the lib/closeout.c file.  */
70
71 /* If a program writes *anything* to stdout, that program should close
72    stdout and make sure that the close succeeds.  Otherwise, suppose that
73    you go to the extreme of checking the return status of every function
74    that does an explicit write to stdout.  The last printf can succeed in
75    writing to the internal stream buffer, and yet the fclose(stdout) could
76    still fail (due e.g., to a disk full error) when it tries to write
77    out that buffered data.  Thus, you would be left with an incomplete
78    output file and the offending program would exit successfully.
79
80    Besides, it's wasteful to check the return value from every call
81    that writes to stdout -- just let the internal stream state record
82    the failure.  That's what the ferror test is checking below.
83
84    It's important to detect such failures and exit nonzero because many
85    tools (most notably `make' and other build-management systems) depend
86    on being able to detect failure in other tools via their exit status.  */