commit 31cfacb2ccaaf9a2126a1197a071e27b36d0693b Author: ari melody Date: Sun Feb 23 17:27:31 2025 +0000 first commit! 🎉 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8031e4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +axd +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..325e142 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CC = clang +CFLAGS = -O2 +TARGET_EXEC = axd + +build: axd.c.o + $(CC) $(CFLAGS) -o $(TARGET_EXEC) axd.c.o + +%.c.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +install: $(TARGET_EXEC) + cp $(TARGET_EXEC) /usr/local/bin/ + chmod +x /usr/local/bin/$(TARGET_EXEC) + +clean: + rm axd axd.c.o diff --git a/README.md b/README.md new file mode 100644 index 0000000..422bbed --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# axd + +Simple recreation of `xxd` from scratch! + +Built purely for learning; I don't expect to pack this with features by any means. + +## Usage + +```sh +axd +``` + +## Build + +**Requirements:** +- a computer +- `clang` + +`make build` will build the binary, and `sudo make install` will install it to `/usr/local/bin` with execute permissions. +If you're on Windows, I'm so sorry! diff --git a/axd.c b/axd.c new file mode 100644 index 0000000..3449eaf --- /dev/null +++ b/axd.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \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; +}