applying:
[oweals/busybox.git] / coreutils / sum.c
1 /*
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 */
38 static 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.  */
48 static 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 {
59                 fp = bb_wfopen(file, "r");
60                 if (fp == NULL)
61                         return 0;
62         }
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)) {
72                 bb_perror_msg(file);
73                 bb_fclose_nonstdin(fp);
74                 return 0;
75         }
76
77         if (bb_fclose_nonstdin(fp) == EOF) {
78                 bb_perror_msg(file);
79                 return 0;
80         }
81
82         printf("%05d %5s ", checksum,
83                make_human_readable_str(total_bytes, 1, 1024));
84         if (print_name > 1)
85                 puts(file);
86         else
87                 printf("\n");
88
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.  */
96 static 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) {
113                         bb_perror_msg(file);
114                         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) {
126                         bb_perror_msg(file);
127                         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) {
138                 bb_perror_msg(file);
139                 return 0;
140         }
141
142         r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
143         checksum = (r & 0xffff) + (r >> 16);
144
145         printf("%d %s ", checksum,
146                make_human_readable_str(total_bytes, 1, 512));
147         if (print_name)
148                 puts(file);
149         else
150                 printf("\n");
151
152         return 1;
153 }
154
155 int 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 }