2001-03-30 23:24:57 +00:00
|
|
|
/*
|
|
|
|
sys.c
|
|
|
|
|
|
|
|
virtual filesystem functions
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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 Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-03-30 23:24:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2001-04-10 20:11:50 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
# include <limits.h>
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
2001-04-10 20:11:50 +00:00
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
# include <windows.h>
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
2001-07-05 03:28:40 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2001-11-27 04:50:41 +00:00
|
|
|
#include <time.h>
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-03-08 23:11:42 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-30 23:24:57 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-07-05 04:59:43 +00:00
|
|
|
static void Sys_StdPrintf (const char *fmt, va_list args);
|
2002-02-20 19:22:52 +00:00
|
|
|
static void Sys_ErrPrintf (const char *fmt, va_list args);
|
2001-07-05 04:59:43 +00:00
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
cvar_t *sys_nostdout;
|
2001-08-29 17:45:53 +00:00
|
|
|
cvar_t *sys_extrasleep;
|
|
|
|
cvar_t *sys_dead_sleep;
|
|
|
|
cvar_t *sys_sleep;
|
|
|
|
|
2002-02-19 20:47:45 +00:00
|
|
|
int sys_checksum;
|
|
|
|
|
2002-02-20 19:22:52 +00:00
|
|
|
static sys_printf_t sys_std_printf_function = Sys_StdPrintf;
|
|
|
|
static sys_printf_t sys_err_printf_function = Sys_ErrPrintf;
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-09-24 21:00:23 +00:00
|
|
|
typedef struct shutdown_list_s {
|
|
|
|
struct shutdown_list_s *next;
|
|
|
|
void (*func)(void);
|
|
|
|
} shutdown_list_t;
|
|
|
|
|
|
|
|
static shutdown_list_t *shutdown_list;
|
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
/* The translation table between the graphical font and plain ASCII --KB */
|
2001-07-10 15:59:25 +00:00
|
|
|
const char sys_char_map[256] = {
|
2001-03-30 23:24:57 +00:00
|
|
|
'\0', '#', '#', '#', '#', '.', '#', '#',
|
|
|
|
'#', 9, 10, '#', ' ', 13, '.', '.',
|
|
|
|
'[', ']', '0', '1', '2', '3', '4', '5',
|
|
|
|
'6', '7', '8', '9', '.', '<', '=', '>',
|
|
|
|
' ', '!', '"', '#', '$', '%', '&', '\'',
|
|
|
|
'(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
|
|
|
'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
|
|
|
|
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{', '|', '}', '~', '<',
|
|
|
|
|
|
|
|
'<', '=', '>', '#', '#', '.', '#', '#',
|
|
|
|
'#', '#', ' ', '#', ' ', '>', '.', '.',
|
|
|
|
'[', ']', '0', '1', '2', '3', '4', '5',
|
|
|
|
'6', '7', '8', '9', '.', '<', '=', '>',
|
|
|
|
' ', '!', '"', '#', '$', '%', '&', '\'',
|
|
|
|
'(', ')', '*', '+', ',', '-', '.', '/',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', ':', ';', '<', '=', '>', '?',
|
|
|
|
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
|
|
|
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
|
|
|
|
'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
|
|
|
|
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
|
|
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
|
|
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
|
|
|
'x', 'y', 'z', '{', '|', '}', '~', '<'
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXPRINTMSG 4096
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
void
|
|
|
|
Sys_mkdir (const char *path)
|
|
|
|
{
|
2001-05-23 03:01:03 +00:00
|
|
|
#ifdef HAVE_MKDIR
|
2001-04-10 20:15:52 +00:00
|
|
|
# ifdef _WIN32
|
2001-04-10 20:11:50 +00:00
|
|
|
if (mkdir (path) == 0)
|
|
|
|
return;
|
|
|
|
# else
|
2001-03-30 23:24:57 +00:00
|
|
|
if (mkdir (path, 0777) == 0)
|
|
|
|
return;
|
2001-04-10 20:11:50 +00:00
|
|
|
# endif
|
2001-03-30 23:24:57 +00:00
|
|
|
#else
|
2001-05-23 03:01:03 +00:00
|
|
|
# ifdef HAVE__MKDIR
|
2001-03-30 23:24:57 +00:00
|
|
|
if (_mkdir (path) == 0)
|
|
|
|
return;
|
|
|
|
# else
|
|
|
|
# error do not know how to make directories
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
if (errno != EEXIST)
|
|
|
|
Sys_Error ("mkdir %s: %s", path, strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Sys_FileTime (const char *path)
|
|
|
|
{
|
2001-05-23 03:01:03 +00:00
|
|
|
#ifdef HAVE_ACCESS
|
2001-03-30 23:24:57 +00:00
|
|
|
if (access (path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
#else
|
2001-05-23 03:01:03 +00:00
|
|
|
# ifdef HAVE__ACCESS
|
2001-03-30 23:24:57 +00:00
|
|
|
if (_access (path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
# else
|
2001-05-23 03:01:03 +00:00
|
|
|
# error do not know how to check access
|
2001-03-30 23:24:57 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-07-05 04:59:43 +00:00
|
|
|
Sys_SetPrintf
|
|
|
|
|
|
|
|
for want of a better name, but it sets the function pointer for the
|
|
|
|
actual implementation of Sys_Printf.
|
2001-03-30 23:24:57 +00:00
|
|
|
*/
|
|
|
|
void
|
2002-02-20 19:22:52 +00:00
|
|
|
Sys_SetStdPrintf (sys_printf_t func)
|
2001-07-05 04:59:43 +00:00
|
|
|
{
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function = func;
|
2001-07-05 04:59:43 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 19:22:52 +00:00
|
|
|
void
|
|
|
|
Sys_SetErrPrintf (sys_printf_t func)
|
2001-03-30 23:24:57 +00:00
|
|
|
{
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_err_printf_function = func;
|
|
|
|
}
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2002-02-20 19:22:52 +00:00
|
|
|
void
|
|
|
|
Sys_Print (FILE *stream, const char *fmt, va_list args)
|
|
|
|
{
|
2002-03-08 23:11:42 +00:00
|
|
|
static dstring_t *msg;
|
2001-03-30 23:24:57 +00:00
|
|
|
unsigned char *p;
|
|
|
|
|
2002-03-08 23:11:42 +00:00
|
|
|
if (!msg)
|
|
|
|
msg = dstring_new ();
|
|
|
|
|
|
|
|
dvsprintf (msg, fmt, args);
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2002-02-20 19:22:52 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
if (stream == stderr)
|
2002-03-08 23:11:42 +00:00
|
|
|
MessageBox (NULL, msg->str, "Error", 0 /* MB_OK */ );
|
2002-02-20 19:22:52 +00:00
|
|
|
#endif
|
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
/* translate to ASCII instead of printing [xx] --KB */
|
2002-03-08 23:11:42 +00:00
|
|
|
for (p = (unsigned char *) msg->str; *p; p++)
|
2002-02-20 19:22:52 +00:00
|
|
|
putc (sys_char_map[*p], stream);
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2002-02-20 19:22:52 +00:00
|
|
|
fflush (stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Sys_StdPrintf (const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
if (sys_nostdout && sys_nostdout->int_val)
|
|
|
|
return;
|
|
|
|
Sys_Print (stdout, fmt, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Sys_ErrPrintf (const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Fatal Error: ");
|
|
|
|
Sys_Print (stderr, fmt, args);
|
2001-03-30 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2001-07-05 04:59:43 +00:00
|
|
|
void
|
|
|
|
Sys_Printf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function (fmt, args);
|
2001-07-05 06:31:05 +00:00
|
|
|
va_end (args);
|
2001-07-05 04:59:43 +00:00
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
void
|
|
|
|
Sys_DPrintf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (!developer || !developer->int_val)
|
|
|
|
return;
|
|
|
|
va_start (args, fmt);
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_std_printf_function (fmt, args);
|
2001-09-21 04:22:46 +00:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2001-03-30 23:24:57 +00:00
|
|
|
double
|
|
|
|
Sys_DoubleTime (void)
|
|
|
|
{
|
2001-04-01 03:30:17 +00:00
|
|
|
static qboolean first = true;
|
2001-03-30 23:24:57 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static DWORD starttime;
|
|
|
|
DWORD now;
|
|
|
|
|
|
|
|
now = timeGetTime ();
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
starttime = now;
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now < starttime) // wrapped?
|
|
|
|
return (now / 1000.0) + (LONG_MAX - starttime / 1000.0);
|
|
|
|
|
|
|
|
if (now - starttime == 0)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
return (now - starttime) / 1000.0;
|
|
|
|
#else
|
|
|
|
struct timeval tp;
|
|
|
|
struct timezone tzp;
|
2001-04-01 03:30:17 +00:00
|
|
|
double now;
|
|
|
|
static double start_time;
|
2001-03-30 23:24:57 +00:00
|
|
|
|
|
|
|
gettimeofday (&tp, &tzp);
|
2001-04-01 03:30:17 +00:00
|
|
|
now = tp.tv_sec + tp.tv_usec / 1e6;
|
2001-03-30 23:24:57 +00:00
|
|
|
|
2001-04-01 03:30:17 +00:00
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
start_time = now;
|
2001-03-30 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2001-04-01 03:30:17 +00:00
|
|
|
return now - start_time;
|
2001-03-30 23:24:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
2001-07-05 03:28:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
|
|
|
|
{
|
2001-07-05 18:47:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD flOldProtect;
|
|
|
|
|
|
|
|
if (!VirtualProtect
|
2001-12-02 17:29:14 +00:00
|
|
|
((LPVOID) startaddr, length, PAGE_READWRITE, &flOldProtect))
|
|
|
|
Sys_Error ("Protection change failed\n");
|
2001-07-05 18:47:19 +00:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_MPROTECT
|
2001-07-05 03:28:40 +00:00
|
|
|
int r;
|
|
|
|
unsigned long addr;
|
|
|
|
int psize = getpagesize ();
|
|
|
|
|
|
|
|
addr = (startaddr & ~(psize - 1)) - psize;
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
// fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
|
|
|
|
// addr, startaddr+length, length);
|
2001-07-05 03:28:40 +00:00
|
|
|
|
|
|
|
r = mprotect ((char *) addr, length + startaddr - addr + psize, 7);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
Sys_Error ("Protection change failed\n");
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|
2001-08-29 17:45:53 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Init_Cvars (void)
|
|
|
|
{
|
|
|
|
sys_nostdout = Cvar_Get ("sys_nostdout", "0", CVAR_NONE, NULL,
|
|
|
|
"Set to disable std out");
|
|
|
|
sys_extrasleep = Cvar_Get ("sys_extrasleep", "0", CVAR_NONE, NULL,
|
|
|
|
"Set to cause whatever amount delay in "
|
|
|
|
"microseconds you want. Mostly "
|
|
|
|
"useful to generate simulated bad "
|
|
|
|
"connections.");
|
|
|
|
sys_dead_sleep = Cvar_Get ("sys_dead_sleep", "1", CVAR_NONE, NULL,
|
|
|
|
"When set, the server gets NO cpu if no "
|
|
|
|
"clients are connected and there's no other "
|
|
|
|
"activity. *MIGHT* cause problems with some "
|
|
|
|
"mods.");
|
|
|
|
sys_sleep = Cvar_Get ("sys_sleep", "8", CVAR_NONE, NULL, "Sleep how long "
|
|
|
|
"in seconds between checking for connections. "
|
|
|
|
"Minimum is 0, maximum is 13");
|
|
|
|
}
|
2001-09-24 21:00:23 +00:00
|
|
|
|
2002-02-27 06:55:21 +00:00
|
|
|
void
|
|
|
|
Sys_Shutdown (void)
|
2001-09-24 21:00:23 +00:00
|
|
|
{
|
|
|
|
shutdown_list_t *p = shutdown_list;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
p->func ();
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Quit (void)
|
|
|
|
{
|
2002-02-27 06:55:21 +00:00
|
|
|
Sys_Shutdown ();
|
2001-09-24 21:00:23 +00:00
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Error (const char *error, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
va_start (argptr, error);
|
2002-02-20 19:22:52 +00:00
|
|
|
sys_err_printf_function (error, argptr);
|
2001-09-24 21:00:23 +00:00
|
|
|
va_end (argptr);
|
|
|
|
|
2002-02-27 06:55:21 +00:00
|
|
|
Sys_Shutdown ();
|
2001-09-24 21:00:23 +00:00
|
|
|
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_RegisterShutdown (void (*func) (void))
|
|
|
|
{
|
|
|
|
shutdown_list_t *p;
|
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
p = malloc (sizeof (*p));
|
|
|
|
if (!p)
|
|
|
|
Sys_Error ("Sys_RegisterShutdown: insufficient memory");
|
|
|
|
p->func = func;
|
|
|
|
p->next = shutdown_list;
|
|
|
|
shutdown_list = p;
|
|
|
|
}
|
2001-11-27 04:50:41 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
Sys_TimeID (void) //FIXME I need a new name, one that doesn't make me feel 3 feet thick
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
#ifdef _WIN32
|
|
|
|
val = ((int) (timeGetTime () * 1000) * time (NULL)) & 0xffff;
|
|
|
|
#else
|
|
|
|
val = ((int) (getpid () + getuid () * 1000) * time (NULL)) & 0xffff;
|
|
|
|
#endif
|
|
|
|
return val;
|
|
|
|
}
|
2002-02-19 20:47:45 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Sys_PageIn (void *ptr, int size)
|
|
|
|
{
|
|
|
|
//may or may not be useful in linux #ifdef WIN32
|
|
|
|
byte *x;
|
|
|
|
int m, n;
|
|
|
|
|
|
|
|
// touch all the memory to make sure it's there. The 16-page skip is to
|
|
|
|
// keep Win 95 from thinking we're trying to page ourselves in (we are
|
|
|
|
// doing that, of course, but there's no reason we shouldn't)
|
|
|
|
x = (byte *) ptr;
|
|
|
|
|
|
|
|
for (n = 0; n < 4; n++) {
|
|
|
|
for (m = 0; m < (size - 16 * 0x1000); m += 4) {
|
|
|
|
sys_checksum += *(int *) &x[m];
|
|
|
|
sys_checksum += *(int *) &x[m + 16 * 0x1000];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endif
|
|
|
|
}
|