Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/yes.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /* yes - output a string repeatedly until killed | - | ||||||
2 | Copyright (C) 1991-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 | /* David MacKenzie <djm@gnu.ai.mit.edu> */ | - | ||||||
18 | - | |||||||
19 | #include <config.h> | - | ||||||
20 | #include <stdio.h> | - | ||||||
21 | #include <sys/types.h> | - | ||||||
22 | #include <getopt.h> | - | ||||||
23 | - | |||||||
24 | #include "system.h" | - | ||||||
25 | - | |||||||
26 | #include "error.h" | - | ||||||
27 | #include "full-write.h" | - | ||||||
28 | #include "long-options.h" | - | ||||||
29 | - | |||||||
30 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||
31 | #define PROGRAM_NAME "yes" | - | ||||||
32 | - | |||||||
33 | #define AUTHORS proper_name ("David MacKenzie") | - | ||||||
34 | - | |||||||
35 | static struct option const long_options[] = | - | ||||||
36 | { | - | ||||||
37 | {NULL, 0, NULL, 0} | - | ||||||
38 | }; | - | ||||||
39 | - | |||||||
40 | void | - | ||||||
41 | usage (int status) | - | ||||||
42 | { | - | ||||||
43 | if (status != EXIT_SUCCESS)
| 3 | ||||||
44 | emit_try_help (); executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
45 | else | - | ||||||
46 | { | - | ||||||
47 | printf (_("\ | - | ||||||
48 | Usage: %s [STRING]...\n\ | - | ||||||
49 | or: %s OPTION\n\ | - | ||||||
50 | "), | - | ||||||
51 | program_name, program_name); | - | ||||||
52 | - | |||||||
53 | fputs (_("\ | - | ||||||
54 | Repeatedly output a line with all specified STRING(s), or 'y'.\n\ | - | ||||||
55 | \n\ | - | ||||||
56 | "), stdout); | - | ||||||
57 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||
58 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||
59 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||
60 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
61 | exit (status); executed 6 times by 1 test: exit (status); Executed by:
| 6 | ||||||
62 | } | - | ||||||
63 | - | |||||||
64 | int | - | ||||||
65 | main (int argc, char **argv) | - | ||||||
66 | { | - | ||||||
67 | initialize_main (&argc, &argv); | - | ||||||
68 | set_program_name (argv[0]); | - | ||||||
69 | setlocale (LC_ALL, ""); | - | ||||||
70 | bindtextdomain (PACKAGE, LOCALEDIR); | - | ||||||
71 | textdomain (PACKAGE); | - | ||||||
72 | - | |||||||
73 | atexit (close_stdout); | - | ||||||
74 | - | |||||||
75 | parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, | - | ||||||
76 | usage, AUTHORS, (char const *) NULL); | - | ||||||
77 | if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
| 3-47 | ||||||
78 | usage (EXIT_FAILURE); executed 3 times by 1 test: usage ( 1 ); Executed by:
| 3 | ||||||
79 | - | |||||||
80 | char **operands = argv + optind; | - | ||||||
81 | char **operand_lim = argv + argc; | - | ||||||
82 | if (optind == argc)
| 23-24 | ||||||
83 | *operand_lim++ = bad_cast ("y"); executed 23 times by 1 test: *operand_lim++ = bad_cast ("y"); Executed by:
| 23 | ||||||
84 | - | |||||||
85 | /* Buffer data locally once, rather than having the | - | ||||||
86 | large overhead of stdio buffering each item. */ | - | ||||||
87 | size_t bufalloc = 0; | - | ||||||
88 | bool reuse_operand_strings = true; | - | ||||||
89 | for (char **operandp = operands; operandp < operand_lim; operandp++)
| 47-4145 | ||||||
90 | { | - | ||||||
91 | size_t operand_len = strlen (*operandp); | - | ||||||
92 | bufalloc += operand_len + 1; | - | ||||||
93 | if (operandp + 1 < operand_lim
| 47-4098 | ||||||
94 | && *operandp + operand_len + 1 != operandp[1])
| 0-4098 | ||||||
95 | reuse_operand_strings = false; never executed: reuse_operand_strings = 0 ; | 0 | ||||||
96 | } executed 4145 times by 1 test: end of block Executed by:
| 4145 | ||||||
97 | - | |||||||
98 | /* Improve performance by using a buffer size greater than BUFSIZ / 2. */ | - | ||||||
99 | if (bufalloc <= BUFSIZ / 2)
| 7-40 | ||||||
100 | { | - | ||||||
101 | bufalloc = BUFSIZ; | - | ||||||
102 | reuse_operand_strings = false; | - | ||||||
103 | } executed 40 times by 1 test: end of block Executed by:
| 40 | ||||||
104 | - | |||||||
105 | /* Fill the buffer with one copy of the output. If possible, reuse | - | ||||||
106 | the operands strings; this wins when the buffer would be large. */ | - | ||||||
107 | char *buf = reuse_operand_strings ? *operands : xmalloc (bufalloc);
| 7-40 | ||||||
108 | size_t bufused = 0; | - | ||||||
109 | for (char **operandp = operands; operandp < operand_lim; operandp++)
| 47-4145 | ||||||
110 | { | - | ||||||
111 | size_t operand_len = strlen (*operandp); | - | ||||||
112 | if (! reuse_operand_strings)
| 139-4006 | ||||||
113 | memcpy (buf + bufused, *operandp, operand_len); executed 139 times by 1 test: memcpy (buf + bufused, *operandp, operand_len); Executed by:
| 139 | ||||||
114 | bufused += operand_len; | - | ||||||
115 | buf[bufused++] = ' '; | - | ||||||
116 | } executed 4145 times by 1 test: end of block Executed by:
| 4145 | ||||||
117 | buf[bufused - 1] = '\n'; | - | ||||||
118 | - | |||||||
119 | /* If a larger buffer was allocated, fill it by repeating the buffer | - | ||||||
120 | contents. */ | - | ||||||
121 | size_t copysize = bufused; | - | ||||||
122 | for (size_t copies = bufalloc / copysize; --copies; )
| 47-109414 | ||||||
123 | { | - | ||||||
124 | memcpy (buf + bufused, buf, copysize); | - | ||||||
125 | bufused += copysize; | - | ||||||
126 | } executed 109414 times by 1 test: end of block Executed by:
| 109414 | ||||||
127 | - | |||||||
128 | /* Repeatedly output the buffer until there is a write error; then fail. */ | - | ||||||
129 | while (full_write (STDOUT_FILENO, buf, bufused) == bufused)
| 45-8995 | ||||||
130 | continue; executed 8995 times by 1 test: continue; Executed by:
| 8995 | ||||||
131 | error (0, errno, _("standard output")); | - | ||||||
132 | return EXIT_FAILURE; executed 45 times by 1 test: return 1 ; Executed by:
| 45 | ||||||
133 | } | - | ||||||
Source code | Switch to Preprocessed file |