sort - Don't live-loop threads
[dragonfly.git] / contrib / grep / lib / closeout.c
blob761d7151831ec832bfbccd06898950e9a65c6b09
1 /* Close standard output and standard error, exiting with a diagnostic on error.
3 Copyright (C) 1998-2002, 2004, 2006, 2008-2015 Free Software Foundation,
4 Inc.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
21 #include "closeout.h"
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <unistd.h>
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
31 #include "close-stream.h"
32 #include "error.h"
33 #include "exitfail.h"
34 #include "quotearg.h"
36 static const char *file_name;
38 /* Set the file name to be reported in the event an error is detected
39 by close_stdout. */
40 void
41 close_stdout_set_file_name (const char *file)
43 file_name = file;
46 static bool ignore_EPIPE /* = false */;
48 /* Specify the reaction to an EPIPE error during the closing of stdout:
49 - If ignore = true, it shall be ignored.
50 - If ignore = false, it shall evoke a diagnostic, along with a nonzero
51 exit status.
52 The default is ignore = false.
54 This setting matters only if the SIGPIPE signal is ignored (i.e. its
55 handler set to SIG_IGN) or blocked. Only particular programs need to
56 temporarily ignore SIGPIPE. If SIGPIPE is ignored or blocked because
57 it was ignored or blocked in the parent process when it created the
58 child process, it usually is a bug in the parent process: It is bad
59 practice to have SIGPIPE ignored or blocked while creating a child
60 process.
62 EPIPE occurs when writing to a pipe or socket that has no readers now,
63 when SIGPIPE is ignored or blocked.
65 The ignore = false setting is suitable for a scenario where it is normally
66 guaranteed that the pipe writer terminates before the pipe reader. In
67 this case, an EPIPE is an indication of a premature termination of the
68 pipe reader and should lead to a diagnostic and a nonzero exit status.
70 The ignore = true setting is suitable for a scenario where you don't know
71 ahead of time whether the pipe writer or the pipe reader will terminate
72 first. In this case, an EPIPE is an indication that the pipe writer can
73 stop doing useless write() calls; this is what close_stdout does anyway.
74 EPIPE is part of the normal pipe/socket shutdown protocol in this case,
75 and should not lead to a diagnostic message. */
77 void
78 close_stdout_set_ignore_EPIPE (bool ignore)
80 ignore_EPIPE = ignore;
83 /* Close standard output. On error, issue a diagnostic and _exit
84 with status 'exit_failure'.
86 Also close standard error. On error, _exit with status 'exit_failure'.
88 Since close_stdout is commonly registered via 'atexit', POSIX
89 and the C standard both say that it should not call 'exit',
90 because the behavior is undefined if 'exit' is called more than
91 once. So it calls '_exit' instead of 'exit'. If close_stdout
92 is registered via atexit before other functions are registered,
93 the other functions can act before this _exit is invoked.
95 Applications that use close_stdout should flush any streams
96 other than stdout and stderr before exiting, since the call to
97 _exit will bypass other buffer flushing. Applications should
98 be flushing and closing other streams anyway, to check for I/O
99 errors. Also, applications should not use tmpfile, since _exit
100 can bypass the removal of these files.
102 It's important to detect such failures and exit nonzero because many
103 tools (most notably 'make' and other build-management systems) depend
104 on being able to detect failure in other tools via their exit status. */
106 void
107 close_stdout (void)
109 if (close_stream (stdout) != 0
110 && !(ignore_EPIPE && errno == EPIPE))
112 char const *write_error = _("write error");
113 if (file_name)
114 error (0, errno, "%s: %s", quotearg_colon (file_name),
115 write_error);
116 else
117 error (0, errno, "%s", write_error);
119 _exit (exit_failure);
122 if (close_stream (stderr) != 0)
123 _exit (exit_failure);