1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3 // Copyright (c) 2001-2003, OpenBeOS
5 // This software is part of the OpenBeOS distribution and is covered
6 // by the OpenBeOS license.
10 // Author: Daniel Reinhold (danielre@users.sf.net)
11 // Description: hex dump utility
13 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
24 void display (uint32
, uint8
*);
26 void dump_file (FILE *);
27 char *hexbytes (uint8
*);
28 char *printable (uint8
*);
32 static int BytesBetweenSpace
= 1;
38 printf ("Usage:\thd [-n N] [file]\n");
39 printf("\t-n expects a number between 1 and 16 and specifies\n");
40 printf("\tthe number of bytes between spaces.\n");
41 printf("\n\tIf no file is specified, input is read from stdin\n");
46 main(int argc
, char *argv
[])
54 char *first
= *++argv
;
56 if (strcmp(first
, "--help") == 0) {
61 if (strcmp(first
, "-n") == 0) {
66 printf("-n option needs a numeric argument\n");
72 BytesBetweenSpace
= b
;
77 printf("no file specified\n");
81 printf("-n option needs a numeric argument\n");
100 if (stat(fname
, &e
) == -1) {
101 fprintf(stderr
, "'%s': no such file or directory\n", fname
);
105 if (S_ISDIR(e
.st_mode
))
106 fprintf(stderr
, "'%s' is a directory\n", fname
);
108 FILE *fp
= fopen(fname
, "rb");
114 fprintf(stderr
, "'%s': %s\n", fname
, strerror(errno
));
126 while ((got
= fread(data
, 1, 16, fp
)) == 16) {
127 display(offset
, data
);
132 memset(data
+got
, ' ', 16-got
);
133 display(offset
, data
);
139 display(uint32 offset
, uint8
*data
)
141 printf("%08" B_PRIx32
" ", offset
);
142 printf(" %s ", hexbytes(data
));
143 printf("%16s ", printable(data
));
158 for (i
= 0; i
< 16; ++i
) {
160 *p
++ = "0123456789abcdef"[c
/16];
161 *p
++ = "0123456789abcdef"[c
%16];
163 if (++n
== BytesBetweenSpace
) {
168 if ((i
== 7) && (BytesBetweenSpace
== 1))
188 *p
++ = (isgraph(c
) ? c
: '.');