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