OpenCoverage

fold.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/fold.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* fold -- wrap each input line to fit in specified width.-
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/* Written by David MacKenzie, djm@gnu.ai.mit.edu. */-
18-
19#include <config.h>-
20-
21#include <stdio.h>-
22#include <getopt.h>-
23#include <sys/types.h>-
24-
25#include "system.h"-
26#include "die.h"-
27#include "error.h"-
28#include "fadvise.h"-
29#include "xdectoint.h"-
30-
31#define TAB_WIDTH 8-
32-
33/* The official name of this program (e.g., no 'g' prefix). */-
34#define PROGRAM_NAME "fold"-
35-
36#define AUTHORS proper_name ("David MacKenzie")-
37-
38/* If nonzero, try to break on whitespace. */-
39static bool break_spaces;-
40-
41/* If nonzero, count bytes, not column positions. */-
42static bool count_bytes;-
43-
44/* If nonzero, at least one of the files we read was standard input. */-
45static bool have_read_stdin;-
46-
47static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";-
48-
49static struct option const longopts[] =-
50{-
51 {"bytes", no_argument, NULL, 'b'},-
52 {"spaces", no_argument, NULL, 's'},-
53 {"width", required_argument, NULL, 'w'},-
54 {GETOPT_HELP_OPTION_DECL},-
55 {GETOPT_VERSION_OPTION_DECL},-
56 {NULL, 0, NULL, 0}-
57};-
58-
59void-
60usage (int status)-
61{-
62 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 7 times by 1 test
Evaluated by:
  • fold
3-7
63 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • fold
3
64 else-
65 {-
66 printf (_("\-
67Usage: %s [OPTION]... [FILE]...\n\-
68"),-
69 program_name);-
70 fputs (_("\-
71Wrap input lines in each FILE, writing to standard output.\n\-
72"), stdout);-
73-
74 emit_stdin_note ();-
75 emit_mandatory_arg_note ();-
76-
77 fputs (_("\-
78 -b, --bytes count bytes rather than columns\n\-
79 -s, --spaces break at spaces\n\-
80 -w, --width=WIDTH use WIDTH columns instead of 80\n\-
81"), stdout);-
82 fputs (HELP_OPTION_DESCRIPTION, stdout);-
83 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
84 emit_ancillary_info (PROGRAM_NAME);-
85 }
executed 7 times by 1 test: end of block
Executed by:
  • fold
7
86 exit (status);
executed 10 times by 1 test: exit (status);
Executed by:
  • fold
10
87}-
88-
89/* Assuming the current column is COLUMN, return the column that-
90 printing C will move the cursor to.-
91 The first column is 0. */-
92-
93static size_t-
94adjust_column (size_t column, char c)-
95{-
96 if (!count_bytes)
!count_bytesDescription
TRUEevaluated 47 times by 1 test
Evaluated by:
  • fold
FALSEnever evaluated
0-47
97 {-
98 if (c == '\b')
c == '\b'Description
TRUEnever evaluated
FALSEevaluated 47 times by 1 test
Evaluated by:
  • fold
0-47
99 {-
100 if (column > 0)
column > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
101 column--;
never executed: column--;
0
102 }
never executed: end of block
0
103 else if (c == '\r')
c == '\r'Description
TRUEnever evaluated
FALSEevaluated 47 times by 1 test
Evaluated by:
  • fold
0-47
104 column = 0;
never executed: column = 0;
0
105 else if (c == '\t')
c == '\t'Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 45 times by 1 test
Evaluated by:
  • fold
2-45
106 column += TAB_WIDTH - column % TAB_WIDTH;
executed 2 times by 1 test: column += 8 - column % 8;
Executed by:
  • fold
2
107 else /* if (isprint (c)) */-
108 column++;
executed 45 times by 1 test: column++;
Executed by:
  • fold
45
109 }-
110 else-
111 column++;
never executed: column++;
0
112 return column;
executed 47 times by 1 test: return column;
Executed by:
  • fold
47
113}-
114-
115/* Fold file FILENAME, or standard input if FILENAME is "-",-
116 to stdout, with maximum line length WIDTH.-
117 Return true if successful. */-
118-
119static bool-
120fold_file (char const *filename, size_t width)-
121{-
122 FILE *istream;-
123 int c;-
124 size_t column = 0; /* Screen column where next char will go. */-
125 size_t offset_out = 0; /* Index in 'line_out' for next char. */-
126 static char *line_out = NULL;-
127 static size_t allocated_out = 0;-
128 int saved_errno;-
129-
130 if (STREQ (filename, "-"))
never executed: __result = (((const unsigned char *) (const char *) ( filename ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
executed 2 times by 1 test: end of block
Executed by:
  • fold
( __extension_...)))); }) == 0)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • fold
FALSEnever evaluated
__result == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • fold
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-6
131 {-
132 istream = stdin;-
133 have_read_stdin = true;-
134 }
executed 2 times by 1 test: end of block
Executed by:
  • fold
2
135 else-
136 istream = fopen (filename, "r");
executed 4 times by 1 test: istream = fopen (filename, "r");
Executed by:
  • fold
4
137-
138 if (istream == NULL)
istream == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • fold
0-6
139 {-
140 error (0, errno, "%s", quotef (filename));-
141 return false;
never executed: return 0 ;
0
142 }-
143-
144 fadvise (istream, FADVISE_SEQUENTIAL);-
145-
146 while ((c = getc (istream)) != EOF)
(c = getc_unlo...ream)) != (-1)Description
TRUEevaluated 44 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 6 times by 1 test
Evaluated by:
  • fold
6-44
147 {-
148 if (offset_out + 1 >= allocated_out)
offset_out + 1... allocated_outDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 38 times by 1 test
Evaluated by:
  • fold
6-38
149 line_out = X2REALLOC (line_out, &allocated_out);
executed 6 times by 1 test: line_out = ((void) (!!sizeof (struct { _Static_assert (sizeof *(line_out) == 1, "verify_true (" "sizeof *(line_out) == 1" ")"); int _gl_dummy; })), x2realloc (line_out, &allocated_out));
Executed by:
  • fold
6
150-
151 if (c == '\n')
c == '\n'Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 39 times by 1 test
Evaluated by:
  • fold
5-39
152 {-
153 line_out[offset_out++] = c;-
154 fwrite (line_out, sizeof (char), offset_out, stdout);
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
0
155 column = offset_out = 0;-
156 continue;
executed 5 times by 1 test: continue;
Executed by:
  • fold
5
157 }-
158-
159 rescan:
code before this statement executed 39 times by 1 test: rescan:
Executed by:
  • fold
39
160 column = adjust_column (column, c);-
161-
162 if (column > width)
column > widthDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 38 times by 1 test
Evaluated by:
  • fold
6-38
163 {-
164 /* This character would make the line too long.-
165 Print the line plus a newline, and make this character-
166 start the next line. */-
167 if (break_spaces)
break_spacesDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • fold
FALSEnever evaluated
0-6
168 {-
169 bool found_blank = false;-
170 size_t logical_end = offset_out;-
171-
172 /* Look for the last blank. */-
173 while (logical_end)
logical_endDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 3 times by 1 test
Evaluated by:
  • fold
3-11
174 {-
175 --logical_end;-
176 if (isblank (to_uchar (line_out[logical_end])))
((*__ctype_b_l...int) _ISblank)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 8 times by 1 test
Evaluated by:
  • fold
3-8
177 {-
178 found_blank = true;-
179 break;
executed 3 times by 1 test: break;
Executed by:
  • fold
3
180 }-
181 }
executed 8 times by 1 test: end of block
Executed by:
  • fold
8
182-
183 if (found_blank)
found_blankDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 3 times by 1 test
Evaluated by:
  • fold
3
184 {-
185 size_t i;-
186-
187 /* Found a blank. Don't output the part after it. */-
188 logical_end++;-
189 fwrite (line_out, sizeof (char), (size_t) logical_end,
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
0
190 stdout);-
191 putchar ('\n');-
192 /* Move the remainder to the beginning of the next line.-
193 The areas being copied here might overlap. */-
194 memmove (line_out, line_out + logical_end,-
195 offset_out - logical_end);-
196 offset_out -= logical_end;-
197 for (column = i = 0; i < offset_out; i++)
i < offset_outDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 3 times by 1 test
Evaluated by:
  • fold
3
198 column = adjust_column (column, line_out[i]);
executed 3 times by 1 test: column = adjust_column (column, line_out[i]);
Executed by:
  • fold
3
199 goto rescan;
executed 3 times by 1 test: goto rescan;
Executed by:
  • fold
3
200 }-
201 }
executed 3 times by 1 test: end of block
Executed by:
  • fold
3
202-
203 if (offset_out == 0)
offset_out == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • fold
FALSEevaluated 2 times by 1 test
Evaluated by:
  • fold
1-2
204 {-
205 line_out[offset_out++] = c;-
206 continue;
executed 1 time by 1 test: continue;
Executed by:
  • fold
1
207 }-
208-
209 line_out[offset_out++] = '\n';-
210 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
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
0
211 column = offset_out = 0;-
212 goto rescan;
executed 2 times by 1 test: goto rescan;
Executed by:
  • fold
2
213 }-
214-
215 line_out[offset_out++] = c;-
216 }
executed 38 times by 1 test: end of block
Executed by:
  • fold
38
217-
218 saved_errno = errno;-
219-
220 if (offset_out)
offset_outDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • fold
FALSEevaluated 5 times by 1 test
Evaluated by:
  • fold
1-5
221 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
executed 1 time by 1 test: (__extension__ ((__builtin_constant_p ( sizeof (char) ) && __builtin_constant_p ( (size_t) offset_out ) && (size_t) ( sizeof (char) ) * (size_t) ( (size_t) offset_out ) <= 8 && (size_t) ( sizeof (char) ) != 0) ? ({ const char *__ptr = (const char *) ( lin...ize_t) offset_out ) && (size_t) ( (size_t) offset_out ) == 0)) ? ((void) ( line_out ), (void) (stdout), (void) ( sizeof (char) ), (void) ( (size_t) offset_out ), (size_t) 0) : fwrite_unlocked ( line_out , sizeof (char) , (size_t) offset_out , stdout)))) ;
Executed by:
  • fold
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
0-1
222-
223 if (ferror (istream))
ferror_unlocked (istream)Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • fold
0-6
224 {-
225 error (0, saved_errno, "%s", quotef (filename));-
226 if (!STREQ (filename, "-"))
never executed: __result = (((const unsigned char *) (const char *) ( filename ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
!( __extension...)))); }) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
227 fclose (istream);
never executed: rpl_fclose (istream);
0
228 return false;
never executed: return 0 ;
0
229 }-
230 if (!STREQ (filename, "-") && fclose (istream) == EOF)
never executed: __result = (((const unsigned char *) (const char *) ( filename ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "-" ))[3] - __s2[3]);
never executed: end of block
executed 2 times by 1 test: end of block
Executed by:
  • fold
