shithub: qk1

ref: 3ba533fb9d5e013768813983fc0a4464f4ba13ac
dir: qk1/qw/sys.c

View raw version
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <thread.h>
#include "quakedef.h"

int svonly;

void *
emalloc(ulong n)
{
	void *p;

	if(p = mallocz(n, 1), p == nil)
		sysfatal("emalloc %r");
	setmalloctag(p, getcallerpc(&n));
	return p;
}

void
Sys_Printf(char *fmt, ...)
{
	char buf[1024];
	char *p;
	va_list arg;

	va_start(arg, fmt);
	vseprint(buf, buf+sizeof(buf), fmt, arg);
	va_end(arg);

	for(p = buf; *p; p++){
		*p &= 0x7f;
		if(*p < 32 && *p != 10 && *p != 13 && *p != 9)
			print("[%02x]", *p);
		else
			print("%c", *p);
	}
}

void
Sys_Error(char *error, ...)
{ 
	char buf[1024], *out;
	va_list arg;

	va_start(arg, error);
	out = vseprint(buf, buf+sizeof(buf), error, arg);
	va_end(arg);
	out = seprint(out, buf+sizeof(buf), "\n");
	write(2, buf, out-buf);
	Host_Shutdown();
	sysfatal("ending.");
}

int
Sys_FileTime(char *path)
{
	uchar bs[1024];

	if(stat(path, bs, sizeof bs) < 0)
		return -1;
	return *((int *)(bs+2+2+4+1+4+8+4+4));	/* mtime[4] */
}

void
Sys_mkdir(char *path)
{
	int d;

	if((d = create(path, OREAD, DMDIR|0777)) < 0)
		fprint(2, "Sys_mkdir:create: %r\n");
	else
		close(d);
}

vlong
flen(int fd)
{
	uchar bs[1024];

	if(fstat(fd, bs, sizeof bs) < 0){
		fprint(2, "flen:fstat: %r\n");
		return -1;
	}
	return *((vlong *)(bs+2+2+4+1+4+8+4+4+4));	/* length[8] */
}

vlong
Sys_FileOpenRead(char *path, int *fd)
{
	if((*fd = open(path, OREAD)) < 0)
		return -1;
	return flen(*fd);
}

int
Sys_FileOpenWrite(char *path)
{
	int fd;

	if((fd = open(path, OREAD|OTRUNC)) < 0)
		sysfatal("Sys_FileOpenWrite:open: %r");
	return fd;
}

double
Sys_DoubleTime(void)
{
	return nanosec()/1000000000.0;
}

void
Sys_HighFPPrecision(void)
{
}

void
Sys_LowFPPrecision(void)
{
}

void
initparm(quakeparms_t *q)
{
	int i;

	memset(q, 0, sizeof *q);
	q->argc = com_argc;
	q->argv = com_argv;
	q->memsize = 16*1024*1024;

	if(i = COM_CheckParm("-mem"))
		q->memsize = atoi(com_argv[i+1]) * 1024*1024;
	if((q->membase = malloc(q->memsize)) == nil)
		sysfatal("initparm:malloc: %r");
}

void
Sys_Quit(void)
{
	Host_Shutdown();
	threadexitsall(nil);
}

void
Sys_Init(void)
{
}