first commit! 🎉
This commit is contained in:
commit
31cfacb2cc
4 changed files with 101 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.DS_Store
|
||||
axd
|
||||
*.o
|
16
Makefile
Normal file
16
Makefile
Normal file
|
@ -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
|
20
README.md
Normal file
20
README.md
Normal file
|
@ -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 <file>
|
||||
```
|
||||
|
||||
## 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!
|
62
axd.c
Normal file
62
axd.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue