summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <t.j.smeding@uu.nl>2024-07-13 18:33:37 +0200
committerTom Smeding <t.j.smeding@uu.nl>2024-07-13 18:33:37 +0200
commita08a2a2dbb3d150bdb2d01bd54c6ad32f02fe5fa (patch)
treeaaac1ffb99935d3b65eed111c9d7f158d28ac176
parenteba8b972d2bc85b8fe6acff90505116da8e29abd (diff)
waar, poging 2
-rw-r--r--.gitignore1
-rw-r--r--Maakbestand7
-rw-r--r--main.c32
-rw-r--r--src/waar.c12
4 files changed, 49 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index 6ab0ef1..b348648 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
drukkedoos
obj/
.ccls-cache/
+compile_commands.json
diff --git a/Maakbestand b/Maakbestand
index e760b65..98e0798 100644
--- a/Maakbestand
+++ b/Maakbestand
@@ -1,5 +1,5 @@
CC := gcc
-CFLAGS := -Wall -Wextra -std=c11 -O2
+CFLAGS := -Wall -Wextra -std=c2x -O2
LDFLAGS :=
OBJDIR := obj
@@ -9,6 +9,8 @@ SOURCES := $(shell find src -type f -name '*.c')
HEADERS := $(shell find src -type f -name '*.h')
OBJECTS := $(patsubst src/%.c,obj/%.o,$(SOURCES))
+PROGNAMES_X := $(patsubst src/%.c,X(%),$(wildcard src/*.c))
+
.PHONY: all clean
all: $(TARGET)
@@ -17,7 +19,8 @@ clean:
rm -rf $(OBJDIR) $(TARGET)
$(TARGET): main.c $(OBJECTS)
- $(CC) -o $@ $^ $(LDFLAGS)
+ $(CC) -o $@ $^ $(LDFLAGS) -DPROGNAMES_XLIST="$(PROGNAMES_X)"
$(OBJECTS): obj/%.o: src/%.c $(HEADERS)
+ @mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $<
diff --git a/main.c b/main.c
index b5e3883..8cde9b8 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,35 @@
#include <stdio.h>
+#include <string.h>
+
+#define X(name) int entry_ ## name (int argc, char **argv);
+PROGNAMES_XLIST
+#undef X
+
+static char* basename(char *path) {
+ size_t len = strlen(path);
+ for (size_t i = len - 1; i != 0; i--) {
+ if (path[i] == '/') return path + (i + 1);
+ }
+ return path;
+}
int main(int argc, char **argv) {
- fprintf(stderr, "Drukkedoos moet nog geïmplementeerd worden\n");
+ const char *prognamearg = basename(argv[0]);
+
+ if (strcmp(prognamearg, "drukkedoos") == 0) {
+ argv++;
+ argc--;
+ if (argc == 0) {
+ fprintf(stderr, "Gebruik: drukkedoos <commando>\n");
+ return 1;
+ }
+ prognamearg = basename(argv[0]);
+ }
+
+#define X(name) if (strcmp(argv[0], #name) == 0) return entry_ ## name (argc, argv);
+PROGNAMES_XLIST
+#undef X
+
+ fprintf(stderr, "Onbekend hulpmiddel: '%s'\n", prognamearg);
+ return 1;
}
diff --git a/src/waar.c b/src/waar.c
new file mode 100644
index 0000000..6fa570c
--- /dev/null
+++ b/src/waar.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <string.h>
+
+int entry_waar(int argc, char **argv) {
+ if (argc == 2 && strcmp(argv[1], "-h") == 0) {
+ fprintf(stderr, "Gebruik: waar\n"
+ "\n"
+ "Geeft waarheid terug.\n");
+ return 0;
+ }
+ return 0;
+}