mirror of
				https://gitlab.com/sortix/sortix.git
				synced 2023-02-13 20:55:38 -05:00 
			
		
		
		
	Obsolete uptime(2).
This commit is contained in:
		
							parent
							
								
									71a2fef5f6
								
							
						
					
					
						commit
						a15ffa955b
					
				
					 8 changed files with 41 additions and 48 deletions
				
			
		| 
						 | 
				
			
			@ -24,6 +24,17 @@
 | 
			
		|||
#include <stdlib.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
static int uptime(uintmax_t* usecs)
 | 
			
		||||
{
 | 
			
		||||
	struct timespec uptime;
 | 
			
		||||
	if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 )
 | 
			
		||||
		return -1;
 | 
			
		||||
	*usecs = uptime.tv_sec * 1000000ULL + uptime.tv_nsec / 1000ULL;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int /*argc*/, char* /*argv*/[])
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,6 +23,16 @@
 | 
			
		|||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
static int uptime(uintmax_t* usecs)
 | 
			
		||||
{
 | 
			
		||||
	struct timespec uptime;
 | 
			
		||||
	if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 )
 | 
			
		||||
		return -1;
 | 
			
		||||
	*usecs = uptime.tv_sec * 1000000ULL + uptime.tv_nsec / 1000ULL;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int /*argc*/, char* /*argv*/[])
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,6 +32,8 @@
 | 
			
		|||
#include <fcntl.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <error.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <timespec.h>
 | 
			
		||||
 | 
			
		||||
#include <dispd.h>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -995,16 +997,16 @@ void Firework::Think(float deltatime)
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uintmax_t lastframeat;
 | 
			
		||||
struct timespec lastframeat;
 | 
			
		||||
 | 
			
		||||
void GameLogic()
 | 
			
		||||
{
 | 
			
		||||
	uintmax_t now;
 | 
			
		||||
	do uptime(&now);
 | 
			
		||||
	while ( now == lastframeat);
 | 
			
		||||
	unsigned long deltausecs = now - lastframeat;
 | 
			
		||||
	struct timespec now;
 | 
			
		||||
	do clock_gettime(CLOCK_MONOTONIC, &now);
 | 
			
		||||
	while ( timespec_eq(now, lastframeat) );
 | 
			
		||||
	struct timespec delta = timespec_sub(now, lastframeat);
 | 
			
		||||
	float deltatime = delta.tv_sec + delta.tv_nsec / 1E9f;
 | 
			
		||||
	lastframeat = now;
 | 
			
		||||
	float deltatime = deltausecs / 1000000.0f;
 | 
			
		||||
	float timescale = 3.0;
 | 
			
		||||
	deltatime *= timescale;
 | 
			
		||||
	Object* first = firstobject;
 | 
			
		||||
| 
						 | 
				
			
			@ -1106,7 +1108,7 @@ int atoi_safe(const char* str)
 | 
			
		|||
 | 
			
		||||
void InitGame()
 | 
			
		||||
{
 | 
			
		||||
	uptime(&lastframeat);
 | 
			
		||||
	clock_gettime(CLOCK_MONOTONIC, &lastframeat);
 | 
			
		||||
	GenerateStarfield(starfield, STARFIELD_WIDTH, STARFIELD_HEIGHT);
 | 
			
		||||
	playership = new Spaceship(0.0, Vector(0, 0), Vector(4.0f, 0));
 | 
			
		||||
	new AsteroidField;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -372,7 +372,6 @@ ttyname.o \
 | 
			
		|||
umask.o \
 | 
			
		||||
unlinkat.o \
 | 
			
		||||
unlink.o \
 | 
			
		||||
uptime.o \
 | 
			
		||||
usleep.o \
 | 
			
		||||
utimensat.o \
 | 
			
		||||
utimens.o \
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -372,7 +372,6 @@ size_t readall(int fd, void* buf, size_t count);
 | 
			
		|||
size_t readleast(int fd, void* buf, size_t least, size_t max);
 | 
			
		||||
pid_t sfork(int flags);
 | 
			
		||||
pid_t tfork(int flags, tforkregs_t* regs);
 | 
			
		||||
int uptime(__uintmax_t* usecssinceboot);
 | 
			
		||||
size_t writeall(int fd, const void* buf, size_t count);
 | 
			
		||||
size_t writeleast(int fd, const void* buf, size_t least, size_t max);
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,8 +29,7 @@
 | 
			
		|||
extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/)
 | 
			
		||||
{
 | 
			
		||||
	struct timespec now;
 | 
			
		||||
	// TODO: We should be using CLOCK_REALTIME.
 | 
			
		||||
	if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 )
 | 
			
		||||
	if ( clock_gettime(CLOCK_REALTIME, &now) < 0 )
 | 
			
		||||
		return -1;
 | 
			
		||||
	tp->tv_sec = now.tv_sec;
 | 
			
		||||
	tp->tv_usec = now.tv_nsec / 1000;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,34 +0,0 @@
 | 
			
		|||
/*******************************************************************************
 | 
			
		||||
 | 
			
		||||
    Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
 | 
			
		||||
 | 
			
		||||
    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/>.
 | 
			
		||||
 | 
			
		||||
    uptime.cpp
 | 
			
		||||
    Returns current system uptime.
 | 
			
		||||
 | 
			
		||||
*******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#include <sys/syscall.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
DEFN_SYSCALL1(int, sys_uptime, SYSCALL_UPTIME, uintmax_t*);
 | 
			
		||||
 | 
			
		||||
extern "C" int uptime(uintmax_t* usecssinceboot)
 | 
			
		||||
{
 | 
			
		||||
	return sys_uptime(usecssinceboot);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
/*******************************************************************************
 | 
			
		||||
 | 
			
		||||
    Copyright(C) Jonas 'Sortie' Termansen 2011.
 | 
			
		||||
    Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
 | 
			
		||||
 | 
			
		||||
    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,8 +20,12 @@
 | 
			
		|||
 | 
			
		||||
*******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <error.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
size_t Seconds(uintmax_t usecs)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -54,9 +58,12 @@ void PrintElement(size_t num, const char* single, const char* multiple)
 | 
			
		|||
 | 
			
		||||
int main(int /*argc*/, char* /*argv*/[])
 | 
			
		||||
{
 | 
			
		||||
	uintmax_t usecssinceboot;
 | 
			
		||||
	if ( uptime(&usecssinceboot) ) { perror("uptime"); return 1; }
 | 
			
		||||
	struct timespec uptime;
 | 
			
		||||
	if ( clock_gettime(CLOCK_BOOT, &uptime) < 0 )
 | 
			
		||||
		error(1, errno, "clock_gettime(CLOCK_BOOT)");
 | 
			
		||||
 | 
			
		||||
	uintmax_t usecssinceboot = uptime.tv_sec * 1000000ULL +
 | 
			
		||||
	                           uptime.tv_nsec / 1000ULL;
 | 
			
		||||
	PrintElement(Days(usecssinceboot), "day", "days");
 | 
			
		||||
	PrintElement(Hours(usecssinceboot), "hour", "hours");
 | 
			
		||||
	PrintElement(Minutes(usecssinceboot), "min", "mins");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue