home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / f2c-93.04.28-src.tgz / tar.out / fsf / f2c / src / xsum.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  234 lines

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