From cfb4baab4b3c81726b4982c578a5572c4b7715de Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 24 Apr 2023 21:02:04 +0200 Subject: [PATCH] apps: Add uname --- apps/CMakeLists.txt | 1 + apps/uname.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ libc/CMakeLists.txt | 1 + libc/src/sys/utsname.cpp | 13 +++++++++ 4 files changed, 74 insertions(+) create mode 100644 apps/uname.cpp create mode 100644 libc/src/sys/utsname.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 6fc6ebda..05b1989d 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -24,3 +24,4 @@ luna_app(chmod.cpp chmod OFF) luna_app(mkdir.cpp mkdir OFF) luna_app(rm.cpp rm OFF) luna_app(stat.cpp stat OFF) +luna_app(uname.cpp uname OFF) diff --git a/apps/uname.cpp b/apps/uname.cpp new file mode 100644 index 00000000..f73a92b3 --- /dev/null +++ b/apps/uname.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +#define OPERATING_SYSTEM "Luna" + +void print_sequenced(const char* string) +{ + static bool first { true }; + if (!first) { putchar(' '); } + first = false; + fputs(string, stdout); +} + +Result luna_main(int argc, char** argv) +{ + bool all { false }; + bool kernel_name { false }; + bool node_name { false }; + bool kernel_release { false }; + bool kernel_version { false }; + bool machine { false }; + bool operating_system { false }; + + os::ArgumentParser parser; + parser.add_description("Print system information. If given no options, defaults to -s."_sv); + parser.add_switch_argument(all, 'a', "all"_sv, "print all information"_sv); + parser.add_switch_argument(kernel_name, 's', "kernel-name"_sv, "print the kernel name"_sv); + parser.add_switch_argument(node_name, 'n', "nodename"_sv, "print the network hostname"_sv); + parser.add_switch_argument(kernel_release, 'r', "kernel-release"_sv, "print the kernel release"_sv); + parser.add_switch_argument(kernel_version, 'v', "kernel-version"_sv, "print the kernel version"_sv); + parser.add_switch_argument(machine, 'm', "machine"_sv, "print the machine name"_sv); + parser.add_switch_argument(operating_system, 'o', "operating-system"_sv, "print the operating system"_sv); + parser.parse(argc, argv); + + // If no options are given, default is -s. + if (!all && !kernel_name && !node_name && !kernel_release && !kernel_version && !machine && !operating_system) + kernel_name = true; + + if (all) kernel_name = node_name = kernel_release = kernel_version = machine = operating_system = true; + + struct utsname info; + if (uname(&info) < 0) + { + perror("uname"); + return 1; + } + + if (kernel_name) print_sequenced(info.sysname); + if (node_name) print_sequenced(info.nodename); + if (kernel_release) print_sequenced(info.release); + if (kernel_version) print_sequenced(info.version); + if (machine) print_sequenced(info.machine); + if (operating_system) print_sequenced(OPERATING_SYSTEM); + + putchar('\n'); + + return 0; +} diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index fb4e591c..8e95f096 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES src/sys/mman.cpp src/sys/wait.cpp src/sys/ioctl.cpp + src/sys/utsname.cpp ) if(${LUNA_ARCH} STREQUAL "x86_64") diff --git a/libc/src/sys/utsname.cpp b/libc/src/sys/utsname.cpp new file mode 100644 index 00000000..c19ccf7f --- /dev/null +++ b/libc/src/sys/utsname.cpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +extern "C" +{ + int uname(struct utsname* buf) + { + long rc = syscall(SYS_uname, buf); + __errno_return(rc, int); + } +}