blob: 0a9c9734f423d2a0393a10043e84e852ccde29ce [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 *
Mike Frysingera80b2902005-09-10 02:47:19 +000013 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Mike Frysinger4a211702005-04-21 23:24:46 +000014 */
15
16#include <stdio.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
Mike Frysinger4a211702005-04-21 23:24:46 +000021
Mike Frysingera80b2902005-09-10 02:47:19 +000022#include "busybox.h"
Mike Frysinger4a211702005-04-21 23:24:46 +000023
24/* 1 if any of the files read were the standard input */
25static int have_read_stdin;
26
27/* make a little more readable and avoid using strcmp for just 2 bytes */
28#define IS_STDIN(s) (s[0] == '-' && s[1] == '\0')
29
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. */
35static int bsd_sum_file(const char *file, int print_name)
36{
37 register FILE *fp;
38 register int checksum = 0; /* The checksum mod 2^16. */
39 register uintmax_t total_bytes = 0; /* The number of bytes. */
40 register int ch; /* Each character read. */
41
42 if (IS_STDIN(file)) {
43 fp = stdin;
44 have_read_stdin = 1;
45 } else {
Mike Frysinger1fb79612005-05-16 22:35:59 +000046 fp = bb_wfopen(file, "r");
47 if (fp == NULL)
Mike Frysinger4a211702005-04-21 23:24:46 +000048 return 0;
Mike Frysinger4a211702005-04-21 23:24:46 +000049 }
50
51 while ((ch = getc(fp)) != EOF) {
52 ++total_bytes;
53 checksum = (checksum >> 1) + ((checksum & 1) << 15);
54 checksum += ch;
55 checksum &= 0xffff; /* Keep it within bounds. */
56 }
57
58 if (ferror(fp)) {
Mike Frysinger1fb79612005-05-16 22:35:59 +000059 bb_perror_msg(file);
60 bb_fclose_nonstdin(fp);
Mike Frysinger4a211702005-04-21 23:24:46 +000061 return 0;
62 }
63
Mike Frysinger1fb79612005-05-16 22:35:59 +000064 if (bb_fclose_nonstdin(fp) == EOF) {
65 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +000066 return 0;
67 }
68
Mike Frysinger1fb79612005-05-16 22:35:59 +000069 printf("%05d %5s ", checksum,
Mike Frysinger4a211702005-04-21 23:24:46 +000070 make_human_readable_str(total_bytes, 1, 1024));
71 if (print_name > 1)
Mike Frysinger1fb79612005-05-16 22:35:59 +000072 puts(file);
73 else
74 printf("\n");
Mike Frysinger4a211702005-04-21 23:24:46 +000075
76 return 1;
77}
78
79/* Calculate and print the checksum and the size in 512-byte blocks
80 of file FILE, or of the standard input if FILE is "-".
81 If PRINT_NAME is >0, print FILE next to the checksum and size.
82 Return 1 if successful. */
Mike Frysingerdb289b22005-09-10 04:10:18 +000083#define MY_BUF_SIZE 8192
Mike Frysinger4a211702005-04-21 23:24:46 +000084static int sysv_sum_file(const char *file, int print_name)
85{
Mike Frysingerdb289b22005-09-10 04:10:18 +000086 RESERVE_CONFIG_UBUFFER(buf, MY_BUF_SIZE);
Mike Frysinger4a211702005-04-21 23:24:46 +000087 int fd;
Mike Frysinger4a211702005-04-21 23:24:46 +000088 uintmax_t total_bytes = 0;
Mike Frysinger4a211702005-04-21 23:24:46 +000089
90 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
91 unsigned int s = 0;
92
93 if (IS_STDIN(file)) {
94 fd = 0;
95 have_read_stdin = 1;
96 } else {
97 fd = open(file, O_RDONLY);
Mike Frysingerdb289b22005-09-10 04:10:18 +000098 if (fd == -1)
99 goto release_and_ret;
Mike Frysinger4a211702005-04-21 23:24:46 +0000100 }
101
102 while (1) {
Mike Frysingera80b2902005-09-10 02:47:19 +0000103 size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
Mike Frysinger4a211702005-04-21 23:24:46 +0000104
105 if (bytes_read == 0)
106 break;
107
108 if (bytes_read == -1) {
Mike Frysingerdb289b22005-09-10 04:10:18 +0000109release_and_ret:
Mike Frysinger1fb79612005-05-16 22:35:59 +0000110 bb_perror_msg(file);
Mike Frysingerdb289b22005-09-10 04:10:18 +0000111 RELEASE_CONFIG_BUFFER(buf);
Mike Frysinger4a211702005-04-21 23:24:46 +0000112 if (!IS_STDIN(file))
113 close(fd);
114 return 0;
115 }
116
Mike Frysinger4a211702005-04-21 23:24:46 +0000117 total_bytes += bytes_read;
Mike Frysingerdb289b22005-09-10 04:10:18 +0000118 while (bytes_read--)
119 s += buf[bytes_read];
Mike Frysinger4a211702005-04-21 23:24:46 +0000120 }
121
Mike Frysingerdb289b22005-09-10 04:10:18 +0000122 if (!IS_STDIN(file) && close(fd) == -1)
123 goto release_and_ret;
124 else
125 RELEASE_CONFIG_BUFFER(buf);
Mike Frysinger4a211702005-04-21 23:24:46 +0000126
Mike Frysingera80b2902005-09-10 02:47:19 +0000127 {
128 int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
Mike Frysingerdb289b22005-09-10 04:10:18 +0000129 s = (r & 0xffff) + (r >> 16);
Mike Frysinger4a211702005-04-21 23:24:46 +0000130
Mike Frysingerdb289b22005-09-10 04:10:18 +0000131 printf("%d %s ", s,
132 make_human_readable_str(total_bytes, 1, 512));
133 }
Mike Frysingera80b2902005-09-10 02:47:19 +0000134 puts(print_name ? file : "");
Mike Frysinger4a211702005-04-21 23:24:46 +0000135
136 return 1;
137}
138
139int sum_main(int argc, char **argv)
140{
141 int flags;
142 int ok;
Mike Frysinger4a211702005-04-21 23:24:46 +0000143 int (*sum_func)(const char *, int) = bsd_sum_file;
144
145 /* give the bsd func priority over sysv func */
146 flags = bb_getopt_ulflags(argc, argv, "sr");
147 if (flags & 1)
148 sum_func = sysv_sum_file;
149 if (flags & 2)
150 sum_func = bsd_sum_file;
151
152 have_read_stdin = 0;
Mike Frysingerdb289b22005-09-10 04:10:18 +0000153 if ((argc - optind) == 0)
154 ok = sum_func("-", 0);
Mike Frysinger4a211702005-04-21 23:24:46 +0000155 else
156 for (ok = 1; optind < argc; optind++)
Mike Frysingerdb289b22005-09-10 04:10:18 +0000157 ok &= sum_func(argv[optind], 1);
Mike Frysinger4a211702005-04-21 23:24:46 +0000158
159 if (have_read_stdin && fclose(stdin) == EOF)
160 bb_perror_msg_and_die("-");
161
162 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
163}