1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 86, 89, 91, 1995-2002, 2004, 2005 Free Software Foundation, Inc.
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 2, or (at your option)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Like BSD sum or SysV sum -r, except like SysV sum if -s option is given. */
20 /* Written by Kayvan Aghaiepour and David MacKenzie. */
25 #include <sys/types.h>
30 #include "safe-read.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "sum"
35 #define AUTHORS "Kayvan Aghaiepour", "David MacKenzie"
37 /* The name this program was run with. */
40 /* True if any of the files read were the standard input. */
41 static bool have_read_stdin;
43 static struct option const longopts[] =
45 {"sysv", no_argument, NULL, 's'},
46 {GETOPT_HELP_OPTION_DECL},
47 {GETOPT_VERSION_OPTION_DECL},
54 if (status != EXIT_SUCCESS)
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
60 Usage: %s [OPTION]... [FILE]...\n\
64 Print checksum and block counts for each FILE.\n\
66 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
67 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
69 fputs (HELP_OPTION_DESCRIPTION, stdout);
70 fputs (VERSION_OPTION_DESCRIPTION, stdout);
73 With no FILE, or when FILE is -, read standard input.\n\
75 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
80 /* Calculate and print the rotated checksum and the size in 1K blocks
81 of file FILE, or of the standard input if FILE is "-".
82 If PRINT_NAME is >1, print FILE next to the checksum and size.
83 The checksum varies depending on sizeof (int).
84 Return true if successful. */
87 bsd_sum_file (const char *file, int print_name)
90 int checksum = 0; /* The checksum mod 2^16. */
91 uintmax_t total_bytes = 0; /* The number of bytes. */
92 int ch; /* Each character read. */
93 char hbuf[LONGEST_HUMAN_READABLE + 1];
94 bool is_stdin = STREQ (file, "-");
99 have_read_stdin = true;
100 if (O_BINARY && ! isatty (STDIN_FILENO))
101 freopen (NULL, "rb", stdin);
105 fp = fopen (file, (O_BINARY ? "rb" : "r"));
108 error (0, errno, "%s", file);
113 while ((ch = getc (fp)) != EOF)
116 checksum = (checksum >> 1) + ((checksum & 1) << 15);
118 checksum &= 0xffff; /* Keep it within bounds. */
123 error (0, errno, "%s", file);
129 if (!is_stdin && fclose (fp) != 0)
131 error (0, errno, "%s", file);
135 printf ("%05d %5s", checksum,
136 human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));
138 printf (" %s", file);
144 /* Calculate and print the checksum and the size in 512-byte blocks
145 of file FILE, or of the standard input if FILE is "-".
146 If PRINT_NAME is >0, print FILE next to the checksum and size.
147 Return true if successful. */
150 sysv_sum_file (const char *file, int print_name)
153 unsigned char buf[8192];
154 uintmax_t total_bytes = 0;
155 char hbuf[LONGEST_HUMAN_READABLE + 1];
159 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
162 bool is_stdin = STREQ (file, "-");
167 have_read_stdin = true;
168 if (O_BINARY && ! isatty (STDIN_FILENO))
169 freopen (NULL, "rb", stdin);
173 fd = open (file, O_RDONLY | O_BINARY);
176 error (0, errno, "%s", file);
184 size_t bytes_read = safe_read (fd, buf, sizeof buf);
189 if (bytes_read == SAFE_READ_ERROR)
191 error (0, errno, "%s", file);
197 for (i = 0; i < bytes_read; i++)
199 total_bytes += bytes_read;
202 if (!is_stdin && close (fd) != 0)
204 error (0, errno, "%s", file);
208 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
209 checksum = (r & 0xffff) + (r >> 16);
211 printf ("%d %s", checksum,
212 human_readable (total_bytes, hbuf, human_ceiling, 1, 512));
214 printf (" %s", file);
221 main (int argc, char **argv)
226 bool (*sum_func) (const char *, int) = bsd_sum_file;
228 initialize_main (&argc, &argv);
229 program_name = argv[0];
230 setlocale (LC_ALL, "");
231 bindtextdomain (PACKAGE, LOCALEDIR);
232 textdomain (PACKAGE);
234 atexit (close_stdout);
236 have_read_stdin = false;
238 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
242 case 'r': /* For SysV compatibility. */
243 sum_func = bsd_sum_file;
247 sum_func = sysv_sum_file;
250 case_GETOPT_HELP_CHAR;
252 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
255 usage (EXIT_FAILURE);
259 files_given = argc - optind;
260 if (files_given <= 0)
261 ok = sum_func ("-", files_given);
263 for (ok = true; optind < argc; optind++)
264 ok &= sum_func (argv[optind], files_given);
266 if (have_read_stdin && fclose (stdin) == EOF)
267 error (EXIT_FAILURE, errno, "-");
268 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);