summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-23 00:02:27 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-23 00:02:27 +0200
commit93394b388f7cbec6640e361aa95c3b30ba2a90fc (patch)
tree40a30f58d39f01f3eaf9df9446b14edd9b09b534
parentbd22d0e47d0fb203286b088f048cf1aff1fa93a1 (diff)
global progname variable
-rw-r--r--Maakbestand4
-rw-r--r--global.h3
-rw-r--r--main.c14
-rw-r--r--src/aardig.c2
-rw-r--r--src/hoofd.c2
-rw-r--r--src/io/lines.c4
-rw-r--r--src/io/read_file.c5
-rw-r--r--src/ja.c2
-rw-r--r--src/kat.c2
-rw-r--r--src/omd.c2
-rw-r--r--src/rij.c2
-rw-r--r--src/spons.c2
-rw-r--r--src/tak.c2
-rw-r--r--src/toilet.c2
-rw-r--r--src/util/error.c3
-rw-r--r--src/util/error.h2
-rw-r--r--src/util/option.c2
-rw-r--r--src/util/versie.c3
-rw-r--r--src/util/versie.h2
-rw-r--r--src/wiebenik.c2
20 files changed, 36 insertions, 26 deletions
diff --git a/Maakbestand b/Maakbestand
index 70df156..c70332d 100644
--- a/Maakbestand
+++ b/Maakbestand
@@ -5,14 +5,14 @@ PREFIX ?= /usr
CC := gcc
CFLAGS := -Wall -Wextra -std=c2x -O2
CFLAGS += -D_GNU_SOURCE -DDRUKKEDOOS_VERSIE=\"$(VERSION)\"
-CFLAGS += -Isrc
+CFLAGS += -Isrc -I.
LDFLAGS :=
OBJDIR := obj
TARGET := drukkedoos
SOURCES := $(shell find src -type f -name '*.c')
-HEADERS := $(shell find src -type f -name '*.h')
+HEADERS := $(shell find . -type f -name '*.h')
OBJECTS := $(patsubst src/%.c,obj/%.o,$(SOURCES))
PROGNAMES := $(patsubst src/%.c,%,$(wildcard src/*.c))
diff --git a/global.h b/global.h
new file mode 100644
index 0000000..5bf8bac
--- /dev/null
+++ b/global.h
@@ -0,0 +1,3 @@
+#pragma once
+
+extern char *progname;
diff --git a/main.c b/main.c
index 898c821..02e7dfa 100644
--- a/main.c
+++ b/main.c
@@ -2,6 +2,10 @@
#include <string.h>
#include <unistd.h>
+#include "global.h"
+
+char *progname = NULL;
+
#define X(name) int entry_ ## name (int argc, char **argv);
PROGNAMES_XLIST
#undef X
@@ -18,9 +22,9 @@ int main(int argc, char **argv) {
// Ensure getopt(3) does not print an (English) error
opterr = 0;
- const char *prognamearg = basename(argv[0]);
+ progname = basename(argv[0]);
- if (strcmp(prognamearg, "drukkedoos") == 0) {
+ if (strcmp(progname, "drukkedoos") == 0) {
argv++;
argc--;
if (argc == 0) {
@@ -31,13 +35,13 @@ int main(int argc, char **argv) {
#undef X
return 1;
}
- prognamearg = basename(argv[0]);
+ progname = basename(argv[0]);
}
-#define X(name) if (strcmp(prognamearg, #name) == 0) return entry_ ## name (argc, argv);
+#define X(name) if (strcmp(progname, #name) == 0) return entry_ ## name (argc, argv);
PROGNAMES_XLIST
#undef X
- fprintf(stderr, "Onbekend hulpmiddel: '%s'\n", prognamearg);
+ fprintf(stderr, "Onbekend hulpmiddel: '%s'\n", progname);
return 1;
}
diff --git a/src/aardig.c b/src/aardig.c
index 0388782..2203ac1 100644
--- a/src/aardig.c
+++ b/src/aardig.c
@@ -37,7 +37,7 @@ static char** parse_options(int argc, char **argv, int *niceness) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "aardig");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/hoofd.c b/src/hoofd.c
index 9e428e9..f043f2b 100644
--- a/src/hoofd.c
+++ b/src/hoofd.c
@@ -40,7 +40,7 @@ static char** parse_options(int argc, char **argv, int *n, int *c) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "hoofd");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/io/lines.c b/src/io/lines.c
index fd9c3a5..e5de6df 100644
--- a/src/io/lines.c
+++ b/src/io/lines.c
@@ -12,7 +12,7 @@ bool file_lines_open(const char *progname, const char *fname, FILE *f, struct fi
dst->cap = 4096;
dst->buffer = malloc(dst->cap);
if (!dst->buffer) {
- print_error_nomem(progname);
+ print_error_nomem();
return false;
}
@@ -56,7 +56,7 @@ bool file_lines_read(struct file_lines *fl, struct string_view *dst) {
}
DEBUG("realloc to cap %zu\n", fl->cap);
fl->buffer = realloc(fl->buffer, fl->cap);
- if (!fl->buffer) print_error_nomem(fl->progname);
+ if (!fl->buffer) print_error_nomem();
}
size_t nr = fread(fl->buffer + fl->cursor, 1, fl->cap - fl->cursor, fl->f);
diff --git a/src/io/read_file.c b/src/io/read_file.c
index 2ff00a5..99bea2a 100644
--- a/src/io/read_file.c
+++ b/src/io/read_file.c
@@ -5,10 +5,11 @@
#include <unistd.h>
#include "read_file.h"
+#include "global.h"
#define CHECK_OOM(ptr) \
if (ptr == NULL) { \
- fprintf(stderr, "geheugen is op\n"); \
+ fprintf(stderr, "%s: geheugen is op\n", progname); \
/* I think I should free the original memory here, but whatever */ \
return NULL; \
}
@@ -35,7 +36,7 @@ struct filebuf *stream_to_filebuf(FILE *restrict stream, int openOptions) {
sz += n;
if (n != amount && !feof(stream)) {
- fprintf(stderr, "fout tijdens lezen van standaard invoer.\n");
+ fprintf(stderr, "%s: fout tijdens lezen van standaard invoer.\n", progname);
free(buf);
return NULL;
}
diff --git a/src/ja.c b/src/ja.c
index 3d6135d..bfb69ed 100644
--- a/src/ja.c
+++ b/src/ja.c
@@ -26,7 +26,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "ja");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/kat.c b/src/kat.c
index 0a17ddc..6ca8d28 100644
--- a/src/kat.c
+++ b/src/kat.c
@@ -26,7 +26,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "kat");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/omd.c b/src/omd.c
index 71b8dee..2658687 100644
--- a/src/omd.c
+++ b/src/omd.c
@@ -25,7 +25,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "omd");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/rij.c b/src/rij.c
index 1ea1b35..1803cdd 100644
--- a/src/rij.c
+++ b/src/rij.c
@@ -26,7 +26,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "rij");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/spons.c b/src/spons.c
index 51bb109..397d9e5 100644
--- a/src/spons.c
+++ b/src/spons.c
@@ -32,7 +32,7 @@ static char** parse_options(int argc, char **argv, bool *a) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "spons");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/tak.c b/src/tak.c
index 245a85c..8ae6ca5 100644
--- a/src/tak.c
+++ b/src/tak.c
@@ -27,7 +27,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "tak");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/toilet.c b/src/toilet.c
index aa4fbfa..10c7510 100644
--- a/src/toilet.c
+++ b/src/toilet.c
@@ -54,7 +54,7 @@ static char** parse_options(int argc, char **argv, int *modeMap) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "toilet");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':
diff --git a/src/util/error.c b/src/util/error.c
index e7f1f02..6c02810 100644
--- a/src/util/error.c
+++ b/src/util/error.c
@@ -2,8 +2,9 @@
#include <stdlib.h>
#include "error.h"
+#include "global.h"
-void print_error_nomem(const char *progname) {
+void print_error_nomem() {
fprintf(stderr, "%s: Kon geen geheugen reserveren!\n", progname);
exit(1);
}
diff --git a/src/util/error.h b/src/util/error.h
index 99a9c77..27f9ea8 100644
--- a/src/util/error.h
+++ b/src/util/error.h
@@ -2,4 +2,4 @@
__attribute__((noreturn))
-void print_error_nomem(const char *progname);
+void print_error_nomem();
diff --git a/src/util/option.c b/src/util/option.c
index ef7a398..5efcbd7 100644
--- a/src/util/option.c
+++ b/src/util/option.c
@@ -65,7 +65,7 @@ char** option_parse(int argc, char **argv, const struct option_spec *speclist) {
exit(0);
case OPTSPECKIND_VERSION:
- drukkedoos_print_versie(stdout, argv[0]);
+ drukkedoos_print_versie(stdout);
exit(0);
case OPTSPECKIND_END: abort(); // unreachable
diff --git a/src/util/versie.c b/src/util/versie.c
index 880823a..cdb7341 100644
--- a/src/util/versie.c
+++ b/src/util/versie.c
@@ -1,6 +1,7 @@
#include "versie.h"
+#include "global.h"
-void drukkedoos_print_versie(FILE *f, const char *progname) {
+void drukkedoos_print_versie(FILE *f) {
fprintf(f,
"%s, deel van drukkedoos versie %s\n",
progname, DRUKKEDOOS_VERSIE);
diff --git a/src/util/versie.h b/src/util/versie.h
index 0f1dafb..f24b359 100644
--- a/src/util/versie.h
+++ b/src/util/versie.h
@@ -3,4 +3,4 @@
#include <stdio.h>
-void drukkedoos_print_versie(FILE *f, const char *progname);
+void drukkedoos_print_versie(FILE *f);
diff --git a/src/wiebenik.c b/src/wiebenik.c
index 5d71ef4..8515701 100644
--- a/src/wiebenik.c
+++ b/src/wiebenik.c
@@ -27,7 +27,7 @@ static char** parse_options(int argc, char **argv) {
exit(0);
case 'V':
- drukkedoos_print_versie(stdout, "wiebenik");
+ drukkedoos_print_versie(stdout);
exit(0);
case '?':