Back to index

tetex-bin  3.0
xexit.c
Go to the documentation of this file.
00001 /* xexit.c -- exit with attention to return values and closing stdout.
00002    $Id: xexit.c,v 1.5 2004/04/11 17:56:46 karl Exp $
00003 
00004    Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License along
00017    with this program; if not, write to the Free Software Foundation, Inc.,
00018    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #include "system.h"
00021 
00022 /* SunOS 4.1.1 gets STDC_HEADERS defined, but it doesn't provide
00023    EXIT_FAILURE.  So far no system has defined one of EXIT_FAILURE and
00024    EXIT_SUCCESS without the other.  */
00025 #ifdef EXIT_SUCCESS
00026  /* The following test is to work around the gross typo in
00027     systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
00028     is defined to 0, not 1.  */
00029 # if !EXIT_FAILURE
00030 #  undef EXIT_FAILURE
00031 #  define EXIT_FAILURE 1
00032 # endif
00033 #else /* not EXIT_SUCCESS */
00034 # ifdef VMS /* these values suppress some messages; from gnuplot */
00035 #   define EXIT_SUCCESS 1
00036 #   define EXIT_FAILURE 0x10000002
00037 # else /* not VMS */
00038 #  define EXIT_SUCCESS 0
00039 #  define EXIT_FAILURE 1
00040 # endif /* not VMS */
00041 #endif /* not EXIT_SUCCESS */
00042 
00043 
00044 /* Flush stdout first, exit if failure (therefore, xexit should be
00045    called to exit every program, not just `return' from main).
00046    Otherwise, if EXIT_STATUS is zero, exit successfully, else
00047    unsuccessfully.  */
00048 
00049 void
00050 xexit (int exit_status)
00051 {
00052   if (ferror (stdout))
00053     {
00054       fputs (_("ferror on stdout\n"), stderr);
00055       exit_status = 1;
00056     }
00057   else if (fflush (stdout) != 0)
00058     {
00059       fputs (_("fflush error on stdout\n"), stderr);
00060       exit_status = 1;
00061     }
00062 
00063   exit_status = exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
00064 
00065   exit (exit_status);
00066 }
00067 
00068 
00069 /* Why do we care about stdout you may ask?  Here's why, from Jim
00070    Meyering in the lib/closeout.c file.  */
00071 
00072 /* If a program writes *anything* to stdout, that program should close
00073    stdout and make sure that the close succeeds.  Otherwise, suppose that
00074    you go to the extreme of checking the return status of every function
00075    that does an explicit write to stdout.  The last printf can succeed in
00076    writing to the internal stream buffer, and yet the fclose(stdout) could
00077    still fail (due e.g., to a disk full error) when it tries to write
00078    out that buffered data.  Thus, you would be left with an incomplete
00079    output file and the offending program would exit successfully.
00080 
00081    Besides, it's wasteful to check the return value from every call
00082    that writes to stdout -- just let the internal stream state record
00083    the failure.  That's what the ferror test is checking below.
00084 
00085    It's important to detect such failures and exit nonzero because many
00086    tools (most notably `make' and other build-management systems) depend
00087    on being able to detect failure in other tools via their exit status.  */