2 * sum -- checksum and count the blocks in a file
3 * Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
5 * Copyright (C) 86, 89, 91, 1995-2002, 2004 Free Software Foundation, Inc.
6 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
9 * Written by Kayvan Aghaiepour and David MacKenzie
10 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
37 /* 1 if any of the files read were the standard input */
38 static int have_read_stdin;
40 /* make a little more readable and avoid using strcmp for just 2 bytes */
41 #define IS_STDIN(s) (s[0] == '-' && s[1] == '\0')
43 /* Calculate and print the rotated checksum and the size in 1K blocks
44 of file FILE, or of the standard input if FILE is "-".
45 If PRINT_NAME is >1, print FILE next to the checksum and size.
46 The checksum varies depending on sizeof (int).
47 Return 1 if successful. */
48 static int bsd_sum_file(const char *file, int print_name)
51 register int checksum = 0; /* The checksum mod 2^16. */
52 register uintmax_t total_bytes = 0; /* The number of bytes. */
53 register int ch; /* Each character read. */
59 fp = fopen(file, "r");
61 bb_perror_msg("%s", file);
66 while ((ch = getc(fp)) != EOF) {
68 checksum = (checksum >> 1) + ((checksum & 1) << 15);
70 checksum &= 0xffff; /* Keep it within bounds. */
74 bb_perror_msg("%s", file);
80 if (!IS_STDIN(file) && fclose(fp) == EOF) {
81 bb_perror_msg("%s", file);
85 printf("%05d %5s", checksum,
86 make_human_readable_str(total_bytes, 1, 1024));
94 /* Calculate and print the checksum and the size in 512-byte blocks
95 of file FILE, or of the standard input if FILE is "-".
96 If PRINT_NAME is >0, print FILE next to the checksum and size.
97 Return 1 if successful. */
98 static int sysv_sum_file(const char *file, int print_name)
101 unsigned char buf[8192];
102 uintmax_t total_bytes = 0;
106 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
109 if (IS_STDIN(file)) {
113 fd = open(file, O_RDONLY);
115 bb_perror_msg("%s", file);
122 size_t bytes_read = safe_read(fd, buf, sizeof(buf));
127 if (bytes_read == -1) {
128 bb_perror_msg("%s", file);
134 for (i = 0; i < bytes_read; i++)
136 total_bytes += bytes_read;
139 if (!IS_STDIN(file) && close(fd) == -1) {
140 bb_perror_msg("%s", file);
144 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
145 checksum = (r & 0xffff) + (r >> 16);
147 printf("%d %s", checksum,
148 make_human_readable_str(total_bytes, 1, 512));
156 int sum_main(int argc, char **argv)
161 int (*sum_func)(const char *, int) = bsd_sum_file;
163 /* give the bsd func priority over sysv func */
164 flags = bb_getopt_ulflags(argc, argv, "sr");
166 sum_func = sysv_sum_file;
168 sum_func = bsd_sum_file;
171 files_given = argc - optind;
172 if (files_given <= 0)
173 ok = sum_func("-", files_given);
175 for (ok = 1; optind < argc; optind++)
176 ok &= sum_func(argv[optind], files_given);
178 if (have_read_stdin && fclose(stdin) == EOF)
179 bb_perror_msg_and_die("-");
181 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);