blob: df5804899ccce6682d4f75e0364cee7dbb140550 [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
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000018enum { sysv_sum, bsd_sum };
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. */
24static int sum_file(const char *file, int type, int print_name)
Mike Frysinger4a211702005-04-21 23:24:46 +000025{
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000026#define buf bb_common_bufsiz1
27 int r, fd;
Mike Frysinger4a211702005-04-21 23:24:46 +000028 uintmax_t total_bytes = 0;
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 fd = 0;
34 if (NOT_LONE_DASH(file)) {
Mike Frysinger4a211702005-04-21 23:24:46 +000035 fd = open(file, O_RDONLY);
Mike Frysingerdb289b22005-09-10 04:10:18 +000036 if (fd == -1)
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000037 goto ret_bad;
Mike Frysinger4a211702005-04-21 23:24:46 +000038 }
39
40 while (1) {
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000041 size_t bytes_read = safe_read(fd, buf, BUFSIZ);
Mike Frysinger4a211702005-04-21 23:24:46 +000042
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000043 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 Frysinger1fb79612005-05-16 22:35:59 +000049 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +000050 return 0;
51 }
52
Mike Frysinger4a211702005-04-21 23:24:46 +000053 total_bytes += bytes_read;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000054 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 Frysinger4a211702005-04-21 23:24:46 +000064 }
65
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000066 if (!print_name) file = "";
67 if (type == sysv_sum) {
68 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
77int sum_main(int argc, char **argv)
78{
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000079 int n;
80 int type = bsd_sum;
Mike Frysinger4a211702005-04-21 23:24:46 +000081
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000082 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 Frysinger4a211702005-04-21 23:24:46 +000086
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000087 if (argc == optind)
88 n = sum_file("-", type, 0);
Mike Frysinger4a211702005-04-21 23:24:46 +000089 else
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000090 for (n = 1; optind < argc; optind++)
91 n &= sum_file(argv[optind], type, 1);
Mike Frysinger4a211702005-04-21 23:24:46 +000092
Denis Vlasenko9ac9e552006-12-23 15:58:11 +000093 return !n;
Mike Frysinger4a211702005-04-21 23:24:46 +000094}