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
|
2005-10-29 01:53:09 +00:00
|
|
|
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
|
|
|
|
===========================================================================
|
|
|
|
*/
|
2007-11-30 18:32:52 +00:00
|
|
|
|
|
|
|
#include "../qcommon/q_shared.h"
|
|
|
|
#include "../qcommon/qcommon.h"
|
|
|
|
#include "sys_local.h"
|
|
|
|
|
2008-08-08 21:35:33 +00:00
|
|
|
#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>
|
2007-09-05 18:17:46 +00:00
|
|
|
#include <libgen.h>
|
2009-09-15 00:19:22 +00:00
|
|
|
#include <fcntl.h>
|
2011-06-13 09:56:39 +00:00
|
|
|
#include <fenv.h>
|
2011-07-24 22:01:50 +00:00
|
|
|
#include <sys/wait.h>
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2009-12-24 00:40:39 +00:00
|
|
|
qboolean stdinIsATTY;
|
|
|
|
|
2007-09-05 18:17:46 +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
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_DefaultHomePath
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
char *Sys_DefaultHomePath(void)
|
|
|
|
{
|
|
|
|
char *p;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2012-07-07 17:32:19 +00:00
|
|
|
if( !*homePath && com_homepath != NULL )
|
2007-09-05 18:17:46 +00:00
|
|
|
{
|
|
|
|
if( ( p = getenv( "HOME" ) ) != NULL )
|
|
|
|
{
|
2011-02-04 12:04:56 +00:00
|
|
|
Com_sprintf(homePath, sizeof(homePath), "%s%c", p, PATH_SEP);
|
2009-09-15 09:48:11 +00:00
|
|
|
#ifdef MACOS_X
|
2011-02-04 12:04:56 +00:00
|
|
|
Q_strcat(homePath, sizeof(homePath),
|
|
|
|
"Library/Application Support/");
|
|
|
|
|
|
|
|
if(com_homepath->string[0])
|
|
|
|
Q_strcat(homePath, sizeof(homePath), com_homepath->string);
|
|
|
|
else
|
2011-05-04 15:39:35 +00:00
|
|
|
Q_strcat(homePath, sizeof(homePath), HOMEPATH_NAME_MACOSX);
|
2007-09-05 18:17:46 +00:00
|
|
|
#else
|
2011-02-04 12:04:56 +00:00
|
|
|
if(com_homepath->string[0])
|
|
|
|
Q_strcat(homePath, sizeof(homePath), com_homepath->string);
|
|
|
|
else
|
2011-05-04 15:39:35 +00:00
|
|
|
Q_strcat(homePath, sizeof(homePath), HOMEPATH_NAME_UNIX);
|
2007-09-05 18:17:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
return homePath;
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
Sys_Milliseconds
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
/* base time in seconds, that's our origin
|
2007-09-05 18:17:46 +00:00
|
|
|
timeval:tv_sec is an int:
|
2010-12-19 17:45:03 +00:00
|
|
|
assuming this wraps every 0x7fffffff - ~68 years since the Epoch (1970) - we're safe till 2038 */
|
2005-08-26 17:39:27 +00:00
|
|
|
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);
|
2007-09-05 18:17:46 +00:00
|
|
|
|
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;
|
2007-09-05 18:17:46 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
return curtime;
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_RandomBytes
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean Sys_RandomBytes( byte *string, int len )
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen( "/dev/urandom", "r" );
|
|
|
|
if( !fp )
|
|
|
|
return qfalse;
|
|
|
|
|
2015-09-07 10:32:43 +00:00
|
|
|
setvbuf( fp, NULL, _IONBF, 0 ); // don't buffer reads from /dev/urandom
|
|
|
|
|
2013-04-14 16:33:25 +00:00
|
|
|
if( fread( string, sizeof( byte ), len, fp ) != len )
|
2005-08-26 17:39:27 +00:00
|
|
|
{
|
2007-09-05 18:17:46 +00:00
|
|
|
fclose( fp );
|
|
|
|
return qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
fclose( fp );
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_GetCurrentUser
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
char *Sys_GetCurrentUser( void )
|
|
|
|
{
|
|
|
|
struct passwd *p;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
if ( (p = getpwuid( getuid() )) == NULL ) {
|
|
|
|
return "player";
|
|
|
|
}
|
|
|
|
return p->pw_name;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +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
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Basename
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
const char *Sys_Basename( char *path )
|
|
|
|
{
|
|
|
|
return basename( path );
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Dirname
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
const char *Sys_Dirname( char *path )
|
2005-08-26 17:39:27 +00:00
|
|
|
{
|
2007-09-05 18:17:46 +00:00
|
|
|
return dirname( path );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 03:08:47 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_FOpen
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
FILE *Sys_FOpen( const char *ospath, const char *mode ) {
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
// check if path exists and is a directory
|
|
|
|
if ( !stat( ospath, &buf ) && S_ISDIR( buf.st_mode ) )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return fopen( ospath, mode );
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Mkdir
|
|
|
|
==================
|
|
|
|
*/
|
2009-11-05 20:20:23 +00:00
|
|
|
qboolean Sys_Mkdir( const char *path )
|
2007-09-05 18:17:46 +00:00
|
|
|
{
|
2009-11-05 20:20:23 +00:00
|
|
|
int result = mkdir( path, 0750 );
|
|
|
|
|
|
|
|
if( result != 0 )
|
|
|
|
return errno == EEXIST;
|
|
|
|
|
|
|
|
return qtrue;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-10 01:01:27 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Mkfifo
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
FILE *Sys_Mkfifo( const char *ospath )
|
|
|
|
{
|
2011-03-30 21:58:31 +00:00
|
|
|
FILE *fifo;
|
|
|
|
int result;
|
|
|
|
int fn;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
// if file already exists AND is a pipefile, remove it
|
|
|
|
if( !stat( ospath, &buf ) && S_ISFIFO( buf.st_mode ) )
|
|
|
|
FS_Remove( ospath );
|
2011-03-10 01:01:27 +00:00
|
|
|
|
|
|
|
result = mkfifo( ospath, 0600 );
|
|
|
|
if( result != 0 )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fifo = fopen( ospath, "w+" );
|
|
|
|
if( fifo )
|
|
|
|
{
|
|
|
|
fn = fileno( fifo );
|
|
|
|
fcntl( fn, F_SETFL, O_NONBLOCK );
|
|
|
|
}
|
|
|
|
|
|
|
|
return fifo;
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Cwd
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
char *Sys_Cwd( void )
|
2007-02-16 23:50:37 +00:00
|
|
|
{
|
2007-09-05 18:17:46 +00:00
|
|
|
static char cwd[MAX_OSPATH];
|
2007-02-16 23:50:37 +00:00
|
|
|
|
2008-11-10 23:55:22 +00:00
|
|
|
char *result = getcwd( cwd, sizeof( cwd ) - 1 );
|
|
|
|
if( result != cwd )
|
|
|
|
return NULL;
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
cwd[MAX_OSPATH-1] = 0;
|
2007-02-16 23:50:37 +00:00
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
return cwd;
|
2007-02-16 23:50:37 +00:00
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==============================================================
|
|
|
|
|
|
|
|
DIRECTORY SCANNING
|
|
|
|
|
|
|
|
==============================================================
|
|
|
|
*/
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
#define MAX_FOUND_FILES 0x1000
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2007-09-05 18:17:46 +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;
|
2007-09-05 18:17:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
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;
|
2007-09-05 18:17:46 +00:00
|
|
|
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 );
|
2009-12-24 00:40:39 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
// 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) {
|
2011-07-29 12:27:00 +00:00
|
|
|
if ( strlen( d->d_name ) < extLen ||
|
2007-09-05 18:17:46 +00:00
|
|
|
Q_stricmp(
|
2011-07-29 12:27:00 +00:00
|
|
|
d->d_name + strlen( d->d_name ) - extLen,
|
2005-08-26 17:39:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:17:46 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:22:58 +00:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_Sleep
|
|
|
|
|
|
|
|
Block execution for msec or until input is recieved.
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Sys_Sleep( int msec )
|
|
|
|
{
|
2008-07-21 22:02:54 +00:00
|
|
|
if( msec == 0 )
|
|
|
|
return;
|
|
|
|
|
2009-12-24 00:40:39 +00:00
|
|
|
if( stdinIsATTY )
|
2007-09-15 02:22:58 +00:00
|
|
|
{
|
2009-12-24 00:40:39 +00:00
|
|
|
fd_set fdset;
|
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(STDIN_FILENO, &fdset);
|
|
|
|
if( msec < 0 )
|
|
|
|
{
|
|
|
|
select(STDIN_FILENO + 1, &fdset, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
timeout.tv_sec = msec/1000;
|
|
|
|
timeout.tv_usec = (msec%1000)*1000;
|
|
|
|
select(STDIN_FILENO + 1, &fdset, NULL, NULL, &timeout);
|
|
|
|
}
|
2007-09-15 02:22:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-24 00:40:39 +00:00
|
|
|
// With nothing to select() on, we can't wait indefinitely
|
|
|
|
if( msec < 0 )
|
|
|
|
msec = 10;
|
2007-09-15 02:22:58 +00:00
|
|
|
|
2009-12-24 00:40:39 +00:00
|
|
|
usleep( msec * 1000 );
|
2007-09-15 02:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-30 18:32:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_ErrorDialog
|
|
|
|
|
|
|
|
Display an error message
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Sys_ErrorDialog( const char *error )
|
|
|
|
{
|
|
|
|
char buffer[ 1024 ];
|
|
|
|
unsigned int size;
|
2009-09-15 00:19:22 +00:00
|
|
|
int f = -1;
|
|
|
|
const char *homepath = Cvar_VariableString( "fs_homepath" );
|
2011-01-30 13:34:54 +00:00
|
|
|
const char *gamedir = Cvar_VariableString( "fs_game" );
|
2007-11-30 18:32:52 +00:00
|
|
|
const char *fileName = "crashlog.txt";
|
2013-09-04 16:55:54 +00:00
|
|
|
char *dirpath = FS_BuildOSPath( homepath, gamedir, "");
|
2009-09-15 00:19:22 +00:00
|
|
|
char *ospath = FS_BuildOSPath( homepath, gamedir, fileName );
|
2007-11-30 18:32:52 +00:00
|
|
|
|
|
|
|
Sys_Print( va( "%s\n", error ) );
|
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
#ifndef DEDICATED
|
|
|
|
Sys_Dialog( DT_ERROR, va( "%s. See \"%s\" for details.", error, ospath ), "Error" );
|
2009-09-15 05:38:27 +00:00
|
|
|
#endif
|
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
// Make sure the write path for the crashlog exists...
|
2013-09-04 16:55:54 +00:00
|
|
|
|
|
|
|
if(!Sys_Mkdir(homepath))
|
|
|
|
{
|
|
|
|
Com_Printf("ERROR: couldn't create path '%s' for crash log.\n", homepath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Sys_Mkdir(dirpath))
|
2013-08-25 10:00:30 +00:00
|
|
|
{
|
2013-09-04 16:55:54 +00:00
|
|
|
Com_Printf("ERROR: couldn't create path '%s' for crash log.\n", dirpath);
|
2009-09-15 00:19:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
// We might be crashing because we maxed out the Quake MAX_FILE_HANDLES,
|
|
|
|
// which will come through here, so we don't want to recurse forever by
|
|
|
|
// calling FS_FOpenFileWrite()...use the Unix system APIs instead.
|
|
|
|
f = open( ospath, O_CREAT | O_TRUNC | O_WRONLY, 0640 );
|
2009-09-15 00:19:22 +00:00
|
|
|
if( f == -1 )
|
2007-11-30 18:32:52 +00:00
|
|
|
{
|
|
|
|
Com_Printf( "ERROR: couldn't open %s\n", fileName );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
// We're crashing, so we don't care much if write() or close() fails.
|
2009-09-15 00:19:22 +00:00
|
|
|
while( ( size = CON_LogRead( buffer, sizeof( buffer ) ) ) > 0 ) {
|
2010-02-15 16:20:33 +00:00
|
|
|
if( write( f, buffer, size ) != size ) {
|
2009-09-15 00:19:22 +00:00
|
|
|
Com_Printf( "ERROR: couldn't fully write to %s\n", fileName );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-30 18:32:52 +00:00
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
close( f );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MACOS_X
|
2011-07-24 22:01:50 +00:00
|
|
|
static char execBuffer[ 1024 ];
|
|
|
|
static char *execBufferPointer;
|
|
|
|
static char *execArgv[ 16 ];
|
|
|
|
static int execArgc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_ClearExecBuffer
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static void Sys_ClearExecBuffer( void )
|
|
|
|
{
|
|
|
|
execBufferPointer = execBuffer;
|
|
|
|
Com_Memset( execArgv, 0, sizeof( execArgv ) );
|
|
|
|
execArgc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_AppendToExecBuffer
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static void Sys_AppendToExecBuffer( const char *text )
|
|
|
|
{
|
|
|
|
size_t size = sizeof( execBuffer ) - ( execBufferPointer - execBuffer );
|
|
|
|
int length = strlen( text ) + 1;
|
|
|
|
|
|
|
|
if( length > size || execArgc >= ARRAY_LEN( execArgv ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Q_strncpyz( execBufferPointer, text, size );
|
|
|
|
execArgv[ execArgc++ ] = execBufferPointer;
|
|
|
|
|
|
|
|
execBufferPointer += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_Exec
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static int Sys_Exec( void )
|
|
|
|
{
|
|
|
|
pid_t pid = fork( );
|
|
|
|
|
|
|
|
if( pid < 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( pid )
|
|
|
|
{
|
|
|
|
// Parent
|
|
|
|
int exitCode;
|
|
|
|
|
|
|
|
wait( &exitCode );
|
|
|
|
|
|
|
|
return WEXITSTATUS( exitCode );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Child
|
|
|
|
execvp( execArgv[ 0 ], execArgv );
|
|
|
|
|
|
|
|
// Failed to execute
|
|
|
|
exit( -1 );
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-15 16:20:33 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_ZenityCommand
|
|
|
|
==============
|
|
|
|
*/
|
2011-07-24 22:01:50 +00:00
|
|
|
static void Sys_ZenityCommand( dialogType_t type, const char *message, const char *title )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_ClearExecBuffer( );
|
|
|
|
Sys_AppendToExecBuffer( "zenity" );
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
default:
|
2011-07-24 22:01:50 +00:00
|
|
|
case DT_INFO: Sys_AppendToExecBuffer( "--info" ); break;
|
|
|
|
case DT_WARNING: Sys_AppendToExecBuffer( "--warning" ); break;
|
|
|
|
case DT_ERROR: Sys_AppendToExecBuffer( "--error" ); break;
|
|
|
|
case DT_YES_NO:
|
|
|
|
Sys_AppendToExecBuffer( "--question" );
|
|
|
|
Sys_AppendToExecBuffer( "--ok-label=Yes" );
|
|
|
|
Sys_AppendToExecBuffer( "--cancel-label=No" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DT_OK_CANCEL:
|
|
|
|
Sys_AppendToExecBuffer( "--question" );
|
|
|
|
Sys_AppendToExecBuffer( "--ok-label=OK" );
|
|
|
|
Sys_AppendToExecBuffer( "--cancel-label=Cancel" );
|
|
|
|
break;
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_AppendToExecBuffer( va( "--text=%s", message ) );
|
|
|
|
Sys_AppendToExecBuffer( va( "--title=%s", title ) );
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_KdialogCommand
|
|
|
|
==============
|
|
|
|
*/
|
2011-07-24 22:01:50 +00:00
|
|
|
static void Sys_KdialogCommand( dialogType_t type, const char *message, const char *title )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_ClearExecBuffer( );
|
|
|
|
Sys_AppendToExecBuffer( "kdialog" );
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
default:
|
2011-07-24 22:01:50 +00:00
|
|
|
case DT_INFO: Sys_AppendToExecBuffer( "--msgbox" ); break;
|
|
|
|
case DT_WARNING: Sys_AppendToExecBuffer( "--sorry" ); break;
|
|
|
|
case DT_ERROR: Sys_AppendToExecBuffer( "--error" ); break;
|
|
|
|
case DT_YES_NO: Sys_AppendToExecBuffer( "--warningyesno" ); break;
|
|
|
|
case DT_OK_CANCEL: Sys_AppendToExecBuffer( "--warningcontinuecancel" ); break;
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_AppendToExecBuffer( message );
|
|
|
|
Sys_AppendToExecBuffer( va( "--title=%s", title ) );
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_XmessageCommand
|
|
|
|
==============
|
|
|
|
*/
|
2011-07-24 22:01:50 +00:00
|
|
|
static void Sys_XmessageCommand( dialogType_t type, const char *message, const char *title )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_ClearExecBuffer( );
|
|
|
|
Sys_AppendToExecBuffer( "xmessage" );
|
|
|
|
Sys_AppendToExecBuffer( "-buttons" );
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
switch( type )
|
|
|
|
{
|
2011-07-24 22:01:50 +00:00
|
|
|
default: Sys_AppendToExecBuffer( "OK:0" ); break;
|
|
|
|
case DT_YES_NO: Sys_AppendToExecBuffer( "Yes:0,No:1" ); break;
|
|
|
|
case DT_OK_CANCEL: Sys_AppendToExecBuffer( "OK:0,Cancel:1" ); break;
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:01:50 +00:00
|
|
|
Sys_AppendToExecBuffer( "-center" );
|
|
|
|
Sys_AppendToExecBuffer( message );
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_Dialog
|
|
|
|
|
|
|
|
Display a *nix dialog box
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
dialogResult_t Sys_Dialog( dialogType_t type, const char *message, const char *title )
|
|
|
|
{
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
ZENITY,
|
|
|
|
KDIALOG,
|
|
|
|
XMESSAGE,
|
|
|
|
NUM_DIALOG_PROGRAMS
|
|
|
|
} dialogCommandType_t;
|
2011-07-24 22:01:50 +00:00
|
|
|
typedef void (*dialogCommandBuilder_t)( dialogType_t, const char *, const char * );
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
const char *session = getenv( "DESKTOP_SESSION" );
|
|
|
|
qboolean tried[ NUM_DIALOG_PROGRAMS ] = { qfalse };
|
|
|
|
dialogCommandBuilder_t commands[ NUM_DIALOG_PROGRAMS ] = { NULL };
|
|
|
|
dialogCommandType_t preferredCommandType = NONE;
|
2014-08-30 16:29:09 +00:00
|
|
|
int i;
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
commands[ ZENITY ] = &Sys_ZenityCommand;
|
|
|
|
commands[ KDIALOG ] = &Sys_KdialogCommand;
|
|
|
|
commands[ XMESSAGE ] = &Sys_XmessageCommand;
|
|
|
|
|
|
|
|
// This may not be the best way
|
|
|
|
if( !Q_stricmp( session, "gnome" ) )
|
|
|
|
preferredCommandType = ZENITY;
|
|
|
|
else if( !Q_stricmp( session, "kde" ) )
|
|
|
|
preferredCommandType = KDIALOG;
|
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
for( i = NONE + 1; i < NUM_DIALOG_PROGRAMS; i++ )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2014-08-30 16:29:09 +00:00
|
|
|
if( preferredCommandType != NONE && preferredCommandType != i )
|
|
|
|
continue;
|
2010-02-15 16:20:33 +00:00
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
if( !tried[ i ] )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2014-08-30 16:29:09 +00:00
|
|
|
int exitCode;
|
2011-07-18 19:32:25 +00:00
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
commands[ i ]( type, message, title );
|
|
|
|
exitCode = Sys_Exec( );
|
2010-02-15 16:20:33 +00:00
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
if( exitCode >= 0 )
|
|
|
|
{
|
|
|
|
switch( type )
|
2010-02-15 16:20:33 +00:00
|
|
|
{
|
2014-08-30 16:29:09 +00:00
|
|
|
case DT_YES_NO: return exitCode ? DR_NO : DR_YES;
|
|
|
|
case DT_OK_CANCEL: return exitCode ? DR_CANCEL : DR_OK;
|
|
|
|
default: return DR_OK;
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
2014-08-30 16:29:09 +00:00
|
|
|
}
|
2010-02-15 16:20:33 +00:00
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
tried[ i ] = qtrue;
|
2010-02-15 16:20:33 +00:00
|
|
|
|
2014-08-30 16:29:09 +00:00
|
|
|
// The preference failed, so start again in order
|
|
|
|
if( preferredCommandType != NONE )
|
|
|
|
{
|
|
|
|
preferredCommandType = NONE;
|
|
|
|
i = NONE + 1;
|
2010-02-15 16:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Com_DPrintf( S_COLOR_YELLOW "WARNING: failed to show a dialog\n" );
|
|
|
|
return DR_OK;
|
2007-11-30 18:32:52 +00:00
|
|
|
}
|
2010-02-15 16:20:33 +00:00
|
|
|
#endif
|
2008-08-03 19:42:53 +00:00
|
|
|
|
2009-09-14 12:34:31 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_GLimpSafeInit
|
|
|
|
|
|
|
|
Unix specific "safe" GL implementation initialisation
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Sys_GLimpSafeInit( void )
|
|
|
|
{
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2008-08-08 21:35:33 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_GLimpInit
|
|
|
|
|
|
|
|
Unix specific GL implementation initialisation
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Sys_GLimpInit( void )
|
|
|
|
{
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2011-06-13 09:56:39 +00:00
|
|
|
void Sys_SetFloatEnv(void)
|
|
|
|
{
|
2011-09-27 14:43:20 +00:00
|
|
|
// rounding toward nearest
|
|
|
|
fesetround(FE_TONEAREST);
|
2011-06-13 09:56:39 +00:00
|
|
|
}
|
|
|
|
|
2008-08-03 19:42:53 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_PlatformInit
|
|
|
|
|
|
|
|
Unix specific initialisation
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Sys_PlatformInit( void )
|
|
|
|
{
|
2009-12-24 00:40:39 +00:00
|
|
|
const char* term = getenv( "TERM" );
|
|
|
|
|
2008-08-08 21:35:33 +00:00
|
|
|
signal( SIGHUP, Sys_SigHandler );
|
|
|
|
signal( SIGQUIT, Sys_SigHandler );
|
|
|
|
signal( SIGTRAP, Sys_SigHandler );
|
2015-01-18 17:41:06 +00:00
|
|
|
signal( SIGABRT, Sys_SigHandler );
|
2008-08-08 21:35:33 +00:00
|
|
|
signal( SIGBUS, Sys_SigHandler );
|
2009-12-24 00:40:39 +00:00
|
|
|
|
2015-01-12 22:31:48 +00:00
|
|
|
Sys_SetFloatEnv();
|
|
|
|
|
2009-12-24 00:40:39 +00:00
|
|
|
stdinIsATTY = isatty( STDIN_FILENO ) &&
|
|
|
|
!( term && ( !strcmp( term, "raw" ) || !strcmp( term, "dumb" ) ) );
|
2008-08-03 19:42:53 +00:00
|
|
|
}
|
2009-11-03 20:29:43 +00:00
|
|
|
|
2011-01-31 20:23:05 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_PlatformExit
|
|
|
|
|
|
|
|
Unix specific deinitialisation
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Sys_PlatformExit( void )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-03 20:29:43 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_SetEnv
|
|
|
|
|
|
|
|
set/unset environment variables (empty value removes it)
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Sys_SetEnv(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
if(value && *value)
|
|
|
|
setenv(name, value, 1);
|
|
|
|
else
|
|
|
|
unsetenv(name);
|
|
|
|
}
|
2010-02-15 16:20:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_PID
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int Sys_PID( void )
|
|
|
|
{
|
|
|
|
return getpid( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Sys_PIDIsRunning
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
qboolean Sys_PIDIsRunning( int pid )
|
|
|
|
{
|
|
|
|
return kill( pid, 0 ) == 0;
|
|
|
|
}
|