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 #include <sys/types.h>
28 static int bsd_sum_file ();
29 static int sysv_sum_file ();
33 /* The name this program was run with. */
36 /* Nonzero if any of the files read were the standard input. */
37 static int have_read_stdin;
39 /* Right-rotate 32-bit integer variable C. */
40 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
42 /* If non-zero, display usage information and exit. */
45 /* If non-zero, print the version on standard output then exit. */
46 static int show_version;
48 static struct option const longopts[] =
50 {"sysv", no_argument, NULL, 's'},
51 {"help", no_argument, &show_help, 1},
52 {"version", no_argument, &show_version, 1},
60 Usage: %s [-rs] [--help] [--version] [--sysv] [file...]\n", program_name);
72 int (*sum_func) () = bsd_sum_file;
74 program_name = argv[0];
77 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
84 case 'r': /* For SysV compatibility. */
85 sum_func = bsd_sum_file;
89 sum_func = sysv_sum_file;
99 printf ("%s\n", version_string);
106 files_given = argc - optind;
107 if (files_given == 0)
109 if ((*sum_func) ("-", files_given) < 0)
113 for (; optind < argc; optind++)
114 if ((*sum_func) (argv[optind], files_given) < 0)
117 if (have_read_stdin && fclose (stdin) == EOF)
118 error (1, errno, "-");
122 /* Calculate and print the rotated checksum and the size in 1K blocks
123 of file FILE, or of the standard input if FILE is "-".
124 If PRINT_NAME is >1, print FILE next to the checksum and size.
125 The checksum varies depending on sizeof(int).
126 Return 0 if successful, -1 if an error occurs. */
129 bsd_sum_file (file, print_name)
134 register unsigned long checksum = 0; /* The checksum mod 2^16. */
135 register long total_bytes = 0; /* The number of bytes. */
136 register int ch; /* Each character read. */
138 if (!strcmp (file, "-"))
145 fp = fopen (file, "r");
148 error (0, errno, "%s", file);
153 /* This algorithm seems to depend on sign extension in `ch' in order to
154 give the right results. Ick. */
155 while ((ch = getc (fp)) != EOF)
158 ROTATE_RIGHT (checksum);
160 checksum &= 0xffff; /* Keep it within bounds. */
165 error (0, errno, "%s", file);
166 if (strcmp (file, "-"))
171 if (strcmp (file, "-") && fclose (fp) == EOF)
173 error (0, errno, "%s", file);
177 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
179 printf (" %s", file);
185 /* Calculate and print the checksum and the size in 512-byte blocks
186 of file FILE, or of the standard input if FILE is "-".
187 If PRINT_NAME is >0, print FILE next to the checksum and size.
188 Return 0 if successful, -1 if an error occurs. */
191 sysv_sum_file (file, print_name)
196 unsigned char buf[8192];
197 register int bytes_read;
198 register unsigned long checksum = 0;
199 long total_bytes = 0;
201 if (!strcmp (file, "-"))
208 fd = open (file, O_RDONLY);
211 error (0, errno, "%s", file);
216 while ((bytes_read = read (fd, buf, sizeof buf)) > 0)
220 for (i = 0; i < bytes_read; i++)
222 total_bytes += bytes_read;
227 error (0, errno, "%s", file);
228 if (strcmp (file, "-"))
233 if (strcmp (file, "-") && close (fd) == -1)
235 error (0, errno, "%s", file);
239 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
241 printf (" %s", file);