]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/cmdlib.c
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / tools / quake3 / common / cmdlib.c
index 94c260d340d117bd4ee790a02f8693f4a37d949b..103047c029eb85e2542c86d7d199bbdff6fc14ab 100644 (file)
@@ -1,5 +1,5 @@
 /*
-   Copyright (C) 1999-2007 id Software, Inc. and contributors.
+   Copyright (C) 1999-2006 Id Software, Inc. and contributors.
    For a list of contributors, see the accompanying CONTRIBUTORS file.
 
    This file is part of GtkRadiant.
@@ -34,7 +34,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifdef _WIN32
+#ifdef WIN32
 #include <direct.h>
 #include <windows.h>
 #endif
@@ -188,7 +188,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 +201,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] = '/';
@@ -297,7 +297,9 @@ void Q_getwd( char *out ){
        strcat( out, "\\" );
 #else
        // Gef: Changed from getwd() to getcwd() to avoid potential buffer overflow
-       getcwd( out, 256 );
+       if ( !getcwd( out, 256 ) ) {
+               *out = 0;
+       }
        strcat( out, "/" );
 #endif
        while ( out[i] != 0 )
@@ -311,15 +313,42 @@ void Q_getwd( char *out ){
 
 
 void Q_mkdir( const char *path ){
+       char parentbuf[256];
+       const char *p = NULL;
+       int retry = 2;
+       while ( retry-- )
+       {
 #ifdef WIN32
-       if ( _mkdir( path ) != -1 ) {
-               return;
-       }
+               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
-       if ( mkdir( path, 0777 ) != -1 ) {
-               return;
-       }
+               if ( mkdir( path, 0777 ) != -1 ) {
+                       return;
+               }
+               if ( errno == ENOENT ) {
+                       p = strrchr( path, '/' );
+               }
 #endif
+               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;
+       }
        if ( errno != EEXIST ) {
                Error( "mkdir %s: %s",path, strerror( errno ) );
        }