ioq3/code/sys/sys_unix.c

543 lines
10 KiB
C
Raw Normal View History

2005-08-26 17:39:27 +00:00
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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 Quake III Arena source code; if not, write to the Free Software
2005-08-26 17:39:27 +00:00
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#include "sys_local.h"
#include <signal.h>
2005-08-26 17:39:27 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <pwd.h>
#include <libgen.h>
2005-08-26 17:39:27 +00:00
// Used to determine where to store user-specific files
static char homePath[ MAX_OSPATH ] = { 0 };
2005-08-26 17:39:27 +00:00
/*
==================
Sys_DefaultHomePath
==================
*/
char *Sys_DefaultHomePath(void)
{
char *p;
2005-08-26 17:39:27 +00:00
if( !*homePath )
{
if( ( p = getenv( "HOME" ) ) != NULL )
{
Q_strncpyz( homePath, p, sizeof( homePath ) );
#ifdef MACOS_X
Q_strcat( homePath, sizeof( homePath ), "/Library/Application Support/Quake3" );
#else
Q_strcat( homePath, sizeof( homePath ), "/.q3a" );
#endif
if( mkdir( homePath, 0777 ) )
{
if( errno != EEXIST )
{
Sys_Error( "Unable to create directory \"%s\", error is %s(%d)\n",
homePath, strerror( errno ), errno );
}
}
}
}
2005-08-26 17:39:27 +00:00
return homePath;
}
2005-08-26 17:39:27 +00:00
/*
================
Sys_Milliseconds
================
*/
/* base time in seconds, that's our origin
timeval:tv_sec is an int:
2005-08-26 17:39:27 +00:00
assuming this wraps every 0x7fffffff - ~68 years since the Epoch (1970) - we're safe till 2038
using unsigned long data type to work right with Sys_XTimeToSysTime */
unsigned long sys_timeBase = 0;
/* current time in ms, using sys_timeBase as origin
NOTE: sys_timeBase*1000 + curtime -> ms since the Epoch
0x7fffffff ms - ~24 days
although timeval:tv_usec is an int, I'm not sure wether it is actually used as an unsigned int
(which would affect the wrap period) */
int curtime;
int Sys_Milliseconds (void)
{
struct timeval tp;
gettimeofday(&tp, NULL);
2005-08-26 17:39:27 +00:00
if (!sys_timeBase)
{
sys_timeBase = tp.tv_sec;
return tp.tv_usec/1000;
}
curtime = (tp.tv_sec - sys_timeBase)*1000 + tp.tv_usec/1000;
2005-08-26 17:39:27 +00:00
return curtime;
}
#if !id386
2005-08-26 17:39:27 +00:00
/*
==================
fastftol
==================
*/
long fastftol( float f )
{
return (long)f;
}
2005-08-26 17:39:27 +00:00
/*
==================
Sys_SnapVector
==================
2005-08-26 17:39:27 +00:00
*/
void Sys_SnapVector( float *v )
2005-08-26 17:39:27 +00:00
{
v[0] = rint(v[0]);
v[1] = rint(v[1]);
v[2] = rint(v[2]);
}
#endif
/*
==================
Sys_RandomBytes
==================
*/
qboolean Sys_RandomBytes( byte *string, int len )
{
FILE *fp;
fp = fopen( "/dev/urandom", "r" );
if( !fp )
return qfalse;
if( !fread( string, sizeof( byte ), len, fp ) )
2005-08-26 17:39:27 +00:00
{
fclose( fp );
return qfalse;
2005-08-26 17:39:27 +00:00
}
fclose( fp );
return qtrue;
}
/*
==================
Sys_GetCurrentUser
==================
*/
char *Sys_GetCurrentUser( void )
{
struct passwd *p;
2005-08-26 17:39:27 +00:00
if ( (p = getpwuid( getuid() )) == NULL ) {
return "player";
}
return p->pw_name;
2005-08-26 17:39:27 +00:00
}
/*
==================
Sys_GetClipboardData
==================
*/
char *Sys_GetClipboardData(void)
{
return NULL;
2005-08-26 17:39:27 +00:00
}
#define MEM_THRESHOLD 96*1024*1024
/*
==================
Sys_LowPhysicalMemory
TODO
==================
*/
qboolean Sys_LowPhysicalMemory( void )
{
return qfalse;
2005-08-26 17:39:27 +00:00
}
/*
==================
Sys_Basename
==================
*/
const char *Sys_Basename( char *path )
{
return basename( path );
}
2005-08-26 17:39:27 +00:00
/*
==================
Sys_Dirname
==================
*/
const char *Sys_Dirname( char *path )
2005-08-26 17:39:27 +00:00
{
return dirname( path );
2005-08-26 17:39:27 +00:00
}
/*
==================
Sys_Mkdir
==================
*/
void Sys_Mkdir( const char *path )
{
mkdir( path, 0777 );
2005-08-26 17:39:27 +00:00
}
/*
==================
Sys_Cwd
==================
*/
char *Sys_Cwd( void )
{
static char cwd[MAX_OSPATH];
getcwd( cwd, sizeof( cwd ) - 1 );
cwd[MAX_OSPATH-1] = 0;
return cwd;
}
/*
==============================================================
DIRECTORY SCANNING
==============================================================
*/
2005-08-26 17:39:27 +00:00
#define MAX_FOUND_FILES 0x1000
2005-08-26 17:39:27 +00:00
/*
==================
Sys_ListFilteredFiles
==================
*/
void Sys_ListFilteredFiles( const char *basedir, char *subdirs, char *filter, char **list, int *numfiles )
{
char search[MAX_OSPATH], newsubdirs[MAX_OSPATH];
char filename[MAX_OSPATH];
DIR *fdir;
2005-08-26 17:39:27 +00:00
struct dirent *d;
struct stat st;
2005-08-26 17:39:27 +00:00
if ( *numfiles >= MAX_FOUND_FILES - 1 ) {
return;
}
if (strlen(subdirs)) {
Com_sprintf( search, sizeof(search), "%s/%s", basedir, subdirs );
}
else {
Com_sprintf( search, sizeof(search), "%s", basedir );
}
if ((fdir = opendir(search)) == NULL) {
return;
}
while ((d = readdir(fdir)) != NULL) {
Com_sprintf(filename, sizeof(filename), "%s/%s", search, d->d_name);
if (stat(filename, &st) == -1)
continue;
if (st.st_mode & S_IFDIR) {
if (Q_stricmp(d->d_name, ".") && Q_stricmp(d->d_name, "..")) {
if (strlen(subdirs)) {
Com_sprintf( newsubdirs, sizeof(newsubdirs), "%s/%s", subdirs, d->d_name);
}
else {
Com_sprintf( newsubdirs, sizeof(newsubdirs), "%s", d->d_name);
}
Sys_ListFilteredFiles( basedir, newsubdirs, filter, list, numfiles );
}
}
if ( *numfiles >= MAX_FOUND_FILES - 1 ) {
break;
}
Com_sprintf( filename, sizeof(filename), "%s/%s", subdirs, d->d_name );
if (!Com_FilterPath( filter, filename, qfalse ))
continue;
list[ *numfiles ] = CopyString( filename );
(*numfiles)++;
}
closedir(fdir);
}
/*
==================
Sys_ListFiles
==================
*/
2005-08-26 17:39:27 +00:00
char **Sys_ListFiles( const char *directory, const char *extension, char *filter, int *numfiles, qboolean wantsubs )
{
struct dirent *d;
DIR *fdir;
qboolean dironly = wantsubs;
char search[MAX_OSPATH];
int nfiles;
char **listCopy;
char *list[MAX_FOUND_FILES];
int i;
struct stat st;
int extLen;
2005-08-26 17:39:27 +00:00
if (filter) {
nfiles = 0;
Sys_ListFilteredFiles( directory, "", filter, list, &nfiles );
2005-09-23 17:39:14 +00:00
list[ nfiles ] = NULL;
2005-08-26 17:39:27 +00:00
*numfiles = nfiles;
if (!nfiles)
return NULL;
listCopy = Z_Malloc( ( nfiles + 1 ) * sizeof( *listCopy ) );
for ( i = 0 ; i < nfiles ; i++ ) {
listCopy[i] = list[i];
}
listCopy[i] = NULL;
return listCopy;
}
if ( !extension)
extension = "";
if ( extension[0] == '/' && extension[1] == 0 ) {
extension = "";
dironly = qtrue;
}
extLen = strlen( extension );
// search
nfiles = 0;
if ((fdir = opendir(directory)) == NULL) {
*numfiles = 0;
return NULL;
}
while ((d = readdir(fdir)) != NULL) {
Com_sprintf(search, sizeof(search), "%s/%s", directory, d->d_name);
if (stat(search, &st) == -1)
continue;
if ((dironly && !(st.st_mode & S_IFDIR)) ||
(!dironly && (st.st_mode & S_IFDIR)))
continue;
if (*extension) {
if ( strlen( d->d_name ) < strlen( extension ) ||
Q_stricmp(
2005-08-26 17:39:27 +00:00
d->d_name + strlen( d->d_name ) - strlen( extension ),
extension ) ) {
continue; // didn't match
}
}
if ( nfiles == MAX_FOUND_FILES - 1 )
break;
list[ nfiles ] = CopyString( d->d_name );
nfiles++;
}
2005-09-23 17:39:14 +00:00
list[ nfiles ] = NULL;
2005-08-26 17:39:27 +00:00
closedir(fdir);
// return a copy of the list
*numfiles = nfiles;
if ( !nfiles ) {
return NULL;
}
listCopy = Z_Malloc( ( nfiles + 1 ) * sizeof( *listCopy ) );
for ( i = 0 ; i < nfiles ; i++ ) {
listCopy[i] = list[i];
}
listCopy[i] = NULL;
return listCopy;
}
/*
==================
Sys_FreeFileList
==================
*/
void Sys_FreeFileList( char **list )
{
int i;
2005-08-26 17:39:27 +00:00
if ( !list ) {
return;
}
for ( i = 0 ; list[i] ; i++ ) {
Z_Free( list[i] );
}
Z_Free( list );
}
#ifdef MACOS_X
/*
=================
Sys_StripAppBundle
2005-08-26 17:39:27 +00:00
Discovers if passed dir is suffixed with the directory structure of a Mac OS X
.app bundle. If it is, the .app directory structure is stripped off the end and
the result is returned. If not, dir is returned untouched.
=================
*/
char *Sys_StripAppBundle( char *dir )
2005-08-26 17:39:27 +00:00
{
static char cwd[MAX_OSPATH];
2005-08-26 17:39:27 +00:00
Q_strncpyz(cwd, dir, sizeof(cwd));
if(strcmp(Sys_Basename(cwd), "MacOS"))
return dir;
Q_strncpyz(cwd, Sys_Dirname(cwd), sizeof(cwd));
if(strcmp(Sys_Basename(cwd), "Contents"))
return dir;
Q_strncpyz(cwd, Sys_Dirname(cwd), sizeof(cwd));
if(!strstr(Sys_Basename(cwd), ".app"))
return dir;
Q_strncpyz(cwd, Sys_Dirname(cwd), sizeof(cwd));
return cwd;
2005-08-26 17:39:27 +00:00
}
#endif // MACOS_X
/*
==================
Sys_Sleep
Block execution for msec or until input is recieved.
==================
*/
void Sys_Sleep( int msec )
{
fd_set fdset;
if( msec == 0 )
return;
FD_ZERO(&fdset);
FD_SET(fileno(stdin), &fdset);
if( msec < 0 )
{
select((fileno(stdin) + 1), &fdset, NULL, NULL, NULL);
}
else
{
struct timeval timeout;
timeout.tv_sec = msec/1000;
timeout.tv_usec = (msec%1000)*1000;
select((fileno(stdin) + 1), &fdset, NULL, NULL, &timeout);
}
}
/*
==============
Sys_ErrorDialog
Display an error message
==============
*/
void Sys_ErrorDialog( const char *error )
{
char buffer[ 1024 ];
unsigned int size;
fileHandle_t f;
const char *fileName = "crashlog.txt";
Sys_Print( va( "%s\n", error ) );
// Write console log to file
f = FS_FOpenFileWrite( fileName );
if( !f )
{
Com_Printf( "ERROR: couldn't open %s\n", fileName );
return;
}
while( ( size = CON_LogRead( buffer, sizeof( buffer ) ) ) > 0 )
FS_Write( buffer, size, f );
FS_FCloseFile( f );
}
/*
==============
Sys_GLimpInit
Unix specific GL implementation initialisation
==============
*/
void Sys_GLimpInit( void )
{
// NOP
}
/*
==============
Sys_PlatformInit
Unix specific initialisation
==============
*/
void Sys_PlatformInit( void )
{
signal( SIGHUP, Sys_SigHandler );
signal( SIGQUIT, Sys_SigHandler );
signal( SIGTRAP, Sys_SigHandler );
signal( SIGIOT, Sys_SigHandler );
signal( SIGBUS, Sys_SigHandler );
}