Mike Frysinger | a80b290 | 2005-09-10 02:47:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 2 | /* |
| 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. |
| 5 | * |
| 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> |
| 9 | * |
| 10 | * Written by Kayvan Aghaiepour and David MacKenzie |
| 11 | * Taken from coreutils and turned into a busybox applet by Mike Frysinger |
| 12 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 13 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Mike Frysinger | a80b290 | 2005-09-10 02:47:19 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 18 | enum { sysv_sum, bsd_sum }; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 20 | /* BSD: calculate and print the rotated checksum and the size in 1K blocks |
| 21 | The checksum varies depending on sizeof (int). */ |
| 22 | /* SYSV: calculate and print the checksum and the size in 512-byte blocks */ |
| 23 | /* Return 1 if successful. */ |
| 24 | static int sum_file(const char *file, int type, int print_name) |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 25 | { |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 26 | #define buf bb_common_bufsiz1 |
| 27 | int r, fd; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 28 | uintmax_t total_bytes = 0; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 29 | |
| 30 | /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 31 | unsigned s = 0; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 33 | fd = 0; |
| 34 | if (NOT_LONE_DASH(file)) { |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 35 | fd = open(file, O_RDONLY); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 36 | if (fd == -1) |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 37 | goto ret_bad; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | while (1) { |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 41 | size_t bytes_read = safe_read(fd, buf, BUFSIZ); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 43 | if ((ssize_t)bytes_read <= 0) { |
| 44 | r = (fd && close(fd) != 0); |
| 45 | if (!bytes_read && !r) |
| 46 | /* no error */ |
| 47 | break; |
| 48 | ret_bad: |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 49 | bb_perror_msg(file); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 53 | total_bytes += bytes_read; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 54 | if (type == sysv_sum) { |
| 55 | do s += buf[--bytes_read]; while (bytes_read); |
| 56 | } else { |
| 57 | r = 0; |
| 58 | do { |
| 59 | s = (s >> 1) + ((s & 1) << 15); |
| 60 | s += buf[r++]; |
| 61 | s &= 0xffff; /* Keep it within bounds. */ |
| 62 | } while (--bytes_read); |
| 63 | } |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 66 | if (!print_name) file = ""; |
| 67 | if (type == sysv_sum) { |
| 68 | r = (s & 0xffff) + ((s & 0xffffffff) >> 16); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 69 | s = (r & 0xffff) + (r >> 16); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 70 | printf("%d %ju %s\n", s, (total_bytes+511)/512, file); |
| 71 | } else |
| 72 | printf("%05d %5ju %s\n", s, (total_bytes+1023)/1024, file); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 73 | return 1; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 74 | #undef buf |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int sum_main(int argc, char **argv) |
| 78 | { |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 79 | int n; |
| 80 | int type = bsd_sum; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 82 | n = getopt32(argc, argv, "sr"); |
| 83 | if (n & 1) type = sysv_sum; |
| 84 | /* give the bsd priority over sysv func */ |
| 85 | if (n & 2) type = bsd_sum; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 87 | if (argc == optind) |
| 88 | n = sum_file("-", type, 0); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 89 | else |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 90 | for (n = 1; optind < argc; optind++) |
| 91 | n &= sum_file(argv[optind], type, 1); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 93 | return !n; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 94 | } |