OpenCoverage

sum.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/sum.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* sum -- checksum and count the blocks in a file-
2 Copyright (C) 1986-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/* Like BSD sum or SysV sum -r, except like SysV sum if -s option is given. */-
18-
19/* Written by Kayvan Aghaiepour and David MacKenzie. */-
20-
21#include <config.h>-
22-
23#include <stdio.h>-
24#include <sys/types.h>-
25#include <getopt.h>-
26#include "system.h"-
27#include "die.h"-
28#include "error.h"-
29#include "fadvise.h"-
30#include "human.h"-
31#include "safe-read.h"-
32#include "xbinary-io.h"-
33-
34/* The official name of this program (e.g., no 'g' prefix). */-
35#define PROGRAM_NAME "sum"-
36-
37#define AUTHORS \-
38 proper_name ("Kayvan Aghaiepour"), \-
39 proper_name ("David MacKenzie")-
40-
41/* True if any of the files read were the standard input. */-
42static bool have_read_stdin;-
43-
44static struct option const longopts[] =-
45{-
46 {"sysv", no_argument, NULL, 's'},-
47 {GETOPT_HELP_OPTION_DECL},-
48 {GETOPT_VERSION_OPTION_DECL},-
49 {NULL, 0, NULL, 0}-
50};-
51-
52void-
53usage (int status)-
54{-
55 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 6 times by 1 test
Evaluated by:
  • sum
3-6
56 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • sum
3
57 else-
58 {-
59 printf (_("\-
60Usage: %s [OPTION]... [FILE]...\n\-
61"),-
62 program_name);-
63 fputs (_("\-
64Print checksum and block counts for each FILE.\n\-
65"), stdout);-
66-
67 emit_stdin_note ();-
68-
69 fputs (_("\-
70\n\-
71 -r use BSD sum algorithm, use 1K blocks\n\-
72 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\-
73"), stdout);-
74 fputs (HELP_OPTION_DESCRIPTION, stdout);-
75 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
76 emit_ancillary_info (PROGRAM_NAME);-
77 }
executed 6 times by 1 test: end of block
Executed by:
  • sum
6
78 exit (status);
executed 9 times by 1 test: exit (status);
Executed by:
  • sum
9
79}-
80-
81/* Calculate and print the rotated checksum and the size in 1K blocks-
82 of file FILE, or of the standard input if FILE is "-".-
83 If PRINT_NAME is >1, print FILE next to the checksum and size.-
84 The checksum varies depending on sizeof (int).-
85 Return true if successful. */-
86-
87static bool-
88bsd_sum_file (const char *file, int print_name)-
89{-
90 FILE *fp;-
91 int checksum = 0; /* The checksum mod 2^16. */-
92 uintmax_t total_bytes = 0; /* The number of bytes. */-
93 int ch; /* Each character read. */-
94 char hbuf[LONGEST_HUMAN_READABLE + 1];-
95 bool is_stdin = STREQ (file, "-");
never executed: __result = (((const unsigned char *) (const char *) ( file ))[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:
  • sum
__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 11 times by 1 test
Evaluated by:
  • sum
FALSEnever evaluated
__result == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • sum
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-11
96-
97 if (is_stdin)
is_stdinDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
2-9
98 {-
99 fp = stdin;-
100 have_read_stdin = true;-
101 xset_binary_mode (STDIN_FILENO, O_BINARY);-
102 }
executed 2 times by 1 test: end of block
Executed by:
  • sum
2
103 else-
104 {-
105 fp = fopen (file, (O_BINARY ? "rb" : "r"));-
106 if (fp == NULL)
fp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
0-9
107 {-
108 error (0, errno, "%s", quotef (file));-
109 return false;
never executed: return 0 ;
0
110 }-
111 }
executed 9 times by 1 test: end of block
Executed by:
  • sum
9
112-
113 fadvise (fp, FADVISE_SEQUENTIAL);-
114-
115 while ((ch = getc (fp)) != EOF)
(ch = getc_unl... (fp)) != (-1)Description
TRUEevaluated 3275 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 11 times by 1 test
Evaluated by:
  • sum
11-3275
116 {-
117 total_bytes++;-
118 checksum = (checksum >> 1) + ((checksum & 1) << 15);-
119 checksum += ch;-
120 checksum &= 0xffff; /* Keep it within bounds. */-
121 }
executed 3275 times by 1 test: end of block
Executed by:
  • sum
3275
122-
123 if (ferror (fp))
ferror_unlocked (fp)Description
TRUEnever evaluated
FALSEevaluated 11 times by 1 test
Evaluated by:
  • sum
0-11
124 {-
125 error (0, errno, "%s", quotef (file));-
126 if (!is_stdin)
!is_stdinDescription
TRUEnever evaluated
FALSEnever evaluated
0
127 fclose (fp);
never executed: rpl_fclose (fp);
0
128 return false;
never executed: return 0 ;
0
129 }-
130-
131 if (!is_stdin && fclose (fp) != 0)
!is_stdinDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 2 times by 1 test
Evaluated by:
  • sum
rpl_fclose (fp) != 0Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
0-9
132 {-
133 error (0, errno, "%s", quotef (file));-
134 return false;
never executed: return 0 ;
0
135 }-
136-
137 printf ("%05d %5s", checksum,-
138 human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));-
139 if (print_name > 1)
print_name > 1Description
TRUEnever evaluated
FALSEevaluated 11 times by 1 test
Evaluated by:
  • sum
0-11
140 printf (" %s", file);
never executed: printf (" %s", file);
0
141 putchar ('\n');-
142-
143 return true;
executed 11 times by 1 test: return 1 ;
Executed by:
  • sum
11
144}-
145-
146/* Calculate and print the checksum and the size in 512-byte blocks-
147 of file FILE, or of the standard input if FILE is "-".-
148 If PRINT_NAME is >0, print FILE next to the checksum and size.-
149 Return true if successful. */-
150-
151static bool-
152sysv_sum_file (const char *file, int print_name)-
153{-
154 int fd;-
155 unsigned char buf[8192];-
156 uintmax_t total_bytes = 0;-
157 char hbuf[LONGEST_HUMAN_READABLE + 1];-
158 int r;-
159 int checksum;-
160-
161 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */-
162 unsigned int s = 0;-
163-
164 bool is_stdin = STREQ (file, "-");
never executed: __result = (((const unsigned char *) (const char *) ( file ))[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:
  • sum
__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 11 times by 1 test
Evaluated by:
  • sum
FALSEnever evaluated
__result == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
__s2_len > 1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • sum
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-11
165-
166 if (is_stdin)
is_stdinDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
2-9
167 {-
168 fd = STDIN_FILENO;-
169 have_read_stdin = true;-
170 xset_binary_mode (STDIN_FILENO, O_BINARY);-
171 }
executed 2 times by 1 test: end of block
Executed by:
  • sum
2
172 else-
173 {-
174 fd = open (file, O_RDONLY | O_BINARY);-
175 if (fd == -1)
fd == -1Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
0-9
176 {-
177 error (0, errno, "%s", quotef (file));-
178 return false;
never executed: return 0 ;
0
179 }-
180 }
executed 9 times by 1 test: end of block
Executed by:
  • sum
9
181-
182 while (1)-
183 {-
184 size_t bytes_read = safe_read (fd, buf, sizeof buf);-
185-
186 if (bytes_read == 0)
bytes_read == 0Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 4122 times by 1 test
Evaluated by:
  • sum
11-4122
187 break;
executed 11 times by 1 test: break;
Executed by:
  • sum
11
188-
189 if (bytes_read == SAFE_READ_ERROR)
bytes_read == ((size_t) -1)Description
TRUEnever evaluated
FALSEevaluated 4122 times by 1 test
Evaluated by:
  • sum
0-4122
190 {-
191 error (0, errno, "%s", quotef (file));-
192 if (!is_stdin)
!is_stdinDescription
TRUEnever evaluated
FALSEnever evaluated
0
193 close (fd);
never executed: close (fd);
0
194 return false;
never executed: return 0 ;
0
195 }-
196-
197 for (size_t i = 0; i < bytes_read; i++)
i < bytes_readDescription
TRUEevaluated 33689277 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 4122 times by 1 test
Evaluated by:
  • sum
4122-33689277
198 s += buf[i];
executed 33689277 times by 1 test: s += buf[i];
Executed by:
  • sum
33689277
199 total_bytes += bytes_read;-
200 }
executed 4122 times by 1 test: end of block
Executed by:
  • sum
4122
201-
202 if (!is_stdin && close (fd) != 0)
!is_stdinDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 2 times by 1 test
Evaluated by:
  • sum
close (fd) != 0Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • sum
0-9
203 {-
204 error (0, errno, "%s", quotef (file));-
205 return false;
never executed: return 0 ;
0
206 }-
207-
208 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);-
209 checksum = (r & 0xffff) + (r >> 16);-
210-
211 printf ("%d %s", checksum,-
212 human_readable (total_bytes, hbuf, human_ceiling, 1, 512));-
213 if (print_name)
print_nameDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 2 times by 1 test
Evaluated by:
  • sum
2-9
214 printf (" %s", file);
executed 9 times by 1 test: printf (" %s", file);
Executed by:
  • sum
9
215 putchar ('\n');-
216-
217 return true;
executed 11 times by 1 test: return 1 ;
Executed by:
  • sum
11
218}-
219-
220int-
221main (int argc, char **argv)-
222{-
223 bool ok;-
224 int optc;-
225 int files_given;-
226 bool (*sum_func) (const char *, int) = bsd_sum_file;-
227-
228 initialize_main (&argc, &argv);-
229 set_program_name (argv[0]);-
230 setlocale (LC_ALL, "");-
231 bindtextdomain (PACKAGE, LOCALEDIR);-
232 textdomain (PACKAGE);-
233-
234 atexit (close_stdout);-
235-
236 /* Line buffer stdout to ensure lines are written atomically and immediately-
237 so that processes running in parallel do not intersperse their output. */-
238 setvbuf (stdout, NULL, _IOLBF, 0);-
239-
240 have_read_stdin = false;-
241-
242 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 22 times by 1 test
Evaluated by:
  • sum
22-30
243 {-
244 switch (optc)-
245 {-
246 case 'r': /* For SysV compatibility. */
executed 3 times by 1 test: case 'r':
Executed by:
  • sum
3
247 sum_func = bsd_sum_file;-
248 break;
executed 3 times by 1 test: break;
Executed by:
  • sum
3
249-
250 case 's':
executed 13 times by 1 test: case 's':
Executed by:
  • sum
13
251 sum_func = sysv_sum_file;-
252 break;
executed 13 times by 1 test: break;
Executed by:
  • sum
13
253-
254 case_GETOPT_HELP_CHAR;
never executed: break;
executed 6 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • sum
0-6
255-
256 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 5 times by 1 test: exit ( 0 );
Executed by:
  • sum
never executed: break;
executed 5 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • sum
0-5
257-
258 default:
executed 3 times by 1 test: default:
Executed by:
  • sum
3
259 usage (EXIT_FAILURE);-
260 }
never executed: end of block
0
261 }-
262-
263 files_given = argc - optind;-
264 if (files_given <= 0)
files_given <= 0Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 18 times by 1 test
Evaluated by:
  • sum
4-18
265 ok = sum_func ("-", files_given);
executed 4 times by 1 test: ok = sum_func ("-", files_given);
Executed by:
  • sum
4
266 else-
267 for (ok = true; optind < argc; optind++)
optind < argcDescription
TRUEevaluated 18 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 18 times by 1 test
Evaluated by:
  • sum
18
268 ok &= sum_func (argv[optind], files_given);
executed 18 times by 1 test: ok &= sum_func (argv[optind], files_given);
Executed by:
  • sum
18
269-
270 if (have_read_stdin && fclose (stdin) == EOF)
have_read_stdinDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • sum
FALSEevaluated 18 times by 1 test
Evaluated by:
  • sum
rpl_fclose ( stdin ) == (-1)Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • sum
0-18
271 die (EXIT_FAILURE, errno, "%s", quotef ("-"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, \"-\")), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ())...yle_colon (0, shell_escape_quoting_style, "-")), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, "-")), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
272 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 22 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • sum
22
273}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2