Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / closeout.c
bloba1f6d973544f3a5e4afafc8a7be001db5a59db44
1 /* closeout.c - close standard output
2 Copyright (C) 1998-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 /* Specification. */
23 #include "closeout.h"
25 #include <stdio.h>
26 #include <errno.h>
28 #if 0
29 #include "unlocked-io.h"
30 #include "__fpending.h"
31 #endif
32 #include "error.h"
33 #include "fwriteerror.h"
34 #include "exit.h"
35 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
39 /* Close standard output, exiting with status STATUS on failure.
40 If a program writes *anything* to stdout, that program should close
41 stdout and make sure that it succeeds before exiting. Otherwise,
42 suppose that you go to the extreme of checking the return status
43 of every function that does an explicit write to stdout. The last
44 printf can succeed in writing to the internal stream buffer, and yet
45 the fclose(stdout) could still fail (due e.g., to a disk full error)
46 when it tries to write out that buffered data. Thus, you would be
47 left with an incomplete output file and the offending program would
48 exit successfully. Even calling fflush is not always sufficient,
49 since some file systems (NFS and CODA) buffer written/flushed data
50 until an actual close call.
52 Besides, it's wasteful to check the return value from every call
53 that writes to stdout -- just let the internal stream state record
54 the failure. That's what the ferror test is checking below.
56 It's important to detect such failures and exit nonzero because many
57 tools (most notably `make' and other build-management systems) depend
58 on being able to detect failure in other tools via their exit status. */
60 static void
61 close_stdout_status (int status)
63 if (fwriteerror (stdout))
64 error (status, errno, "%s", _("write error"));
67 /* Close standard output, exiting with status EXIT_FAILURE on failure. */
68 void
69 close_stdout (void)
71 close_stdout_status (EXIT_FAILURE);
74 /* Note: When exit (...) calls the atexit-registered
75 close_stdout (), which calls
76 error (status, ...), which calls
77 exit (status),
78 we have undefined behaviour according to ISO C 99 section 7.20.4.3.(2).
79 But in practice there is no problem: The second exit call is executed
80 at a moment when the atexit handlers are no longer active. */