Line data Source code
1 : /* sum -- checksum and count the blocks in a file
2 : Copyright (C) 86, 89, 91, 1995-2002, 2004, 2005 Free Software Foundation, Inc.
3 :
4 : This program is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation, either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : This program is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 :
17 : /* Like BSD sum or SysV sum -r, except like SysV sum if -s option is given. */
18 :
19 : /* Written by Kayvan Aghaiepour and David MacKenzie. */
20 :
21 : #include <config.h>
22 :
23 : #include <stdio.h>
24 : #include <sys/types.h>
25 : #include <getopt.h>
26 : #include "system.h"
27 : #include "error.h"
28 : #include "human.h"
29 : #include "safe-read.h"
30 :
31 : /* The official name of this program (e.g., no `g' prefix). */
32 : #define PROGRAM_NAME "sum"
33 :
34 : #define AUTHORS "Kayvan Aghaiepour", "David MacKenzie"
35 :
36 : /* The name this program was run with. */
37 : char *program_name;
38 :
39 : /* True if any of the files read were the standard input. */
40 : static bool have_read_stdin;
41 :
42 : static struct option const longopts[] =
43 : {
44 : {"sysv", no_argument, NULL, 's'},
45 : {GETOPT_HELP_OPTION_DECL},
46 : {GETOPT_VERSION_OPTION_DECL},
47 : {NULL, 0, NULL, 0}
48 : };
49 :
50 : void
51 5 : usage (int status)
52 : {
53 5 : if (status != EXIT_SUCCESS)
54 4 : fprintf (stderr, _("Try `%s --help' for more information.\n"),
55 : program_name);
56 : else
57 : {
58 1 : printf (_("\
59 : Usage: %s [OPTION]... [FILE]...\n\
60 : "),
61 : program_name);
62 1 : fputs (_("\
63 : Print checksum and block counts for each FILE.\n\
64 : \n\
65 : -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
66 : -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
67 : "), stdout);
68 1 : fputs (HELP_OPTION_DESCRIPTION, stdout);
69 1 : fputs (VERSION_OPTION_DESCRIPTION, stdout);
70 1 : fputs (_("\
71 : \n\
72 : With no FILE, or when FILE is -, read standard input.\n\
73 : "), stdout);
74 1 : emit_bug_reporting_address ();
75 : }
76 5 : exit (status);
77 : }
78 :
79 : /* Calculate and print the rotated checksum and the size in 1K blocks
80 : of file FILE, or of the standard input if FILE is "-".
81 : If PRINT_NAME is >1, print FILE next to the checksum and size.
82 : The checksum varies depending on sizeof (int).
83 : Return true if successful. */
84 :
85 : static bool
86 44 : bsd_sum_file (const char *file, int print_name)
87 : {
88 : FILE *fp;
89 44 : int checksum = 0; /* The checksum mod 2^16. */
90 44 : uintmax_t total_bytes = 0; /* The number of bytes. */
91 : int ch; /* Each character read. */
92 : char hbuf[LONGEST_HUMAN_READABLE + 1];
93 44 : bool is_stdin = STREQ (file, "-");
94 :
95 44 : if (is_stdin)
96 : {
97 30 : fp = stdin;
98 30 : have_read_stdin = true;
99 : if (O_BINARY && ! isatty (STDIN_FILENO))
100 : freopen (NULL, "rb", stdin);
101 : }
102 : else
103 : {
104 14 : fp = fopen (file, (O_BINARY ? "rb" : "r"));
105 14 : if (fp == NULL)
106 : {
107 7 : error (0, errno, "%s", file);
108 7 : return false;
109 : }
110 : }
111 :
112 274 : while ((ch = getc (fp)) != EOF)
113 : {
114 200 : total_bytes++;
115 200 : checksum = (checksum >> 1) + ((checksum & 1) << 15);
116 200 : checksum += ch;
117 200 : checksum &= 0xffff; /* Keep it within bounds. */
118 : }
119 :
120 37 : if (ferror (fp))
121 : {
122 5 : error (0, errno, "%s", file);
123 5 : if (!is_stdin)
124 5 : fclose (fp);
125 5 : return false;
126 : }
127 :
128 32 : if (!is_stdin && fclose (fp) != 0)
129 : {
130 0 : error (0, errno, "%s", file);
131 0 : return false;
132 : }
133 :
134 32 : printf ("%05d %5s", checksum,
135 : human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));
136 32 : if (print_name > 1)
137 27 : printf (" %s", file);
138 32 : putchar ('\n');
139 :
140 32 : return true;
141 : }
142 :
143 : /* Calculate and print the checksum and the size in 512-byte blocks
144 : of file FILE, or of the standard input if FILE is "-".
145 : If PRINT_NAME is >0, print FILE next to the checksum and size.
146 : Return true if successful. */
147 :
148 : static bool
149 13 : sysv_sum_file (const char *file, int print_name)
150 : {
151 : int fd;
152 : unsigned char buf[8192];
153 13 : uintmax_t total_bytes = 0;
154 : char hbuf[LONGEST_HUMAN_READABLE + 1];
155 : int r;
156 : int checksum;
157 :
158 : /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
159 13 : unsigned int s = 0;
160 :
161 13 : bool is_stdin = STREQ (file, "-");
162 :
163 13 : if (is_stdin)
164 : {
165 10 : fd = STDIN_FILENO;
166 10 : have_read_stdin = true;
167 : if (O_BINARY && ! isatty (STDIN_FILENO))
168 : freopen (NULL, "rb", stdin);
169 : }
170 : else
171 : {
172 3 : fd = open (file, O_RDONLY | O_BINARY);
173 3 : if (fd == -1)
174 : {
175 1 : error (0, errno, "%s", file);
176 1 : return false;
177 : }
178 : }
179 :
180 : while (1)
181 8 : {
182 : size_t i;
183 20 : size_t bytes_read = safe_read (fd, buf, sizeof buf);
184 :
185 20 : if (bytes_read == 0)
186 11 : break;
187 :
188 9 : if (bytes_read == SAFE_READ_ERROR)
189 : {
190 1 : error (0, errno, "%s", file);
191 1 : if (!is_stdin)
192 1 : close (fd);
193 1 : return false;
194 : }
195 :
196 72 : for (i = 0; i < bytes_read; i++)
197 64 : s += buf[i];
198 8 : total_bytes += bytes_read;
199 : }
200 :
201 11 : if (!is_stdin && close (fd) != 0)
202 : {
203 0 : error (0, errno, "%s", file);
204 0 : return false;
205 : }
206 :
207 11 : r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
208 11 : checksum = (r & 0xffff) + (r >> 16);
209 :
210 11 : printf ("%d %s", checksum,
211 : human_readable (total_bytes, hbuf, human_ceiling, 1, 512));
212 11 : if (print_name)
213 9 : printf (" %s", file);
214 11 : putchar ('\n');
215 :
216 11 : return true;
217 : }
218 :
219 : int
220 38 : main (int argc, char **argv)
221 : {
222 : bool ok;
223 : int optc;
224 : int files_given;
225 38 : bool (*sum_func) (const char *, int) = bsd_sum_file;
226 :
227 : initialize_main (&argc, &argv);
228 38 : program_name = argv[0];
229 38 : setlocale (LC_ALL, "");
230 : bindtextdomain (PACKAGE, LOCALEDIR);
231 : textdomain (PACKAGE);
232 :
233 38 : atexit (close_stdout);
234 :
235 38 : have_read_stdin = false;
236 :
237 88 : while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
238 : {
239 18 : switch (optc)
240 : {
241 4 : case 'r': /* For SysV compatibility. */
242 4 : sum_func = bsd_sum_file;
243 4 : break;
244 :
245 8 : case 's':
246 8 : sum_func = sysv_sum_file;
247 8 : break;
248 :
249 1 : case_GETOPT_HELP_CHAR;
250 :
251 1 : case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
252 :
253 4 : default:
254 4 : usage (EXIT_FAILURE);
255 : }
256 : }
257 :
258 32 : files_given = argc - optind;
259 32 : if (files_given <= 0)
260 6 : ok = sum_func ("-", files_given);
261 : else
262 77 : for (ok = true; optind < argc; optind++)
263 51 : ok &= sum_func (argv[optind], files_given);
264 :
265 32 : if (have_read_stdin && fclose (stdin) == EOF)
266 0 : error (EXIT_FAILURE, errno, "-");
267 32 : exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
268 : }
|