summaryrefslogtreecommitdiff
path: root/src/sys_unix.c
diff options
context:
space:
mode:
authorAndrey Nazarov <skuller@skuller.net>2011-12-20 14:37:44 +0400
committerAndrey Nazarov <skuller@skuller.net>2012-04-03 01:25:47 +0400
commitb3e271a53de2610de173f9b2bda5d15dd4d78909 (patch)
treed567b91dadac42e168bcb13db5fe93ffbc7e1533 /src/sys_unix.c
parentca4a019a74b11aa21fea2a03c6dff3b183463aab (diff)
Massive coding style change.
Use linux style brackets. Pad operators with spaces. Unpad parenthesis, except for ‘if’, ‘for’, ‘while’ constructs.
Diffstat (limited to 'src/sys_unix.c')
-rw-r--r--src/sys_unix.c600
1 files changed, 316 insertions, 284 deletions
diff --git a/src/sys_unix.c b/src/sys_unix.c
index 4f80593..d1d7dee 100644
--- a/src/sys_unix.c
+++ b/src/sys_unix.c
@@ -123,7 +123,7 @@ static void stdout_write(const void *buf, size_t len)
tty_enabled = qfalse;
Com_Error(ERR_FATAL, "%s: %s() failed: %s",
- __func__, what, strerror(errno));
+ __func__, what, strerror(errno));
}
buf += ret;
@@ -131,55 +131,58 @@ static void stdout_write(const void *buf, size_t len)
}
}
-static void tty_hide_input( void ) {
+static void tty_hide_input(void)
+{
int i;
- if( !tty_hidden ) {
- for( i = 0; i <= tty_prompt.inputLine.cursorPos; i++ ) {
- stdout_write( "\b \b", 3 );
+ if (!tty_hidden) {
+ for (i = 0; i <= tty_prompt.inputLine.cursorPos; i++) {
+ stdout_write("\b \b", 3);
}
}
tty_hidden++;
}
-static void tty_show_input( void ) {
- if( !tty_hidden ) {
+static void tty_show_input(void)
+{
+ if (!tty_hidden) {
return;
}
tty_hidden--;
- if( !tty_hidden ) {
- stdout_write( "]", 1 );
- stdout_write( tty_prompt.inputLine.text,
- tty_prompt.inputLine.cursorPos );
+ if (!tty_hidden) {
+ stdout_write("]", 1);
+ stdout_write(tty_prompt.inputLine.text,
+ tty_prompt.inputLine.cursorPos);
}
}
-static void tty_init_input( void ) {
+static void tty_init_input(void)
+{
struct termios tty;
#ifdef TIOCGWINSZ
struct winsize ws;
#endif
int width;
- if( !isatty( STDIN_FILENO ) || !isatty( STDOUT_FILENO ) ) {
- Com_Printf( "stdin/stdout don't both refer to a TTY\n" );
- Cvar_Set( "sys_console", "1" );
+ if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
+ Com_Printf("stdin/stdout don't both refer to a TTY\n");
+ Cvar_Set("sys_console", "1");
return;
}
- tcgetattr( STDIN_FILENO, &tty_orig );
+ tcgetattr(STDIN_FILENO, &tty_orig);
tty = tty_orig;
- tty.c_lflag &= ~( ECHO | ICANON | INPCK | ISTRIP );
+ tty.c_lflag &= ~(ECHO | ICANON | INPCK | ISTRIP);
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
- tcsetattr( STDIN_FILENO, TCSADRAIN, &tty );
+ tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
// determine terminal width
width = 80;
#ifdef TIOCGWINSZ
- if( ioctl( STDIN_FILENO, TIOCGWINSZ, &ws ) == 0 ) {
- if( ws.ws_col ) {
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) {
+ if (ws.ws_col) {
width = ws.ws_col;
}
}
@@ -190,44 +193,46 @@ static void tty_init_input( void ) {
// figure out input line width
width--;
- if( width > MAX_FIELD_TEXT - 1 ) {
+ if (width > MAX_FIELD_TEXT - 1) {
width = MAX_FIELD_TEXT - 1;
}
- IF_Init( &tty_prompt.inputLine, width, width );
+ IF_Init(&tty_prompt.inputLine, width, width);
// display command prompt
- stdout_write( "]", 1 );
+ stdout_write("]", 1);
}
-static void tty_shutdown_input( void ) {
- if( tty_io ) {
- IO_Remove( STDIN_FILENO );
+static void tty_shutdown_input(void)
+{
+ if (tty_io) {
+ IO_Remove(STDIN_FILENO);
tty_io = NULL;
}
- if( tty_enabled ) {
+ if (tty_enabled) {
tty_hide_input();
- tcsetattr( STDIN_FILENO, TCSADRAIN, &tty_orig );
+ tcsetattr(STDIN_FILENO, TCSADRAIN, &tty_orig);
tty_enabled = qfalse;
}
}
-void Sys_SetConsoleColor( color_index_t color ) {
+void Sys_SetConsoleColor(color_index_t color)
+{
static const char color_to_ansi[8] =
- { '0', '1', '2', '3', '4', '6', '5', '7' };
+ { '0', '1', '2', '3', '4', '6', '5', '7' };
char buf[5];
size_t len;
- if( !sys_console || !sys_console->integer ) {
+ if (!sys_console || !sys_console->integer) {
return;
}
- if( !tty_enabled ) {
+ if (!tty_enabled) {
return;
}
buf[0] = '\033';
buf[1] = '[';
- switch( color ) {
+ switch (color) {
case COLOR_NONE:
buf[2] = '0';
buf[3] = 'm';
@@ -247,28 +252,29 @@ void Sys_SetConsoleColor( color_index_t color ) {
break;
}
- if( color != COLOR_NONE ) {
+ if (color != COLOR_NONE) {
tty_hide_input();
}
- stdout_write( buf, len );
- if( color == COLOR_NONE ) {
+ stdout_write(buf, len);
+ if (color == COLOR_NONE) {
tty_show_input();
}
}
-static void tty_write_output( const char *text ) {
+static void tty_write_output(const char *text)
+{
char buf[MAXPRINTMSG];
size_t len;
- for( len = 0; len < MAXPRINTMSG; len++ ) {
+ for (len = 0; len < MAXPRINTMSG; len++) {
int c = *text++;
- if( !c ) {
+ if (!c) {
break;
}
- buf[len] = Q_charascii( c );
+ buf[len] = Q_charascii(c);
}
- stdout_write( buf, len );
+ stdout_write(buf, len);
}
/*
@@ -276,29 +282,31 @@ static void tty_write_output( const char *text ) {
Sys_ConsoleOutput
=================
*/
-void Sys_ConsoleOutput( const char *text ) {
- if( !sys_console || !sys_console->integer ) {
+void Sys_ConsoleOutput(const char *text)
+{
+ if (!sys_console || !sys_console->integer) {
return;
}
- if( !tty_enabled ) {
- tty_write_output( text );
+ if (!tty_enabled) {
+ tty_write_output(text);
} else {
tty_hide_input();
- tty_write_output( text );
+ tty_write_output(text);
tty_show_input();
}
}
-void Sys_SetConsoleTitle( const char *title ) {
+void Sys_SetConsoleTitle(const char *title)
+{
char buf[MAX_STRING_CHARS];
size_t len;
- if( !sys_console || !sys_console->integer ) {
+ if (!sys_console || !sys_console->integer) {
return;
}
- if( !tty_enabled ) {
+ if (!tty_enabled) {
return;
}
@@ -307,108 +315,109 @@ void Sys_SetConsoleTitle( const char *title ) {
buf[2] = '0';
buf[3] = ';';
- for( len = 4; len < MAX_STRING_CHARS - 1; len++ ) {
+ for (len = 4; len < MAX_STRING_CHARS - 1; len++) {
int c = *title++;
- if( !c ) {
+ if (!c) {
break;
}
- buf[len] = Q_charascii( c );
+ buf[len] = Q_charascii(c);
}
buf[len++] = '\007';
- stdout_write( buf, len );
+ stdout_write(buf, len);
}
-static void tty_parse_input( const char *text ) {
+static void tty_parse_input(const char *text)
+{
inputField_t *f;
char *s;
int i, key;
f = &tty_prompt.inputLine;
- while( *text ) {
+ while (*text) {
key = *text++;
- if( key == tty_orig.c_cc[VERASE] || key == 127 || key == 8 ) {
- if( f->cursorPos ) {
+ if (key == tty_orig.c_cc[VERASE] || key == 127 || key == 8) {
+ if (f->cursorPos) {
f->text[--f->cursorPos] = 0;
- stdout_write( "\b \b", 3 );
+ stdout_write("\b \b", 3);
}
continue;
}
- if( key == tty_orig.c_cc[VKILL] ) {
- for( i = 0; i < f->cursorPos; i++ ) {
- stdout_write( "\b \b", 3 );
+ if (key == tty_orig.c_cc[VKILL]) {
+ for (i = 0; i < f->cursorPos; i++) {
+ stdout_write("\b \b", 3);
}
f->cursorPos = 0;
continue;
}
- if( key >= 32 ) {
- if( f->cursorPos == f->maxChars - 1 ) {
- stdout_write( va( "\b \b%c", key ), 4 );
- f->text[f->cursorPos+0] = key;
- f->text[f->cursorPos+1] = 0;
+ if (key >= 32) {
+ if (f->cursorPos == f->maxChars - 1) {
+ stdout_write(va("\b \b%c", key), 4);
+ f->text[f->cursorPos + 0] = key;
+ f->text[f->cursorPos + 1] = 0;
} else {
- stdout_write( va( "%c", key ), 1 );
- f->text[f->cursorPos+0] = key;
- f->text[f->cursorPos+1] = 0;
+ stdout_write(va("%c", key), 1);
+ f->text[f->cursorPos + 0] = key;
+ f->text[f->cursorPos + 1] = 0;
f->cursorPos++;
}
continue;
}
- if( key == '\n' ) {
+ if (key == '\n') {
tty_hide_input();
- s = Prompt_Action( &tty_prompt );
- if( s ) {
- if( *s == '\\' || *s == '/' ) {
+ s = Prompt_Action(&tty_prompt);
+ if (s) {
+ if (*s == '\\' || *s == '/') {
s++;
}
- Sys_Printf( "]%s\n", s );
- Cbuf_AddText( &cmd_buffer, s );
+ Sys_Printf("]%s\n", s);
+ Cbuf_AddText(&cmd_buffer, s);
} else {
- stdout_write( "]\n", 2 );
+ stdout_write("]\n", 2);
}
tty_show_input();
continue;
}
- if( key == '\t' ) {
+ if (key == '\t') {
tty_hide_input();
- Prompt_CompleteCommand( &tty_prompt, qfalse );
- f->cursorPos = strlen( f->text ); // FIXME
+ Prompt_CompleteCommand(&tty_prompt, qfalse);
+ f->cursorPos = strlen(f->text); // FIXME
tty_show_input();
continue;
}
- if( *text ) {
+ if (*text) {
key = *text++;
- if( key == '[' || key == 'O' ) {
- if( *text ) {
+ if (key == '[' || key == 'O') {
+ if (*text) {
key = *text++;
- switch( key ) {
+ switch (key) {
case 'A':
tty_hide_input();
- Prompt_HistoryUp( &tty_prompt );
+ Prompt_HistoryUp(&tty_prompt);
tty_show_input();
break;
case 'B':
tty_hide_input();
- Prompt_HistoryDown( &tty_prompt );
+ Prompt_HistoryDown(&tty_prompt);
tty_show_input();
break;
#if 0
case 'C':
- if( f->text[f->cursorPos] ) {
- Sys_ConsoleWrite( "\033[C", 3 );
+ if (f->text[f->cursorPos]) {
+ Sys_ConsoleWrite("\033[C", 3);
f->cursorPos++;
}
break;
case 'D':
- if( f->cursorPos ) {
- Sys_ConsoleWrite( "\033[D", 3 );
+ if (f->cursorPos) {
+ Sys_ConsoleWrite("\033[D", 3);
f->cursorPos--;
}
break;
@@ -420,45 +429,46 @@ static void tty_parse_input( const char *text ) {
}
}
-void Sys_RunConsole( void ) {
+void Sys_RunConsole(void)
+{
char text[MAX_STRING_CHARS];
ssize_t ret;
- if( !sys_console || !sys_console->integer ) {
+ if (!sys_console || !sys_console->integer) {
return;
}
- if( !tty_io || !tty_io->canread ) {
+ if (!tty_io || !tty_io->canread) {
return;
}
- ret = read( STDIN_FILENO, text, sizeof( text ) - 1 );
- if( !ret ) {
- Com_DPrintf( "Read EOF from stdin.\n" );
+ ret = read(STDIN_FILENO, text, sizeof(text) - 1);
+ if (!ret) {
+ Com_DPrintf("Read EOF from stdin.\n");
tty_shutdown_input();
- Cvar_Set( "sys_console", "0" );
+ Cvar_Set("sys_console", "0");
return;
}
// make sure the next call will not block
tty_io->canread = qfalse;
- if( ret < 0 ) {
- if( errno == EAGAIN || errno == EINTR ) {
+ if (ret < 0) {
+ if (errno == EAGAIN || errno == EINTR) {
return;
}
- Com_Error( ERR_FATAL, "%s: read() failed: %s",
- __func__, strerror( errno ) );
+ Com_Error(ERR_FATAL, "%s: read() failed: %s",
+ __func__, strerror(errno));
}
text[ret] = 0;
- if( !tty_enabled ) {
- Cbuf_AddText( &cmd_buffer, text );
+ if (!tty_enabled) {
+ Cbuf_AddText(&cmd_buffer, text);
return;
}
- tty_parse_input( text );
+ tty_parse_input(text);
}
#endif // USE_SYSCON
@@ -471,67 +481,71 @@ HUNK
===============================================================================
*/
-void Hunk_Begin( mempool_t *pool, size_t maxsize ) {
+void Hunk_Begin(mempool_t *pool, size_t maxsize)
+{
void *buf;
// reserve a huge chunk of memory, but don't commit any yet
- pool->maxsize = ( maxsize + 4095 ) & ~4095;
+ pool->maxsize = (maxsize + 4095) & ~4095;
pool->cursize = 0;
- buf = mmap( NULL, pool->maxsize, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_ANON, -1, 0 );
- if( buf == NULL || buf == ( void * )-1 ) {
- Com_Error( ERR_FATAL, "%s: unable to reserve %"PRIz" bytes: %s",
- __func__, pool->maxsize, strerror( errno ) );
+ buf = mmap(NULL, pool->maxsize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (buf == NULL || buf == (void *) - 1) {
+ Com_Error(ERR_FATAL, "%s: unable to reserve %"PRIz" bytes: %s",
+ __func__, pool->maxsize, strerror(errno));
}
pool->base = buf;
pool->mapped = pool->maxsize;
}
-void *Hunk_Alloc( mempool_t *pool, size_t size ) {
+void *Hunk_Alloc(mempool_t *pool, size_t size)
+{
void *buf;
// round to cacheline
- size = ( size + 63 ) & ~63;
- if( pool->cursize + size > pool->maxsize ) {
- Com_Error( ERR_FATAL, "%s: unable to allocate %"PRIz" bytes out of %"PRIz,
- __func__, size, pool->maxsize );
+ size = (size + 63) & ~63;
+ if (pool->cursize + size > pool->maxsize) {
+ Com_Error(ERR_FATAL, "%s: unable to allocate %"PRIz" bytes out of %"PRIz,
+ __func__, size, pool->maxsize);
}
- buf = ( byte * )pool->base + pool->cursize;
+ buf = (byte *)pool->base + pool->cursize;
pool->cursize += size;
return buf;
}
-void Hunk_End( mempool_t *pool ) {
- size_t newsize = ( pool->cursize + 4095 ) & ~4095;
+void Hunk_End(mempool_t *pool)
+{
+ size_t newsize = (pool->cursize + 4095) & ~4095;
- if( newsize > pool->maxsize ) {
- Com_Error( ERR_FATAL, "%s: newsize > maxsize", __func__ );
+ if (newsize > pool->maxsize) {
+ Com_Error(ERR_FATAL, "%s: newsize > maxsize", __func__);
}
- if( newsize < pool->maxsize ) {
+ if (newsize < pool->maxsize) {
#ifdef _GNU_SOURCE
- void *buf = mremap( pool->base, pool->maxsize, newsize, 0 );
+ void *buf = mremap(pool->base, pool->maxsize, newsize, 0);
#else
- void *unmap_base = ( byte * )pool->base + newsize;
+ void *unmap_base = (byte *)pool->base + newsize;
size_t unmap_len = pool->maxsize - newsize;
- void *buf = munmap( unmap_base, unmap_len ) + pool->base;
+ void *buf = munmap(unmap_base, unmap_len) + pool->base;
#endif
- if( buf != pool->base ) {
- Com_Error( ERR_FATAL, "%s: could not remap virtual block: %s",
- __func__, strerror( errno ) );
+ if (buf != pool->base) {
+ Com_Error(ERR_FATAL, "%s: could not remap virtual block: %s",
+ __func__, strerror(errno));
}
}
pool->mapped = newsize;
}
-void Hunk_Free( mempool_t *pool ) {
- if( pool->base ) {
- if( munmap( pool->base, pool->mapped ) ) {
- Com_Error( ERR_FATAL, "%s: munmap failed: %s",
- __func__, strerror( errno ) );
+void Hunk_Free(mempool_t *pool)
+{
+ if (pool->base) {
+ if (munmap(pool->base, pool->mapped)) {
+ Com_Error(ERR_FATAL, "%s: munmap failed: %s",
+ __func__, strerror(errno));
}
}
- memset( pool, 0, sizeof( *pool ) );
+ memset(pool, 0, sizeof(*pool));
}
/*
@@ -542,15 +556,17 @@ GENERAL ROUTINES
===============================================================================
*/
-void Sys_DebugBreak( void ) {
- raise( SIGTRAP );
+void Sys_DebugBreak(void)
+{
+ raise(SIGTRAP);
}
-unsigned Sys_Milliseconds( void ) {
+unsigned Sys_Milliseconds(void)
+{
struct timeval tp;
unsigned time;
- gettimeofday( &tp, NULL );
+ gettimeofday(&tp, NULL);
time = tp.tv_sec * 1000 + tp.tv_usec / 1000;
return time;
}
@@ -560,18 +576,19 @@ unsigned Sys_Milliseconds( void ) {
Sys_GetPathInfo
================
*/
-qerror_t Sys_GetPathInfo( const char *path, file_info_t *info ) {
+qerror_t Sys_GetPathInfo(const char *path, file_info_t *info)
+{
struct stat st;
- if( stat( path, &st ) == -1 ) {
+ if (stat(path, &st) == -1) {
return Q_ERR(errno);
}
- if( !S_ISREG( st.st_mode ) ) {
+ if (!S_ISREG(st.st_mode)) {
return Q_ERR_ISDIR;
}
- if( info ) {
+ if (info) {
info->size = st.st_size;
info->ctime = st.st_ctime;
info->mtime = st.st_mtime;
@@ -580,18 +597,19 @@ qerror_t Sys_GetPathInfo( const char *path, file_info_t *info ) {
return Q_ERR_SUCCESS;
}
-qerror_t Sys_GetFileInfo( FILE *fp, file_info_t *info ) {
+qerror_t Sys_GetFileInfo(FILE *fp, file_info_t *info)
+{
struct stat st;
- if( fstat( fileno( fp ), &st ) == -1 ) {
+ if (fstat(fileno(fp), &st) == -1) {
return Q_ERR(errno);
}
- if( !S_ISREG( st.st_mode ) ) {
+ if (!S_ISREG(st.st_mode)) {
return Q_ERR_ISDIR;
}
- if( info ) {
+ if (info) {
info->size = st.st_size;
info->ctime = st.st_ctime;
info->mtime = st.st_mtime;
@@ -607,72 +625,79 @@ Sys_Quit
This function never returns.
=================
*/
-void Sys_Quit( void ) {
+void Sys_Quit(void)
+{
#if USE_SYSCON
tty_shutdown_input();
#endif
- exit( EXIT_SUCCESS );
+ exit(EXIT_SUCCESS);
}
-void Sys_AddDefaultConfig( void ) {
+void Sys_AddDefaultConfig(void)
+{
FILE *fp;
struct stat st;
size_t len, r;
- fp = fopen( SYS_SITECFG_NAME, "r" );
- if( !fp ) {
+ fp = fopen(SYS_SITECFG_NAME, "r");
+ if (!fp) {
return;
}
- if( fstat( fileno( fp ), &st ) == 0 ) {
+ if (fstat(fileno(fp), &st) == 0) {
len = st.st_size;
- if( len >= cmd_buffer.maxsize ) {
+ if (len >= cmd_buffer.maxsize) {
len = cmd_buffer.maxsize - 1;
}
- r = fread( cmd_buffer.text, 1, len, fp );
+ r = fread(cmd_buffer.text, 1, len, fp);
cmd_buffer.text[r] = 0;
- cmd_buffer.cursize = COM_Compress( cmd_buffer.text );
+ cmd_buffer.cursize = COM_Compress(cmd_buffer.text);
}
- fclose( fp );
+ fclose(fp);
- if( cmd_buffer.cursize ) {
- Com_Printf( "Execing %s\n", SYS_SITECFG_NAME );
- Cbuf_Execute( &cmd_buffer );
+ if (cmd_buffer.cursize) {
+ Com_Printf("Execing %s\n", SYS_SITECFG_NAME);
+ Cbuf_Execute(&cmd_buffer);
}
}
-void Sys_Sleep( int msec ) {
+void Sys_Sleep(int msec)
+{
struct timespec req;
req.tv_sec = msec / 1000;
- req.tv_nsec = ( msec % 1000 ) * 1000000;
- nanosleep( &req, NULL );
+ req.tv_nsec = (msec % 1000) * 1000000;
+ nanosleep(&req, NULL);
}
#if USE_AC_CLIENT
-qboolean Sys_GetAntiCheatAPI( void ) {
- Sys_Sleep( 1500 );
+qboolean Sys_GetAntiCheatAPI(void)
+{
+ Sys_Sleep(1500);
return qfalse;
}
#endif
-static void hup_handler( int signum ) {
+static void hup_handler(int signum)
+{
Com_FlushLogs();
}
-static void term_handler( int signum ) {
+static void term_handler(int signum)
+{
#ifdef _GNU_SOURCE
- Com_Printf( "%s\n", strsignal( signum ) );
+ Com_Printf("%s\n", strsignal(signum));
#else
- Com_Printf( "Received signal %d, exiting\n", signum );
+ Com_Printf("Received signal %d, exiting\n", signum);
#endif
- Com_Quit( NULL, ERR_DISCONNECT );
+ Com_Quit(NULL, ERR_DISCONNECT);
}
-static void kill_handler( int signum ) {
+static void kill_handler(int signum)
+{
#if USE_SYSCON
tty_shutdown_input();
#endif
@@ -682,12 +707,12 @@ static void kill_handler( int signum ) {
#endif
#ifdef _GNU_SOURCE
- fprintf( stderr, "%s\n", strsignal( signum ) );
+ fprintf(stderr, "%s\n", strsignal(signum));
#else
- fprintf( stderr, "Received signal %d, aborting\n", signum );
+ fprintf(stderr, "Received signal %d, aborting\n", signum);
#endif
- exit( EXIT_FAILURE );
+ exit(EXIT_FAILURE);
}
/*
@@ -695,78 +720,79 @@ static void kill_handler( int signum ) {
Sys_Init
=================
*/
-void Sys_Init( void ) {
+void Sys_Init(void)
+{
char *homedir;
- signal( SIGTERM, term_handler );
- signal( SIGINT, term_handler );
- signal( SIGTTIN, SIG_IGN );
- signal( SIGTTOU, SIG_IGN );
- signal( SIGUSR1, hup_handler );
+ signal(SIGTERM, term_handler);
+ signal(SIGINT, term_handler);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTTOU, SIG_IGN);
+ signal(SIGUSR1, hup_handler);
// basedir <path>
// allows the game to run from outside the data tree
- sys_basedir = Cvar_Get( "basedir", DATADIR, CVAR_NOSET );
+ sys_basedir = Cvar_Get("basedir", DATADIR, CVAR_NOSET);
// homedir <path>
// specifies per-user writable directory for demos, screenshots, etc
- if( HOMEDIR[0] == '~' ) {
- char *s = getenv( "HOME" );
- if( s && *s ) {
- homedir = va( "%s%s", s, HOMEDIR + 1 );
+ if (HOMEDIR[0] == '~') {
+ char *s = getenv("HOME");
+ if (s && *s) {
+ homedir = va("%s%s", s, HOMEDIR + 1);
} else {
homedir = "";
}
} else {
homedir = HOMEDIR;
}
- sys_homedir = Cvar_Get( "homedir", homedir, CVAR_NOSET );
- sys_libdir = Cvar_Get( "libdir", LIBDIR, CVAR_NOSET );
- sys_forcegamelib = Cvar_Get( "sys_forcegamelib", "", CVAR_NOSET );
+ sys_homedir = Cvar_Get("homedir", homedir, CVAR_NOSET);
+ sys_libdir = Cvar_Get("libdir", LIBDIR, CVAR_NOSET);
+ sys_forcegamelib = Cvar_Get("sys_forcegamelib", "", CVAR_NOSET);
#if USE_SYSCON
// we want TTY support enabled if started from terminal,
// but don't want any output by default if launched without one
// (from X session for example)
- sys_console = Cvar_Get( "sys_console", isatty( STDIN_FILENO ) &&
- isatty( STDOUT_FILENO ) ? "2" : "0", CVAR_NOSET );
+ sys_console = Cvar_Get("sys_console", isatty(STDIN_FILENO) &&
+ isatty(STDOUT_FILENO) ? "2" : "0", CVAR_NOSET);
- if( sys_console->integer > 0 ) {
+ if (sys_console->integer > 0) {
int ret;
// change stdin to non-blocking
- ret = fcntl( STDIN_FILENO, F_GETFL, 0 );
- if( !(ret & O_NONBLOCK) )
- fcntl( STDIN_FILENO, F_SETFL, ret | O_NONBLOCK );
+ ret = fcntl(STDIN_FILENO, F_GETFL, 0);
+ if (!(ret & O_NONBLOCK))
+ fcntl(STDIN_FILENO, F_SETFL, ret | O_NONBLOCK);
// change stdout to non-blocking
- ret = fcntl( STDOUT_FILENO, F_GETFL, 0 );
- if( !(ret & O_NONBLOCK) )
- fcntl( STDOUT_FILENO, F_SETFL, ret | O_NONBLOCK );
+ ret = fcntl(STDOUT_FILENO, F_GETFL, 0);
+ if (!(ret & O_NONBLOCK))
+ fcntl(STDOUT_FILENO, F_SETFL, ret | O_NONBLOCK);
// add stdin to the list of descriptors to wait on
- tty_io = IO_Add( STDIN_FILENO );
+ tty_io = IO_Add(STDIN_FILENO);
tty_io->wantread = qtrue;
// init optional TTY support
- if( sys_console->integer > 1 )
+ if (sys_console->integer > 1)
tty_init_input();
- signal( SIGHUP, term_handler );
+ signal(SIGHUP, term_handler);
} else
#endif
- if( Com_IsDedicated() ) {
- signal( SIGHUP, hup_handler );
- }
+ if (Com_IsDedicated()) {
+ signal(SIGHUP, hup_handler);
+ }
- sys_parachute = Cvar_Get( "sys_parachute", "1", CVAR_NOSET );
+ sys_parachute = Cvar_Get("sys_parachute", "1", CVAR_NOSET);
- if( sys_parachute->integer ) {
+ if (sys_parachute->integer) {
// perform some cleanup when crashing
- signal( SIGSEGV, kill_handler );
- signal( SIGILL, kill_handler );
- signal( SIGFPE, kill_handler );
- signal( SIGTRAP, kill_handler );
+ signal(SIGSEGV, kill_handler);
+ signal(SIGILL, kill_handler);
+ signal(SIGFPE, kill_handler);
+ signal(SIGTRAP, kill_handler);
}
}
@@ -776,15 +802,16 @@ void Sys_Init( void ) {
Sys_Printf
================
*/
-void Sys_Printf( const char *fmt, ... ) {
+void Sys_Printf(const char *fmt, ...)
+{
va_list argptr;
char msg[MAXPRINTMSG];
- va_start( argptr, fmt );
- Q_vsnprintf( msg, sizeof( msg ), fmt, argptr );
- va_end( argptr );
+ va_start(argptr, fmt);
+ Q_vsnprintf(msg, sizeof(msg), fmt, argptr);
+ va_end(argptr);
- Sys_ConsoleOutput( msg );
+ Sys_ConsoleOutput(msg);
}
#endif
@@ -793,7 +820,8 @@ void Sys_Printf( const char *fmt, ... ) {
Sys_Error
=================
*/
-void Sys_Error( const char *error, ... ) {
+void Sys_Error(const char *error, ...)
+{
va_list argptr;
char text[MAXERRORMSG];
@@ -805,14 +833,14 @@ void Sys_Error( const char *error, ... ) {
VID_FatalShutdown();
#endif
- va_start( argptr, error );
- Q_vsnprintf( text, sizeof( text ), error, argptr );
- va_end( argptr );
+ va_start(argptr, error);
+ Q_vsnprintf(text, sizeof(text), error, argptr);
+ va_end(argptr);
- fprintf( stderr, "********************\n"
- "FATAL: %s\n"
- "********************\n", text );
- exit( EXIT_FAILURE );
+ fprintf(stderr, "********************\n"
+ "FATAL: %s\n"
+ "********************\n", text);
+ exit(EXIT_FAILURE);
}
/*
@@ -828,9 +856,10 @@ DLL LOADING
Sys_FreeLibrary
=================
*/
-void Sys_FreeLibrary( void *handle ) {
- if( handle && dlclose( handle ) ) {
- Com_Error( ERR_FATAL, "dlclose failed on %p: %s", handle, dlerror() );
+void Sys_FreeLibrary(void *handle)
+{
+ if (handle && dlclose(handle)) {
+ Com_Error(ERR_FATAL, "dlclose failed on %p: %s", handle, dlerror());
}
}
@@ -839,37 +868,39 @@ void Sys_FreeLibrary( void *handle ) {
Sys_LoadLibrary
=================
*/
-void *Sys_LoadLibrary( const char *path, const char *sym, void **handle ) {
+void *Sys_LoadLibrary(const char *path, const char *sym, void **handle)
+{
void *module, *entry;
*handle = NULL;
- module = dlopen( path, RTLD_LAZY );
- if( !module ) {
- Com_DPrintf( "%s failed: %s\n", __func__, dlerror() );
+ module = dlopen(path, RTLD_LAZY);
+ if (!module) {
+ Com_DPrintf("%s failed: %s\n", __func__, dlerror());
return NULL;
}
- if( sym ) {
- entry = dlsym( module, sym );
- if( !entry ) {
- Com_DPrintf( "%s failed: %s\n", __func__, dlerror() );
- dlclose( module );
+ if (sym) {
+ entry = dlsym(module, sym);
+ if (!entry) {
+ Com_DPrintf("%s failed: %s\n", __func__, dlerror());
+ dlclose(module);
return NULL;
}
} else {
entry = NULL;
}
- Com_DPrintf( "%s succeeded: %s\n", __func__, path );
+ Com_DPrintf("%s succeeded: %s\n", __func__, path);
*handle = module;
return entry;
}
-void *Sys_GetProcAddress( void *handle, const char *sym ) {
- return dlsym( handle, sym );
+void *Sys_GetProcAddress(void *handle, const char *sym)
+{
+ return dlsym(handle, sym);
}
/*
@@ -880,8 +911,9 @@ MISC
===============================================================================
*/
-static void *copy_info( const char *name, const struct stat *st ) {
- return FS_CopyInfo( name, st->st_size, st->st_ctime, st->st_mtime );
+static void *copy_info(const char *name, const struct stat *st)
+{
+ return FS_CopyInfo(name, st->st_size, st->st_ctime, st->st_mtime);
}
/*
@@ -894,13 +926,13 @@ Internal function to filesystem. Conventions apply:
- depth must be 0 on the first call
=================
*/
-void Sys_ListFiles_r( const char *path,
- const char *filter,
- unsigned flags,
- size_t baselen,
- int *count_p,
- void **files,
- int depth )
+void Sys_ListFiles_r(const char *path,
+ const char *filter,
+ unsigned flags,
+ size_t baselen,
+ int *count_p,
+ void **files,
+ int depth)
{
struct dirent *ent;
DIR *dir;
@@ -910,93 +942,92 @@ void Sys_ListFiles_r( const char *path,
size_t len;
void *info;
- if( ( dir = opendir( path ) ) == NULL ) {
+ if ((dir = opendir(path)) == NULL) {
return;
}
- while( ( ent = readdir( dir ) ) != NULL ) {
- if( ent->d_name[0] == '.' ) {
+ while ((ent = readdir(dir)) != NULL) {
+ if (ent->d_name[0] == '.') {
continue; // ignore dotfiles
}
- len = Q_concat( fullpath, sizeof( fullpath ),
- path, "/", ent->d_name, NULL );
- if( len >= sizeof( fullpath ) ) {
+ len = Q_concat(fullpath, sizeof(fullpath),
+ path, "/", ent->d_name, NULL);
+ if (len >= sizeof(fullpath)) {
continue;
}
- if( stat( fullpath, &st ) == -1 ) {
+ if (stat(fullpath, &st) == -1) {
continue;
}
// pattern search implies recursive search
- if( ( flags & FS_SEARCH_BYFILTER ) &&
- S_ISDIR( st.st_mode ) && depth < MAX_LISTED_DEPTH )
- {
- Sys_ListFiles_r( fullpath, filter, flags, baselen,
- count_p, files, depth + 1 );
+ if ((flags & FS_SEARCH_BYFILTER) &&
+ S_ISDIR(st.st_mode) && depth < MAX_LISTED_DEPTH) {
+ Sys_ListFiles_r(fullpath, filter, flags, baselen,
+ count_p, files, depth + 1);
// re-check count
- if( *count_p >= MAX_LISTED_FILES ) {
+ if (*count_p >= MAX_LISTED_FILES) {
break;
}
}
// check type
- if( flags & FS_SEARCH_DIRSONLY ) {
- if( !S_ISDIR( st.st_mode ) ) {
+ if (flags & FS_SEARCH_DIRSONLY) {
+ if (!S_ISDIR(st.st_mode)) {
continue;
}
} else {
- if( !S_ISREG( st.st_mode ) ) {
+ if (!S_ISREG(st.st_mode)) {
continue;
}
}
// check filter
- if( filter ) {
- if( flags & FS_SEARCH_BYFILTER ) {
- if( !FS_WildCmp( filter, fullpath + baselen ) ) {
+ if (filter) {
+ if (flags & FS_SEARCH_BYFILTER) {
+ if (!FS_WildCmp(filter, fullpath + baselen)) {
continue;
}
} else {
- if( !FS_ExtCmp( filter, ent->d_name ) ) {
+ if (!FS_ExtCmp(filter, ent->d_name)) {
continue;
}
}
}
// strip path
- if( flags & FS_SEARCH_SAVEPATH ) {
+ if (flags & FS_SEARCH_SAVEPATH) {
name = fullpath + baselen;
} else {
name = ent->d_name;
}
// strip extension
- if( flags & FS_SEARCH_STRIPEXT ) {
- *COM_FileExtension( name ) = 0;
+ if (flags & FS_SEARCH_STRIPEXT) {
+ *COM_FileExtension(name) = 0;
- if( !*name ) {
+ if (!*name) {
continue;
}
}
// copy info off
- if( flags & FS_SEARCH_EXTRAINFO ) {
- info = copy_info( name, &st );
+ if (flags & FS_SEARCH_EXTRAINFO) {
+ info = copy_info(name, &st);
} else {
- info = FS_CopyString( name );
+ info = FS_CopyString(name);
}
files[(*count_p)++] = info;
- if( *count_p >= MAX_LISTED_FILES ) {
+ if (*count_p >= MAX_LISTED_FILES) {
break;
}
}
- closedir( dir );
+ closedir(dir);
}
/*
@@ -1004,26 +1035,27 @@ void Sys_ListFiles_r( const char *path,
main
=================
*/
-int main( int argc, char **argv ) {
- if( argc > 1 ) {
- if( !strcmp( argv[1], "-v" ) || !strcmp( argv[1], "--version" ) ) {
- fprintf( stderr, "%s\n", com_version_string );
+int main(int argc, char **argv)
+{
+ if (argc > 1) {
+ if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
+ fprintf(stderr, "%s\n", com_version_string);
return 0;
}
- if( !strcmp( argv[1], "-h" ) || !strcmp( argv[1], "--help" ) ) {
- fprintf( stderr, "Usage: %s [+command arguments] [...]\n", argv[0] );
+ if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
+ fprintf(stderr, "Usage: %s [+command arguments] [...]\n", argv[0]);
return 0;
}
}
- if( !getuid() || !geteuid() ) {
- fprintf( stderr, "You can not run " PRODUCT " as superuser "
- "for security reasons!\n" );
+ if (!getuid() || !geteuid()) {
+ fprintf(stderr, "You can not run " PRODUCT " as superuser "
+ "for security reasons!\n");
return 1;
}
- Qcommon_Init( argc, argv );
- while( 1 ) {
+ Qcommon_Init(argc, argv);
+ while (1) {
Qcommon_Frame();
}