From 8c146f14c0c42360e9a03b8f8469a78947a46300 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 28 Nov 2011 16:28:56 +0100 Subject: [PATCH] Added uptime(1). --- libmaxsi/Makefile | 1 + libmaxsi/c/hsrc/unistd.h | 2 ++ libmaxsi/time.cpp | 41 ++++++++++++++++++++++++++++++++++++ sortix/syscallnum.h | 3 ++- sortix/time.cpp | 12 ++++++++++- utils/Makefile | 1 + utils/uptime.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 libmaxsi/time.cpp create mode 100644 utils/uptime.cpp diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index ba111040..e969d26a 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -60,6 +60,7 @@ init.o \ signal.o \ $(CPU)/signal.o \ start.o \ +time.o \ random.o \ MAXSIHEADERS=\ diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index 6cb2f206..5ae24953 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -172,7 +172,9 @@ int unlink(const char*); ssize_t write(int, const void*, size_t); #ifdef SORTIX_EXTENSIONS +@include(intn_t.h) int memstat(size_t* memused, size_t* memtotal); +int uptime(uintmax_t* mssinceboot); int writeall(int fd, const void* buffer, size_t len); #endif diff --git a/libmaxsi/time.cpp b/libmaxsi/time.cpp new file mode 100644 index 00000000..4ba2c7ae --- /dev/null +++ b/libmaxsi/time.cpp @@ -0,0 +1,41 @@ +/****************************************************************************** + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + + This file is part of LibMaxsi. + + LibMaxsi 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. + + LibMaxsi 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 LibMaxsi. If not, see . + + time.cpp + Useful time functions. + +******************************************************************************/ + +#include "platform.h" +#include "string.h" +#include "memory.h" +#include "syscall.h" + +namespace Maxsi +{ + namespace Time + { + DEFN_SYSCALL1(int, SysUptime, 34, uintmax_t*); + + extern "C" int uptime(uintmax_t* mssinceboot) + { + return SysUptime(mssinceboot); + } + } +} diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index 9b458fb1..f144cc04 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -59,7 +59,8 @@ #define SYSCALL_KILL 31 #define SYSCALL_MEMSTAT 32 #define SYSCALL_ISATTY 33 -#define SYSCALL_MAX_NUM 34 /* index of highest constant + 1 */ +#define SYSCALL_UPTIME 34 +#define SYSCALL_MAX_NUM 35 /* index of highest constant + 1 */ #endif diff --git a/sortix/time.cpp b/sortix/time.cpp index b0f03f58..808efecf 100644 --- a/sortix/time.cpp +++ b/sortix/time.cpp @@ -30,6 +30,7 @@ #include "scheduler.h" #include "log.h" #include "sound.h" +#include "syscall.h" #ifdef PLATFORM_SERIAL #include "serialterminal.h" @@ -84,6 +85,13 @@ namespace Sortix bool didUglyIRQ0Hack; + int SysUptime(uintmax_t* mssinceboot) + { + // TODO: HACK! + *mssinceboot = ((size_t)microsecondssinceboot) / 1000; + return 0; + } + void Init() { // Initialize our variables. @@ -94,6 +102,8 @@ namespace Sortix Interrupt::RegisterHandler(Interrupt::IRQ0, &OnIRQ0); Interrupt::RegisterHandler(177, &OnInt177); + Syscall::Register(SYSCALL_UPTIME, (void*) SysUptime); + didUglyIRQ0Hack = false; RequestIQR0(); @@ -118,6 +128,6 @@ namespace Sortix if ( !didUglyIRQ0Hack ) { RequestIQR0(); didUglyIRQ0Hack = true; } } - // TODO: Implement all the other useful functions regarding tiem. + // TODO: Implement all the other useful functions regarding time. } } diff --git a/utils/Makefile b/utils/Makefile index a42d4696..68f2aa70 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -14,6 +14,7 @@ mxsh \ clear \ ls \ help \ +uptime \ memstat \ uname \ idle \ diff --git a/utils/uptime.cpp b/utils/uptime.cpp new file mode 100644 index 00000000..d2076042 --- /dev/null +++ b/utils/uptime.cpp @@ -0,0 +1,45 @@ +#include +#include + +size_t Seconds(size_t ms) +{ + return (ms / 1000UL) % (60UL); +} + +size_t Minutes(size_t ms) +{ + return (ms / (1000UL * 60UL)) % (60UL); +} + +size_t Hours(size_t ms) +{ + return (ms / (1000UL * 60UL * 60UL)) % (24UL); +} + +size_t Days(size_t ms) +{ + return (ms / (1000UL * 60UL * 60UL * 24UL)) % (24UL); +} + +void PrintElement(size_t num, const char* single, const char* multiple) +{ + static const char* prefix = ""; + if ( !num ) { return; } + const char* str = (num>1) ? multiple : single; + printf("%s%zu %s", prefix, num, str); + prefix = ", "; +} + +int main(int argc, char* argv[]) +{ + uintmax_t mssinceboot; + if ( uptime(&mssinceboot) ) { perror("uptime"); return 1; } + + PrintElement(Days(mssinceboot), "day", "days"); + PrintElement(Hours(mssinceboot), "hour", "hours"); + PrintElement(Minutes(mssinceboot), "min", "mins"); + PrintElement(Seconds(mssinceboot), "sec", "secs"); + printf("\n"); + + return 0; +}