coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-26 12:36:30
Commit35a6103176443f6447f0186b7de860b1f9ff35a0
Parent9e5b4df5c22c4891fd7a27badc4f539c14d8d3ea

uname: Add POSIX uname

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
M man/basename.1 | 4 ++--
A man/uname.1 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
A src/uname.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5 files changed, 165 insertions, 2 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0551795..217d169 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,5 +25,6 @@ ADD_EXECUTABLE(sync     ${CMAKE_SOURCE_DIR}/src/sync.c)
 ADD_EXECUTABLE(tee      ${CMAKE_SOURCE_DIR}/src/tee.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
 ADD_EXECUTABLE(tty      ${CMAKE_SOURCE_DIR}/src/tty.c)
+ADD_EXECUTABLE(uname    ${CMAKE_SOURCE_DIR}/src/uname.c)
 ADD_EXECUTABLE(unlink   ${CMAKE_SOURCE_DIR}/src/unlink.c)
 ADD_EXECUTABLE(yes      ${CMAKE_SOURCE_DIR}/src/yes.c)
diff --git a/README.md b/README.md
index f1e7811..a29971b 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@ UNIX-like systems.
 | tee              | Duplicate standard input                 | POSIX    |
 | true             | Return true value                        | POSIX    |
 | tty              | Return user's terminal name              | POSIX    |
+| uname            | Return system name                       | POSIX    |
 | unlink           | Remove a file using the unlink function  | POSIX    |
 | yes              | Output a string repeatedly               |          |
 
diff --git a/man/basename.1 b/man/basename.1
index db1d5a4..c21305c 100644
--- a/man/basename.1
+++ b/man/basename.1
@@ -30,8 +30,8 @@ Successful completion.
 >0
 An error occurred.
 .SH STANDARDS
-The \fIbasename\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
-specification.
+The \fIbasename\fR utility is compliant with the IEEE Std 1003.2-1992
+("POSIX.2") specification.
 .SH COPYRIGHT
 .nf
 Copyright (C) 2020, Jakob Wakeling
diff --git a/man/uname.1 b/man/uname.1
new file mode 100644
index 0000000..181226a
--- /dev/null
+++ b/man/uname.1
@@ -0,0 +1,50 @@
+.TH UNAME 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+uname \(em return system name
+.SH SYNOPSYS
+\fBuname\fR [-amnrsv]
+.SH DESCRIPTION
+Write specified system characteristics to standard output.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -a
+Print all system characteristics.
+.TP
+.B -m
+Print the system hardware type.
+.TP
+.B -n
+Print the system network node name.
+.TP
+.B -r
+Print the current release level of the Operating System.
+.TP
+.B -s
+Print the name of the Operating System.
+.TP
+.B -v
+Print the current version of the Operating System.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+Successful completion.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fIuname\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/uname.c b/src/uname.c
new file mode 100644
index 0000000..ecd9e04
--- /dev/null
+++ b/src/uname.c
@@ -0,0 +1,111 @@
+// uname.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX uname
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "optget.h"
+
+#include <sys/utsname.h>
+#include <unistd.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+
+static int mode;
+
+static inline void print(char *string);
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) { (void)(argc);
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "amnrsv"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'a': { mode |= 31; break; }
+	case 'm': { mode |=  1; break; }
+	case 'n': { mode |=  2; break; }
+	case 'r': { mode |=  4; break; }
+	case 's': { mode |=  8; break; }
+	case 'v': { mode |= 16; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	if (mode == 0) { mode |= 8; }
+	struct utsname name; uname(&name);
+	
+	if (mode &  8) { print(name.sysname); }
+	if (mode &  2) { print(name.nodename); }
+	if (mode &  4) { print(name.release); }
+	if (mode & 16) { print(name.version); }
+	if (mode &  1) { print(name.machine); }
+	fputc('\n', stdout);
+	
+	return 0;
+}
+
+static inline void print(char *string) {
+	static bool spc = false;
+	if (spc) { fputc(' ', stdout); } else { spc = true; }
+	fputs(string, stdout); return;
+}
+
+static void help(void) {
+	puts("uname - return system name\n");
+	puts("usage: uname [-amnrsv]\n");
+	puts("options:");
+	puts("  -a         Print all system characteristics");
+	puts("  -m         Print the system hardware type");
+	puts("  -n         Print the system network node name");
+	puts("  -r         Print the current release level of the OS");
+	puts("  -s         Print the name of the OS");
+	puts("  -v         Print the current version of the OS");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils uname, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}