OpenCoverage

tee.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/tee.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* tee - read from standard input and write to standard output and files.-
2 Copyright (C) 1985-2018 Free Software Foundation, Inc.-
3-
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 3 of the License, or-
7 (at your option) any later version.-
8-
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.-
13-
14 You should have received a copy of the GNU General Public License-
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
16-
17/* Mike Parker, Richard M. Stallman, and David MacKenzie */-
18-
19#include <config.h>-
20#include <sys/types.h>-
21#include <signal.h>-
22#include <getopt.h>-
23-
24#include "system.h"-
25#include "argmatch.h"-
26#include "die.h"-
27#include "error.h"-
28#include "fadvise.h"-
29#include "stdio--.h"-
30#include "xbinary-io.h"-
31-
32/* The official name of this program (e.g., no 'g' prefix). */-
33#define PROGRAM_NAME "tee"-
34-
35#define AUTHORS \-
36 proper_name ("Mike Parker"), \-
37 proper_name ("Richard M. Stallman"), \-
38 proper_name ("David MacKenzie")-
39-
40static bool tee_files (int nfiles, char **files);-
41-
42/* If true, append to output files rather than truncating them. */-
43static bool append;-
44-
45/* If true, ignore interrupts. */-
46static bool ignore_interrupts;-
47-
48enum output_error-
49 {-
50 output_error_sigpipe, /* traditional behavior, sigpipe enabled. */-
51 output_error_warn, /* warn on EPIPE, but continue. */-
52 output_error_warn_nopipe, /* ignore EPIPE, continue. */-
53 output_error_exit, /* exit on any output error. */-
54 output_error_exit_nopipe /* exit on any output error except EPIPE. */-
55 };-
56-
57static enum output_error output_error;-
58-
59static struct option const long_options[] =-
60{-
61 {"append", no_argument, NULL, 'a'},-
62 {"ignore-interrupts", no_argument, NULL, 'i'},-
63 {"output-error", optional_argument, NULL, 'p'},-
64 {GETOPT_HELP_OPTION_DECL},-
65 {GETOPT_VERSION_OPTION_DECL},-
66 {NULL, 0, NULL, 0}-
67};-
68-
69static char const *const output_error_args[] =-
70{-
71 "warn", "warn-nopipe", "exit", "exit-nopipe", NULL-
72};-
73static enum output_error const output_error_types[] =-
74{-
75 output_error_warn, output_error_warn_nopipe,-
76 output_error_exit, output_error_exit_nopipe-
77};-
78ARGMATCH_VERIFY (output_error_args, output_error_types);-
79-
80void-
81usage (int status)-
82{-
83 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 9 times by 1 test
Evaluated by:
  • tee
3-9
84 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • tee
3
85 else-
86 {-
87 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);-
88 fputs (_("\-
89Copy standard input to each FILE, and also to standard output.\n\-
90\n\-
91 -a, --append append to the given FILEs, do not overwrite\n\-
92 -i, --ignore-interrupts ignore interrupt signals\n\-
93"), stdout);-
94 fputs (_("\-
95 -p diagnose errors writing to non pipes\n\-
96 --output-error[=MODE] set behavior on write error. See MODE below\n\-
97"), stdout);-
98 fputs (HELP_OPTION_DESCRIPTION, stdout);-
99 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
100 fputs (_("\-
101\n\-
102MODE determines behavior with write errors on the outputs:\n\-
103 'warn' diagnose errors writing to any output\n\-
104 'warn-nopipe' diagnose errors writing to any output not a pipe\n\-
105 'exit' exit on error writing to any output\n\-
106 'exit-nopipe' exit on error writing to any output not a pipe\n\-
107The default MODE for the -p option is 'warn-nopipe'.\n\-
108The default operation when --output-error is not specified, is to\n\-
109exit immediately on error writing to a pipe, and diagnose errors\n\-
110writing to non pipe outputs.\n\-
111"), stdout);-
112 emit_ancillary_info (PROGRAM_NAME);-
113 }
executed 9 times by 1 test: end of block
Executed by:
  • tee
9
114 exit (status);
executed 12 times by 1 test: exit (status);
Executed by:
  • tee
12
115}-
116-
117int-
118main (int argc, char **argv)-
119{-
120 bool ok;-
121 int optc;-
122-
123 initialize_main (&argc, &argv);-
124 set_program_name (argv[0]);-
125 setlocale (LC_ALL, "");-
126 bindtextdomain (PACKAGE, LOCALEDIR);-
127 textdomain (PACKAGE);-
128-
129 atexit (close_stdout);-
130-
131 append = false;-
132 ignore_interrupts = false;-
133-
134 while ((optc = getopt_long (argc, argv, "aip", long_options, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 61 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 52 times by 1 test
Evaluated by:
  • tee
52-61
135 {-
136 switch (optc)-
137 {-
138 case 'a':
executed 36 times by 1 test: case 'a':
Executed by:
  • tee
36
139 append = true;-
140 break;
executed 36 times by 1 test: break;
Executed by:
  • tee
36
141-
142 case 'i':
executed 2 times by 1 test: case 'i':
Executed by:
  • tee
2
143 ignore_interrupts = true;-
144 break;
executed 2 times by 1 test: break;
Executed by:
  • tee
2
145-
146 case 'p':
executed 7 times by 1 test: case 'p':
Executed by:
  • tee
7
147 if (optarg)
optargDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tee
3-4
148 output_error = XARGMATCH ("--output-error", optarg,
executed 4 times by 1 test: output_error = ((output_error_types) [__xargmatch_internal ("--output-error", optarg, output_error_args, (char const *) (output_error_types), sizeof *(output_error_types), argmatch_die)]) ;
Executed by:
  • tee
4
149 output_error_args, output_error_types);
executed 4 times by 1 test: output_error = ((output_error_types) [__xargmatch_internal ("--output-error", optarg, output_error_args, (char const *) (output_error_types), sizeof *(output_error_types), argmatch_die)]) ;
Executed by:
  • tee
4
150 else-
151 output_error = output_error_warn_nopipe;
executed 3 times by 1 test: output_error = output_error_warn_nopipe;
Executed by:
  • tee
3
152 break;
executed 7 times by 1 test: break;
Executed by:
  • tee
7
153-
154 case_GETOPT_HELP_CHAR;
never executed: break;
executed 9 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • tee
0-9
155-
156 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • tee
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • tee
0-4
157-
158 default:
executed 3 times by 1 test: default:
Executed by:
  • tee
3
159 usage (EXIT_FAILURE);-
160 }
never executed: end of block
0
161 }-
162-
163 if (ignore_interrupts)
ignore_interruptsDescription
TRUEnever evaluated
FALSEevaluated 52 times by 1 test
Evaluated by:
  • tee
0-52
164 signal (SIGINT, SIG_IGN);
never executed: signal ( 2 , ((__sighandler_t) 1) );
0
165-
166 if (output_error != output_error_sigpipe)
output_error !..._error_sigpipeDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 47 times by 1 test
Evaluated by:
  • tee
5-47
167 signal (SIGPIPE, SIG_IGN);
executed 5 times by 1 test: signal ( 13 , ((__sighandler_t) 1) );
Executed by:
  • tee
5
168-
169 /* Do *not* warn if tee is given no file arguments.-
170 POSIX requires that it work when given no arguments. */-
171-
172 ok = tee_files (argc - optind, &argv[optind]);-
173 if (close (STDIN_FILENO) != 0)
close ( 0 ) != 0Description
TRUEnever evaluated
FALSEevaluated 50 times by 1 test
Evaluated by:
  • tee
0-50
174 die (EXIT_FAILURE, errno, "%s", _("standard input"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"%s\", dcgettext (((void *)0), \"standard input\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , "%s", dcgettext (((void *)0), "standard input" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "%s", dcgettext (((void *)0), "standard input" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
175-
176 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 50 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • tee
50
177}-
178-
179/* Copy the standard input into each of the NFILES files in FILES-
180 and into the standard output. As a side effect, modify FILES[-1].-
181 Return true if successful. */-
182-
183static bool-
184tee_files (int nfiles, char **files)-
185{-
186 size_t n_outputs = 0;-
187 FILE **descriptors;-
188 char buffer[BUFSIZ];-
189 ssize_t bytes_read = 0;-
190 int i;-
191 bool ok = true;-
192 char const *mode_string =-
193 (O_BINARY-
194 ? (append ? "ab" : "wb")
dead code: (append ? "ab" : "wb")
-
195 : (append ? "a" : "w"));
appendDescription
TRUEevaluated 34 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 18 times by 1 test
Evaluated by:
  • tee
18-34
196-
197 xset_binary_mode (STDIN_FILENO, O_BINARY);-
198 xset_binary_mode (STDOUT_FILENO, O_BINARY);-
199 fadvise (stdin, FADVISE_SEQUENTIAL);-
200-
201 /* Set up FILES[0 .. NFILES] and DESCRIPTORS[0 .. NFILES].-
202 In both arrays, entry 0 corresponds to standard output. */-
203-
204 descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);-
205 files--;-
206 descriptors[0] = stdout;-
207 files[0] = bad_cast (_("standard output"));-
208 setvbuf (stdout, NULL, _IONBF, 0);-
209 n_outputs++;-
210-
211 for (i = 1; i <= nfiles; i++)
i <= nfilesDescription
TRUEevaluated 72 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 51 times by 1 test
Evaluated by:
  • tee
51-72
212 {-
213 /* Do not treat "-" specially - as mandated by POSIX. */-
214 descriptors[i] = fopen (files[i], mode_string);-
215 if (descriptors[i] == NULL)
descriptors[i] == ((void *)0)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 70 times by 1 test
Evaluated by:
  • tee
2-70
216 {-
217 error (output_error == output_error_exit-
218 || output_error == output_error_exit_nopipe,-
219 errno, "%s", quotef (files[i]));-
220 ok = false;-
221 }
executed 1 time by 1 test: end of block
Executed by:
  • tee
1
222 else-
223 {-
224 setvbuf (descriptors[i], NULL, _IONBF, 0);-
225 n_outputs++;-
226 }
executed 70 times by 1 test: end of block
Executed by:
  • tee
70
227 }-
228-
229 while (n_outputs)
n_outputsDescription
TRUEevaluated 143 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 5 times by 1 test
Evaluated by:
  • tee
5-143
230 {-
231 bytes_read = read (0, buffer, sizeof buffer);-
232 if (bytes_read < 0 && errno == EINTR)
bytes_read < 0Description
TRUEnever evaluated
FALSEevaluated 143 times by 1 test
Evaluated by:
  • tee
(*__errno_location ()) == 4Description
TRUEnever evaluated
FALSEnever evaluated
0-143
233 continue;
never executed: continue;
0
234 if (bytes_read <= 0)
bytes_read <= 0Description
TRUEevaluated 45 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 98 times by 1 test
Evaluated by:
  • tee
45-98
235 break;
executed 45 times by 1 test: break;
Executed by:
  • tee
45
236-
237 /* Write to all NFILES + 1 descriptors.-
238 Standard output is the first one. */-
239 for (i = 0; i <= nfiles; i++)
i <= nfilesDescription
TRUEevaluated 201 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 97 times by 1 test
Evaluated by:
  • tee
97-201
240 if (descriptors[i]
descriptors[i]Description
TRUEevaluated 182 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 19 times by 1 test
Evaluated by:
  • tee
19-182
241 && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
never executed: break;
(__builtin_exp...r++))) == (-1)Description
TRUEnever evaluated
FALSEnever evaluated
__cnt > 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_expe...write_end), 0)Description
TRUEnever evaluated
FALSEnever evaluated
(__extension__...s[i] )))) != 1Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 173 times by 1 test
Evaluated by:
  • tee
__builtin_cons...( bytes_read )Description
TRUEnever evaluated
FALSEevaluated 182 times by 1 test
Evaluated by:
  • tee
__builtin_constant_p ( 1 )Description
TRUEnever evaluated
FALSEnever evaluated
(size_t) ( byt..._t) ( 1 ) <= 8Description
TRUEnever evaluated
FALSEnever evaluated
(size_t) ( bytes_read ) != 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...( bytes_read )Description
TRUEnever evaluated
FALSEevaluated 182 times by 1 test
Evaluated by:
  • tee
(size_t) ( bytes_read ) == 0Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( 1 )Description
TRUEevaluated 182 times by 1 test
Evaluated by:
  • tee
FALSEnever evaluated
(size_t) ( 1 ) == 0Description
TRUEnever evaluated
FALSEevaluated 182 times by 1 test
Evaluated by:
  • tee
0-182
242 {-
243 int w_errno = errno;-
244 bool fail = errno != EPIPE || (output_error == output_error_exit
(*__errno_location ()) != 32Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 5 times by 1 test
Evaluated by:
  • tee
output_error =...put_error_exitDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tee
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tee
1-5
245 || output_error == output_error_warn);
output_error =...put_error_warnDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tee
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tee
1-3
246 if (descriptors[i] == stdout)
descriptors[i] == stdoutDescription
TRUEevaluated 7 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 2 times by 1 test
Evaluated by:
  • tee
2-7
247 clearerr (stdout); /* Avoid redundant close_stdout diagnostic. */
executed 7 times by 1 test: clearerr_unlocked ( stdout );
Executed by:
  • tee
7
248 if (fail)
failDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tee
3-6
249 {-
250 error (output_error == output_error_exit-
251 || output_error == output_error_exit_nopipe,-
252 w_errno, "%s", quotef (files[i]));-
253 }
executed 5 times by 1 test: end of block
Executed by:
  • tee
5
254 descriptors[i] = NULL;-
255 if (fail)
failDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tee
3-5
256 ok = false;
executed 5 times by 1 test: ok = 0 ;
Executed by:
  • tee
5
257 n_outputs--;-
258 }
executed 8 times by 1 test: end of block
Executed by:
  • tee
8
259 }
executed 97 times by 1 test: end of block
Executed by:
  • tee
97
260-
261 if (bytes_read == -1)
bytes_read == -1Description
TRUEnever evaluated
FALSEevaluated 50 times by 1 test
Evaluated by:
  • tee
0-50
262 {-
263 error (0, errno, _("read error"));-
264 ok = false;-
265 }
never executed: end of block
0
266-
267 /* Close the files, but not standard output. */-
268 for (i = 1; i <= nfiles; i++)
i <= nfilesDescription
TRUEevaluated 70 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 50 times by 1 test
Evaluated by:
  • tee
50-70
269 if (descriptors[i] && fclose (descriptors[i]) != 0)
descriptors[i]Description
TRUEevaluated 67 times by 1 test
Evaluated by:
  • tee
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tee
rpl_fclose (de...ptors[i]) != 0Description
TRUEnever evaluated
FALSEevaluated 67 times by 1 test
Evaluated by:
  • tee
0-67
270 {-
271 error (0, errno, "%s", quotef (files[i]));-
272 ok = false;-
273 }
never executed: end of block
0
274-
275 free (descriptors);-
276-
277 return ok;
executed 50 times by 1 test: return ok;
Executed by:
  • tee
50
278}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2