]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/cmdlib.c
quake3: delete dead strupr
[xonotic/netradiant.git] / tools / quake3 / common / cmdlib.c
index 35f4d75bd68dcb0767c820e73963420a8607c1c8..60edebad465e5ca22c5d1b156a96b2e932fee5f2 100644 (file)
 // replaced qprintf with Sys_Printf
 
 #include "cmdlib.h"
+#include "globaldefs.h"
 #include "mathlib.h"
 #include "inout.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifdef _WIN32
+#if GDEF_OS_WINDOWS
 #include <direct.h>
 #include <windows.h>
-#endif
-
-#if defined ( __linux__ ) || defined ( __APPLE__ )
-#include <unistd.h>
-#endif
-
-#ifdef NeXT
+#elif GDEF_OS_NEXT
 #include <libc.h>
-#endif
+#else // OTHER OS
+#include <unistd.h>
+#endif // OTHER OS
 
-#define BASEDIRNAME "quake"     // assumed to have a 2 or 3 following
-#define PATHSEPERATOR   '/'
+#define BASEDIRNAME "quake" // assumed to have a 2 or 3 following
+#define PATHSEPERATOR '/'
 
-#ifdef SAFE_MALLOC
 void *safe_malloc( size_t size ){
        void *p;
 
@@ -72,7 +68,28 @@ void *safe_malloc_info( size_t size, char* info ){
 
        return p;
 }
-#endif
+
+void *safe_malloc0( size_t size ){
+       void *p;
+
+       p = calloc( 1, size );
+       if ( !p ) {
+               Error( "safe_malloc0 failed on allocation of %i bytes", size );
+       }
+
+       return p;
+}
+
+void *safe_malloc0_info( size_t size, char* info ){
+       void *p;
+
+       p = calloc( 1, size );
+       if ( !p ) {
+               Error( "%s: safe_malloc0 failed on allocation of %i bytes", info, size );
+       }
+
+       return p;
+}
 
 // set these before calling CheckParm
 int myargc;
@@ -95,7 +112,8 @@ char archivedir[1024];
 #define MAX_EX_ARGC 1024
 int ex_argc;
 char    *ex_argv[MAX_EX_ARGC];
-#ifdef _WIN32
+
+#if GDEF_OS_WINDOWS
 #include "io.h"
 void ExpandWildcards( int *argc, char ***argv ){
        struct _finddata_t fileinfo;
@@ -134,10 +152,10 @@ void ExpandWildcards( int *argc, char ***argv ){
        *argc = ex_argc;
        *argv = ex_argv;
 }
-#else
+#else // !GDEF_OS_WINDOWS
 void ExpandWildcards( int *argc, char ***argv ){
 }
-#endif
+#endif // !GDEF_OS_WINDOWS
 
 /*
 
@@ -188,7 +206,7 @@ void SetQdirFromPath( const char *path ){
                        }
                        strncpy( qdir, path, c + len + count - path );
                        Sys_Printf( "qdir: %s\n", qdir );
-                       for ( i = 0; i < strlen( qdir ); i++ )
+                       for ( i = 0; i < (int) strlen( qdir ); i++ )
                        {
                                if ( qdir[i] == '\\' ) {
                                        qdir[i] = '/';
@@ -201,7 +219,7 @@ void SetQdirFromPath( const char *path ){
                                if ( *c == '/' || *c == '\\' ) {
                                        strncpy( gamedir, path, c + 1 - path );
 
-                                       for ( i = 0; i < strlen( gamedir ); i++ )
+                                       for ( i = 0; i < (int) strlen( gamedir ); i++ )
                                        {
                                                if ( gamedir[i] == '\\' ) {
                                                        gamedir[i] = '/';
@@ -244,9 +262,6 @@ char *ExpandArg( const char *path ){
 
 char *ExpandPath( const char *path ){
        static char full[1024];
-       if ( !qdir[0] ) {
-               Error( "ExpandPath called without qdir set" );
-       }
        if ( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) {
                strcpy( full, path );
                return full;
@@ -255,33 +270,6 @@ char *ExpandPath( const char *path ){
        return full;
 }
 
-char *ExpandGamePath( const char *path ){
-       static char full[1024];
-       if ( !qdir[0] ) {
-               Error( "ExpandGamePath called without qdir set" );
-       }
-       if ( path[0] == '/' || path[0] == '\\' || path[1] == ':' ) {
-               strcpy( full, path );
-               return full;
-       }
-       sprintf( full, "%s%s", gamedir, path );
-       return full;
-}
-
-char *ExpandPathAndArchive( const char *path ){
-       char    *expanded;
-       char archivename[1024];
-
-       expanded = ExpandPath( path );
-
-       if ( archive ) {
-               sprintf( archivename, "%s/%s", archivedir, path );
-               QCopyFile( expanded, archivename );
-       }
-       return expanded;
-}
-
-
 char *copystring( const char *s ){
        char    *b;
        b = safe_malloc( strlen( s ) + 1 );
@@ -322,14 +310,16 @@ double I_FloatTime( void ){
 void Q_getwd( char *out ){
        int i = 0;
 
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
        _getcwd( out, 256 );
        strcat( out, "\\" );
-#else
+#else // !GDEF_OS_WINDOWS
        // Gef: Changed from getwd() to getcwd() to avoid potential buffer overflow
-       getcwd( out, 256 );
+       if ( !getcwd( out, 256 ) ) {
+               *out = 0;
+       }
        strcat( out, "/" );
-#endif
+#endif // !GDEF_OS_WINDOWS
        while ( out[i] != 0 )
        {
                if ( out[i] == '\\' ) {
@@ -341,15 +331,42 @@ void Q_getwd( char *out ){
 
 
 void Q_mkdir( const char *path ){
-#ifdef WIN32
-       if ( _mkdir( path ) != -1 ) {
-               return;
-       }
-#else
-       if ( mkdir( path, 0777 ) != -1 ) {
-               return;
+       char parentbuf[256];
+       const char *p = NULL;
+       int retry = 2;
+       while ( retry-- )
+       {
+#if GDEF_OS_WINDOWS
+               const char *q = NULL;
+               if ( _mkdir( path ) != -1 ) {
+                       return;
+               }
+               if ( errno == ENOENT ) {
+                       p = strrchr( path, '/' );
+                       q = strrchr( path, '\\' );
+                       if ( q && ( !p || q < p ) ) {
+                               p = q;
+                       }
+               }
+#else // !GDEF_OS_WINDOWS
+               if ( mkdir( path, 0777 ) != -1 ) {
+                       return;
+               }
+               if ( errno == ENOENT ) {
+                       p = strrchr( path, '/' );
+               }
+#endif // !GDEF_OS_WINDOWS
+               if ( p ) {
+                       strncpy( parentbuf, path, sizeof( parentbuf ) );
+                       if ( (int) ( p - path ) < (int) sizeof( parentbuf ) ) {
+                               parentbuf[p - path] = 0;
+                               Sys_Printf( "mkdir: %s: creating parent %s first\n", path, parentbuf );
+                               Q_mkdir( parentbuf );
+                               continue;
+                       }
+               }
+               break;
        }
-#endif
        if ( errno != EEXIST ) {
                Error( "mkdir %s: %s",path, strerror( errno ) );
        }
@@ -482,23 +499,6 @@ int Q_stricmp( const char *s1, const char *s2 ){
        return Q_strncasecmp( s1, s2, 99999 );
 }
 
-// NOTE TTimo when switching to Multithread DLL (Release/Debug) in the config
-//   started getting warnings about that function, prolly a duplicate with the runtime function
-//   maybe we still need to have it in linux builds
-/*
-   char *strupr (char *start)
-   {
-    char       *in;
-    in = start;
-    while (*in)
-    {
-   *in = toupper(*in);
-        in++;
-    }
-    return start;
-   }
- */
-
 char *strlower( char *start ){
        char    *in;
        in = start;
@@ -658,8 +658,7 @@ int    LoadFileBlock( const char *filename, void **bufferptr ){
        if ( nBlock > 0 ) {
                nAllocSize += MEM_BLOCKSIZE - nBlock;
        }
-       buffer = safe_malloc( nAllocSize + 1 );
-       memset( buffer, 0, nAllocSize + 1 );
+       buffer = safe_malloc0( nAllocSize + 1 );
        SafeRead( f, buffer, length );
        fclose( f );
 
@@ -883,11 +882,7 @@ int ParseNum( const char *str ){
    ============================================================================
  */
 
-#ifdef _SGI_SOURCE
-#define __BIG_ENDIAN__
-#endif
-
-#ifdef __BIG_ENDIAN__
+#if GDEF_ARCH_ENDIAN_BIG
 
 short   LittleShort( short l ){
        byte b1,b2;
@@ -935,9 +930,7 @@ float   BigFloat( float l ){
        return l;
 }
 
-
-#else
-
+#else // !GDEF_ARCH_ENDIAN_BIG
 
 short   BigShort( short l ){
        byte b1,b2;
@@ -984,8 +977,7 @@ float   LittleFloat( float l ){
        return l;
 }
 
-
-#endif
+#endif // !GDEF_ARCH_ENDIAN_BIG
 
 
 //=======================================================
@@ -1059,14 +1051,14 @@ void    CreatePath( const char *path ){
        char c;
        char dir[1024];
 
-#ifdef _WIN32
+#if GDEF_OS_WINDOWS
        int olddrive = -1;
 
        if ( path[1] == ':' ) {
                olddrive = _getdrive();
                _chdrive( toupper( path[0] ) - 'A' + 1 );
        }
-#endif
+#endif // !GDEF_OS_WINDOWS
 
        if ( path[1] == ':' ) {
                path += 2;
@@ -1082,11 +1074,11 @@ void    CreatePath( const char *path ){
                }
        }
 
-#ifdef _WIN32
+#if GDEF_OS_WINDOWS
        if ( olddrive != -1 ) {
                _chdrive( olddrive );
        }
-#endif
+#endif // !>GDEF_OS_WINDOWS
 }
 
 
@@ -1108,10 +1100,9 @@ void QCopyFile( const char *from, const char *to ){
 }
 
 void Sys_Sleep( int n ){
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
        Sleep( n );
-#endif
-#if defined ( __linux__ ) || defined ( __APPLE__ )
+#else // !GDEF_OS_WINDOWS
        usleep( n * 1000 );
-#endif
+#endif // !GDEF_OS_WINDOWS
 }