blob: 9327ca55e3c9b40975e6f308bd8a357753c53f07 [file] [log] [blame]
Mike Frysingera80b2902005-09-10 02:47:19 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger4a211702005-04-21 23:24:46 +00002/*
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-Fischer7fee0c42006-09-13 16:39:19 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Mike Frysinger4a211702005-04-21 23:24:46 +000014 */
15
Mike Frysingera80b2902005-09-10 02:47:19 +000016#include "busybox.h"
Mike Frysinger4a211702005-04-21 23:24:46 +000017
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000018enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
Mike Frysinger4a211702005-04-21 23:24:46 +000019
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000020/* 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. */
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000024static unsigned sum_file(const char *file, const unsigned type)
Mike Frysinger4a211702005-04-21 23:24:46 +000025{
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000026#define buf bb_common_bufsiz1
Mike Frysinger4a211702005-04-21 23:24:46 +000027 uintmax_t total_bytes = 0;
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000028 int fd = 0, r;
Mike Frysinger4a211702005-04-21 23:24:46 +000029
30 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000031 unsigned s = 0;
Mike Frysinger4a211702005-04-21 23:24:46 +000032
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000033 if (NOT_LONE_DASH(file)) {
Mike Frysinger4a211702005-04-21 23:24:46 +000034 fd = open(file, O_RDONLY);
Mike Frysingerdb289b22005-09-10 04:10:18 +000035 if (fd == -1)
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000036 goto ret_bad;
Mike Frysinger4a211702005-04-21 23:24:46 +000037 }
38
39 while (1) {
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000040 size_t bytes_read = safe_read(fd, buf, BUFSIZ);
Mike Frysinger4a211702005-04-21 23:24:46 +000041
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000042 if ((ssize_t)bytes_read <= 0) {
43 r = (fd && close(fd) != 0);
44 if (!bytes_read && !r)
45 /* no error */
46 break;
47 ret_bad:
Mike Frysinger1fb79612005-05-16 22:35:59 +000048 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +000049 return 0;
50 }
51
Mike Frysinger4a211702005-04-21 23:24:46 +000052 total_bytes += bytes_read;
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000053 if (type >= SUM_SYSV) {
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000054 do s += buf[--bytes_read]; while (bytes_read);
55 } else {
56 r = 0;
57 do {
58 s = (s >> 1) + ((s & 1) << 15);
59 s += buf[r++];
60 s &= 0xffff; /* Keep it within bounds. */
61 } while (--bytes_read);
62 }
Mike Frysinger4a211702005-04-21 23:24:46 +000063 }
64
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000065 if (type < PRINT_NAME)
66 file = "";
67 if (type >= SUM_SYSV) {
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000068 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
Mike Frysingerdb289b22005-09-10 04:10:18 +000069 s = (r & 0xffff) + (r >> 16);
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000070 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 Frysinger4a211702005-04-21 23:24:46 +000073 return 1;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000074#undef buf
Mike Frysinger4a211702005-04-21 23:24:46 +000075}
76
Denis Vlasenko06af2162007-02-03 17:28:39 +000077int sum_main(int argc, char **argv);
Mike Frysinger4a211702005-04-21 23:24:46 +000078int sum_main(int argc, char **argv)
79{
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000080 unsigned n;
81 unsigned type = SUM_BSD;
Mike Frysinger4a211702005-04-21 23:24:46 +000082
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000083 n = getopt32(argc, argv, "sr");
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000084 if (n & 1) type = SUM_SYSV;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000085 /* give the bsd priority over sysv func */
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000086 if (n & 2) type = SUM_BSD;
Mike Frysinger4a211702005-04-21 23:24:46 +000087
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000088 if (argc == optind) {
89 /* Do not print the name */
90 n = sum_file("-", type);
91 } else {
92 /* Need to print the name if either
93 - more than one file given
94 - doing sysv */
95 type += argc - 1 > optind || type == SUM_SYSV;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000096 for (n = 1; optind < argc; optind++)
Bernhard Reutner-Fischercd75a962007-01-27 22:11:28 +000097 n &= sum_file(argv[optind], type);
98 }
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000099 return !n;
Mike Frysinger4a211702005-04-21 23:24:46 +0000100}