00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #undef _POSIX_SOURCE
00023 #define _POSIX_SOURCE
00024 #include "stdio.h"
00025 #ifndef KR_headers
00026 #include "stdlib.h"
00027 #include "sys/types.h"
00028 #ifndef MSDOS
00029 #include "unistd.h"
00030 #endif
00031 #include "fcntl.h"
00032 #endif
00033 #ifdef MSDOS
00034 #include "io.h"
00035 #endif
00036 #ifndef O_RDONLY
00037 #define O_RDONLY 0
00038 #endif
00039 #ifndef O_BINARY
00040 #define O_BINARY O_RDONLY
00041 #endif
00042
00043
00044
00045 char *progname;
00046 static int ignore_cr;
00047
00048 void
00049 #ifdef KR_headers
00050 usage(rc)
00051 #else
00052 usage(int rc)
00053 #endif
00054 {
00055 fprintf(stderr, "usage: %s [-r] [file [file...]]\n\
00056 option -r ignores carriage return characters\n", progname);
00057 exit(rc);
00058 }
00059
00060 typedef unsigned char Uchar;
00061
00062 long
00063 #ifdef KR_headers
00064 sum32(sum, x, n)
00065 register long sum;
00066 register Uchar *x;
00067 int n;
00068 #else
00069 sum32(register long sum, register Uchar *x, int n)
00070 #endif
00071 {
00072 register Uchar *xe;
00073 static long crc_table[256] = {
00074 0, 151466134, 302932268, 453595578,
00075 -9583591, -160762737, -312236747, -463170141,
00076 -19167182, -136529756, -321525474, -439166584,
00077 28724267, 145849533, 330837255, 448732561,
00078 -38334364, -189783822, -273059512, -423738914,
00079 47895677, 199091435, 282375505, 433292743,
00080 57448534, 174827712, 291699066, 409324012,
00081 -67019697, -184128295, -300991133, -418902539,
00082 -76668728, -227995554, -379567644, -530091662,
00083 67364049, 218420295, 369985021, 520795499,
00084 95791354, 213031020, 398182870, 515701056,
00085 -86479645, -203465611, -388624945, -506380967,
00086 114897068, 266207290, 349655424, 500195606,
00087 -105581387, -256654301, -340093543, -490887921,
00088 -134039394, -251295736, -368256590, -485758684,
00089 124746887, 241716241, 358686123, 476458301,
00090 -153337456, -2395898, -455991108, -304803798,
00091 162629001, 11973919, 465560741, 314102835,
00092 134728098, 16841012, 436840590, 319723544,
00093 -144044613, -26395347, -446403433, -329032703,
00094 191582708, 40657250, 426062040, 274858062,
00095 -200894995, -50223749, -435620671, -284179369,
00096 -172959290, -55056048, -406931222, -289830788,
00097 182263263, 64630089, 416513267, 299125861,
00098 229794136, 78991822, 532414580, 381366498,
00099 -220224191, -69691945, -523123603, -371788549,
00100 -211162774, -93398532, -513308602, -396314416,
00101 201600371, 84090341, 503991391, 386759881,
00102 -268078788, -117292630, -502591472, -351526778,
00103 258520357, 107972019, 493278217, 341959839,
00104 249493774, 131713432, 483432482, 366454964,
00105 -239911657, -122417791, -474129349, -356881235,
00106 -306674912, -457198666, -4791796, -156118374,
00107 315967289, 466778031, 14362133, 165418627,
00108 325258002, 442776452, 23947838, 141187752,
00109 -334573813, -452329571, -33509849, -150495567,
00110 269456196, 419996626, 33682024, 184992510,
00111 -278767779, -429561909, -43239823, -194312473,
00112 -288089226, -405591072, -52790694, -170046772,
00113 297394031, 415166457, 62373443, 179343061,
00114 383165416, 533828478, 81314500, 232780370,
00115 -373594127, -524527769, -72022307, -223201717,
00116 -401789990, -519431348, -100447498, -217810336,
00117 392228803, 510123861, 91131631, 208256633,
00118 -345918580, -496598246, -110112096, -261561802,
00119 336361365, 487278339, 100800185, 251995695,
00120 364526526, 482151208, 129260178, 246639108,
00121 -354943065, -472854735, -119955829, -237064675,
00122 459588272, 308539942, 157983644, 7181066,
00123 -469170519, -317835713, -167286907, -16754925,
00124 -440448382, -323454444, -139383890, -21619912,
00125 450006683, 332774925, 148697015, 31186721,
00126 -422325548, -271261118, -186797064, -36011154,
00127 431888077, 280569435, 196114401, 45565815,
00128 403200742, 286222960, 168180682, 50400092,
00129 -412770561, -295522711, -177471533, -59977915,
00130 -536157576, -384970002, -234585260, -83643454,
00131 526853729, 375396087, 225003341, 74348507,
00132 517040714, 399923932, 215944038, 98057200,
00133 -507728301, -390357307, -206385281, -88735767,
00134 498987548, 347783818, 263426864, 112501670,
00135 -489671163, -338229613, -253864151, -103192641,
00136 -479823314, -362722632, -244835582, -126932076,
00137 470531639, 353144481, 235265819, 117632909
00138 };
00139
00140 xe = x + n;
00141 while(x < xe)
00142 sum = crc_table[(sum ^ *x++) & 0xff] ^ (sum >> 8 & 0xffffff);
00143 return sum;
00144 }
00145
00146 int
00147 #ifdef KR_headers
00148 cr_purge(buf, n)
00149 Uchar *buf;
00150 int n;
00151 #else
00152 cr_purge(Uchar *buf, int n)
00153 #endif
00154 {
00155 register Uchar *b, *b1, *be;
00156 b = buf;
00157 be = b + n;
00158 while(b < be)
00159 if (*b++ == '\r') {
00160 b1 = b - 1;
00161 while(b < be)
00162 if ((*b1 = *b++) != '\r')
00163 b1++;
00164 return b1 - buf;
00165 }
00166 return n;
00167 }
00168
00169 static Uchar Buf[16*1024];
00170
00171 void
00172 #ifdef KR_headers
00173 process(s, x)
00174 char *s;
00175 int x;
00176 #else
00177 process(char *s, int x)
00178 #endif
00179
00180 #if _MSC_VER
00181 #define read _read
00182 #define close _close
00183 #endif
00184
00185 {
00186 register int n;
00187 long fsize, sum;
00188
00189 sum = 0;
00190 fsize = 0;
00191 while((n = read(x, (char *)Buf, sizeof(Buf))) > 0) {
00192 if (ignore_cr)
00193 n = cr_purge(Buf, n);
00194 fsize += n;
00195 sum = sum32(sum, Buf, n);
00196 }
00197 sum &= 0xffffffff;
00198 if (n==0)
00199 printf("%s\t%lx\t%ld\n", s, sum & 0xffffffff, fsize);
00200 else { perror(s); }
00201 close(x);
00202 }
00203
00204 #ifdef KR_headers
00205 main(argc, argv)
00206 char **argv;
00207 #else
00208 main(int argc, char **argv)
00209 #endif
00210 {
00211 int x;
00212 char *s;
00213 static int rc;
00214
00215 progname = *argv;
00216 argc = argc;
00217 s = *++argv;
00218 if (s && *s == '-') {
00219 switch(s[1]) {
00220 case '?':
00221 usage(0);
00222 case 'r':
00223 ignore_cr = 1;
00224 case '-':
00225 break;
00226 default:
00227 fprintf(stderr, "invalid option %s\n", s);
00228 usage(1);
00229 }
00230 s = *++argv;
00231 }
00232 if (s) do {
00233 x = open(s, O_RDONLY|O_BINARY);
00234 if (x < 0) {
00235 fprintf(stderr, "%s: can't open %s\n", progname, s);
00236 rc |= 1;
00237 }
00238 else
00239 process(s, x);
00240 }
00241 while(s = *++argv);
00242 else {
00243 process("/dev/stdin", fileno(stdin));
00244 }
00245 return rc;
00246 }