1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 86, 89, 91, 1995-2002, 2004, 2005 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>
29 #include "safe-read.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "sum"
34 #define AUTHORS "Kayvan Aghaiepour", "David MacKenzie"
36 /* The name this program was run with. */
39 /* True if any of the files read were the standard input. */
40 static bool have_read_stdin;
42 static struct option const longopts[] =
44 {"sysv", no_argument, NULL, 's'},
45 {GETOPT_HELP_OPTION_DECL},
46 {GETOPT_VERSION_OPTION_DECL},
53 if (status != EXIT_SUCCESS)
54 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 Usage: %s [OPTION]... [FILE]...\n\
63 Print checksum and block counts for each FILE.\n\
65 -r defeat -s, 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_bug_reporting_address ();
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 freopen (NULL, "rb", stdin);
104 fp = fopen (file, (O_BINARY ? "rb" : "r"));
107 error (0, errno, "%s", file);
112 while ((ch = getc (fp)) != EOF)
115 checksum = (checksum >> 1) + ((checksum & 1) << 15);
117 checksum &= 0xffff; /* Keep it within bounds. */
122 error (0, errno, "%s", file);
128 if (!is_stdin && fclose (fp) != 0)
130 error (0, errno, "%s", file);
134 printf ("%05d %5s", checksum,
135 human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));
137 printf (" %s", file);
143 /* Calculate and print the checksum and the size in 512-byte blocks
144 of file FILE, or of the standard input if FILE is "-".
145 If PRINT_NAME is >0, print FILE next to the checksum and size.
146 Return true if successful. */
149 sysv_sum_file (const char *file, int print_name)
152 unsigned char buf[8192];
153 uintmax_t total_bytes = 0;
154 char hbuf[LONGEST_HUMAN_READABLE + 1];
158 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
161 bool is_stdin = STREQ (file, "-");
166 have_read_stdin = true;
167 if (O_BINARY && ! isatty (STDIN_FILENO))
168 freopen (NULL, "rb", stdin);
172 fd = open (file, O_RDONLY | O_BINARY);
175 error (0, errno, "%s", file);
183 size_t bytes_read = safe_read (fd, buf, sizeof buf);
188 if (bytes_read == SAFE_READ_ERROR)
190 error (0, errno, "%s", file);
196 for (i = 0; i < bytes_read; i++)
198 total_bytes += bytes_read;
201 if (!is_stdin && close (fd) != 0)
203 error (0, errno, "%s", file);
207 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
208 checksum = (r & 0xffff) + (r >> 16);
210 printf ("%d %s", checksum,
211 human_readable (total_bytes, hbuf, human_ceiling, 1, 512));
213 printf (" %s", file);
220 main (int argc, char **argv)
225 bool (*sum_func) (const char *, int) = bsd_sum_file;
227 initialize_main (&argc, &argv);
228 program_name = argv[0];
229 setlocale (LC_ALL, "");
230 bindtextdomain (PACKAGE, LOCALEDIR);
231 textdomain (PACKAGE);
233 atexit (close_stdout);
235 have_read_stdin = false;
237 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
241 case 'r': /* For SysV compatibility. */
242 sum_func = bsd_sum_file;
246 sum_func = sysv_sum_file;
249 case_GETOPT_HELP_CHAR;
251 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
254 usage (EXIT_FAILURE);
258 files_given = argc - optind;
259 if (files_given <= 0)
260 ok = sum_func ("-", files_given);
262 for (ok = true; optind < argc; optind++)
263 ok &= sum_func (argv[optind], files_given);
265 if (have_read_stdin && fclose (stdin) == EOF)
266 error (EXIT_FAILURE, errno, "-");
267 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);