1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 Nurail: Swiped from Q3Map2
31 #if defined( __linux__ ) || defined( __APPLE__ )
49 #define MAX_BASE_PATHS 10
50 #define MAX_GAME_PATHS 10
53 char installPath[ MAX_OS_PATH ];
56 char *basePaths[ MAX_BASE_PATHS ];
58 char *gamePaths[ MAX_GAME_PATHS ];
61 some of this code is based off the original q3map port from loki
62 and finds various paths. moved here from bsp.c for clarity.
67 gets the user's home dir (for ~/.q3a)
70 char *LokiGetHomeDir( void ){
79 /* get the home environment variable */
80 home = getenv( "HOME" );
82 /* do some more digging */
85 while ( ( pwd = getpwent() ) != NULL )
87 if ( pwd->pw_uid == id ) {
104 initializes some paths on linux/os x
107 void LokiInitPaths( char *argv0 ){
109 /* this is kinda crap, but hey */
110 strcpy( installPath, "../" );
112 char temp[ MAX_OS_PATH ];
120 home = LokiGetHomeDir();
121 if ( home == NULL ) {
125 /* do some path divining */
126 strcpy( temp, argv0 );
127 if ( strrchr( temp, '/' ) ) {
128 argv0 = strrchr( argv0, '/' ) + 1;
132 /* get path environment variable */
133 path = getenv( "PATH" );
136 last[ 0 ] = path[ 0 ];
140 /* go through each : segment of path */
141 while ( last[ 0 ] != '\0' && found == false )
146 /* find next chunk */
147 last = strchr( path, ':' );
148 if ( last == NULL ) {
149 last = path + strlen( path );
152 /* found home dir candidate */
153 if ( *path == '~' ) {
154 strcpy( temp, home );
159 if ( last > ( path + 1 ) ) {
160 strncat( temp, path, ( last - path ) );
163 strcat( temp, "./" );
164 strcat( temp, argv0 );
166 /* verify the path */
167 if ( access( temp, X_OK ) == 0 ) {
175 if ( realpath( temp, installPath ) ) {
176 /* q3map is in "tools/" */
177 *( strrchr( installPath, '/' ) ) = '\0';
178 *( strrchr( installPath, '/' ) + 1 ) = '\0';
190 cleans a dos path \ -> /
193 void CleanPath( char *path ){
196 if ( *path == '\\' ) {
204 AddBasePath() - ydnar
205 adds a base path to the list
208 void AddBasePath( char *path ){
210 if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
214 /* add it to the list */
215 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
216 strcpy( basePaths[ numBasePaths ], path );
217 CleanPath( basePaths[ numBasePaths ] );
224 AddHomeBasePath() - ydnar
225 adds a base path to the beginning of the list, prefixed by ~/
228 void AddHomeBasePath( char *path ){
231 char temp[ MAX_OS_PATH ];
235 if ( path == NULL || path[ 0 ] == '\0' ) {
240 for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
241 basePaths[ i + 1 ] = basePaths[ i ];
243 /* concatenate home dir and path */
244 sprintf( temp, "%s/%s", homePath, path );
246 /* add it to the list */
247 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
248 strcpy( basePaths[ 0 ], temp );
249 CleanPath( basePaths[ 0 ] );
257 AddGamePath() - ydnar
258 adds a game path to the list
261 void AddGamePath( char *path ){
263 if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
267 /* add it to the list */
268 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
269 strcpy( gamePaths[ numGamePaths ], path );
270 CleanPath( gamePaths[ numGamePaths ] );
279 cleaned up some of the path initialization code from bsp.c
280 will remove any arguments it uses
283 void InitPaths( int *argc, char **argv ){
284 int i, j, k, len, len2;
285 char temp[ MAX_OS_PATH ];
286 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
288 strcpy( gamePath, "baseq2" );
289 strcpy( game_magic, "quake" );
290 strcpy( homeBasePath, ".quake2" );
293 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
295 /* get the install path for backup */
296 LokiInitPaths( argv[ 0 ] );
298 /* set game to default (q3a) */
302 /* parse through the arguments and extract those relevant to paths */
303 for ( i = 0; i < *argc; i++ )
306 if ( argv[ i ] == NULL ) {
311 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
312 if ( ++i >= *argc ) {
313 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
315 argv[ i - 1 ] = NULL;
316 AddBasePath( argv[ i ] );
322 /* remove processed arguments */
323 for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
325 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
326 argv[ i ] = argv[ j ];
327 if ( argv[ i ] != NULL ) {
333 /* add standard game path */
334 AddGamePath( gamePath );
336 /* if there is no base path set, figure it out */
337 if ( numBasePaths == 0 ) {
338 /* this is another crappy replacement for SetQdirFromPath() */
339 len2 = strlen( game_magic );
340 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
342 /* extract the arg */
343 strcpy( temp, argv[ i ] );
345 len = strlen( temp );
346 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
348 /* this is slow, but only done once */
349 for ( j = 0; j < ( len - len2 ); j++ )
351 /* check for the game's magic word */
352 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
353 /* now find the next slash and nuke everything after it */
354 while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
357 /* add this as a base path */
364 /* add install path */
365 if ( numBasePaths == 0 ) {
366 AddBasePath( installPath );
370 if ( numBasePaths == 0 ) {
371 Error( "Failed to find a valid base path." );
375 /* this only affects unix */
376 AddHomeBasePath( homeBasePath );
378 /* initialize vfs paths */
379 if ( numBasePaths > MAX_BASE_PATHS ) {
380 numBasePaths = MAX_BASE_PATHS;
382 if ( numGamePaths > MAX_GAME_PATHS ) {
383 numGamePaths = MAX_GAME_PATHS;
386 /* walk the list of game paths */
387 //for( j = 0; j < numGamePaths; j++ )
389 /* walk the list of base paths */
390 // for( i = 0; i < numBasePaths; i++ )
392 /* create a full path and initialize it */
393 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
394 // vfsInitDirectory( temp );