1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 1986, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, 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. */
23 #if defined (CONFIG_BROKETS)
24 /* We use <config.h> instead of "config.h" so that a compilation
25 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
26 (which it would do because it found this file in $srcdir). */
34 #include <sys/types.h>
39 static int bsd_sum_file ();
40 static int sysv_sum_file ();
44 /* The name this program was run with. */
47 /* Nonzero if any of the files read were the standard input. */
48 static int have_read_stdin;
50 /* Right-rotate 32-bit integer variable C. */
51 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
53 /* If non-zero, display usage information and exit. */
56 /* If non-zero, print the version on standard output then exit. */
57 static int show_version;
59 static struct option const longopts[] =
61 {"sysv", no_argument, NULL, 's'},
62 {"help", no_argument, &show_help, 1},
63 {"version", no_argument, &show_version, 1},
71 Usage: %s [-rs] [--help] [--version] [--sysv] [file...]\n", program_name);
83 int (*sum_func) () = bsd_sum_file;
85 program_name = argv[0];
88 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
95 case 'r': /* For SysV compatibility. */
96 sum_func = bsd_sum_file;
100 sum_func = sysv_sum_file;
110 printf ("%s\n", version_string);
117 files_given = argc - optind;
118 if (files_given == 0)
120 if ((*sum_func) ("-", files_given) < 0)
124 for (; optind < argc; optind++)
125 if ((*sum_func) (argv[optind], files_given) < 0)
128 if (have_read_stdin && fclose (stdin) == EOF)
129 error (1, errno, "-");
133 /* Calculate and print the rotated checksum and the size in 1K blocks
134 of file FILE, or of the standard input if FILE is "-".
135 If PRINT_NAME is >1, print FILE next to the checksum and size.
136 The checksum varies depending on sizeof(int).
137 Return 0 if successful, -1 if an error occurs. */
140 bsd_sum_file (file, print_name)
145 register unsigned long checksum = 0; /* The checksum mod 2^16. */
146 register long total_bytes = 0; /* The number of bytes. */
147 register int ch; /* Each character read. */
149 if (!strcmp (file, "-"))
156 fp = fopen (file, "r");
159 error (0, errno, "%s", file);
164 /* This algorithm seems to depend on sign extension in `ch' in order to
165 give the right results. Ick. */
166 while ((ch = getc (fp)) != EOF)
169 ROTATE_RIGHT (checksum);
171 checksum &= 0xffff; /* Keep it within bounds. */
176 error (0, errno, "%s", file);
177 if (strcmp (file, "-"))
182 if (strcmp (file, "-") && fclose (fp) == EOF)
184 error (0, errno, "%s", file);
188 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
190 printf (" %s", file);
196 /* Calculate and print the checksum and the size in 512-byte blocks
197 of file FILE, or of the standard input if FILE is "-".
198 If PRINT_NAME is >0, print FILE next to the checksum and size.
199 Return 0 if successful, -1 if an error occurs. */
202 sysv_sum_file (file, print_name)
207 unsigned char buf[8192];
208 register int bytes_read;
209 register unsigned long checksum = 0;
210 long total_bytes = 0;
212 if (!strcmp (file, "-"))
219 fd = open (file, O_RDONLY);
222 error (0, errno, "%s", file);
227 while ((bytes_read = read (fd, buf, sizeof buf)) > 0)
231 for (i = 0; i < bytes_read; i++)
233 total_bytes += bytes_read;
238 error (0, errno, "%s", file);
239 if (strcmp (file, "-"))
244 if (strcmp (file, "-") && close (fd) == -1)
246 error (0, errno, "%s", file);
250 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
252 printf (" %s", file);