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>
27 static int bsd_sum_file ();
28 static int sysv_sum_file ();
32 /* The name this program was run with. */
35 /* Nonzero if any of the files read were the standard input. */
36 static int have_read_stdin;
38 /* Right-rotate 32-bit integer variable C. */
39 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
41 static struct option const longopts[] =
43 {"sysv", no_argument, NULL, 's'},
55 int (*sum_func) () = bsd_sum_file;
57 program_name = argv[0];
60 while ((optc = getopt_long (argc, argv, "rs", longopts, (int *) 0)) != -1)
64 case 'r': /* For SysV compatibility. */
65 sum_func = bsd_sum_file;
69 sum_func = sysv_sum_file;
74 Usage: %s [-rs] [--sysv] [file...]\n", argv[0]);
79 files_given = argc - optind;
82 if ((*sum_func) ("-", files_given) < 0)
86 for (; optind < argc; optind++)
87 if ((*sum_func) (argv[optind], files_given) < 0)
90 if (have_read_stdin && fclose (stdin) == EOF)
91 error (1, errno, "-");
95 /* Calculate and print the rotated checksum and the size in 1K blocks
96 of file FILE, or of the standard input if FILE is "-".
97 If PRINT_NAME is >1, print FILE next to the checksum and size.
98 The checksum varies depending on sizeof(int).
99 Return 0 if successful, -1 if an error occurs. */
102 bsd_sum_file (file, print_name)
107 register unsigned long checksum = 0; /* The checksum mod 2^16. */
108 register long total_bytes = 0; /* The number of bytes. */
109 register int ch; /* Each character read. */
111 if (!strcmp (file, "-"))
118 fp = fopen (file, "r");
121 error (0, errno, "%s", file);
126 /* This algorithm seems to depend on sign extension in `ch' in order to
127 give the right results. Ick. */
128 while ((ch = getc (fp)) != EOF)
131 ROTATE_RIGHT (checksum);
133 checksum &= 0xffff; /* Keep it within bounds. */
138 error (0, errno, "%s", file);
139 if (strcmp (file, "-"))
144 if (strcmp (file, "-") && fclose (fp) == EOF)
146 error (0, errno, "%s", file);
150 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
152 printf (" %s", file);
158 /* Calculate and print the checksum and the size in 512-byte blocks
159 of file FILE, or of the standard input if FILE is "-".
160 If PRINT_NAME is >0, print FILE next to the checksum and size.
161 Return 0 if successful, -1 if an error occurs. */
164 sysv_sum_file (file, print_name)
169 unsigned char buf[8192];
170 register int bytes_read;
171 register unsigned long checksum = 0;
172 long total_bytes = 0;
174 if (!strcmp (file, "-"))
181 fd = open (file, O_RDONLY);
184 error (0, errno, "%s", file);
189 while ((bytes_read = read (fd, buf, sizeof buf)) > 0)
193 for (i = 0; i < bytes_read; i++)
195 total_bytes += bytes_read;
200 error (0, errno, "%s", file);
201 if (strcmp (file, "-"))
206 if (strcmp (file, "-") && close (fd) == -1)
208 error (0, errno, "%s", file);
212 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
214 printf (" %s", file);