8 #include <syslinux/loadfile.h>
11 #define ROWS_PER_PAGE 24
12 #define COLS_PER_ROW 16
13 #define BYTES_PER_PAGE (ROWS_PER_PAGE * COLS_PER_ROW)
15 /* Functions declarations */
16 static int usage(void);
17 static void eat_stdin(void);
18 static int do_page(void);
19 static void hexdump(const void *memory
, size_t bytes
);
22 static const char *prog_name
;
24 static int opt_no_buffer
;
25 static int opt_extended_ascii
;
27 int main(int argc
, char **argv
)
41 /* Determine the program name, as invoked */
42 if (argc
< 1 || !argv
|| !argv
[0]) {
43 fprintf(stderr
, "argc or argv failure!\n");
48 /* Process arguments */
50 for (i
= 1; i
< argc
; ++i
) {
52 fprintf(stderr
, "argc and argv mismatch!\n");
56 if (!strncmp(argv
[i
], "--page", sizeof "--page") ||
57 !strncmp(argv
[i
], "-p", sizeof "-p")) {
62 if (!strncmp(argv
[i
], "--no-buffer", sizeof "--no-buffer")) {
67 if (!strncmp(argv
[i
], "--extended-ascii", sizeof "--extended-ascii")) {
68 opt_extended_ascii
= 1;
72 if (!strncmp(argv
[i
], "--help", sizeof "--help") ||
73 !strncmp(argv
[i
], "-h", sizeof "-h") ||
74 !strncmp(argv
[i
], "-?", sizeof "-?"))
77 /* Otherwise, interpret as a filename, but only accept one */
84 fprintf(stdout
, "Dumping file: %s\n", filename
);
86 /* Either fetch the whole file, or just allocate a buffer */
90 if (loadfile(filename
, &file_data
, &file_sz
)) {
91 fprintf(stderr
, "Couldn't load file. Error: %d\n", errno
);
95 file_sz
= BYTES_PER_PAGE
;
96 file_data
= malloc(file_sz
);
98 fprintf(stderr
, "Couldn't allocate file data buffer\n");
102 f
= fopen(filename
, "r");
104 fprintf(stderr
, "Couldn't open file. Error: %d\n", errno
);
110 len
= BYTES_PER_PAGE
;
115 len
= fread(file_data
, 1, file_sz
, f
);
125 hexdump(cur_pos
, len
);
127 /* Pause, if requested */
129 /* The user might choose to quit */
134 /* Reduce file_sz for non-buffered mode */
137 } while (cur_pos
+= len
);
155 static int usage(void)
157 static const char usage
[] =
158 "Usage: %s [<option> [...]] <filename> [<option> [...]]\n"
161 " --page . . . . . . . Pause output every 24 lines\n"
162 " --no-buffer . . . . Load the entire file before dumping\n"
163 " --extended-ascii . . Use extended ASCII chars in dump\n"
166 " --help . . . . . . Display this help\n";
168 fprintf(stderr
, usage
, prog_name
);
172 static void eat_stdin(void)
178 if (i
== EOF
|| i
== '\n')
182 static int do_page(void)
187 fprintf(stdout
, "Continue? [Y|n]: ");
196 fprintf(stderr
, "No response. Continuing...\n");
197 /* Fall through to "yes" */
206 fprintf(stderr
, "Invalid choice\n");
212 static void hexdump(const void *memory
, size_t bytes
)
214 const unsigned char *p
, *q
;
220 printf("%p: ", (void *) p
);
221 for (i
= 0; i
< 16 && bytes
; ++i
) {
233 for (i
= 0; i
< 16 && bytes
; ++i
) {
234 printf("%c", isprint(*p
) && !isspace(*p
) ? *p
: ' ');