-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.c
More file actions
90 lines (84 loc) · 1.65 KB
/
dump.c
File metadata and controls
90 lines (84 loc) · 1.65 KB
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
89
90
#include <stdio.h>
#include <sys/types.h>
static void dump(FILE *fp);
static void puthex2(u_long n);
static void puthex8(u_long n);
int main(int argc, char **argv)
{
int ne = 0;
if (argc > 1) {
int i;
for (i = 1; i < argc; i++) {
FILE *fp = NULL;
printf("# %s\n", argv[i]);
if (argv[i][0] == '-' && argv[i][1] == '\0')
fp = stdin;
else {
if ((fp = fopen(argv[i], "rb")) == NULL)
fp = fopen(argv[i], "r");
}
if (fp != NULL)
dump(fp);
else {
perror(argv[i]);
ne++;
}
}
} else {
dump(stdin);
}
return ne;
}
static char hexchar[16] = "0123456789abcdef";
static void puthex2(u_long n)
{
putchar(hexchar[(n >> 4) & 0xf]);
putchar(hexchar[(n >> 0) & 0xf]);
}
static void puthex8(u_long n)
{
putchar(hexchar[(n >> 28) & 0xf]);
putchar(hexchar[(n >> 24) & 0xf]);
putchar(hexchar[(n >> 20) & 0xf]);
putchar(hexchar[(n >> 16) & 0xf]);
putchar(hexchar[(n >> 12) & 0xf]);
putchar(hexchar[(n >> 8) & 0xf]);
putchar(hexchar[(n >> 4) & 0xf]);
putchar(hexchar[(n >> 0) & 0xf]);
}
static void dump(FILE *fp)
{
u_long pos;
u_char buf[16];
int i, len;
pos = 0;
while (1) {
len = fread(buf, 1, 16, fp);
if (len == 0)
break;
puthex8(pos);
putchar(' ');
for (i = 0; i < len; i++) {
if ((i & 7) == 0)
putchar(' ');
puthex2(buf[i]);
putchar(' ');
}
for ( ; i < 16; i++) {
if ((i & 7) == 0)
fputs(" ", stdout);
else
fputs(" ", stdout);
}
putchar(' ');
for (i = 0; i < len; i++) {
if (buf[i] < 0x20 || buf[i] > 0x7e)
putchar('.');
else
putchar(buf[i]);
}
pos += len;
putchar('\n');
}
}
/*EOF*/