Use AUTHORS in place of string in parse_long_options call.
[platform/upstream/coreutils.git] / src / sum.c
1 /* sum -- checksum and count the blocks in a file
2    Copyright (C) 86, 89, 91, 1995-1999 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Like BSD sum or SysV sum -r, except like SysV sum if -s option is given. */
19
20 /* Written by Kayvan Aghaiepour and David MacKenzie. */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <getopt.h>
27 #include "system.h"
28 #include "error.h"
29 #include "long-options.h"
30 #include "safe-read.h"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "sum"
34
35 #define AUTHORS "Kayvan Aghaiepour and David MacKenzie"
36
37 /* The name this program was run with. */
38 char *program_name;
39
40 /* Nonzero if any of the files read were the standard input. */
41 static int have_read_stdin;
42
43 /* Right-rotate 32-bit integer variable C. */
44 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
45
46 static struct option const longopts[] =
47 {
48   {"sysv", no_argument, NULL, 's'},
49   {NULL, 0, NULL, 0}
50 };
51
52 void
53 usage (int status)
54 {
55   if (status != 0)
56     fprintf (stderr, _("Try `%s --help' for more information.\n"),
57              program_name);
58   else
59     {
60       printf (_("\
61 Usage: %s [OPTION]... [FILE]...\n\
62 "),
63               program_name);
64       printf (_("\
65 Print checksum and block counts for each FILE.\n\
66 \n\
67   -r              defeat -s, use BSD sum algorithm, use 1K blocks\n\
68   -s, --sysv      use System V sum algorithm, use 512 bytes blocks\n\
69       --help      display this help and exit\n\
70       --version   output version information and exit\n\
71 \n\
72 With no FILE, or when FILE is -, read standard input.\n\
73 "));
74       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
75     }
76   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
77 }
78
79 /* Calculate and print the rotated checksum and the size in 1K blocks
80    of file FILE, or of the standard input if FILE is "-".
81    If PRINT_NAME is >1, print FILE next to the checksum and size.
82    The checksum varies depending on sizeof(int).
83    Return 0 if successful, -1 if an error occurs. */
84
85 static int
86 bsd_sum_file (const char *file, int print_name)
87 {
88   register FILE *fp;
89   register unsigned long checksum = 0; /* The checksum mod 2^16. */
90   register long total_bytes = 0; /* The number of bytes. */
91   register int ch;              /* Each character read. */
92
93   if (STREQ (file, "-"))
94     {
95       fp = stdin;
96       have_read_stdin = 1;
97     }
98   else
99     {
100       fp = fopen (file, "r");
101       if (fp == NULL)
102         {
103           error (0, errno, "%s", file);
104           return -1;
105         }
106     }
107   /* Need binary I/O, or else byte counts and checksums are incorrect.  */
108   SET_BINARY (fileno(fp));
109
110   while ((ch = getc (fp)) != EOF)
111     {
112       total_bytes++;
113       ROTATE_RIGHT (checksum);
114       checksum += ch;
115       checksum &= 0xffff;       /* Keep it within bounds. */
116     }
117
118   if (ferror (fp))
119     {
120       error (0, errno, "%s", file);
121       if (!STREQ (file, "-"))
122         fclose (fp);
123       return -1;
124     }
125
126   if (!STREQ (file, "-") && fclose (fp) == EOF)
127     {
128       error (0, errno, "%s", file);
129       return -1;
130     }
131
132   printf ("%05lu %5ld", checksum, (total_bytes + 1024 - 1) / 1024);
133   if (print_name > 1)
134     printf (" %s", file);
135   putchar ('\n');
136
137   return 0;
138 }
139
140 /* Calculate and print the checksum and the size in 512-byte blocks
141    of file FILE, or of the standard input if FILE is "-".
142    If PRINT_NAME is >0, print FILE next to the checksum and size.
143    Return 0 if successful, -1 if an error occurs. */
144
145 static int
146 sysv_sum_file (const char *file, int print_name)
147 {
148   int fd;
149   unsigned char buf[8192];
150   register int bytes_read;
151   register unsigned long checksum = 0;
152   long total_bytes = 0;
153
154   if (STREQ (file, "-"))
155     {
156       fd = 0;
157       have_read_stdin = 1;
158     }
159   else
160     {
161       fd = open (file, O_RDONLY);
162       if (fd == -1)
163         {
164           error (0, errno, "%s", file);
165           return -1;
166         }
167     }
168   /* Need binary I/O, or else byte counts and checksums are incorrect.  */
169   SET_BINARY (fd);
170
171   while ((bytes_read = safe_read (fd, buf, sizeof buf)) > 0)
172     {
173       register int i;
174
175       for (i = 0; i < bytes_read; i++)
176         checksum += buf[i];
177       total_bytes += bytes_read;
178     }
179
180   if (bytes_read < 0)
181     {
182       error (0, errno, "%s", file);
183       if (!STREQ (file, "-"))
184         close (fd);
185       return -1;
186     }
187
188   if (!STREQ (file, "-") && close (fd) == -1)
189     {
190       error (0, errno, "%s", file);
191       return -1;
192     }
193
194   printf ("%lu %ld", checksum % 0xffff, (total_bytes + 512 - 1) / 512);
195   if (print_name)
196     printf (" %s", file);
197   putchar ('\n');
198
199   return 0;
200 }
201
202 int
203 main (int argc, char **argv)
204 {
205   int errors = 0;
206   int optc;
207   int files_given;
208   int (*sum_func) () = bsd_sum_file;
209
210   program_name = argv[0];
211   setlocale (LC_ALL, "");
212   bindtextdomain (PACKAGE, LOCALEDIR);
213   textdomain (PACKAGE);
214
215   have_read_stdin = 0;
216
217   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
218                       AUTHORS, usage);
219
220   while ((optc = getopt_long (argc, argv, "rs", longopts, NULL)) != -1)
221     {
222       switch (optc)
223         {
224         case 0:
225           break;
226
227         case 'r':               /* For SysV compatibility. */
228           sum_func = bsd_sum_file;
229           break;
230
231         case 's':
232           sum_func = sysv_sum_file;
233           break;
234
235         default:
236           usage (1);
237         }
238     }
239
240   files_given = argc - optind;
241   if (files_given == 0)
242     {
243       if ((*sum_func) ("-", files_given) < 0)
244         errors = 1;
245     }
246   else
247     for (; optind < argc; optind++)
248       if ((*sum_func) (argv[optind], files_given) < 0)
249         errors = 1;
250
251   if (have_read_stdin && fclose (stdin) == EOF)
252     error (EXIT_FAILURE, errno, "-");
253   exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
254 }