commit: 2a893bb9cf730febb3b3f22ff95d7525606386e3
parent cc07e9b70a5cd570c5cee1213f4f8f437ea904cf
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 21 Apr 2024 04:23:02 +0200
cmd/uname: new
Diffstat:
8 files changed, 178 insertions(+), 3 deletions(-)
diff --git a/cmd/uname.1 b/cmd/uname.1
@@ -0,0 +1,46 @@
+.\" utils-std: Collection of commonly available Unix tools
+.\" Copyright 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+.\" SPDX-License-Identifier: MPL-2.0
+.Dd 2024-04-21
+.Dt UNAME 1
+.Os
+.Sh NAME
+.Nm uname
+.Nd return system name and information
+.Sh SYNOPSIS
+.Nm
+.Op Fl amnrsv
+.Sh DESCRIPTION
+.Nm
+prints the operating system name, and optionally more information about the system to standard output, separated by spaces.
+.Sh OPTIONS
+.Bl -tag -width __
+.It Fl a
+Enable all options, equivalent to passing
+.Fl mnrsv .
+.It Fl m
+Write machine name, also known as the processor architecture.
+.It Fl n
+Write network node name, also known as hostname.
+.It Fl r
+Write running operating system release.
+.It Fl s
+Write running operating system name.
+.It Fl v
+Write version level of running operating system release.
+.El
+.Pp
+If no options are specified,
+.Nm
+prints the operating system as if
+.Fl s
+was specified.
+.Sh EXIT STATUS
+.Ex -std
+.Sh STANDARDS
+.Nm
+should be compliant with the
+.St -p1003.1-2008
+specification.
+.Sh AUTHORS
+.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/uname.c b/cmd/uname.c
@@ -0,0 +1,102 @@
+// utils-std: Collection of commonly available Unix tools
+// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+#include <errno.h>
+#include <string.h> // strerror
+#include <stdio.h> // printf
+#include <stdlib.h> // exit, abort
+#include <sys/utsname.h> // uname
+#include <unistd.h> // getopt
+
+#include "../lib/bitmasks.h"
+
+enum uname_names {
+ CMD_UNAME_SYSNAME = 1 << 1,
+ CMD_UNAME_NODENAME = 1 << 2,
+ CMD_UNAME_RELEASE = 1 << 3,
+ CMD_UNAME_VERSION = 1 << 4,
+ CMD_UNAME_MACHINE = 1 << 5,
+ CMD_UNAME_ALL = CMD_UNAME_SYSNAME | CMD_UNAME_NODENAME | CMD_UNAME_RELEASE | CMD_UNAME_VERSION | CMD_UNAME_MACHINE,
+};
+
+const char *fmt = "%s";
+enum uname_names names = 0;
+
+static void
+maybe_print_name(enum uname_names key, char *value)
+{
+ if(FIELD_MATCH(names, key)) {
+ if(printf(fmt, value) < 0)
+ {
+ fprintf(stderr, "uname: Write Error: %s", strerror(errno));
+ exit(1);
+ }
+ fmt = " %s";
+ }
+}
+
+static void
+usage()
+{
+ fprintf(stderr, "Usage: uname [-amnrsv]\n");
+}
+
+int
+main(int argc, char *argv[])
+{
+ int c = -1;
+ while((c = getopt(argc, argv, ":amnrsv")) != -1)
+ {
+ switch(c)
+ {
+ case 'a':
+ names = CMD_UNAME_ALL;
+ break;
+ case 'm':
+ names |= CMD_UNAME_MACHINE;
+ break;
+ case 'n':
+ names |= CMD_UNAME_NODENAME;
+ break;
+ case 'r':
+ names |= CMD_UNAME_RELEASE;
+ break;
+ case 's':
+ names |= CMD_UNAME_SYSNAME;
+ break;
+ case 'v':
+ names |= CMD_UNAME_VERSION;
+ break;
+ case ':':
+ fprintf(stderr, "uname: Error: Missing operand for option: '-%c'\n", optopt);
+ usage();
+ return 1;
+ case '?':
+ fprintf(stderr, "uname: Error: Unrecognised option: '-%c'\n", optopt);
+ usage();
+ return 1;
+ default:
+ abort();
+ }
+ }
+ if(names == 0) names |= CMD_UNAME_SYSNAME;
+
+ struct utsname name;
+ if(uname(&name) != 0)
+ {
+ fprintf(stderr, "uname: Failed getting current system names via uname(): %s\n", strerror(errno));
+ return 1;
+ }
+
+ maybe_print_name(CMD_UNAME_SYSNAME, name.sysname);
+ maybe_print_name(CMD_UNAME_NODENAME, name.nodename);
+ maybe_print_name(CMD_UNAME_RELEASE, name.release);
+ maybe_print_name(CMD_UNAME_VERSION, name.version);
+ maybe_print_name(CMD_UNAME_MACHINE, name.machine);
+
+ printf("\n");
+
+ return 0;
+}
diff --git a/coreutils.txt b/coreutils.txt
@@ -94,7 +94,7 @@ true: Done
truncate: Todo
tsort: ?
tty: Done
-uname: Use sname
+uname: Done
unexpand: Use sed
uniq: Todo
unlink: Done
diff --git a/lsb_commands.txt b/lsb_commands.txt
@@ -130,7 +130,7 @@ true: Done
tsort: ?
tty: Done
umount: out of scope
-uname: Use sname
+uname: Done
unexpand: Use sed
uniq: Todo
useradd: out of scope
diff --git a/makeless.sh b/makeless.sh
@@ -43,6 +43,7 @@ $CC -std=c99 $CFLAGS -o cmd/time cmd/time.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/touch cmd/touch.c lib/iso_parse.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/true cmd/true.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/tty cmd/tty.c $LDFLAGS $LDSTATIC
+$CC -std=c99 $CFLAGS -o cmd/uname cmd/uname.c $LDFLAGS $LDSTATIC
$CC -std=c99 $CFLAGS -o cmd/unlink cmd/unlink.c $LDFLAGS $LDSTATIC
$M4 cmd/date.1.in > build/cmd/date.1
$M4 cmd/touch.1.in > build/cmd/touch.1
diff --git a/posix_utilities.txt b/posix_utilities.txt
@@ -141,7 +141,7 @@ type: no, sh built-in
ulimit: no, sh built-in
umask: no, sh built-in
unalias: no, sh built-in
-uname
+uname: done
uncompress: no
unexpand
unget: no, SCCS
diff --git a/test-cmd/Kyuafile b/test-cmd/Kyuafile
@@ -7,6 +7,7 @@ test_suite("utils-std commands")
basedir = fs.dirname(fs.dirname(current_kyuafile()))
-- 9,$|LC_ALL=C.UTF-8 sort
+atf_test_program{name="uname", required_files=basedir.."/cmd/uname", timeout=1}
atf_test_program{name="base64", required_files=basedir.."/cmd/base64", timeout=1}
atf_test_program{name="basename", required_files=basedir.."/cmd/basename", timeout=1}
atf_test_program{name="cat", required_files=basedir.."/cmd/cat", timeout=1}
diff --git a/test-cmd/uname b/test-cmd/uname
@@ -0,0 +1,25 @@
+#!/usr/bin/env atf-sh
+# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: MPL-2.0
+
+atf_test_case noargs
+noargs_body() {
+ atf_check -o "inline:$(uname -s)\n" ../cmd/uname
+}
+
+atf_test_case all
+all_body() {
+ atf_check -o "inline:$(uname -s) $(uname -n) $(uname -r) $(uname -v) $(uname -m)\n" ../cmd/uname -a
+}
+
+atf_test_case sysname
+sysname_body() {
+ atf_check -o "inline:$(uname -s)\n" ../cmd/uname -s
+}
+
+atf_init_test_cases() {
+ cd "$(atf_get_srcdir)" || exit 1
+ atf_add_test_case noargs
+ atf_add_test_case all
+ atf_add_test_case sysname
+}