Added uptime(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-28 16:28:56 +01:00
parent f232a054d3
commit 8c146f14c0
7 changed files with 103 additions and 2 deletions

View File

@ -60,6 +60,7 @@ init.o \
signal.o \
$(CPU)/signal.o \
start.o \
time.o \
random.o \
MAXSIHEADERS=\

View File

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

41
libmaxsi/time.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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);
}
}
}

View File

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

View File

@ -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.
}
}

View File

@ -14,6 +14,7 @@ mxsh \
clear \
ls \
help \
uptime \
memstat \
uname \
idle \

45
utils/uptime.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <unistd.h>
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;
}