diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-13 21:19:28 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-07-13 22:07:56 +0200 |
commit | cefa48203b65f38b3d581a78ac70f9bb77c4b616 (patch) | |
tree | a7174e3c67f2591f556168bf199fec5a0cad9954 /src | |
parent | 8fe668666234f5053db31f7519d96174add22543 (diff) |
kat
Diffstat (limited to 'src')
-rw-r--r-- | src/kat.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/kat.c b/src/kat.c new file mode 100644 index 0000000..7a279d0 --- /dev/null +++ b/src/kat.c @@ -0,0 +1,42 @@ +#include <fcntl.h> +#include <stdio.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> + +int entry_kat(int argc, char **argv) { + if (argc == 2 && strcmp(argv[1], "-h") == 0) { + fprintf(stderr, "Gebruik: kat [BESTAND]...\n" + "\n" + "Schakel BESTAND(en) aaneen naar standaard uitvoer..\n"); + return 0; + } + + for (int i = 1; i < argc; i++) { + const char *fname = argv[i]; + int fd = open(fname, O_RDONLY); + + struct stat sb; + if (fstat(fd, &sb) == -1) { + fprintf(stderr, "Kon bestand niet lezen\n"); + return 0; + } + + char *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + fprintf(stderr, "Kon bestand niet kaarten\n"); + return 0; + } + + for (size_t i = 0; i < sb.st_size; i++) { + putchar(addr[i]); + } + + munmap(addr, sb.st_size); + close(fd); + } + + return 0; +} |