| 1 |
/**************************************************************** |
| 2 |
Copyright 1990, 1993, 1994, 2000 by AT&T, Lucent Technologies and Bellcore. |
| 3 |
|
| 4 |
Permission to use, copy, modify, and distribute this software and its |
| 5 |
documentation for any purpose and without fee is hereby granted, |
| 6 |
provided that the above copyright notice appear in all copies and that |
| 7 |
both that the copyright notice and this permission notice and warranty |
| 8 |
disclaimer appear in supporting documentation, and that the names of |
| 9 |
AT&T, Bell Laboratories, Lucent or Bellcore or any of their entities |
| 10 |
not be used in advertising or publicity pertaining to distribution of |
| 11 |
the software without specific, written prior permission. |
| 12 |
|
| 13 |
AT&T, Lucent and Bellcore disclaim all warranties with regard to this |
| 14 |
software, including all implied warranties of merchantability and |
| 15 |
fitness. In no event shall AT&T or Bellcore be liable for any |
| 16 |
special, indirect or consequential damages or any damages whatsoever |
| 17 |
resulting from loss of use, data or profits, whether in an action of |
| 18 |
contract, negligence or other tortious action, arising out of or in |
| 19 |
connection with the use or performance of this software. |
| 20 |
****************************************************************/ |
| 21 |
|
| 22 |
#undef _POSIX_SOURCE |
| 23 |
#define _POSIX_SOURCE |
| 24 |
#include "stdio.h" |
| 25 |
#ifndef KR_headers |
| 26 |
#include "stdlib.h" |
| 27 |
#include "sys/types.h" |
| 28 |
#ifndef MSDOS |
| 29 |
#include "unistd.h" /* for read, close */ |
| 30 |
#endif |
| 31 |
#include "fcntl.h" /* for declaration of open, O_RDONLY */ |
| 32 |
#endif |
| 33 |
#ifdef MSDOS |
| 34 |
#include "io.h" |
| 35 |
#endif |
| 36 |
#ifndef O_RDONLY |
| 37 |
#define O_RDONLY 0 |
| 38 |
#endif |
| 39 |
#ifndef O_BINARY |
| 40 |
#define O_BINARY O_RDONLY |
| 41 |
#endif |
| 42 |
|
| 43 |
char *progname; |
| 44 |
static int ignore_cr; |
| 45 |
|
| 46 |
void |
| 47 |
#ifdef KR_headers |
| 48 |
usage(rc) |
| 49 |
#else |
| 50 |
usage(int rc) |
| 51 |
#endif |
| 52 |
{ |
| 53 |
fprintf(stderr, "usage: %s [-r] [file [file...]]\n\ |
| 54 |
option -r ignores carriage return characters\n", progname); |
| 55 |
exit(rc); |
| 56 |
} |
| 57 |
|
| 58 |
typedef unsigned char Uchar; |
| 59 |
|
| 60 |
long |
| 61 |
#ifdef KR_headers |
| 62 |
sum32(sum, x, n) |
| 63 |
register long sum; |
| 64 |
register Uchar *x; |
| 65 |
int n; |
| 66 |
#else |
| 67 |
sum32(register long sum, register Uchar *x, int n) |
| 68 |
#endif |
| 69 |
{ |
| 70 |
register Uchar *xe; |
| 71 |
static long crc_table[256] = { |
| 72 |
0, 151466134, 302932268, 453595578, |
| 73 |
-9583591, -160762737, -312236747, -463170141, |
| 74 |
-19167182, -136529756, -321525474, -439166584, |
| 75 |
28724267, 145849533, 330837255, 448732561, |
| 76 |
-38334364, -189783822, -273059512, -423738914, |
| 77 |
47895677, 199091435, 282375505, 433292743, |
| 78 |
57448534, 174827712, 291699066, 409324012, |
| 79 |
-67019697, -184128295, -300991133, -418902539, |
| 80 |
-76668728, -227995554, -379567644, -530091662, |
| 81 |
67364049, 218420295, 369985021, 520795499, |
| 82 |
95791354, 213031020, 398182870, 515701056, |
| 83 |
-86479645, -203465611, -388624945, -506380967, |
| 84 |
114897068, 266207290, 349655424, 500195606, |
| 85 |
-105581387, -256654301, -340093543, -490887921, |
| 86 |
-134039394, -251295736, -368256590, -485758684, |
| 87 |
124746887, 241716241, 358686123, 476458301, |
| 88 |
-153337456, -2395898, -455991108, -304803798, |
| 89 |
162629001, 11973919, 465560741, 314102835, |
| 90 |
134728098, 16841012, 436840590, 319723544, |
| 91 |
-144044613, -26395347, -446403433, -329032703, |
| 92 |
191582708, 40657250, 426062040, 274858062, |
| 93 |
-200894995, -50223749, -435620671, -284179369, |
| 94 |
-172959290, -55056048, -406931222, -289830788, |
| 95 |
182263263, 64630089, 416513267, 299125861, |
| 96 |
229794136, 78991822, 532414580, 381366498, |
| 97 |
-220224191, -69691945, -523123603, -371788549, |
| 98 |
-211162774, -93398532, -513308602, -396314416, |
| 99 |
201600371, 84090341, 503991391, 386759881, |
| 100 |
-268078788, -117292630, -502591472, -351526778, |
| 101 |
258520357, 107972019, 493278217, 341959839, |
| 102 |
249493774, 131713432, 483432482, 366454964, |
| 103 |
-239911657, -122417791, -474129349, -356881235, |
| 104 |
-306674912, -457198666, -4791796, -156118374, |
| 105 |
315967289, 466778031, 14362133, 165418627, |
| 106 |
325258002, 442776452, 23947838, 141187752, |
| 107 |
-334573813, -452329571, -33509849, -150495567, |
| 108 |
269456196, 419996626, 33682024, 184992510, |
| 109 |
-278767779, -429561909, -43239823, -194312473, |
| 110 |
-288089226, -405591072, -52790694, -170046772, |
| 111 |
297394031, 415166457, 62373443, 179343061, |
| 112 |
383165416, 533828478, 81314500, 232780370, |
| 113 |
-373594127, -524527769, -72022307, -223201717, |
| 114 |
-401789990, -519431348, -100447498, -217810336, |
| 115 |
392228803, 510123861, 91131631, 208256633, |
| 116 |
-345918580, -496598246, -110112096, -261561802, |
| 117 |
336361365, 487278339, 100800185, 251995695, |
| 118 |
364526526, 482151208, 129260178, 246639108, |
| 119 |
-354943065, -472854735, -119955829, -237064675, |
| 120 |
459588272, 308539942, 157983644, 7181066, |
| 121 |
-469170519, -317835713, -167286907, -16754925, |
| 122 |
-440448382, -323454444, -139383890, -21619912, |
| 123 |
450006683, 332774925, 148697015, 31186721, |
| 124 |
-422325548, -271261118, -186797064, -36011154, |
| 125 |
431888077, 280569435, 196114401, 45565815, |
| 126 |
403200742, 286222960, 168180682, 50400092, |
| 127 |
-412770561, -295522711, -177471533, -59977915, |
| 128 |
-536157576, -384970002, -234585260, -83643454, |
| 129 |
526853729, 375396087, 225003341, 74348507, |
| 130 |
517040714, 399923932, 215944038, 98057200, |
| 131 |
-507728301, -390357307, -206385281, -88735767, |
| 132 |
498987548, 347783818, 263426864, 112501670, |
| 133 |
-489671163, -338229613, -253864151, -103192641, |
| 134 |
-479823314, -362722632, -244835582, -126932076, |
| 135 |
470531639, 353144481, 235265819, 117632909 |
| 136 |
}; |
| 137 |
|
| 138 |
xe = x + n; |
| 139 |
while(x < xe) |
| 140 |
sum = crc_table[(sum ^ *x++) & 0xff] ^ (sum >> 8 & 0xffffff); |
| 141 |
return sum; |
| 142 |
} |
| 143 |
|
| 144 |
int |
| 145 |
#ifdef KR_headers |
| 146 |
cr_purge(buf, n) |
| 147 |
Uchar *buf; |
| 148 |
int n; |
| 149 |
#else |
| 150 |
cr_purge(Uchar *buf, int n) |
| 151 |
#endif |
| 152 |
{ |
| 153 |
register Uchar *b, *b1, *be; |
| 154 |
b = buf; |
| 155 |
be = b + n; |
| 156 |
while(b < be) |
| 157 |
if (*b++ == '\r') { |
| 158 |
b1 = b - 1; |
| 159 |
while(b < be) |
| 160 |
if ((*b1 = *b++) != '\r') |
| 161 |
b1++; |
| 162 |
return b1 - buf; |
| 163 |
} |
| 164 |
return n; |
| 165 |
} |
| 166 |
|
| 167 |
static Uchar Buf[16*1024]; |
| 168 |
|
| 169 |
void |
| 170 |
#ifdef KR_headers |
| 171 |
process(s, x) |
| 172 |
char *s; |
| 173 |
int x; |
| 174 |
#else |
| 175 |
process(char *s, int x) |
| 176 |
#endif |
| 177 |
{ |
| 178 |
register int n; |
| 179 |
long fsize, sum; |
| 180 |
|
| 181 |
sum = 0; |
| 182 |
fsize = 0; |
| 183 |
while((n = read(x, (char *)Buf, sizeof(Buf))) > 0) { |
| 184 |
if (ignore_cr) |
| 185 |
n = cr_purge(Buf, n); |
| 186 |
fsize += n; |
| 187 |
sum = sum32(sum, Buf, n); |
| 188 |
} |
| 189 |
sum &= 0xffffffff; |
| 190 |
if (n==0) |
| 191 |
printf("%s\t%lx\t%ld\n", s, sum & 0xffffffff, fsize); |
| 192 |
else { perror(s); } |
| 193 |
close(x); |
| 194 |
} |
| 195 |
|
| 196 |
#ifdef KR_headers |
| 197 |
main(argc, argv) |
| 198 |
char **argv; |
| 199 |
#else |
| 200 |
main(int argc, char **argv) |
| 201 |
#endif |
| 202 |
{ |
| 203 |
int x; |
| 204 |
char *s; |
| 205 |
static int rc; |
| 206 |
|
| 207 |
progname = *argv; |
| 208 |
argc = argc; /* turn off "not used" warning */ |
| 209 |
s = *++argv; |
| 210 |
if (s && *s == '-') { |
| 211 |
switch(s[1]) { |
| 212 |
case '?': |
| 213 |
usage(0); |
| 214 |
case 'r': |
| 215 |
ignore_cr = 1; |
| 216 |
case '-': |
| 217 |
break; |
| 218 |
default: |
| 219 |
fprintf(stderr, "invalid option %s\n", s); |
| 220 |
usage(1); |
| 221 |
} |
| 222 |
s = *++argv; |
| 223 |
} |
| 224 |
if (s) do { |
| 225 |
x = open(s, O_RDONLY|O_BINARY); |
| 226 |
if (x < 0) { |
| 227 |
fprintf(stderr, "%s: can't open %s\n", progname, s); |
| 228 |
rc |= 1; |
| 229 |
} |
| 230 |
else |
| 231 |
process(s, x); |
| 232 |
} |
| 233 |
while(s = *++argv); |
| 234 |
else { |
| 235 |
process("/dev/stdin", fileno(stdin)); |
| 236 |
} |
| 237 |
return rc; |
| 238 |
} |