summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-21 13:03:03 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-21 13:03:03 +0200
commitb069a252929a4a634650ce04d4a498266563af08 (patch)
treef4b4f2593f862899a4b458e5c701d41e58c07a7f
parentd1d02f18d36579566d1ba18edb27079e1d3980d0 (diff)
unaam
-rw-r--r--src/unaam.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/unaam.c b/src/unaam.c
new file mode 100644
index 0000000..7a4a853
--- /dev/null
+++ b/src/unaam.c
@@ -0,0 +1,84 @@
+#include <ctype.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/utsname.h>
+
+#include "io/read_file.h"
+#include "util/option.h"
+
+static const char *usage_string =
+ "Gebruik: %s [OPTIES]...\n"
+ "\n"
+ "Toon systeem informatie\n"
+ "\n"
+ " -a Toon alle informatie\n"
+ " -s Toon kernnaam\n"
+ " -n Toon netwerk knooppunt gastheernaam\n"
+ " -r Toon kernuitgave\n"
+ " -v Toon kernversie\n"
+ " -m Toon machine harde-goederen-naam\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n";
+
+struct options {
+ bool print_all;
+
+ bool print_sysname;
+ bool print_nodename;
+ bool print_release;
+ bool print_version;
+ bool print_machine;
+};
+
+static void parse_options(int argc, char **argv, struct options *opts) {
+ const struct option_spec spec[] = {
+ {'a', OPTION_SETBOOL(&opts->print_all)},
+
+ {'s', OPTION_SETBOOL(&opts->print_sysname)},
+ {'n', OPTION_SETBOOL(&opts->print_nodename)},
+ {'r', OPTION_SETBOOL(&opts->print_release)},
+ {'v', OPTION_SETBOOL(&opts->print_version)},
+ {'m', OPTION_SETBOOL(&opts->print_machine)},
+
+ {'h', OPTION_HELPUSAGE(usage_string)},
+ {'V', OPTION_VERSION()},
+ OPTION_SPEC_END
+ };
+
+ option_parse(argc, argv, spec);
+}
+
+int entry_unaam(int argc, char **argv) {
+ struct options opts = {0};
+ parse_options(argc, argv, &opts);
+
+ if (opts.print_all == false &&
+ opts.print_sysname == false &&
+ opts.print_nodename == false &&
+ opts.print_release == false &&
+ opts.print_version == false &&
+ opts.print_machine == false) {
+ opts.print_sysname = true;
+ }
+
+ struct utsname name;
+ uname(&name); // can't fail
+
+ bool first = true;
+
+#define PRINT(typ) \
+ if (opts.print_all || opts.print_ ## typ) { \
+ printf("%s%s", first ? "" : " ", name.typ); \
+ first = false; \
+ }
+
+ PRINT(sysname);
+ PRINT(nodename);
+ PRINT(release);
+ PRINT(version);
+ PRINT(machine);
+ printf("\n");
+
+ return 0;
+}