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\
74 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
75 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
76 --help display this help and exit\n\
77 --version output version information and exit\n\
79 With no FILE, or when FILE is -, read standard input.\n\
93 int (*sum_func) () = bsd_sum_file;
95 program_name = argv[0];
98 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
105 case 'r': /* For SysV compatibility. */
106 sum_func = bsd_sum_file;
110 sum_func = sysv_sum_file;
120 printf ("sum - %s\n", version_string);
127 files_given = argc - optind;
128 if (files_given == 0)
130 if ((*sum_func) ("-", files_given) < 0)
134 for (; optind < argc; optind++)
135 if ((*sum_func) (argv[optind], files_given) < 0)
138 if (have_read_stdin && fclose (stdin) == EOF)
139 error (1, errno, "-");
143 /* Calculate and print the rotated checksum and the size in 1K blocks
144 of file FILE, or of the standard input if FILE is "-".
145 If PRINT_NAME is >1, print FILE next to the checksum and size.
146 The checksum varies depending on sizeof(int).
147 Return 0 if successful, -1 if an error occurs. */
150 bsd_sum_file (file, print_name)
155 register unsigned long checksum = 0; /* The checksum mod 2^16. */
156 register long total_bytes = 0; /* The number of bytes. */
157 register int ch; /* Each character read. */
159 if (!strcmp (file, "-"))
166 fp = fopen (file, "r");
169 error (0, errno, "%s", file);
174 /* This algorithm seems to depend on sign extension in `ch' in order to
175 give the right results. Ick. */
176 while ((ch = getc (fp)) != EOF)
179 ROTATE_RIGHT (checksum);
181 checksum &= 0xffff; /* Keep it within bounds. */
186 error (0, errno, "%s", file);
187 if (strcmp (file, "-"))
192 if (strcmp (file, "-") && fclose (fp) == EOF)
194 error (0, errno, "%s", file);
198 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
200 printf (" %s", file);
206 /* Calculate and print the checksum and the size in 512-byte blocks
207 of file FILE, or of the standard input if FILE is "-".
208 If PRINT_NAME is >0, print FILE next to the checksum and size.
209 Return 0 if successful, -1 if an error occurs. */
212 sysv_sum_file (file, print_name)
217 unsigned char buf[8192];
218 register int bytes_read;
219 register unsigned long checksum = 0;
220 long total_bytes = 0;
222 if (!strcmp (file, "-"))
229 fd = open (file, O_RDONLY);
232 error (0, errno, "%s", file);
237 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
241 for (i = 0; i < bytes_read; i++)
243 total_bytes += bytes_read;
248 error (0, errno, "%s", file);
249 if (strcmp (file, "-"))
254 if (strcmp (file, "-") && close (fd) == -1)
256 error (0, errno, "%s", file);
260 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
262 printf (" %s", file);