From f0700effc02477ec4e2d2dd5b4f52bc4870aebc7 Mon Sep 17 00:00:00 2001 From: Alan Potteiger Date: Wed, 23 Nov 2022 00:52:27 +0100 Subject: [PATCH] `uname`! --- Makefile | 2 +- README | 1 + uname.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 uname.c diff --git a/Makefile b/Makefile index 61ac29f..778cf5f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -BIN=cat logname pwd sleep tty +BIN=cat logname pwd sleep tty uname CFLAGS=-std=c99 all: $(BIN) clean: diff --git a/README b/README index 497ed4f..ff6d855 100644 --- a/README +++ b/README @@ -15,6 +15,7 @@ Utilities - pwd - sleep - tty +- uname License ------- diff --git a/uname.c b/uname.c new file mode 100644 index 0000000..8ba5a73 --- /dev/null +++ b/uname.c @@ -0,0 +1,86 @@ +/* `uname.c` - return system name + Copyright (c) 2022, Alan Potteiger + See `LICENSE` for copyright and license details */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include + +static const char *usage = { + "usage: uname [-amnrsv]\n" +}; + +int +main(int argc, char *argv[]) +{ + struct utsname name; + int m, n, r, s, v; + char ch; + + m = n = r = s = v = 0; + + while ((ch = getopt(argc, argv, "amnrsv")) != -1) { + switch (ch) { + case 'a': + m = n = r = s = v = 1; + break; + case 'm': + m = 1; + break; + case 'n': + n = 1; + break; + case 'r': + r = 1; + break; + case 's': + s = 1; + break; + case 'v': + v = 1; + break; + case '?': + default: + fputs(usage, stderr); + return 1; + } + } + + if (uname(&name) == -1) { + perror("uname"); + return 1; + } + + if (!(m || n || r || s || v)) + s = 1; + + if (m && n && r && s && v) { + printf( + "%s %s %s %s %s\n", + name.sysname, name.nodename, name.release, + name.version, name.machine + ); + + return 0; + } + + /* I think this could be done better or more efficiently. I'm tired + as hell though. I'll revisit this at some point */ + if (s) + printf("%s ", name.sysname); + if (n) + printf("%s ", name.nodename); + if (r) + printf("%s ", name.release); + if (v) + printf("%s ", name.version); + if (m) + printf("%s ", name.machine); + + puts(""); + + return 0; +} + -- 2.11.4.GIT