2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
27 #if defined( __linux__ ) || defined( __APPLE__ )
\r
45 #define MAX_BASE_PATHS 10
\r
46 #define MAX_GAME_PATHS 10
\r
49 char installPath[ MAX_OS_PATH ];
\r
52 char *basePaths[ MAX_BASE_PATHS ];
\r
54 char *gamePaths[ MAX_GAME_PATHS ];
\r
57 some of this code is based off the original q3map port from loki
\r
58 and finds various paths. moved here from bsp.c for clarity.
\r
62 PathLokiGetHomeDir()
\r
63 gets the user's home dir (for ~/.q3a)
\r
66 char *LokiGetHomeDir( void )
\r
76 /* get the home environment variable */
\r
77 home = getenv( "HOME" );
\r
80 /* do some more digging */
\r
83 while( (pwd = getpwent()) != NULL )
\r
85 if( pwd->pw_uid == id )
\r
102 PathLokiInitPaths()
\r
103 initializes some paths on linux/os x
\r
106 void LokiInitPaths( char *argv0 )
\r
109 /* this is kinda crap, but hey */
\r
110 strcpy( installPath, "../" );
\r
112 char temp[ MAX_OS_PATH ];
\r
120 home = LokiGetHomeDir();
\r
124 /* do some path divining */
\r
125 strcpy( temp, argv0 );
\r
126 if( strrchr( temp, '/' ) )
\r
127 argv0 = strrchr( argv0, '/' ) + 1;
\r
130 /* get path environment variable */
\r
131 path = getenv( "PATH" );
\r
134 last[ 0 ] = path[ 0 ];
\r
138 /* go through each : segment of path */
\r
139 while( last[ 0 ] != '\0' && found == false )
\r
141 /* null out temp */
\r
144 /* find next chunk */
\r
145 last = strchr( path, ':' );
\r
147 last = path + strlen( path );
\r
149 /* found home dir candidate */
\r
152 strcpy( temp, home );
\r
157 if( last > (path + 1) )
\r
159 strncat( temp, path, (last - path) );
\r
160 strcat( temp, "/" );
\r
162 strcat( temp, "./" );
\r
163 strcat( temp, argv0 );
\r
165 /* verify the path */
\r
166 if( access( temp, X_OK ) == 0 )
\r
173 if( realpath( temp, installPath ) )
\r
175 /* q3map is in "tools/" */
\r
176 *(strrchr( installPath, '/' )) = '\0';
\r
177 *(strrchr( installPath, '/' ) + 1) = '\0';
\r
180 /* set home path */
\r
188 CleanPath() - ydnar
\r
189 cleans a dos path \ -> /
\r
192 void CleanPath( char *path )
\r
196 if( *path == '\\' )
\r
203 AddBasePath() - ydnar
\r
204 adds a base path to the list
\r
207 void AddBasePath( char *path )
\r
210 if( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS )
\r
213 /* add it to the list */
\r
214 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
\r
215 strcpy( basePaths[ numBasePaths ], path );
\r
216 CleanPath( basePaths[ numBasePaths ] );
\r
223 AddHomeBasePath() - ydnar
\r
224 adds a base path to the beginning of the list, prefixed by ~/
\r
227 void AddHomeBasePath( char *path )
\r
231 char temp[ MAX_OS_PATH ];
\r
235 if( path == NULL || path[ 0 ] == '\0' )
\r
239 for( i = 0; i < (MAX_BASE_PATHS - 1); i++ )
\r
240 basePaths[ i + 1 ] = basePaths[ i ];
\r
242 /* concatenate home dir and path */
\r
243 sprintf( temp, "%s/%s", homePath, path );
\r
245 /* add it to the list */
\r
246 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
\r
247 strcpy( basePaths[ 0 ], temp );
\r
248 CleanPath( basePaths[ 0 ] );
\r
256 AddGamePath() - ydnar
\r
257 adds a game path to the list
\r
260 void AddGamePath( char *path )
\r
263 if( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS )
\r
266 /* add it to the list */
\r
267 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
\r
268 strcpy( gamePaths[ numGamePaths ], path );
\r
269 CleanPath( gamePaths[ numGamePaths ] );
\r
277 InitPaths() - ydnar
\r
278 cleaned up some of the path initialization code from bsp.c
\r
279 will remove any arguments it uses
\r
282 void InitPaths( int *argc, char **argv )
\r
284 int i, j, k, len, len2;
\r
285 char temp[ MAX_OS_PATH ];
\r
286 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
\r
288 strcpy(gamePath, "baseq2");
\r
289 strcpy(game_magic, "quake");
\r
290 strcpy(homeBasePath, ".quake2");
\r
293 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
\r
295 /* get the install path for backup */
\r
296 LokiInitPaths( argv[ 0 ] );
\r
298 /* set game to default (q3a) */
\r
302 /* parse through the arguments and extract those relevant to paths */
\r
303 for( i = 0; i < *argc; i++ )
\r
305 /* check for null */
\r
306 if( argv[ i ] == NULL )
\r
310 if( strcmp( argv[ i ], "-fs_basepath" ) == 0 )
\r
313 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
\r
314 argv[ i - 1 ] = NULL;
\r
315 AddBasePath( argv[ i ] );
\r
321 /* remove processed arguments */
\r
322 for( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
\r
324 for( j; j < *argc && argv[ j ] == NULL; j++ );
\r
325 argv[ i ] = argv[ j ];
\r
326 if( argv[ i ] != NULL )
\r
331 /* add standard game path */
\r
332 AddGamePath( gamePath );
\r
334 /* if there is no base path set, figure it out */
\r
335 if( numBasePaths == 0 )
\r
337 /* this is another crappy replacement for SetQdirFromPath() */
\r
338 len2 = strlen( game_magic );
\r
339 for( i = 0; i < *argc && numBasePaths == 0; i++ )
\r
341 /* extract the arg */
\r
342 strcpy( temp, argv[ i ] );
\r
344 len = strlen( temp );
\r
345 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
\r
347 /* this is slow, but only done once */
\r
348 for( j = 0; j < (len - len2); j++ )
\r
350 /* check for the game's magic word */
\r
351 if( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 )
\r
353 /* now find the next slash and nuke everything after it */
\r
354 while( temp[ ++j ] != '/' && temp[ j ] != '\0' );
\r
357 /* add this as a base path */
\r
358 AddBasePath( temp );
\r
364 /* add install path */
\r
365 if( numBasePaths == 0 )
\r
366 AddBasePath( installPath );
\r
369 if( numBasePaths == 0 )
\r
370 Error( "Failed to find a valid base path." );
\r
373 /* this only affects unix */
\r
374 AddHomeBasePath( homeBasePath );
\r
376 /* initialize vfs paths */
\r
377 if( numBasePaths > MAX_BASE_PATHS )
\r
378 numBasePaths = MAX_BASE_PATHS;
\r
379 if( numGamePaths > MAX_GAME_PATHS )
\r
380 numGamePaths = MAX_GAME_PATHS;
\r
382 /* walk the list of game paths */
\r
383 //for( j = 0; j < numGamePaths; j++ )
\r
385 /* walk the list of base paths */
\r
386 // for( i = 0; i < numBasePaths; i++ )
\r
388 /* create a full path and initialize it */
\r
389 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
\r
390 // vfsInitDirectory( temp );
\r
395 Sys_Printf( "\n" );
\r