From 687096ec8a955311841e7557cc9855599421d67d Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 8 Jan 2014 00:34:21 +0100 Subject: [PATCH] Add uname(3). --- libc/Makefile | 1 + libc/include/sys/utsname.h | 51 +++++++++++++++++++++++ libc/sys/utsname/uname.cpp | 63 ++++++++++++++++++++++++++++ utils/uname.cpp | 85 +++++++------------------------------- 4 files changed, 129 insertions(+), 71 deletions(-) create mode 100644 libc/include/sys/utsname.h create mode 100644 libc/sys/utsname/uname.cpp diff --git a/libc/Makefile b/libc/Makefile index 45fcfc88..d27703aa 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -433,6 +433,7 @@ sys/uio/preadv.o \ sys/uio/pwritev.o \ sys/uio/readv.o \ sys/uio/writev.o \ +sys/utsname/uname.o \ sys/wait/wait.o \ sys/wait/waitpid.o \ termios/tcgetwincurpos.o \ diff --git a/libc/include/sys/utsname.h b/libc/include/sys/utsname.h new file mode 100644 index 00000000..b9951561 --- /dev/null +++ b/libc/include/sys/utsname.h @@ -0,0 +1,51 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + sys/utsname.h + System name structure. + +*******************************************************************************/ + +#ifndef INCLUDE_SYS_UTSNAME_H +#define INCLUDE_SYS_UTSNAME_H + +#include + +__BEGIN_DECLS + +#define _UTSNAME_LENGTH 65 + +struct utsname +{ + char sysname[_UTSNAME_LENGTH]; + char nodename[_UTSNAME_LENGTH]; + char release[_UTSNAME_LENGTH]; + char version[_UTSNAME_LENGTH]; + char machine[_UTSNAME_LENGTH]; + char processor[_UTSNAME_LENGTH]; + char hwplatform[_UTSNAME_LENGTH]; + char opsysname[_UTSNAME_LENGTH]; + char domainname[_UTSNAME_LENGTH]; +}; + +int uname(struct utsname*); + +__END_DECLS + +#endif diff --git a/libc/sys/utsname/uname.cpp b/libc/sys/utsname/uname.cpp new file mode 100644 index 00000000..e88cc7fb --- /dev/null +++ b/libc/sys/utsname/uname.cpp @@ -0,0 +1,63 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2014. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + sys/utsname/uname.cpp + Get name of the current system. + +*******************************************************************************/ + +#include +#include + +#include +#include +#include + +#if defined(__x86_64__) +static const char* machine = "x86_64"; +static const char* processor = "x86_64"; +static const char* hwplatform = "x86_64"; +#elif defined(__i386__) +static const char* machine = "i386"; +static const char* processor = "i386"; +static const char* hwplatform = "i386"; +#else +static const char* machine = "unknown"; +static const char* processor = "unknown"; +static const char* hwplatform = "unknown"; +#endif +static const char* opsysname = BRAND_OPERATING_SYSTEM_NAME; + +extern "C" int uname(struct utsname* name) +{ + if ( kernelinfo("name", name->sysname, sizeof(name->sysname)) != 0 ) + strlcpy(name->sysname, "unknown", sizeof(name->sysname)); + if ( gethostname(name->nodename, sizeof(name->nodename)) < 0 ) + strlcpy(name->nodename, "unknown", sizeof(name->nodename)); + if ( kernelinfo("version", name->release, sizeof(name->release)) != 0 ) + strlcpy(name->release, "unknown", sizeof(name->release)); + if ( kernelinfo("builddate", name->version, sizeof(name->version)) != 0 ) + strlcpy(name->version, "unknown", sizeof(name->version)); + strlcpy(name->machine, machine, sizeof(name->machine)); + strlcpy(name->processor, processor, sizeof(name->processor)); + strlcpy(name->hwplatform, hwplatform, sizeof(name->hwplatform)); + strlcpy(name->opsysname, opsysname, sizeof(name->opsysname)); + strlcpy(name->domainname, "(none)", sizeof(name->domainname)); + return 0; +} diff --git a/utils/uname.cpp b/utils/uname.cpp index ed31e13f..93c16eb2 100644 --- a/utils/uname.cpp +++ b/utils/uname.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -20,9 +20,8 @@ *******************************************************************************/ -#include +#include -#include #include #include #include @@ -44,66 +43,6 @@ const unsigned long PRINT_PROCESSOR = 1UL << 5UL; const unsigned long PRINT_HWPLATFORM = 1UL << 6UL; const unsigned long PRINT_OPSYS = 1UL << 7UL; -const size_t INFOBUFSIZE = 256UL; -char infobuf[INFOBUFSIZE]; - -const char* GetProcessorArchitecture() -{ -#if defined(__x86_64__) - return "x86_64"; -#elif defined(__i386__) - return "i386"; -#else - return "unknown"; -#endif -} - -const char* GetKernelName() -{ - if ( kernelinfo("name", infobuf, INFOBUFSIZE) ) - return "unknown"; - return infobuf; -} - -const char* GetNodeName() -{ - return "sortix"; -} - -const char* GetKernelRelease() -{ - if ( kernelinfo("version", infobuf, INFOBUFSIZE) ) - return "unknown"; - return infobuf; -} - -const char* GetKernelVersion() -{ - if ( kernelinfo("builddate", infobuf, INFOBUFSIZE) ) - return "unknown"; - return infobuf; -} - -const char* GetMachine() -{ - return GetProcessorArchitecture(); -} - -const char* GetProcessor() -{ - return GetProcessorArchitecture(); -} - -const char* GetHardwarePlatform() -{ - return GetProcessorArchitecture(); -} - -const char* GetOperatingSystem() -{ - return BRAND_OPERATING_SYSTEM_NAME; -} - bool has_printed = false; void DoPrint(const char* msg) @@ -191,24 +130,28 @@ int main(int argc, char* argv[]) } } + static struct utsname utsname; + if ( uname(&utsname) < 0 ) + error(1, errno, "uname"); + if ( !flags ) flags = PRINT_KERNELNAME; if ( flags & PRINT_KERNELNAME ) - DoPrint(GetKernelName()); + DoPrint(utsname.sysname); if ( flags & PRINT_NODENAME ) - DoPrint(GetNodeName()); + DoPrint(utsname.nodename); if ( flags & PRINT_KERNELREL ) - DoPrint(GetKernelRelease()); + DoPrint(utsname.release); if ( flags & PRINT_KERNELVER ) - DoPrint(GetKernelVersion()); + DoPrint(utsname.version); if ( flags & PRINT_MACHINE ) - DoPrint(GetMachine()); + DoPrint(utsname.machine); if ( flags & PRINT_PROCESSOR ) - DoPrint(GetProcessor()); + DoPrint(utsname.processor); if ( flags & PRINT_HWPLATFORM ) - DoPrint(GetHardwarePlatform()); + DoPrint(utsname.hwplatform); if ( flags & PRINT_OPSYS ) - DoPrint(GetOperatingSystem()); + DoPrint(utsname.opsysname); printf("\n"); return 0; }