*** empty log message ***
[coreutils.git] / src / sum.c
blob81a5897869733204db90fc47c8bb6b694c9db522
1 /* sum -- checksum and count the blocks in a file
2 Copyright (C) 86, 89, 91, 1995-2002, 2004 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 Foundation,
16 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 "human.h"
30 #include "safe-read.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "sum"
35 #define AUTHORS "Kayvan Aghaiepour", "David MacKenzie"
37 /* The name this program was run with. */
38 char *program_name;
40 /* Nonzero if any of the files read were the standard input. */
41 static int have_read_stdin;
43 static struct option const longopts[] =
45 {"sysv", no_argument, NULL, 's'},
46 {GETOPT_HELP_OPTION_DECL},
47 {GETOPT_VERSION_OPTION_DECL},
48 {NULL, 0, NULL, 0}
51 void
52 usage (int status)
54 if (status != EXIT_SUCCESS)
55 fprintf (stderr, _("Try `%s --help' for more information.\n"),
56 program_name);
57 else
59 printf (_("\
60 Usage: %s [OPTION]... [FILE]...\n\
61 "),
62 program_name);
63 fputs (_("\
64 Print checksum and block counts for each FILE.\n\
65 \n\
66 -r defeat -s, use BSD sum algorithm, use 1K blocks\n\
67 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
68 "), stdout);
69 fputs (HELP_OPTION_DESCRIPTION, stdout);
70 fputs (VERSION_OPTION_DESCRIPTION, stdout);
71 fputs (_("\
72 \n\
73 With no FILE, or when FILE is -, read standard input.\n\
74 "), stdout);
75 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
77 exit (status);
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 int checksum = 0; /* The checksum mod 2^16. */
91 register uintmax_t total_bytes = 0; /* The number of bytes. */
92 register int ch; /* Each character read. */
93 char hbuf[LONGEST_HUMAN_READABLE + 1];
95 if (STREQ (file, "-"))
97 fp = stdin;
98 have_read_stdin = 1;
100 else
102 fp = fopen (file, "r");
103 if (fp == NULL)
105 error (0, errno, "%s", file);
106 return -1;
109 /* Need binary I/O, or else byte counts and checksums are incorrect. */
110 SET_BINARY (fileno(fp));
112 while ((ch = getc (fp)) != EOF)
114 total_bytes++;
115 checksum = (checksum >> 1) + ((checksum & 1) << 15);
116 checksum += ch;
117 checksum &= 0xffff; /* Keep it within bounds. */
120 if (ferror (fp))
122 error (0, errno, "%s", file);
123 if (!STREQ (file, "-"))
124 fclose (fp);
125 return -1;
128 if (!STREQ (file, "-") && fclose (fp) == EOF)
130 error (0, errno, "%s", file);
131 return -1;
134 printf ("%05d %5s", checksum,
135 human_readable (total_bytes, hbuf, human_ceiling, 1, 1024));
136 if (print_name > 1)
137 printf (" %s", file);
138 putchar ('\n');
140 return 0;
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. */
148 static int
149 sysv_sum_file (const char *file, int print_name)
151 int fd;
152 unsigned char buf[8192];
153 uintmax_t total_bytes = 0;
154 char hbuf[LONGEST_HUMAN_READABLE + 1];
155 int r;
156 int checksum;
158 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
159 unsigned int s = 0;
161 if (STREQ (file, "-"))
163 fd = 0;
164 have_read_stdin = 1;
166 else
168 fd = open (file, O_RDONLY);
169 if (fd == -1)
171 error (0, errno, "%s", file);
172 return -1;
175 /* Need binary I/O, or else byte counts and checksums are incorrect. */
176 SET_BINARY (fd);
178 while (1)
180 size_t i;
181 size_t bytes_read = safe_read (fd, buf, sizeof buf);
183 if (bytes_read == 0)
184 break;
186 if (bytes_read == SAFE_READ_ERROR)
188 error (0, errno, "%s", file);
189 if (!STREQ (file, "-"))
190 close (fd);
191 return -1;
194 for (i = 0; i < bytes_read; i++)
195 s += buf[i];
196 total_bytes += bytes_read;
199 if (!STREQ (file, "-") && close (fd) == -1)
201 error (0, errno, "%s", file);
202 return -1;
205 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
206 checksum = (r & 0xffff) + (r >> 16);
208 printf ("%d %s", checksum,
209 human_readable (total_bytes, hbuf, human_ceiling, 1, 512));
210 if (print_name)
211 printf (" %s", file);
212 putchar ('\n');
214 return 0;
218 main (int argc, char **argv)
220 int errors = 0;
221 int optc;
222 int files_given;
223 int (*sum_func) (const char *, int) = bsd_sum_file;
225 initialize_main (&argc, &argv);
226 program_name = argv[0];
227 setlocale (LC_ALL, "");
228 bindtextdomain (PACKAGE, LOCALEDIR);
229 textdomain (PACKAGE);
231 atexit (close_stdout);
233 have_read_stdin = 0;
235 while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
237 switch (optc)
239 case 0:
240 break;
242 case 'r': /* For SysV compatibility. */
243 sum_func = bsd_sum_file;
244 break;
246 case 's':
247 sum_func = sysv_sum_file;
248 break;
250 case_GETOPT_HELP_CHAR;
252 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
254 default:
255 usage (EXIT_FAILURE);
259 files_given = argc - optind;
260 if (files_given == 0)
262 if ((*sum_func) ("-", files_given) < 0)
263 errors = 1;
265 else
266 for (; optind < argc; optind++)
267 if ((*sum_func) (argv[optind], files_given) < 0)
268 errors = 1;
270 if (have_read_stdin && fclose (stdin) == EOF)
271 error (EXIT_FAILURE, errno, "-");
272 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);