shell/hexdump.c (view raw)
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
#include "cedos.h"
#include "stdio.h"
#include <stdint.h>
void main(char *args) {
FILE* file = fopen(args, "r");
if (file == NULL) {
printf("Could not find file: %s\n", args);
return;
}
uint8_t in_buffer[16];
uint8_t out_buffer[64];
while (1) {
int in_offset = ftell(file);
int size = fread(in_buffer, 1, 16, file);
if (size == 0) { break; }
int out_offset = 0;
out_offset += sprintf(out_buffer + out_offset, "%x%x%x%x ", in_offset >> 24, in_offset >> 16, in_offset >> 8, in_offset);
for (int i = 0; i < 16; i++) {
if (i < size) {
out_offset += sprintf(out_buffer + out_offset, "%x ", in_buffer[i]);
} else {
out_offset += sprintf(out_buffer + out_offset, " ");
}
}
out_offset += sprintf(out_buffer + out_offset, " |");
for (int i = 0; i < 16; i++) {
if (i >= size) { break; }
uint8_t c = in_buffer[i];
if (c < 0x20 || c > 0x7F) { c = '.'; }
out_offset += sprintf(out_buffer + out_offset, "%c", c);
}
out_offset += sprintf(out_buffer + out_offset, "|\n");
fwrite(out_buffer, 1, out_offset, stdout);
}
}