summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-22 01:17:23 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-22 12:22:09 +0200
commit0ec55999744b3092e68ea0f5b390dbed653fab30 (patch)
tree32b1f4d01b6e64ec446bd2d57ee489bf4613c3f0
parentff6598dbdfeb7870b84ebdb81b0cc18c3c25a475 (diff)
betere foutmeldingen als een mapje geprobeerd geprojecteerd te worden
-rw-r--r--src/hoofd.c9
-rw-r--r--src/kat.c9
-rw-r--r--src/toilet.c13
-rw-r--r--src/util/map.c5
-rw-r--r--src/util/map.h3
5 files changed, 29 insertions, 10 deletions
diff --git a/src/hoofd.c b/src/hoofd.c
index 64c2bfd..62d1151 100644
--- a/src/hoofd.c
+++ b/src/hoofd.c
@@ -58,8 +58,12 @@ int entry_hoofd(int argc, char **argv) {
char **args = parse_options(argc, argv, &n, &c);
while (*args != NULL) {
- struct map *map = open_map(*args);
- if (map == NULL) {
+ bool isdir;
+ struct map *map = open_map(*args, &isdir);
+ if (isdir) {
+ fprintf(stderr, "hoofd: %s: is een mapje\n", *args);
+ goto next;
+ } else if (map == NULL) {
fprintf(stderr, "hoofd: fout bij lezen bestand");
return 1;
}
@@ -73,6 +77,7 @@ int entry_hoofd(int argc, char **argv) {
}
fwrite(map->addr, 1, i, stdout);
+next:
args++;
}
diff --git a/src/kat.c b/src/kat.c
index 8a0fe59..5549b24 100644
--- a/src/kat.c
+++ b/src/kat.c
@@ -39,8 +39,13 @@ static char** parse_options(int argc, char **argv) {
}
static void process(const char *fname) {
- struct map *map = open_map(fname);
- if (map == NULL) {
+ bool isdir;
+
+ struct map *map = open_map(fname, &isdir);
+ if (isdir) {
+ fprintf(stderr, "kat: %s: is een mapje\n", fname);
+ return;
+ } else if (map == NULL) {
exit(1);
}
diff --git a/src/toilet.c b/src/toilet.c
index 0f4d66f..ce42aca 100644
--- a/src/toilet.c
+++ b/src/toilet.c
@@ -116,10 +116,14 @@ int entry_toilet(int argc, char **argv) {
}
while (*args != NULL) {
- struct map *map = open_map(*args);
- if (map == NULL) {
- fprintf(stderr, "toilet: fout bij lezen bestand");
+ bool isdir;
+ struct map *map = open_map(*args, &isdir);
+ if (map == NULL && !isdir) {
+ fprintf(stderr, "toilet: fout bij lezen bestand '%s'\n", *args);
return 1;
+ } else if (isdir) {
+ fprintf(stderr, "toilet: %s: is een mapje\n", *args);
+ goto next;
}
for (enum MODE mode = 1; mode <= M_BYTES; mode <<= 1) {
@@ -128,10 +132,11 @@ int entry_toilet(int argc, char **argv) {
printf("%li ", count);
}
}
-
printf("%s\n", *args);
close_map(map);
+
+next:
args++;
}
diff --git a/src/util/map.c b/src/util/map.c
index 27e1c04..6a1faf1 100644
--- a/src/util/map.c
+++ b/src/util/map.c
@@ -9,7 +9,9 @@
#include "map.h"
-struct map *open_map(const char *fname) {
+struct map *open_map(const char *fname, bool *isdir) {
+ if (isdir != NULL) *isdir = false;
+
int fd = open(fname, O_RDONLY);
struct stat sb;
@@ -21,6 +23,7 @@ struct map *open_map(const char *fname) {
char *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
//fprintf(stderr, "Kon bestand niet projecteren in geheugen\n");
+ if (isdir != NULL) *isdir = S_ISDIR(sb.st_mode);
return NULL;
}
diff --git a/src/util/map.h b/src/util/map.h
index 67beb9c..14a48e1 100644
--- a/src/util/map.h
+++ b/src/util/map.h
@@ -1,3 +1,4 @@
+#include <stdbool.h>
#include <sys/stat.h>
struct map {
@@ -7,5 +8,5 @@ struct map {
int fd;
};
-struct map *open_map(const char *fname);
+struct map *open_map(const char *fname, bool *isdir);
void close_map(struct map *m);