*** empty log message ***
[coreutils.git] / src / sum.c
blobcadcee0070d1e46586a9ac27dafab601b185e4a6
1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 86, 89, 91, 1995-1999 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)
7 any later version.
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. */
22 #include <config.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <getopt.h>
27 #include "system.h"
28 #include "error.h"
29 #include "safe-read.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "sum"
34 #define AUTHORS "Kayvan Aghaiepour and David MacKenzie"
36 /* The name this program was run with. */
37 char *program_name;
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 static struct option const longopts[] =
47 {"sysv", no_argument, NULL, 's'},
48 {GETOPT_HELP_OPTION_DECL},
49 {GETOPT_VERSION_OPTION_DECL},
50 {NULL, 0, NULL, 0}
53 void
54 usage (int status)
56 if (status != 0)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 program_name);
59 else
61 printf (_("\
62 Usage: %s [OPTION]... [FILE]...\n\
63 "),
64 program_name);
65 printf (_("\
66 Print checksum and block counts for each FILE.\n\
67 \n\
68 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
69 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
70 --help display this help and exit\n\
71 --version output version information and exit\n\
72 \n\
73 With no FILE, or when FILE is -, read standard input.\n\
74 "));
75 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
77 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
80 /* Calculate and print the rotated checksum and the size in 1K blocks
81 of file FILE, or of the standard input if FILE is "-".
82 If PRINT_NAME is >1, print FILE next to the checksum and size.
83 The checksum varies depending on sizeof(int).
84 Return 0 if successful, -1 if an error occurs. */
86 static int
87 bsd_sum_file (const char *file, int print_name)
89 register FILE *fp;
90 register unsigned long checksum = 0; /* The checksum mod 2^16. */
91 register long total_bytes = 0; /* The number of bytes. */
92 register int ch; /* Each character read. */
94 if (STREQ (file, "-"))
96 fp = stdin;
97 have_read_stdin = 1;
99 else
101 fp = fopen (file, "r");
102 if (fp == NULL)
104 error (0, errno, "%s", file);
105 return -1;
108 /* Need binary I/O, or else byte counts and checksums are incorrect. */
109 SET_BINARY (fileno(fp));
111 while ((ch = getc (fp)) != EOF)
113 total_bytes++;
114 ROTATE_RIGHT (checksum);
115 checksum += ch;
116 checksum &= 0xffff; /* Keep it within bounds. */
119 if (ferror (fp))
121 error (0, errno, "%s", file);
122 if (!STREQ (file, "-"))
123 fclose (fp);
124 return -1;
127 if (!STREQ (file, "-") && fclose (fp) == EOF)
129 error (0, errno, "%s", file);
130 return -1;
133 printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
134 if (print_name > 1)
135 printf (" %s", file);
136 putchar ('\n');
138 return 0;
141 /* Calculate and print the checksum and the size in 512-byte blocks
142 of file FILE, or of the standard input if FILE is "-".
143 If PRINT_NAME is >0, print FILE next to the checksum and size.
144 Return 0 if successful, -1 if an error occurs. */
146 static int
147 sysv_sum_file (const char *file, int print_name)
149 int fd;
150 unsigned char buf[8192];
151 register int bytes_read;
152 register unsigned long checksum = 0;
153 long total_bytes = 0;
155 if (STREQ (file, "-"))
157 fd = 0;
158 have_read_stdin = 1;
160 else
162 fd = open (file, O_RDONLY);
163 if (fd == -1)
165 error (0, errno, "%s", file);
166 return -1;
169 /* Need binary I/O, or else byte counts and checksums are incorrect. */
170 SET_BINARY (fd);
172 while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
174 register int i;
176 for (i = 0; i < bytes_read; i++)
177 checksum += buf[i];
178 total_bytes += bytes_read;
181 if (bytes_read < 0)
183 error (0, errno, "%s", file);
184 if (!STREQ (file, "-"))
185 close (fd);
186 return -1;
189 if (!STREQ (file, "-") && close (fd) == -1)
191 error (0, errno, "%s", file);
192 return -1;
195 printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
196 if (print_name)
197 printf (" %s", file);
198 putchar ('\n');
200 return 0;
204 main (int argc, char **argv)
206 int errors = 0;
207 int optc;
208 int files_given;
209 int (*sum_func) () = bsd_sum_file;
211 program_name = argv[0];
212 setlocale (LC_ALL, "");
213 bindtextdomain (PACKAGE, LOCALEDIR);
214 textdomain (PACKAGE);
216 have_read_stdin = 0;
218 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
220 switch (optc)
222 case 0:
223 break;
225 case 'r': /* For SysV compatibility. */
226 sum_func = bsd_sum_file;
227 break;
229 case 's':
230 sum_func = sysv_sum_file;
231 break;
233 case_GETOPT_HELP_CHAR;
235 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
237 default:
238 usage (1);
242 files_given = argc - optind;
243 if (files_given == 0)
245 if ((*sum_func) ("-", files_given) < 0)
246 errors = 1;
248 else
249 for (; optind < argc; optind++)
250 if ((*sum_func) (argv[optind], files_given) < 0)
251 errors = 1;
253 if (have_read_stdin && fclose (stdin) == EOF)
254 error (EXIT_FAILURE, errno, "-");
255 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);