summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/hexdump.c
blob: bb90b7a9d786fea13d364034db9e2954a09b3e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
**  HEXDUMP.C - Dump a file.
**
**  This Program Written By Paul Edwards w/ modifications by Bob Stout
**  Released to the public domain
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static void dodump(FILE *fp, long start, long count);
static void skipb(FILE *fp, long start);

main(int argc, char **argv)
{
      FILE *fp;
      long start, count;

      if (argc < 2)
      {
            puts("Usage: HEXDUMP file_name [start] [length]");
            return (EXIT_FAILURE);
      }
      if (argc > 2)
            start = atol(*(argv + 2));
      else  start = 0L;
      if (argc > 3)
            count = atol(*(argv + 3));
      else  count = -1L;
      fp = fopen(*(argv + 1), "rb");
      if (fp == NULL)
      {
            printf("unable to open file %s for input\n", *(argv+1));
            return (EXIT_FAILURE);
      }
      skipb(fp, start);
      dodump(fp, start, count);
      return (EXIT_SUCCESS);
}

static void dodump(FILE *fp, long start, long count)
{
      int c, pos1, pos2;
      long x = 0L;
      char prtln[100];

      while (((c = fgetc(fp)) != EOF) && (x != count))
      {
            if (x%16 == 0)
            {
                  memset(prtln,' ',sizeof prtln);
                  sprintf(prtln,"%0.6X   ", start + x);
                  pos1 = 8;
                  pos2 = 45;
            }
            sprintf(prtln + pos1, "%0.2X", c);
            if (isprint(c))
                  sprintf(prtln + pos2, "%c", c);
            else  sprintf(prtln + pos2, ".");
            pos1 += 2;
            *(prtln+pos1) = ' ';
            pos2++;
            if (x % 4 == 3)
                  *(prtln + pos1++) = ' ';
            if (x % 16 == 15)
                  printf("%s\n", prtln);
            x++;
      }
      if (x % 16 != 15)
            printf("%s\n", prtln);
      return;
}

static void skipb(FILE *fp, long start)
{
      long x = 0;

      if (start == 0)
            return;
      while (x < start)
      {
            fgetc(fp);
            x++;
      }
      return;
}