1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 1986-2013 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Like BSD sum or SysV sum -r, except like SysV sum if -s option is given. */
19 /* Written by Kayvan Aghaiepour and David MacKenzie. */
24 #include <sys/types.h>
30 #include "safe-read.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "sum"
37 proper_name ("Kayvan Aghaiepour"), \
38 proper_name ("David MacKenzie")
40 /* True if any of the files read were the standard input. */
41 static bool have_read_stdin
;
43 static struct option
const longopts
[] =
45 {"sysv", no_argument
, NULL
, 's'},
46 {GETOPT_HELP_OPTION_DECL
},
47 {GETOPT_VERSION_OPTION_DECL
},
54 if (status
!= EXIT_SUCCESS
)
59 Usage: %s [OPTION]... [FILE]...\n\
63 Print checksum and block counts for each FILE.\n\
65 -r use BSD sum algorithm, use 1K blocks\n\
66 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
68 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
69 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
72 With no FILE, or when FILE is -, read standard input.\n\
74 emit_ancillary_info ();
79 /* Calculate and print the rotated checksum and the size in 1K blocks
80 of file FILE, or of the standard input if FILE is "-".
81 If PRINT_NAME is >1, print FILE next to the checksum and size.
82 The checksum varies depending on sizeof (int).
83 Return true if successful. */
86 bsd_sum_file (const char *file
, int print_name
)
89 int checksum
= 0; /* The checksum mod 2^16. */
90 uintmax_t total_bytes
= 0; /* The number of bytes. */
91 int ch
; /* Each character read. */
92 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
93 bool is_stdin
= STREQ (file
, "-");
98 have_read_stdin
= true;
99 if (O_BINARY
&& ! isatty (STDIN_FILENO
))
100 xfreopen (NULL
, "rb", stdin
);
104 fp
= fopen (file
, (O_BINARY
? "rb" : "r"));
107 error (0, errno
, "%s", file
);
112 fadvise (fp
, FADVISE_SEQUENTIAL
);
114 while ((ch
= getc (fp
)) != EOF
)
117 checksum
= (checksum
>> 1) + ((checksum
& 1) << 15);
119 checksum
&= 0xffff; /* Keep it within bounds. */
124 error (0, errno
, "%s", file
);
130 if (!is_stdin
&& fclose (fp
) != 0)
132 error (0, errno
, "%s", file
);
136 printf ("%05d %5s", checksum
,
137 human_readable (total_bytes
, hbuf
, human_ceiling
, 1, 1024));
139 printf (" %s", file
);
145 /* Calculate and print the checksum and the size in 512-byte blocks
146 of file FILE, or of the standard input if FILE is "-".
147 If PRINT_NAME is >0, print FILE next to the checksum and size.
148 Return true if successful. */
151 sysv_sum_file (const char *file
, int print_name
)
154 unsigned char buf
[8192];
155 uintmax_t total_bytes
= 0;
156 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
160 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
163 bool is_stdin
= STREQ (file
, "-");
168 have_read_stdin
= true;
169 if (O_BINARY
&& ! isatty (STDIN_FILENO
))
170 xfreopen (NULL
, "rb", stdin
);
174 fd
= open (file
, O_RDONLY
| O_BINARY
);
177 error (0, errno
, "%s", file
);
185 size_t bytes_read
= safe_read (fd
, buf
, sizeof buf
);
190 if (bytes_read
== SAFE_READ_ERROR
)
192 error (0, errno
, "%s", file
);
198 for (i
= 0; i
< bytes_read
; i
++)
200 total_bytes
+= bytes_read
;
203 if (!is_stdin
&& close (fd
) != 0)
205 error (0, errno
, "%s", file
);
209 r
= (s
& 0xffff) + ((s
& 0xffffffff) >> 16);
210 checksum
= (r
& 0xffff) + (r
>> 16);
212 printf ("%d %s", checksum
,
213 human_readable (total_bytes
, hbuf
, human_ceiling
, 1, 512));
215 printf (" %s", file
);
222 main (int argc
, char **argv
)
227 bool (*sum_func
) (const char *, int) = bsd_sum_file
;
229 initialize_main (&argc
, &argv
);
230 set_program_name (argv
[0]);
231 setlocale (LC_ALL
, "");
232 bindtextdomain (PACKAGE
, LOCALEDIR
);
233 textdomain (PACKAGE
);
235 atexit (close_stdout
);
237 /* Line buffer stdout to ensure lines are written atomically and immediately
238 so that processes running in parallel do not intersperse their output. */
239 setvbuf (stdout
, NULL
, _IOLBF
, 0);
241 have_read_stdin
= false;
243 while ((optc
= getopt_long (argc
, argv
, "rs", longopts
, NULL
)) != -1)
247 case 'r': /* For SysV compatibility. */
248 sum_func
= bsd_sum_file
;
252 sum_func
= sysv_sum_file
;
255 case_GETOPT_HELP_CHAR
;
257 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
260 usage (EXIT_FAILURE
);
264 files_given
= argc
- optind
;
265 if (files_given
<= 0)
266 ok
= sum_func ("-", files_given
);
268 for (ok
= true; optind
< argc
; optind
++)
269 ok
&= sum_func (argv
[optind
], files_given
);
271 if (have_read_stdin
&& fclose (stdin
) == EOF
)
272 error (EXIT_FAILURE
, errno
, "-");
273 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);