apps: Add uname
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-24 21:02:04 +02:00
parent 9eab0886b6
commit cfb4baab4b
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 74 additions and 0 deletions

View File

@ -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)

59
apps/uname.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <os/ArgumentParser.h>
#include <stdio.h>
#include <sys/utsname.h>
#define OPERATING_SYSTEM "Luna"
void print_sequenced(const char* string)
{
static bool first { true };
if (!first) { putchar(' '); }
first = false;
fputs(string, stdout);
}
Result<int> 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;
}

View File

@ -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")

13
libc/src/sys/utsname.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <bits/errno-return.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
extern "C"
{
int uname(struct utsname* buf)
{
long rc = syscall(SYS_uname, buf);
__errno_return(rc, int);
}
}