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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
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 nonzero, display usage information and exit. */
45 /* If nonzero, 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 Usage: %s [OPTION]... [FILE]...\n\
69 Print checksum and block counts for each FILE.\n\
71 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
72 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
73 --help display this help and exit\n\
74 --version output version information and exit\n\
76 With no FILE, or when FILE is -, read standard input.\n\
82 /* Calculate and print the rotated checksum and the size in 1K blocks
83 of file FILE, or of the standard input if FILE is "-".
84 If PRINT_NAME is >1, print FILE next to the checksum and size.
85 The checksum varies depending on sizeof(int).
86 Return 0 if successful, -1 if an error occurs. */
89 bsd_sum_file (const char *file, int print_name)
92 register unsigned long checksum = 0; /* The checksum mod 2^16. */
93 register long total_bytes = 0; /* The number of bytes. */
94 register int ch; /* Each character read. */
96 if (!strcmp (file, "-"))
103 fp = fopen (file, "r");
106 error (0, errno, "%s", file);
111 /* This algorithm seems to depend on sign extension in `ch' in order to
112 give the right results. Ick. */
113 while ((ch = getc (fp)) != EOF)
116 ROTATE_RIGHT (checksum);
118 checksum &= 0xffff; /* Keep it within bounds. */
123 error (0, errno, "%s", file);
124 if (strcmp (file, "-"))
129 if (strcmp (file, "-") && fclose (fp) == EOF)
131 error (0, errno, "%s", file);
135 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
137 printf (" %s", file);
143 /* Calculate and print the checksum and the size in 512-byte blocks
144 of file FILE, or of the standard input if FILE is "-".
145 If PRINT_NAME is >0, print FILE next to the checksum and size.
146 Return 0 if successful, -1 if an error occurs. */
149 sysv_sum_file (const char *file, int print_name)
152 unsigned char buf[8192];
153 register int bytes_read;
154 register unsigned long checksum = 0;
155 long total_bytes = 0;
157 if (!strcmp (file, "-"))
164 fd = open (file, O_RDONLY);
167 error (0, errno, "%s", file);
172 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
176 for (i = 0; i < bytes_read; i++)
178 total_bytes += bytes_read;
183 error (0, errno, "%s", file);
184 if (strcmp (file, "-"))
189 if (strcmp (file, "-") && close (fd) == -1)
191 error (0, errno, "%s", file);
195 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
197 printf (" %s", file);
204 main (int argc, char **argv)
209 int (*sum_func) () = bsd_sum_file;
211 program_name = argv[0];
214 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
221 case 'r': /* For SysV compatibility. */
222 sum_func = bsd_sum_file;
226 sum_func = sysv_sum_file;
236 printf ("sum - %s\n", version_string);
243 files_given = argc - optind;
244 if (files_given == 0)
246 if ((*sum_func) ("-", files_given) < 0)
250 for (; optind < argc; optind++)
251 if ((*sum_func) (argv[optind], files_given) < 0)
254 if (have_read_stdin && fclose (stdin) == EOF)
255 error (1, errno, "-");