axd/axd.c
2025-02-23 17:27:31 +00:00

62 lines
1.4 KiB
C

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <file>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
return 1;
}
fseek(file, 0, SEEK_END);
size_t filesize = ftell(file);
fseek(file, 0, 0);
char buffer[filesize + 1];
fread(buffer, 1, filesize, file);
buffer[filesize] = '\0';
static int width = 0x10;
size_t offset = 0;
while (offset < filesize) {
printf("%08lx: ", offset);
// print hex chars
for (int c = 0; c < 0x10; c++) {
if (offset + c >= filesize)
printf(" ");
else {
if ((buffer[offset + c] & 0xff) < 0x10)
printf("0");
printf("%x", buffer[offset + c] & 0xff);
}
if (c % 2 != 0)
printf(" ");
}
printf(" ");
// print ASCII chars
for (int o = 0; o < 0x10 && offset + o < filesize; o++) {
char c = buffer[offset + o];
if (c > 31)
printf("%c", c);
else
printf(".");
}
printf("\n");
offset += 0x10;
}
return 0;
}