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 ();
45 /* The name this program was run with. */
48 /* Nonzero if any of the files read were the standard input. */
49 static int have_read_stdin;
51 /* Right-rotate 32-bit integer variable C. */
52 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
54 /* If non-zero, display usage information and exit. */
57 /* If non-zero, print the version on standard output then exit. */
58 static int show_version;
60 static struct option const longopts[] =
62 {"sysv", no_argument, NULL, 's'},
63 {"help", no_argument, &show_help, 1},
64 {"version", no_argument, &show_version, 1},
73 fprintf (stderr, "Try `%s --help' for more information.\n",
78 Usage: %s [OPTION]... [FILE]...\n\
83 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
84 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
85 --help display this help and exit\n\
86 --version output version information and exit\n\
88 With no FILE, or when FILE is -, read standard input.\n\
102 int (*sum_func) () = bsd_sum_file;
104 program_name = argv[0];
107 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
114 case 'r': /* For SysV compatibility. */
115 sum_func = bsd_sum_file;
119 sum_func = sysv_sum_file;
129 printf ("%s\n", version_string);
136 files_given = argc - optind;
137 if (files_given == 0)
139 if ((*sum_func) ("-", files_given) < 0)
143 for (; optind < argc; optind++)
144 if ((*sum_func) (argv[optind], files_given) < 0)
147 if (have_read_stdin && fclose (stdin) == EOF)
148 error (1, errno, "-");
152 /* Calculate and print the rotated checksum and the size in 1K blocks
153 of file FILE, or of the standard input if FILE is "-".
154 If PRINT_NAME is >1, print FILE next to the checksum and size.
155 The checksum varies depending on sizeof(int).
156 Return 0 if successful, -1 if an error occurs. */
159 bsd_sum_file (file, print_name)
164 register unsigned long checksum = 0; /* The checksum mod 2^16. */
165 register long total_bytes = 0; /* The number of bytes. */
166 register int ch; /* Each character read. */
168 if (!strcmp (file, "-"))
175 fp = fopen (file, "r");
178 error (0, errno, "%s", file);
183 /* This algorithm seems to depend on sign extension in `ch' in order to
184 give the right results. Ick. */
185 while ((ch = getc (fp)) != EOF)
188 ROTATE_RIGHT (checksum);
190 checksum &= 0xffff; /* Keep it within bounds. */
195 error (0, errno, "%s", file);
196 if (strcmp (file, "-"))
201 if (strcmp (file, "-") && fclose (fp) == EOF)
203 error (0, errno, "%s", file);
207 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
209 printf (" %s", file);
215 /* Calculate and print the checksum and the size in 512-byte blocks
216 of file FILE, or of the standard input if FILE is "-".
217 If PRINT_NAME is >0, print FILE next to the checksum and size.
218 Return 0 if successful, -1 if an error occurs. */
221 sysv_sum_file (file, print_name)
226 unsigned char buf[8192];
227 register int bytes_read;
228 register unsigned long checksum = 0;
229 long total_bytes = 0;
231 if (!strcmp (file, "-"))
238 fd = open (file, O_RDONLY);
241 error (0, errno, "%s", file);
246 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
250 for (i = 0; i < bytes_read; i++)
252 total_bytes += bytes_read;
257 error (0, errno, "%s", file);
258 if (strcmp (file, "-"))
263 if (strcmp (file, "-") && close (fd) == -1)
265 error (0, errno, "%s", file);
269 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
271 printf (" %s", file);