Add uname(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-01-08 00:34:21 +01:00
parent f34279cc60
commit 687096ec8a
4 changed files with 129 additions and 71 deletions

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
sys/utsname.h
System name structure.
*******************************************************************************/
#ifndef INCLUDE_SYS_UTSNAME_H
#define INCLUDE_SYS_UTSNAME_H
#include <sys/cdefs.h>
__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

View File

@ -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 <http://www.gnu.org/licenses/>.
sys/utsname/uname.cpp
Get name of the current system.
*******************************************************************************/
#include <sys/kernelinfo.h>
#include <sys/utsname.h>
#include <brand.h>
#include <string.h>
#include <unistd.h>
#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;
}

View File

@ -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 <sys/kernelinfo.h>
#include <sys/utsname.h>
#include <brand.h>
#include <errno.h>
#include <error.h>
#include <limits.h>
@ -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;
}