!( __extension...)))); }) == 0)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 2 times by 1 test
Evaluated by:
  • fold
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • fold
FALSEnever evaluated
__result == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • fold
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
rpl_fclose (istream) == (-1)Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
0-6
231 {-
232 error (0, errno, "%s", quotef (filename));-
233 return false;
never executed: return 0 ;
0
234 }-
235-
236 return true;
executed 6 times by 1 test: return 1 ;
Executed by:
  • fold
6
237}-
238-
239int-
240main (int argc, char **argv)-
241{-
242 size_t width = 80;-
243 int i;-
244 int optc;-
245 bool ok;-
246-
247 initialize_main (&argc, &argv);-
248 set_program_name (argv[0]);-
249 setlocale (LC_ALL, "");-
250 bindtextdomain (PACKAGE, LOCALEDIR);-
251 textdomain (PACKAGE);-
252-
253 atexit (close_stdout);-
254-
255 break_spaces = count_bytes = have_read_stdin = false;-
256-
257 while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 28 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 6 times by 1 test
Evaluated by:
  • fold
6-28
258 {-
259 char optargbuf[2];-
260-
261 switch (optc)-
262 {-
263 case 'b': /* Count bytes rather than columns. */
executed 2 times by 1 test: case 'b':
Executed by:
  • fold
2
264 count_bytes = true;-
265 break;
executed 2 times by 1 test: break;
Executed by:
  • fold
2
266-
267 case 's': /* Break at word boundaries. */
executed 6 times by 1 test: case 's':
Executed by:
  • fold
6
268 break_spaces = true;-
269 break;
executed 6 times by 1 test: break;
Executed by:
  • fold
6
270-
271 case '0': case '1': case '2': case '3': case '4':
never executed: case '0':
never executed: case '1':
never executed: case '2':
never executed: case '3':
never executed: case '4':
0
272 case '5': case '6': case '7': case '8': case '9':
never executed: case '5':
never executed: case '6':
never executed: case '7':
never executed: case '8':
never executed: case '9':
0
273 if (optarg)
optargDescription
TRUEnever evaluated
FALSEnever evaluated
0
274 optarg--;
never executed: optarg--;
0
275 else-
276 {-
277 optargbuf[0] = optc;-
278 optargbuf[1] = '\0';-
279 optarg = optargbuf;-
280 }
never executed: end of block
0
281 FALLTHROUGH;-
282 case 'w': /* Line width. */
code before this statement never executed: case 'w':
executed 6 times by 1 test: case 'w':
Executed by:
  • fold
0-6
283 width = xdectoumax (optarg, 1, SIZE_MAX - TAB_WIDTH - 1, "",-
284 _("invalid number of columns"), 0);-
285 break;
executed 4 times by 1 test: break;
Executed by:
  • fold
4
286-
287 case_GETOPT_HELP_CHAR;
never executed: break;
executed 7 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • fold
0-7
288-
289 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • fold
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • fold
0-4
290-
291 default:
executed 3 times by 1 test: default:
Executed by:
  • fold
3
292 usage (EXIT_FAILURE);-
293 }
never executed: end of block
0
294 }-
295-
296 if (argc == optind)
argc == optindDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
2-4
297 ok = fold_file ("-", width);
executed 2 times by 1 test: ok = fold_file ("-", width);
Executed by:
  • fold
2
298 else-
299 {-
300 ok = true;-
301 for (i = optind; i < argc; i++)
i < argcDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
4
302 ok &= fold_file (argv[i], width);
executed 4 times by 1 test: ok &= fold_file (argv[i], width);
Executed by:
  • fold
4
303 }
executed 4 times by 1 test: end of block
Executed by:
  • fold
4
304-
305 if (have_read_stdin && fclose (stdin) == EOF)
have_read_stdinDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • fold
FALSEevaluated 4 times by 1 test
Evaluated by:
  • fold
rpl_fclose ( stdin ) == (-1)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • fold
0-4
306 die (EXIT_FAILURE, errno, "-");
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"-\"), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , "-"), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "-"), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
307-
308 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 6 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • fold
6
309}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2