blob: d68043cf3322988c8ca1294b14b4e17e8b6a3ecf [file] [log] [blame]
Mike Frysinger4a211702005-04-21 23:24:46 +00001/*
2 * sum -- checksum and count the blocks in a file
3 * Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
4 *
5 * Copyright (C) 86, 89, 91, 1995-2002, 2004 Free Software Foundation, Inc.
6 * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
8 *
9 * Written by Kayvan Aghaiepour and David MacKenzie
10 * Taken from coreutils and turned into a busybox applet by Mike Frysinger
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <stdio.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <getopt.h>
34
35#include "libbb.h"
36
37/* 1 if any of the files read were the standard input */
38static int have_read_stdin;
39
40/* make a little more readable and avoid using strcmp for just 2 bytes */
41#define IS_STDIN(s) (s[0] == '-' && s[1] == '\0')
42
43/* Calculate and print the rotated checksum and the size in 1K blocks
44 of file FILE, or of the standard input if FILE is "-".
45 If PRINT_NAME is >1, print FILE next to the checksum and size.
46 The checksum varies depending on sizeof (int).
47 Return 1 if successful. */
48static int bsd_sum_file(const char *file, int print_name)
49{
50 register FILE *fp;
51 register int checksum = 0; /* The checksum mod 2^16. */
52 register uintmax_t total_bytes = 0; /* The number of bytes. */
53 register int ch; /* Each character read. */
54
55 if (IS_STDIN(file)) {
56 fp = stdin;
57 have_read_stdin = 1;
58 } else {
Mike Frysinger1fb79612005-05-16 22:35:59 +000059 fp = bb_wfopen(file, "r");
60 if (fp == NULL)
Mike Frysinger4a211702005-04-21 23:24:46 +000061 return 0;
Mike Frysinger4a211702005-04-21 23:24:46 +000062 }
63
64 while ((ch = getc(fp)) != EOF) {
65 ++total_bytes;
66 checksum = (checksum >> 1) + ((checksum & 1) << 15);
67 checksum += ch;
68 checksum &= 0xffff; /* Keep it within bounds. */
69 }
70
71 if (ferror(fp)) {
Mike Frysinger1fb79612005-05-16 22:35:59 +000072 bb_perror_msg(file);
73 bb_fclose_nonstdin(fp);
Mike Frysinger4a211702005-04-21 23:24:46 +000074 return 0;
75 }
76
Mike Frysinger1fb79612005-05-16 22:35:59 +000077 if (bb_fclose_nonstdin(fp) == EOF) {
78 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +000079 return 0;
80 }
81
Mike Frysinger1fb79612005-05-16 22:35:59 +000082 printf("%05d %5s ", checksum,
Mike Frysinger4a211702005-04-21 23:24:46 +000083 make_human_readable_str(total_bytes, 1, 1024));
84 if (print_name > 1)
Mike Frysinger1fb79612005-05-16 22:35:59 +000085 puts(file);
86 else
87 printf("\n");
Mike Frysinger4a211702005-04-21 23:24:46 +000088
89 return 1;
90}
91
92/* Calculate and print the checksum and the size in 512-byte blocks
93 of file FILE, or of the standard input if FILE is "-".
94 If PRINT_NAME is >0, print FILE next to the checksum and size.
95 Return 1 if successful. */
96static int sysv_sum_file(const char *file, int print_name)
97{
98 int fd;
99 unsigned char buf[8192];
100 uintmax_t total_bytes = 0;
101 int r;
102 int checksum;
103
104 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
105 unsigned int s = 0;
106
107 if (IS_STDIN(file)) {
108 fd = 0;
109 have_read_stdin = 1;
110 } else {
111 fd = open(file, O_RDONLY);
112 if (fd == -1) {
Mike Frysinger1fb79612005-05-16 22:35:59 +0000113 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +0000114 return 0;
115 }
116 }
117
118 while (1) {
119 size_t i;
120 size_t bytes_read = safe_read(fd, buf, sizeof(buf));
121
122 if (bytes_read == 0)
123 break;
124
125 if (bytes_read == -1) {
Mike Frysinger1fb79612005-05-16 22:35:59 +0000126 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +0000127 if (!IS_STDIN(file))
128 close(fd);
129 return 0;
130 }
131
132 for (i = 0; i < bytes_read; i++)
133 s += buf[i];
134 total_bytes += bytes_read;
135 }
136
137 if (!IS_STDIN(file) && close(fd) == -1) {
Mike Frysinger1fb79612005-05-16 22:35:59 +0000138 bb_perror_msg(file);
Mike Frysinger4a211702005-04-21 23:24:46 +0000139 return 0;
140 }
141
142 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
143 checksum = (r & 0xffff) + (r >> 16);
144
Mike Frysinger1fb79612005-05-16 22:35:59 +0000145 printf("%d %s ", checksum,
Mike Frysinger4a211702005-04-21 23:24:46 +0000146 make_human_readable_str(total_bytes, 1, 512));
147 if (print_name)
Mike Frysinger1fb79612005-05-16 22:35:59 +0000148 puts(file);
149 else
150 printf("\n");
Mike Frysinger4a211702005-04-21 23:24:46 +0000151
152 return 1;
153}
154
155int sum_main(int argc, char **argv)
156{
157 int flags;
158 int ok;
159 int files_given;
160 int (*sum_func)(const char *, int) = bsd_sum_file;
161
162 /* give the bsd func priority over sysv func */
163 flags = bb_getopt_ulflags(argc, argv, "sr");
164 if (flags & 1)
165 sum_func = sysv_sum_file;
166 if (flags & 2)
167 sum_func = bsd_sum_file;
168
169 have_read_stdin = 0;
170 files_given = argc - optind;
171 if (files_given <= 0)
172 ok = sum_func("-", files_given);
173 else
174 for (ok = 1; optind < argc; optind++)
175 ok &= sum_func(argv[optind], files_given);
176
177 if (have_read_stdin && fclose(stdin) == EOF)
178 bb_perror_msg_and_die("-");
179
180 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
181}