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 | |
| 18 | /* 1 if any of the files read were the standard input */ |
| 19 | static int have_read_stdin; |
| 20 | |
| 21 | /* make a little more readable and avoid using strcmp for just 2 bytes */ |
| 22 | #define IS_STDIN(s) (s[0] == '-' && s[1] == '\0') |
| 23 | |
| 24 | /* Calculate and print the rotated checksum and the size in 1K blocks |
| 25 | of file FILE, or of the standard input if FILE is "-". |
| 26 | If PRINT_NAME is >1, print FILE next to the checksum and size. |
| 27 | The checksum varies depending on sizeof (int). |
| 28 | Return 1 if successful. */ |
| 29 | static int bsd_sum_file(const char *file, int print_name) |
| 30 | { |
Rob Landley | 02794e1 | 2006-02-14 17:47:05 +0000 | [diff] [blame] | 31 | FILE *fp; |
| 32 | int checksum = 0; /* The checksum mod 2^16. */ |
| 33 | uintmax_t total_bytes = 0; /* The number of bytes. */ |
| 34 | int ch; /* Each character read. */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 35 | int ret = 0; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 36 | |
| 37 | if (IS_STDIN(file)) { |
| 38 | fp = stdin; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 39 | have_read_stdin++; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 40 | } else { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 41 | fp = fopen_or_warn(file, "r"); |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 42 | if (fp == NULL) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 43 | goto out; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | while ((ch = getc(fp)) != EOF) { |
| 47 | ++total_bytes; |
| 48 | checksum = (checksum >> 1) + ((checksum & 1) << 15); |
| 49 | checksum += ch; |
| 50 | checksum &= 0xffff; /* Keep it within bounds. */ |
| 51 | } |
| 52 | |
| 53 | if (ferror(fp)) { |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 54 | bb_perror_msg(file); |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 55 | fclose_if_not_stdin(fp); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 56 | goto out; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 59 | if (fclose_if_not_stdin(fp) == EOF) { |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 60 | bb_perror_msg(file); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 61 | goto out; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 62 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 63 | ret++; |
Rob Landley | 02794e1 | 2006-02-14 17:47:05 +0000 | [diff] [blame] | 64 | printf("%05d %5ju ", checksum, (total_bytes+1023)/1024); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 65 | if (print_name > 1) |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 66 | puts(file); |
| 67 | else |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 68 | puts(""); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 69 | out: |
| 70 | return ret; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* Calculate and print the checksum and the size in 512-byte blocks |
| 74 | of file FILE, or of the standard input if FILE is "-". |
| 75 | If PRINT_NAME is >0, print FILE next to the checksum and size. |
| 76 | Return 1 if successful. */ |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 77 | #define MY_BUF_SIZE 8192 |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 78 | static int sysv_sum_file(const char *file, int print_name) |
| 79 | { |
Rob Landley | 998f449 | 2006-04-10 16:40:47 +0000 | [diff] [blame] | 80 | RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 81 | int fd; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 82 | uintmax_t total_bytes = 0; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 83 | |
| 84 | /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ |
| 85 | unsigned int s = 0; |
| 86 | |
| 87 | if (IS_STDIN(file)) { |
| 88 | fd = 0; |
| 89 | have_read_stdin = 1; |
| 90 | } else { |
| 91 | fd = open(file, O_RDONLY); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 92 | if (fd == -1) |
| 93 | goto release_and_ret; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | while (1) { |
Mike Frysinger | a80b290 | 2005-09-10 02:47:19 +0000 | [diff] [blame] | 97 | size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 98 | |
| 99 | if (bytes_read == 0) |
| 100 | break; |
| 101 | |
| 102 | if (bytes_read == -1) { |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 103 | release_and_ret: |
Mike Frysinger | 1fb7961 | 2005-05-16 22:35:59 +0000 | [diff] [blame] | 104 | bb_perror_msg(file); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 105 | RELEASE_CONFIG_BUFFER(buf); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 106 | if (!IS_STDIN(file)) |
| 107 | close(fd); |
| 108 | return 0; |
| 109 | } |
| 110 | |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 111 | total_bytes += bytes_read; |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 112 | while (bytes_read--) |
| 113 | s += buf[bytes_read]; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 116 | if (!IS_STDIN(file) && close(fd) == -1) |
| 117 | goto release_and_ret; |
| 118 | else |
| 119 | RELEASE_CONFIG_BUFFER(buf); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 120 | |
Mike Frysinger | a80b290 | 2005-09-10 02:47:19 +0000 | [diff] [blame] | 121 | { |
| 122 | int r = (s & 0xffff) + ((s & 0xffffffff) >> 16); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 123 | s = (r & 0xffff) + (r >> 16); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 124 | |
Rob Landley | 02794e1 | 2006-02-14 17:47:05 +0000 | [diff] [blame] | 125 | printf("%d %ju ", s, (total_bytes+511)/512); |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 126 | } |
Mike Frysinger | a80b290 | 2005-09-10 02:47:19 +0000 | [diff] [blame] | 127 | puts(print_name ? file : ""); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | int sum_main(int argc, char **argv) |
| 133 | { |
| 134 | int flags; |
| 135 | int ok; |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 136 | int (*sum_func)(const char *, int) = bsd_sum_file; |
| 137 | |
| 138 | /* give the bsd func priority over sysv func */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 139 | flags = getopt32(argc, argv, "sr"); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 140 | if (flags & 1) |
| 141 | sum_func = sysv_sum_file; |
| 142 | if (flags & 2) |
| 143 | sum_func = bsd_sum_file; |
| 144 | |
| 145 | have_read_stdin = 0; |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 146 | if ((argc - optind) == 0) |
| 147 | ok = sum_func("-", 0); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 148 | else |
| 149 | for (ok = 1; optind < argc; optind++) |
Mike Frysinger | db289b2 | 2005-09-10 04:10:18 +0000 | [diff] [blame] | 150 | ok &= sum_func(argv[optind], 1); |
Mike Frysinger | 4a21170 | 2005-04-21 23:24:46 +0000 | [diff] [blame] | 151 | |
| 152 | if (have_read_stdin && fclose(stdin) == EOF) |
| 153 | bb_perror_msg_and_die("-"); |
| 154 | |
| 155 | exit(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 156 | } |