-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdump.c
158 lines (126 loc) · 2.91 KB
/
hdump.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
* hdump.c : hex dump utility
*
* Author : GVT Giuseppe Vitillaro [email protected]
*
* 10/21/93 GVT First Implementation
* 11/21/93 GVT Added options via getopt
****************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "ebcdic.h"
#define BYTESTR 16
#define BYTELEN 4
void help ( );
extern int optind;
extern char *optarg;
char *prog;
unsigned char buf[BYTESTR];
/****************************************************************************
* main
****************************************************************************/
int main ( argc, argv )
int argc;
char **argv;
{
FILE *f;
char *fname;
size_t nread;
int byte;
long pos;
int c;
char e;
int eflag = 0;
int dflag = 0;
int flen = BYTELEN;
char *flenenv;
prog = argv[0];
initialize_code_pages();
flenenv = getenv("HDUMPSARG");
if ( flenenv )
flen = atoi ( flenenv );
while ( ( c = getopt ( argc, argv, "dehs:" ) ) != EOF ) {
switch ( c ) {
case 'e':
eflag++;
break;
case 'd':
dflag++;
break;
case 's':
flen = atoi ( optarg );
if ( ( flen < 1 ) || ( flen > 16 ) ) {
fprintf ( stderr, "%s: -s Illegal Value %d\n", prog, flen );
fprintf ( stderr, "%s: value must be from 1 to 16\n", prog );
help ( );
exit ( 1 );
}
break;
case 'h':
help ( );
exit ( 1 );
break;
default:
help ( );
exit ( 1 );
break;
}
}
if ( optind == argc ) {
f = stdin;
}
else {
fname = argv[optind];
f = fopen ( fname, "r" );
if ( !f ) {
fprintf ( stderr, "%s: %s ", prog, fname );
perror ( "" );
exit ( 2 );
}
}
pos = 0;
while ( ( nread = fread ( buf, 1, BYTESTR, f ) ) > 0 ) {
if ( dflag )
printf ( "%010ld: ", pos );
else
printf ( "%08lx: ", pos );
for ( byte = 0; byte < BYTESTR; byte++ ) {
if ( byte < nread )
printf ( "%02x", buf[byte] );
else
printf ( " " );
if ( ( byte % flen ) == ( flen - 1 ) )
printf ( " " );
}
printf ( " [" );
for ( byte = 0; byte < BYTESTR; byte++ ) {
if ( byte < nread ) {
if ( eflag )
e = EBCDIC_to_ASCII ( buf[byte] );
else
e = buf[byte];
if ( isprint ( e ) )
printf ( "%c", e );
else
printf ( "." );
}
}
printf ( "]\n" );
pos += nread;
}
if ( f != stdin )
fclose ( f );
return 0;
}
/****************************************************************************
* help : Syntax
****************************************************************************/
void help ( )
{
fprintf ( stderr, "%s usage: hdump [-dh] [-s len] filename\n", prog );
return;
}