1 /* vi: set sw=4 ts=4: */
3 * sum -- checksum and count the blocks in a file
4 * Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
6 * Copyright (C) 86, 89, 91, 1995-2002, 2004 Free Software Foundation, Inc.
7 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
8 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
10 * Written by Kayvan Aghaiepour and David MacKenzie
11 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
13 * Licensed under the GPL v2, see the file LICENSE in this tarball.
17 #include <sys/types.h>
24 /* 1 if any of the files read were the standard input */
25 static int have_read_stdin;
27 /* make a little more readable and avoid using strcmp for just 2 bytes */
28 #define IS_STDIN(s) (s[0] == '-' && s[1] == '\0')
30 /* Calculate and print the rotated checksum and the size in 1K blocks
31 of file FILE, or of the standard input if FILE is "-".
32 If PRINT_NAME is >1, print FILE next to the checksum and size.
33 The checksum varies depending on sizeof (int).
34 Return 1 if successful. */
35 static int bsd_sum_file(const char *file, int print_name)
38 int checksum = 0; /* The checksum mod 2^16. */
39 uintmax_t total_bytes = 0; /* The number of bytes. */
40 int ch; /* Each character read. */
46 fp = bb_wfopen(file, "r");
51 while ((ch = getc(fp)) != EOF) {
53 checksum = (checksum >> 1) + ((checksum & 1) << 15);
55 checksum &= 0xffff; /* Keep it within bounds. */
60 bb_fclose_nonstdin(fp);
64 if (bb_fclose_nonstdin(fp) == EOF) {
69 printf("%05d %5ju ", checksum, (total_bytes+1023)/1024);
78 /* Calculate and print the checksum and the size in 512-byte blocks
79 of file FILE, or of the standard input if FILE is "-".
80 If PRINT_NAME is >0, print FILE next to the checksum and size.
81 Return 1 if successful. */
82 #define MY_BUF_SIZE 8192
83 static int sysv_sum_file(const char *file, int print_name)
85 RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE);
87 uintmax_t total_bytes = 0;
89 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
96 fd = open(file, O_RDONLY);
102 size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
107 if (bytes_read == -1) {
110 RELEASE_CONFIG_BUFFER(buf);
116 total_bytes += bytes_read;
118 s += buf[bytes_read];
121 if (!IS_STDIN(file) && close(fd) == -1)
122 goto release_and_ret;
124 RELEASE_CONFIG_BUFFER(buf);
127 int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
128 s = (r & 0xffff) + (r >> 16);
130 printf("%d %ju ", s, (total_bytes+511)/512);
132 puts(print_name ? file : "");
137 int sum_main(int argc, char **argv)
141 int (*sum_func)(const char *, int) = bsd_sum_file;
143 /* give the bsd func priority over sysv func */
144 flags = bb_getopt_ulflags(argc, argv, "sr");
146 sum_func = sysv_sum_file;
148 sum_func = bsd_sum_file;
151 if ((argc - optind) == 0)
152 ok = sum_func("-", 0);
154 for (ok = 1; optind < argc; optind++)
155 ok &= sum_func(argv[optind], 1);
157 if (have_read_stdin && fclose(stdin) == EOF)
158 bb_perror_msg_and_die("-");
160 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);