home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / pct.arc / DUMP.C < prev    next >
C/C++ Source or Header  |  1986-03-29  |  1KB  |  60 lines

  1. /* hexdump.c:   dump a file to standard output in hex format */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. main(argc,argv)
  7. int argc;
  8. char *argv[];
  9. {
  10.     FILE *f;
  11.     int i;
  12.     
  13.     for (i = 1; i < argc; ++i)
  14.         if ((f = fopen(argv[i],"rb")) == NULL)
  15.             fprintf(stderr,"can't open %s\n");
  16.         else
  17.         {
  18.             dump(f,argv[i]);
  19.             fclose(f);
  20.             putchar('\f');
  21.         }
  22. }
  23.  
  24. dump(f,s)
  25. FILE *f;
  26. char *s;        
  27. {
  28.     unsigned char buf[16];
  29.     int count, i;
  30.     long size;
  31.     
  32.     printf("Dump of %s\n\n",s);
  33.     size = 0;
  34.     
  35.     while ((count = fread(buf,1,16,f)) > 0)
  36.     {
  37.         printf("  %06X ",size+=count);
  38.  
  39.         /* ..Print Hex Bytes.. */
  40.         for (i = 0; i < 16; ++i)
  41.         {
  42.             if (i < count)
  43.             {
  44.                 printf(" %02x",buf[i]);
  45.                 if (iscntrl(buf[i]))
  46.                     buf[i] = '.';
  47.             }
  48.             else
  49.             {
  50.                 fputs("   ",stdout);
  51.                 buf[i] = ' ';
  52.             }
  53.             if (i == 7)
  54.                 putchar(' ');
  55.         }
  56.        
  57.         /* ..Print Text Bytes.. */
  58.         printf(" |%16.16s|\n",buf);
  59.     }
  60. }