xref: /netbsd-src/external/gpl2/grep/dist/lib/closeout.c (revision a8fa202a6440953be7b92a8960a811bff58203f4)
1 /*	$NetBSD: closeout.c,v 1.1.1.1 2016/01/10 21:36:18 christos Exp $	*/
2 
3 /* closeout.c - close standard output
4    Copyright (C) 1998, 1999, 2000, 2001 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
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19 
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 
24 #if ENABLE_NLS
25 # include <libintl.h>
26 # define _(Text) gettext (Text)
27 #else
28 # define _(Text) Text
29 #endif
30 
31 #if HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #ifndef EXIT_FAILURE
35 # define EXIT_FAILURE 1
36 #endif
37 
38 #include <stdio.h>
39 
40 #include <errno.h>
41 #ifndef errno
42 extern int errno;
43 #endif
44 
45 #include "closeout.h"
46 #include "error.h"
47 #include "quotearg.h"
48 #if 0
49 #include "__fpending.h"
50 #endif
51 
52 static int default_exit_status = EXIT_FAILURE;
53 static const char *file_name;
54 
55 /* Set the value to be used for the exit status when close_stdout is called.
56    This is useful when it is not convenient to call close_stdout_status,
57    e.g., when close_stdout is called via atexit.  */
58 void
close_stdout_set_status(int status)59 close_stdout_set_status (int status)
60 {
61   default_exit_status = status;
62 }
63 
64 /* Set the file name to be reported in the event an error is detected
65    by close_stdout_status.  */
66 void
close_stdout_set_file_name(const char * file)67 close_stdout_set_file_name (const char *file)
68 {
69   file_name = file;
70 }
71 
72 /* Close standard output, exiting with status STATUS on failure.
73    If a program writes *anything* to stdout, that program should `fflush'
74    stdout and make sure that it succeeds before exiting.  Otherwise,
75    suppose that you go to the extreme of checking the return status
76    of every function that does an explicit write to stdout.  The last
77    printf can succeed in writing to the internal stream buffer, and yet
78    the fclose(stdout) could still fail (due e.g., to a disk full error)
79    when it tries to write out that buffered data.  Thus, you would be
80    left with an incomplete output file and the offending program would
81    exit successfully.
82 
83    FIXME: note the fflush suggested above is implicit in the fclose
84    we actually do below.  Consider doing only the fflush and/or using
85    setvbuf to inhibit buffering.
86 
87    Besides, it's wasteful to check the return value from every call
88    that writes to stdout -- just let the internal stream state record
89    the failure.  That's what the ferror test is checking below.
90 
91    It's important to detect such failures and exit nonzero because many
92    tools (most notably `make' and other build-management systems) depend
93    on being able to detect failure in other tools via their exit status.  */
94 
95 void
close_stdout_status(int status)96 close_stdout_status (int status)
97 {
98   int e = ferror (stdout) ? 0 : -1;
99 
100 #if 0
101   if (__fpending (stdout) == 0)
102     return;
103 #endif
104 
105   if (fclose (stdout) != 0)
106     e = errno;
107 
108   if (0 < e)
109     {
110       char const *write_error = _("write error");
111       if (file_name)
112 	error (status, e, "%s: %s", quotearg_colon (file_name), write_error);
113       else
114 	error (status, e, "%s", write_error);
115     }
116 }
117 
118 /* Close standard output, exiting with status EXIT_FAILURE on failure.  */
119 void
close_stdout(void)120 close_stdout (void)
121 {
122   close_stdout_status (default_exit_status);
123 }
124