1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 86, 89, 91, 1995-1998, 1999 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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
29 #include "safe-read.h"
31 /* The name this program was run with. */
34 /* Nonzero if any of the files read were the standard input. */
35 static int have_read_stdin;
37 /* Right-rotate 32-bit integer variable C. */
38 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
40 /* If nonzero, display usage information and exit. */
43 /* If nonzero, print the version on standard output then exit. */
44 static int show_version;
46 static struct option const longopts[] =
48 {"sysv", no_argument, NULL, 's'},
49 {"help", no_argument, &show_help, 1},
50 {"version", no_argument, &show_version, 1},
58 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 Usage: %s [OPTION]... [FILE]...\n\
67 Print checksum and block counts for each FILE.\n\
69 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
70 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
74 With no FILE, or when FILE is -, read standard input.\n\
76 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
78 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
81 /* Calculate and print the rotated checksum and the size in 1K blocks
82 of file FILE, or of the standard input if FILE is "-".
83 If PRINT_NAME is >1, print FILE next to the checksum and size.
84 The checksum varies depending on sizeof(int).
85 Return 0 if successful, -1 if an error occurs. */
88 bsd_sum_file (const char *file, int print_name)
91 register unsigned long checksum = 0; /* The checksum mod 2^16. */
92 register long total_bytes = 0; /* The number of bytes. */
93 register int ch; /* Each character read. */
95 if (STREQ (file, "-"))
102 fp = fopen (file, "r");
105 error (0, errno, "%s", file);
109 /* Need binary I/O, or else byte counts and checksums are incorrect. */
110 SET_BINARY (fileno(fp));
112 while ((ch = getc (fp)) != EOF)
115 ROTATE_RIGHT (checksum);
117 checksum &= 0xffff; /* Keep it within bounds. */
122 error (0, errno, "%s", file);
123 if (!STREQ (file, "-"))
128 if (!STREQ (file, "-") && fclose (fp) == EOF)
130 error (0, errno, "%s", file);
134 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
136 printf (" %s", file);
142 /* Calculate and print the checksum and the size in 512-byte blocks
143 of file FILE, or of the standard input if FILE is "-".
144 If PRINT_NAME is >0, print FILE next to the checksum and size.
145 Return 0 if successful, -1 if an error occurs. */
148 sysv_sum_file (const char *file, int print_name)
151 unsigned char buf[8192];
152 register int bytes_read;
153 register unsigned long checksum = 0;
154 long total_bytes = 0;
156 if (STREQ (file, "-"))
163 fd = open (file, O_RDONLY);
166 error (0, errno, "%s", file);
170 /* Need binary I/O, or else byte counts and checksums are incorrect. */
173 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
177 for (i = 0; i < bytes_read; i++)
179 total_bytes += bytes_read;
184 error (0, errno, "%s", file);
185 if (!STREQ (file, "-"))
190 if (!STREQ (file, "-") && close (fd) == -1)
192 error (0, errno, "%s", file);
196 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
198 printf (" %s", file);
205 main (int argc, char **argv)
210 int (*sum_func) () = bsd_sum_file;
212 program_name = argv[0];
213 setlocale (LC_ALL, "");
214 bindtextdomain (PACKAGE, LOCALEDIR);
215 textdomain (PACKAGE);
219 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
226 case 'r': /* For SysV compatibility. */
227 sum_func = bsd_sum_file;
231 sum_func = sysv_sum_file;
241 printf ("sum (%s) %s\n", GNU_PACKAGE, VERSION);
248 files_given = argc - optind;
249 if (files_given == 0)
251 if ((*sum_func) ("-", files_given) < 0)
255 for (; optind < argc; optind++)
256 if ((*sum_func) (argv[optind], files_given) < 0)
259 if (have_read_stdin && fclose (stdin) == EOF)
260 error (EXIT_FAILURE, errno, "-");
261 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);