home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / HEXDUMP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  95 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  HEXDUMP.C - Dump a file.
  5. **
  6. **  Originally written By Paul Edwards
  7. **  Released to the public domain
  8. **
  9. **  Modified for SNIPPETS by Bob Stout
  10. **
  11. **  Uses ERR_EXIT.C and FERROF.C, also in SNIPPETS
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "errors.h"
  19.  
  20. static void dodump(FILE *fp, long start, long count);
  21.  
  22. main(int argc, char **argv)
  23. {
  24.       FILE *fp;
  25.       long start, count, length;
  26.  
  27.       if (argc < 2)
  28.             ErrExit("Usage: HEXDUMP file_name [start] [length]");
  29.       if (argc > 2)
  30.             sscanf(argv[2], "%li", &start);
  31.       else  start = 0L;
  32.       if (argc > 3)
  33.             sscanf(argv[3], "%li", &count);
  34.       else  count = -1L;
  35.  
  36.       fp = cant(argv[1], "rb");
  37.  
  38.       fseek(fp, 0L, SEEK_END);
  39.       length = ftell(fp);
  40.       if (start > length)
  41.       {
  42.             ErrExit("Can't find position %ld in a %ld byte file",
  43.                   start, length);
  44.       }
  45.       if (fseek(fp, start, SEEK_SET))
  46.             ErrExit("Unable to find position %ld", start);
  47.  
  48.       dodump(fp, start, count);
  49.       return (EXIT_SUCCESS);
  50. }
  51.  
  52. static void dodump(FILE *fp, long start, long count)
  53. {
  54.       int c, pos1, pos2, posn = (int)(start % 16L);
  55.       long x = 0L;
  56.       char prtln[80];
  57.  
  58.       while (((c = fgetc(fp)) != EOF) && (x != count))
  59.       {
  60.             if (0 == (posn % 16) || 0 == x)
  61.             {
  62.                   memset(prtln,' ',sizeof prtln);
  63.                   sprintf(prtln,"%0.6X:", start + x);
  64.                   prtln[7] = ' ';
  65.                   pos1 = 8 + (int)(3 * posn);
  66.                   if (posn > 3)
  67.                         ++pos1;
  68.                   if (posn > 7)
  69.                         ++pos1;
  70.                   if (posn > 11)
  71.                         ++pos1;
  72.                   pos2 = 60 + (int)(posn);
  73.             }
  74.             sprintf(prtln + pos1, "%0.2X ", c);
  75.             if (isprint(c))
  76.                   sprintf(prtln + pos2, "%c", c);
  77.             else  sprintf(prtln + pos2, ".");
  78.             pos1 += 3;
  79.             *(prtln+pos1) = ' ';
  80.             pos2++;
  81.             if (posn % 4 == 3)
  82.                   *(prtln + pos1++) = ' ';
  83.             if (posn % 16 == 15)
  84.             {
  85.                   printf("%s\n", prtln);
  86.                   posn = 0;
  87.             }
  88.             else  ++posn;
  89.             ++x;
  90.       }
  91.       if (posn % 16)
  92.             printf("%s\n", prtln);
  93.       return;
  94. }
  95.