11cc83814Sespie /* xexit.c -- exit with attention to return values and closing stdout.
2*a1acfa9bSespie $Id: xexit.c,v 1.1.1.2 2006/07/17 16:03:41 espie Exp $
31cc83814Sespie
4*a1acfa9bSespie Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
51cc83814Sespie
61cc83814Sespie This program is free software; you can redistribute it and/or modify
71cc83814Sespie it under the terms of the GNU General Public License as published by
81cc83814Sespie the Free Software Foundation; either version 2, or (at your option)
91cc83814Sespie any later version.
101cc83814Sespie
111cc83814Sespie This program is distributed in the hope that it will be useful,
121cc83814Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
131cc83814Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
141cc83814Sespie GNU General Public License for more details.
151cc83814Sespie
161cc83814Sespie You should have received a copy of the GNU General Public License along
171cc83814Sespie with this program; if not, write to the Free Software Foundation, Inc.,
181cc83814Sespie 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
191cc83814Sespie
201cc83814Sespie #include "system.h"
211cc83814Sespie
221cc83814Sespie /* SunOS 4.1.1 gets STDC_HEADERS defined, but it doesn't provide
231cc83814Sespie EXIT_FAILURE. So far no system has defined one of EXIT_FAILURE and
241cc83814Sespie EXIT_SUCCESS without the other. */
251cc83814Sespie #ifdef EXIT_SUCCESS
261cc83814Sespie /* The following test is to work around the gross typo in
271cc83814Sespie systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
281cc83814Sespie is defined to 0, not 1. */
291cc83814Sespie # if !EXIT_FAILURE
301cc83814Sespie # undef EXIT_FAILURE
311cc83814Sespie # define EXIT_FAILURE 1
321cc83814Sespie # endif
331cc83814Sespie #else /* not EXIT_SUCCESS */
341cc83814Sespie # ifdef VMS /* these values suppress some messages; from gnuplot */
351cc83814Sespie # define EXIT_SUCCESS 1
361cc83814Sespie # define EXIT_FAILURE 0x10000002
371cc83814Sespie # else /* not VMS */
381cc83814Sespie # define EXIT_SUCCESS 0
391cc83814Sespie # define EXIT_FAILURE 1
401cc83814Sespie # endif /* not VMS */
411cc83814Sespie #endif /* not EXIT_SUCCESS */
421cc83814Sespie
431cc83814Sespie
44*a1acfa9bSespie /* Flush stdout first, exit if failure (therefore, xexit should be
45*a1acfa9bSespie called to exit every program, not just `return' from main).
46*a1acfa9bSespie Otherwise, if EXIT_STATUS is zero, exit successfully, else
47*a1acfa9bSespie unsuccessfully. */
481cc83814Sespie
491cc83814Sespie void
xexit(int exit_status)50*a1acfa9bSespie xexit (int exit_status)
511cc83814Sespie {
521cc83814Sespie if (ferror (stdout))
531cc83814Sespie {
54*a1acfa9bSespie fputs (_("ferror on stdout\n"), stderr);
551cc83814Sespie exit_status = 1;
561cc83814Sespie }
571cc83814Sespie else if (fflush (stdout) != 0)
581cc83814Sespie {
59*a1acfa9bSespie fputs (_("fflush error on stdout\n"), stderr);
601cc83814Sespie exit_status = 1;
611cc83814Sespie }
621cc83814Sespie
631cc83814Sespie exit_status = exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
641cc83814Sespie
651cc83814Sespie exit (exit_status);
661cc83814Sespie }
671cc83814Sespie
681cc83814Sespie
691cc83814Sespie /* Why do we care about stdout you may ask? Here's why, from Jim
701cc83814Sespie Meyering in the lib/closeout.c file. */
711cc83814Sespie
721cc83814Sespie /* If a program writes *anything* to stdout, that program should close
731cc83814Sespie stdout and make sure that the close succeeds. Otherwise, suppose that
741cc83814Sespie you go to the extreme of checking the return status of every function
751cc83814Sespie that does an explicit write to stdout. The last printf can succeed in
761cc83814Sespie writing to the internal stream buffer, and yet the fclose(stdout) could
771cc83814Sespie still fail (due e.g., to a disk full error) when it tries to write
781cc83814Sespie out that buffered data. Thus, you would be left with an incomplete
791cc83814Sespie output file and the offending program would exit successfully.
801cc83814Sespie
811cc83814Sespie Besides, it's wasteful to check the return value from every call
821cc83814Sespie that writes to stdout -- just let the internal stream state record
831cc83814Sespie the failure. That's what the ferror test is checking below.
841cc83814Sespie
851cc83814Sespie It's important to detect such failures and exit nonzero because many
861cc83814Sespie tools (most notably `make' and other build-management systems) depend
871cc83814Sespie on being able to detect failure in other tools via their exit status. */
88