1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 1986, 1989, 1991, 1995 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. */
25 #include <sys/types.h>
31 static int bsd_sum_file ();
32 static int sysv_sum_file ();
36 /* The name this program was run with. */
39 /* Nonzero if any of the files read were the standard input. */
40 static int have_read_stdin;
42 /* Right-rotate 32-bit integer variable C. */
43 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
45 /* If non-zero, display usage information and exit. */
48 /* If non-zero, print the version on standard output then exit. */
49 static int show_version;
51 static struct option const longopts[] =
53 {"sysv", no_argument, NULL, 's'},
54 {"help", no_argument, &show_help, 1},
55 {"version", no_argument, &show_version, 1},
64 fprintf (stderr, "Try `%s --help' for more information.\n",
69 Usage: %s [OPTION]... [FILE]...\n\
73 Print checksum and block counts for each FILE.\n\
75 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
76 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
77 --help display this help and exit\n\
78 --version output version information and exit\n\
80 With no FILE, or when FILE is -, read standard input.\n\
94 int (*sum_func) () = bsd_sum_file;
96 program_name = argv[0];
99 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
106 case 'r': /* For SysV compatibility. */
107 sum_func = bsd_sum_file;
111 sum_func = sysv_sum_file;
121 printf ("sum - %s\n", version_string);
128 files_given = argc - optind;
129 if (files_given == 0)
131 if ((*sum_func) ("-", files_given) < 0)
135 for (; optind < argc; optind++)
136 if ((*sum_func) (argv[optind], files_given) < 0)
139 if (have_read_stdin && fclose (stdin) == EOF)
140 error (1, errno, "-");
144 /* Calculate and print the rotated checksum and the size in 1K blocks
145 of file FILE, or of the standard input if FILE is "-".
146 If PRINT_NAME is >1, print FILE next to the checksum and size.
147 The checksum varies depending on sizeof(int).
148 Return 0 if successful, -1 if an error occurs. */
151 bsd_sum_file (file, print_name)
156 register unsigned long checksum = 0; /* The checksum mod 2^16. */
157 register long total_bytes = 0; /* The number of bytes. */
158 register int ch; /* Each character read. */
160 if (!strcmp (file, "-"))
167 fp = fopen (file, "r");
170 error (0, errno, "%s", file);
175 /* This algorithm seems to depend on sign extension in `ch' in order to
176 give the right results. Ick. */
177 while ((ch = getc (fp)) != EOF)
180 ROTATE_RIGHT (checksum);
182 checksum &= 0xffff; /* Keep it within bounds. */
187 error (0, errno, "%s", file);
188 if (strcmp (file, "-"))
193 if (strcmp (file, "-") && fclose (fp) == EOF)
195 error (0, errno, "%s", file);
199 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
201 printf (" %s", file);
207 /* Calculate and print the checksum and the size in 512-byte blocks
208 of file FILE, or of the standard input if FILE is "-".
209 If PRINT_NAME is >0, print FILE next to the checksum and size.
210 Return 0 if successful, -1 if an error occurs. */
213 sysv_sum_file (file, print_name)
218 unsigned char buf[8192];
219 register int bytes_read;
220 register unsigned long checksum = 0;
221 long total_bytes = 0;
223 if (!strcmp (file, "-"))
230 fd = open (file, O_RDONLY);
233 error (0, errno, "%s", file);
238 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
242 for (i = 0; i < bytes_read; i++)
244 total_bytes += bytes_read;
249 error (0, errno, "%s", file);
250 if (strcmp (file, "-"))
255 if (strcmp (file, "-") && close (fd) == -1)
257 error (0, errno, "%s", file);
261 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
263 printf (" %s", file);