diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-08-23 16:22:13 +0200 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-08-23 16:22:13 +0200 |
| commit | ea20bd9b5bcff9db1d86d83188e1e899799f324b (patch) | |
| tree | 615e2f00e385e93061524c9647b64010ba7df3dd /archived/less_old_sim8086/code/print_binary.c | |
checkpoint
Diffstat (limited to 'archived/less_old_sim8086/code/print_binary.c')
| -rw-r--r-- | archived/less_old_sim8086/code/print_binary.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/archived/less_old_sim8086/code/print_binary.c b/archived/less_old_sim8086/code/print_binary.c new file mode 100644 index 0000000..1382cae --- /dev/null +++ b/archived/less_old_sim8086/code/print_binary.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <signal.h> + +#define Assert(Expr) \ + if (!(Expr)) \ + { \ + raise(SIGTRAP); \ + } + + +int +main(int ArgC, char *Args[]) +{ + if (ArgC < 2) + { + fprintf(stderr, "Missing argument.\n"); + fprintf(stderr, "Usage: %s <filename>\n", Args[0]); + } + else + { + struct stat StatBuffer = {0}; + char *Filename = 0; + int FD = -1; + int Err = -1; + size_t Filesize = 0; + char *Buffer = 0; + + Filename = Args[1]; + FD = open(Filename, O_RDONLY); + Assert(FD != -1); + Err = stat(Filename, &StatBuffer); + Assert(Err != -1); + Filesize = StatBuffer.st_size; + + if (Filesize) + { + Buffer = mmap(0, Filesize, PROT_READ, MAP_SHARED, FD, 0); + Assert(Buffer); + + for (size_t At = 0; At < Filesize; At++) + { + unsigned char Byte = Buffer[At]; + int Count = 8; + while (Count--) + { + printf("%d", Byte >> 7); + Byte <<= 1; + } + printf(" "); + } + printf("\n"); + } + else + { + fprintf(stderr, "Empty file.\n"); + } + } + + return 0; +} |
