2009-02-28 14:41:18 +00:00
|
|
|
/*
|
2010-10-18 13:04:28 +00:00
|
|
|
* Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* 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.
|
2010-10-18 13:04:28 +00:00
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-10-18 13:04:28 +00:00
|
|
|
* 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 the Free Software
|
2010-10-18 13:19:17 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
2010-10-18 13:04:28 +00:00
|
|
|
*
|
2010-10-18 13:19:17 +00:00
|
|
|
* =======================================================================
|
|
|
|
*
|
2018-02-04 09:46:44 +00:00
|
|
|
* This file implements all system dependent generic functions.
|
2010-10-18 13:19:17 +00:00
|
|
|
*
|
|
|
|
* =======================================================================
|
2010-10-18 13:04:28 +00:00
|
|
|
*/
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <dlfcn.h>
|
2009-02-28 14:41:18 +00:00
|
|
|
#include <fcntl.h>
|
2018-02-04 09:46:44 +00:00
|
|
|
#include <stdlib.h>
|
2009-02-28 14:41:18 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-09-06 16:07:34 +00:00
|
|
|
#include <time.h>
|
2018-02-04 09:46:44 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2018-03-29 13:29:30 +00:00
|
|
|
#include <sys/select.h> /* for fd_set */
|
2020-06-20 15:37:21 +00:00
|
|
|
#ifndef FNDELAY
|
|
|
|
#define FNDELAY O_NDELAY
|
|
|
|
#endif
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2017-10-02 15:58:03 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 11:58:10 +00:00
|
|
|
#include "../../common/header/common.h"
|
|
|
|
#include "../../common/header/glob.h"
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
// Pointer to game library
|
2010-10-18 13:04:28 +00:00
|
|
|
static void *game_library;
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
// Evil hack to determine if stdin is available
|
2009-02-28 14:41:18 +00:00
|
|
|
qboolean stdin_active = true;
|
2018-02-04 09:46:44 +00:00
|
|
|
|
2020-03-16 13:28:12 +00:00
|
|
|
// Config dir
|
|
|
|
char cfgdir[MAX_OSPATH] = CFGDIR;
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
// Console logfile
|
2012-04-29 13:57:33 +00:00
|
|
|
extern FILE *logfile;
|
2010-10-18 16:06:40 +00:00
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Error(char *error, ...)
|
2010-10-18 16:06:40 +00:00
|
|
|
{
|
2018-02-04 09:46:44 +00:00
|
|
|
va_list argptr;
|
|
|
|
char string[1024];
|
|
|
|
|
|
|
|
/* change stdin to non blocking */
|
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY);
|
|
|
|
|
|
|
|
#ifndef DEDICATED_ONLY
|
|
|
|
CL_Shutdown();
|
|
|
|
#endif
|
|
|
|
Qcommon_Shutdown();
|
|
|
|
|
|
|
|
va_start(argptr, error);
|
|
|
|
vsnprintf(string, 1024, error, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
fprintf(stderr, "Error: %s\n", string);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_Quit(void)
|
|
|
|
{
|
|
|
|
#ifndef DEDICATED_ONLY
|
|
|
|
CL_Shutdown();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (logfile)
|
2010-10-18 16:06:40 +00:00
|
|
|
{
|
2018-02-04 09:46:44 +00:00
|
|
|
fclose(logfile);
|
|
|
|
logfile = NULL;
|
2010-10-18 16:06:40 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
Qcommon_Shutdown();
|
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~FNDELAY);
|
|
|
|
|
|
|
|
printf("------------------------------------\n");
|
|
|
|
|
|
|
|
exit(0);
|
2010-10-18 16:06:40 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-03 20:26:55 +00:00
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Init(void)
|
2012-06-03 20:26:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
|
|
|
|
|
|
|
char *
|
|
|
|
Sys_ConsoleInput(void)
|
|
|
|
{
|
|
|
|
static char text[256];
|
|
|
|
int len;
|
|
|
|
fd_set fdset;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
if (!dedicated || !dedicated->value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stdin_active)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(0, &fdset); /* stdin */
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if ((select(1, &fdset, NULL, NULL, &timeout) == -1) || !FD_ISSET(0, &fdset))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = read(0, text, sizeof(text));
|
|
|
|
|
|
|
|
if (len == 0) /* eof! */
|
|
|
|
{
|
|
|
|
stdin_active = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 1)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
text[len - 1] = 0; /* rip off the /n and terminate */
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_ConsoleOutput(char *string)
|
|
|
|
{
|
|
|
|
fputs(string, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ================================================================ */
|
|
|
|
|
2017-10-02 15:58:03 +00:00
|
|
|
long long
|
|
|
|
Sys_Microseconds(void)
|
|
|
|
{
|
2017-10-22 15:54:19 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// OSX didn't have clock_gettime() until recently, so use Mach's clock_get_time()
|
|
|
|
// instead. fortunately its mach_timespec_t seems identical to POSIX struct timespec
|
|
|
|
// so lots of code can be shared
|
2017-10-02 15:58:03 +00:00
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t now;
|
|
|
|
static mach_timespec_t first;
|
|
|
|
|
|
|
|
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &now);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
|
2017-10-22 15:54:19 +00:00
|
|
|
#else // not __APPLE__ - other Unix-likes will hopefully support clock_gettime()
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2017-10-22 15:54:19 +00:00
|
|
|
struct timespec now;
|
|
|
|
static struct timespec first;
|
2018-02-04 09:46:44 +00:00
|
|
|
#ifdef _POSIX_MONOTONIC_CLOCK
|
2017-09-06 16:07:34 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
2018-02-04 09:46:44 +00:00
|
|
|
#else
|
2017-10-02 15:58:03 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &now);
|
2018-02-04 09:46:44 +00:00
|
|
|
#endif
|
2017-10-22 15:54:19 +00:00
|
|
|
|
|
|
|
#endif // not __APPLE__
|
2017-09-06 16:07:34 +00:00
|
|
|
|
2017-10-02 15:58:03 +00:00
|
|
|
if(first.tv_sec == 0)
|
2017-09-03 11:32:55 +00:00
|
|
|
{
|
2017-10-22 15:54:19 +00:00
|
|
|
long long nsec = now.tv_nsec;
|
|
|
|
long long sec = now.tv_sec;
|
|
|
|
// set back first by 1ms so neither this function nor Sys_Milliseconds()
|
|
|
|
// (which calls this) will ever return 0
|
2017-12-09 11:30:52 +00:00
|
|
|
nsec -= 1000000;
|
2017-10-22 15:54:19 +00:00
|
|
|
if(nsec < 0)
|
|
|
|
{
|
|
|
|
nsec += 1000000000ll; // 1s in ns => definitely positive now
|
|
|
|
--sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
first.tv_sec = sec;
|
|
|
|
first.tv_nsec = nsec;
|
2017-09-03 11:32:55 +00:00
|
|
|
}
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2017-10-02 15:58:03 +00:00
|
|
|
long long sec = now.tv_sec - first.tv_sec;
|
|
|
|
long long nsec = now.tv_nsec - first.tv_nsec;
|
2017-09-06 16:07:34 +00:00
|
|
|
|
2017-09-03 11:32:55 +00:00
|
|
|
if(nsec < 0)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2017-09-03 11:32:55 +00:00
|
|
|
nsec += 1000000000ll; // 1s in ns
|
|
|
|
--sec;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:32:55 +00:00
|
|
|
return sec*1000000ll + nsec/1000ll;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Sys_Milliseconds(void)
|
|
|
|
{
|
2017-09-07 14:28:31 +00:00
|
|
|
return (int)(Sys_Microseconds()/1000ll);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-04 09:46:44 +00:00
|
|
|
Sys_Nanosleep(int nanosec)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2018-02-04 09:46:44 +00:00
|
|
|
struct timespec t = {0, nanosec};
|
|
|
|
nanosleep(&t, NULL);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2018-02-04 10:49:03 +00:00
|
|
|
/* The musthave and canhave arguments are unused in YQ2. We
|
|
|
|
can't remove them since Sys_FindFirst() and Sys_FindNext()
|
|
|
|
are defined in shared.h and may be used in custom game DLLs. */
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2018-02-04 11:10:13 +00:00
|
|
|
static char findbase[MAX_OSPATH];
|
|
|
|
static char findpath[MAX_OSPATH];
|
|
|
|
static char findpattern[MAX_OSPATH];
|
|
|
|
static DIR *fdir;
|
|
|
|
|
2010-10-18 13:19:17 +00:00
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindFirst(char *path, unsigned musthave, unsigned canhave)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
char *p;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_Error("Sys_BeginFind without close");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findbase, path);
|
2010-10-18 13:19:17 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((p = strrchr(findbase, '/')) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
*p = 0;
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, p + 1);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, "*");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (strcmp(findpattern, "*.*") == 0)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
strcpy(findpattern, "*");
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if ((fdir = opendir(findbase)) == NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while ((d = readdir(fdir)) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!*findpattern || glob_match(findpattern, d->d_name))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2018-02-04 10:49:03 +00:00
|
|
|
if ((strcmp(d->d_name, ".") != 0) || (strcmp(d->d_name, "..") != 0))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2020-01-26 20:11:28 +00:00
|
|
|
snprintf(findpath, sizeof(findpath), "%s/%s", findbase, d->d_name);
|
2012-06-08 11:01:56 +00:00
|
|
|
return findpath;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindNext(unsigned musthave, unsigned canhave)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir == NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while ((d = readdir(fdir)) != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!*findpattern || glob_match(findpattern, d->d_name))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2018-02-04 10:49:03 +00:00
|
|
|
if ((strcmp(d->d_name, ".") != 0) || (strcmp(d->d_name, "..") != 0))
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2020-01-26 20:11:28 +00:00
|
|
|
snprintf(findpath, sizeof(findpath), "%s/%s", findbase, d->d_name);
|
2012-06-08 11:01:56 +00:00
|
|
|
return findpath;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_FindClose(void)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fdir != NULL)
|
2010-10-18 13:19:17 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
closedir(fdir);
|
2010-10-18 13:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fdir = NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
2010-10-18 13:04:28 +00:00
|
|
|
|
|
|
|
void
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_UnloadGame(void)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
dlclose(game_library);
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
game_library = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void *
|
2012-06-08 11:01:56 +00:00
|
|
|
Sys_GetGameAPI(void *parms)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
void *(*GetGameAPI)(void *);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
char name[MAX_OSPATH];
|
|
|
|
char *path;
|
|
|
|
char *str_p;
|
2018-02-04 09:46:44 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
const char *gamename = "game.dylib";
|
|
|
|
#else
|
|
|
|
const char *gamename = "game.so";
|
|
|
|
#endif
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Error(ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2020-10-08 10:38:29 +00:00
|
|
|
Com_Printf("Loading library: %s\n", gamename);
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
/* now run through the search paths */
|
2009-02-28 14:41:18 +00:00
|
|
|
path = NULL;
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
while (1)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2017-12-09 11:30:52 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
path = FS_NextPath(path);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!path)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL; /* couldn't find one anywhere */
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
snprintf(name, MAX_OSPATH, "%s/%s", path, gamename);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2009-02-28 14:41:18 +00:00
|
|
|
/* skip it if it just doesn't exist */
|
2012-06-08 11:01:56 +00:00
|
|
|
fp = fopen(name, "rb");
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (fp == NULL)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
continue;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
fclose(fp);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
game_library = dlopen(name, RTLD_NOW);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (game_library)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2020-10-08 10:38:29 +00:00
|
|
|
Com_MDPrintf("Loading library: %s\n", name);
|
2009-02-28 14:41:18 +00:00
|
|
|
break;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2020-10-08 10:38:29 +00:00
|
|
|
Com_Printf("Loading library: %s\n: ", name);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
path = (char *)dlerror();
|
|
|
|
str_p = strchr(path, ':'); /* skip the path (already shown) */
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (str_p == NULL)
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
str_p = path;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
else
|
2010-10-18 13:04:28 +00:00
|
|
|
{
|
2009-02-28 14:41:18 +00:00
|
|
|
str_p++;
|
2010-10-18 13:04:28 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
Com_Printf("%s\n", str_p);
|
2010-10-18 13:04:28 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
GetGameAPI = (void *)dlsym(game_library, "GetGameAPI");
|
2009-02-28 14:41:18 +00:00
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
if (!GetGameAPI)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2010-10-18 13:04:28 +00:00
|
|
|
Sys_UnloadGame();
|
2012-06-08 11:01:56 +00:00
|
|
|
return NULL;
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:01:56 +00:00
|
|
|
return GetGameAPI(parms);
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
|
|
|
|
2010-10-18 13:04:28 +00:00
|
|
|
void
|
2020-10-20 07:35:07 +00:00
|
|
|
Sys_Mkdir(const char *path)
|
2009-02-28 14:41:18 +00:00
|
|
|
{
|
2020-12-31 07:55:09 +00:00
|
|
|
if (!Sys_IsDir(path))
|
|
|
|
{
|
|
|
|
if (mkdir(path, 0755) != 0)
|
|
|
|
{
|
|
|
|
Com_Error(ERR_FATAL, "Couldn't create dir %s\n", path);
|
|
|
|
}
|
|
|
|
}
|
2009-02-28 14:41:18 +00:00
|
|
|
}
|
2012-06-11 07:55:54 +00:00
|
|
|
|
2018-06-10 03:12:49 +00:00
|
|
|
qboolean
|
|
|
|
Sys_IsDir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) != -1)
|
|
|
|
{
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
Sys_IsFile(const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) != -1)
|
|
|
|
{
|
|
|
|
if (S_ISREG(sb.st_mode))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-11 07:55:54 +00:00
|
|
|
char *
|
|
|
|
Sys_GetHomeDir(void)
|
|
|
|
{
|
|
|
|
static char gdir[MAX_OSPATH];
|
|
|
|
char *home;
|
2013-04-28 19:33:08 +00:00
|
|
|
|
2012-06-11 07:55:54 +00:00
|
|
|
home = getenv("HOME");
|
|
|
|
|
|
|
|
if (!home)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-16 13:28:12 +00:00
|
|
|
snprintf(gdir, sizeof(gdir), "%s/%s/", home, cfgdir);
|
2020-12-30 17:45:16 +00:00
|
|
|
Sys_Mkdir(gdir);
|
2012-06-11 07:55:54 +00:00
|
|
|
|
|
|
|
return gdir;
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:57:06 +00:00
|
|
|
void
|
|
|
|
Sys_Remove(const char *path)
|
|
|
|
{
|
|
|
|
remove(path);
|
|
|
|
}
|
|
|
|
|
2018-12-18 18:11:39 +00:00
|
|
|
int
|
|
|
|
Sys_Rename(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
return rename(from, to);
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:01:26 +00:00
|
|
|
void
|
|
|
|
Sys_RemoveDir(const char *path)
|
|
|
|
{
|
2019-01-04 12:49:55 +00:00
|
|
|
char filepath[MAX_OSPATH];
|
2018-12-30 21:01:26 +00:00
|
|
|
DIR *directory = opendir(path);
|
|
|
|
struct dirent *file;
|
|
|
|
|
|
|
|
if (Sys_IsDir(path))
|
|
|
|
{
|
|
|
|
while ((file = readdir(directory)) != NULL)
|
|
|
|
{
|
2019-01-04 18:20:17 +00:00
|
|
|
snprintf(filepath, MAX_OSPATH, "%s/%s", path, file->d_name);
|
2019-01-04 12:49:55 +00:00
|
|
|
Sys_Remove(filepath);
|
2018-12-30 21:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(directory);
|
|
|
|
Sys_Remove(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 16:20:16 +00:00
|
|
|
qboolean
|
2020-10-20 06:20:20 +00:00
|
|
|
Sys_Realpath(const char *in, char *out, size_t size)
|
|
|
|
{
|
|
|
|
char *converted = realpath(in, NULL);
|
|
|
|
|
|
|
|
if (converted == NULL)
|
|
|
|
{
|
2021-06-20 16:20:16 +00:00
|
|
|
Com_Printf("Couldn't get realpath for %s\n", in);
|
|
|
|
return false;
|
2020-10-20 06:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_strlcpy(out, converted, size);
|
|
|
|
free(converted);
|
2021-06-20 16:20:16 +00:00
|
|
|
|
|
|
|
return true;
|
2020-10-20 06:20:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
/* ================================================================ */
|
|
|
|
|
2012-08-01 12:54:18 +00:00
|
|
|
void *
|
|
|
|
Sys_GetProcAddress(void *handle, const char *sym)
|
|
|
|
{
|
2016-06-30 08:41:57 +00:00
|
|
|
if (handle == NULL)
|
|
|
|
{
|
|
|
|
#ifdef RTLD_DEFAULT
|
|
|
|
return dlsym(RTLD_DEFAULT, sym);
|
|
|
|
#else
|
|
|
|
/* POSIX suggests that this is a portable equivalent */
|
|
|
|
static void *global_namespace = NULL;
|
|
|
|
|
|
|
|
if (global_namespace == NULL)
|
|
|
|
global_namespace = dlopen(NULL, RTLD_GLOBAL|RTLD_LAZY);
|
|
|
|
|
|
|
|
return dlsym(global_namespace, sym);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return dlsym(handle, sym);
|
2012-08-01 12:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 09:46:44 +00:00
|
|
|
void
|
|
|
|
Sys_FreeLibrary(void *handle)
|
|
|
|
{
|
|
|
|
if (handle && dlclose(handle))
|
|
|
|
{
|
|
|
|
Com_Error(ERR_FATAL, "dlclose failed on %p: %s", handle, dlerror());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 12:54:18 +00:00
|
|
|
void *
|
|
|
|
Sys_LoadLibrary(const char *path, const char *sym, void **handle)
|
|
|
|
{
|
|
|
|
void *module, *entry;
|
|
|
|
|
|
|
|
*handle = NULL;
|
|
|
|
|
|
|
|
module = dlopen(path, RTLD_LAZY);
|
|
|
|
|
|
|
|
if (!module)
|
|
|
|
{
|
2012-08-03 12:35:31 +00:00
|
|
|
Com_Printf("%s failed: %s\n", __func__, dlerror());
|
2012-08-01 12:54:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym)
|
|
|
|
{
|
|
|
|
entry = dlsym(module, sym);
|
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
{
|
2012-08-03 12:35:31 +00:00
|
|
|
Com_Printf("%s failed: %s\n", __func__, dlerror());
|
2012-08-01 12:54:18 +00:00
|
|
|
dlclose(module);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Com_DPrintf("%s succeeded: %s\n", __func__, path);
|
|
|
|
|
|
|
|
*handle = module;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
2018-02-05 16:21:55 +00:00
|
|
|
|
|
|
|
/* ================================================================ */
|
|
|
|
|
|
|
|
void
|
|
|
|
Sys_GetWorkDir(char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (getcwd(buffer, len) != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
Sys_SetWorkDir(char *path)
|
|
|
|
{
|
|
|
|
if (chdir(path) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|