summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-14 22:17:39 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-14 22:17:41 +0200
commit376a83ff4558f0ec4d04a4166ac38fba221c2c1a (patch)
treefa14d132cb109b24c1dcf9ee36d0f05da90f966c
parentf51dce7fe7f2bbe8d3ac412ef3c17385b4571162 (diff)
aardig
-rw-r--r--src/aardig.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/aardig.c b/src/aardig.c
new file mode 100644
index 0000000..a56f52d
--- /dev/null
+++ b/src/aardig.c
@@ -0,0 +1,69 @@
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <getopt.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "util/versie.h"
+#include "util/error.h"
+#include "util/debug.h"
+#include "util/map.h"
+
+static void usage(FILE *f) {
+ fprintf(f,
+ "Gebruik: aardig [-n AARDIGHEID] COMMANDO...\n"
+ "\n"
+ "Voer COMMANDO uit met een bepaalde niveau van aardigheid.\n"
+ "\n"
+ " -n AARDIGHEID Niveau van aardigheid (standaard 10)\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n");
+}
+
+// Returns pointer to argument array containing the command arguments
+static char** parse_options(int argc, char **argv, int *niceness) {
+ int opt;
+ while ((opt = getopt(argc, argv, "n:hV")) != -1) {
+ switch (opt) {
+ case 'n':
+ *niceness = atoi(optarg);
+ break;
+
+ case 'h':
+ usage(stdout);
+ exit(0);
+
+ case 'V':
+ drukkedoos_print_versie(stdout, "aardig");
+ exit(0);
+
+ case '?':
+ usage(stderr);
+ exit(1);
+ }
+ }
+
+ return argv + optind;
+}
+
+int entry_aardig(int argc, char **argv) {
+ int niceness = 10;
+ char **args = parse_options(argc, argv, &niceness);
+
+ if (*args == NULL) {
+ fprintf(stderr, "aardig: geen commando opgegeven\n");
+ return 1;
+ }
+
+ if (nice(niceness) == -1 && errno) {
+ fprintf(stderr, "aardig: kon niet aardig worden.\n");
+ return 1;
+ }
+
+ execvp(args[0], args);
+
+ return 0;
+}