commit 86b483dfb27ad3bb5e32a55e79e92424d2e9e57b
parent bed1b1e4ce924930965f5369a5b45d1c93a44a3d
Author: phoebos <ben@bvnf.space>
Date: Thu, 10 Mar 2022 03:25:27 +0000
uname: add
Diffstat:
4 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -15,6 +15,7 @@ BINS = \
tee \
true \
tty \
+ uname \
wc \
CC = cc
diff --git a/PROGRESS b/PROGRESS
@@ -44,7 +44,7 @@
[ ] tr
[x] true
[x] tty
-[ ] uname
+[x] uname [-amnrsv]
[ ] uudecode
[ ] uuencode
[ ] wc [-lwc]
diff --git a/TODO.posix b/TODO.posix
@@ -110,7 +110,7 @@
[x] tty
[ ] ulimit
[ ] umask
-[ ] uname
+[x] uname
[ ] uncompress
[ ] unexpand
[ ] uniq
diff --git a/uname.c b/uname.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <sys/utsname.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv) {
+ int c;
+ int OPT_m, OPT_n, OPT_r, OPT_s, OPT_v;
+ OPT_m = OPT_n = OPT_r = OPT_s = OPT_v = 0;
+
+ if (argc == 1)
+ OPT_s = 1;
+
+ while ((c = getopt(argc, argv, ":amnrsv")) != -1) {
+ switch (c) {
+ case 'a':
+ OPT_m = OPT_n = OPT_r = OPT_s = OPT_v = 1;
+ break;
+ case 'm':
+ OPT_m = 1;
+ break;
+ case 'n':
+ OPT_n = 1;
+ break;
+ case 'r':
+ OPT_r = 1;
+ break;
+ case 's':
+ OPT_s = 1;
+ break;
+ case 'v':
+ OPT_v = 1;
+ break;
+ case '?':
+ fprintf(stderr, "usage: %s [-amnrsv]\n", *argv);
+ return 1;
+ }
+ }
+
+ struct utsname ut;
+ if (uname(&ut) == -1) {
+ perror("uname");
+ return 1;
+ }
+
+ /* TODO: don't print unnecessary trailing spaces */
+ if (OPT_s) printf("%s ", ut.sysname);
+ if (OPT_n) printf("%s ", ut.nodename);
+ if (OPT_r) printf("%s ", ut.release);
+ if (OPT_v) printf("%s ", ut.version);
+ if (OPT_m) printf("%s ", ut.machine);
+ printf("\n");
+
+ return 0;
+}