1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 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
24 Nurail: Swiped from Q3Map2
27 #include "globaldefs.h"
29 #if GDEF_OS_LINUX || GDEF_OS_MACOS
47 #define MAX_BASE_PATHS 10
48 #define MAX_GAME_PATHS 10
51 char installPath[ MAX_OS_PATH ];
54 char *basePaths[ MAX_BASE_PATHS ];
56 char *gamePaths[ MAX_GAME_PATHS ];
59 some of this code is based off the original q3map port from loki
60 and finds various paths. moved here from bsp.c for clarity.
65 gets the user's home dir (for ~/.q3a)
68 char *LokiGetHomeDir( void ){
77 /* get the home environment variable */
78 home = getenv( "HOME" );
80 /* do some more digging */
83 while ( ( pwd = getpwent() ) != NULL )
85 if ( pwd->pw_uid == id ) {
102 initializes some paths on linux/os x
105 void LokiInitPaths( char *argv0 ){
107 /* this is kinda crap, but hey */
108 strcpy( installPath, "../" );
110 char temp[ MAX_OS_PATH ];
118 home = LokiGetHomeDir();
119 if ( home == NULL ) {
123 /* do some path divining */
124 strcpy( temp, argv0 );
125 if ( strrchr( temp, '/' ) ) {
126 argv0 = strrchr( argv0, '/' ) + 1;
130 /* get path environment variable */
131 path = getenv( "PATH" );
134 last[ 0 ] = path[ 0 ];
138 /* go through each : segment of path */
139 while ( last[ 0 ] != '\0' && found == false )
144 /* find next chunk */
145 last = strchr( path, ':' );
146 if ( last == NULL ) {
147 last = path + strlen( path );
150 /* found home dir candidate */
151 if ( *path == '~' ) {
152 strcpy( temp, home );
157 if ( last > ( path + 1 ) ) {
158 strncat( temp, path, ( last - path ) );
161 strcat( temp, "./" );
162 strcat( temp, argv0 );
164 /* verify the path */
165 if ( access( temp, X_OK ) == 0 ) {
173 if ( realpath( temp, installPath ) ) {
174 /* q3map is in "tools/" */
175 *( strrchr( installPath, '/' ) ) = '\0';
176 *( strrchr( installPath, '/' ) + 1 ) = '\0';
188 cleans a dos path \ -> /
191 void CleanPath( char *path ){
194 if ( *path == '\\' ) {
202 AddBasePath() - ydnar
203 adds a base path to the list
206 void AddBasePath( char *path ){
208 if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
212 /* add it to the list */
213 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
214 strcpy( basePaths[ numBasePaths ], path );
215 CleanPath( basePaths[ numBasePaths ] );
222 AddHomeBasePath() - ydnar
223 adds a base path to the beginning of the list, prefixed by ~/
226 void AddHomeBasePath( char *path ){
229 char temp[ MAX_OS_PATH ];
233 if ( path == NULL || path[ 0 ] == '\0' ) {
238 for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
239 basePaths[ i + 1 ] = basePaths[ i ];
241 /* concatenate home dir and path */
242 sprintf( temp, "%s/%s", homePath, path );
244 /* add it to the list */
245 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
246 strcpy( basePaths[ 0 ], temp );
247 CleanPath( basePaths[ 0 ] );
255 AddGamePath() - ydnar
256 adds a game path to the list
259 void AddGamePath( char *path ){
261 if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
265 /* add it to the list */
266 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
267 strcpy( gamePaths[ numGamePaths ], path );
268 CleanPath( gamePaths[ numGamePaths ] );
277 cleaned up some of the path initialization code from bsp.c
278 will remove any arguments it uses
281 void InitPaths( int *argc, char **argv ){
282 int i, j, k, len, len2;
283 char temp[ MAX_OS_PATH ];
284 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
286 strcpy( gamePath, "base" );
287 strcpy( game_magic, "h" );
288 strcpy( homeBasePath, ".heretic2" );
291 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
293 /* get the install path for backup */
294 LokiInitPaths( argv[ 0 ] );
296 /* set game to default (q3a) */
300 /* parse through the arguments and extract those relevant to paths */
301 for ( i = 0; i < *argc; i++ )
304 if ( argv[ i ] == NULL ) {
309 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
310 if ( ++i >= *argc ) {
311 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
313 argv[ i - 1 ] = NULL;
314 AddBasePath( argv[ i ] );
320 /* remove processed arguments */
321 for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
323 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
324 argv[ i ] = argv[ j ];
325 if ( argv[ i ] != NULL ) {
331 /* add standard game path */
332 AddGamePath( gamePath );
334 /* if there is no base path set, figure it out */
335 if ( numBasePaths == 0 ) {
336 /* this is another crappy replacement for SetQdirFromPath() */
337 len2 = strlen( game_magic );
338 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
340 /* extract the arg */
341 strcpy( temp, argv[ i ] );
343 len = strlen( temp );
344 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
346 /* this is slow, but only done once */
347 for ( j = 0; j < ( len - len2 ); j++ )
349 /* check for the game's magic word */
350 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
351 /* now find the next slash and nuke everything after it */
352 while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
355 /* add this as a base path */
362 /* add install path */
363 if ( numBasePaths == 0 ) {
364 AddBasePath( installPath );
368 if ( numBasePaths == 0 ) {
369 Error( "Failed to find a valid base path." );
373 /* this only affects unix */
374 AddHomeBasePath( homeBasePath );
376 /* initialize vfs paths */
377 if ( numBasePaths > MAX_BASE_PATHS ) {
378 numBasePaths = MAX_BASE_PATHS;
380 if ( numGamePaths > MAX_GAME_PATHS ) {
381 numGamePaths = MAX_GAME_PATHS;
384 /* walk the list of game paths */
385 //for( j = 0; j < numGamePaths; j++ )
387 /* walk the list of base paths */
388 // for( i = 0; i < numBasePaths; i++ )
390 /* create a full path and initialize it */
391 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
392 // vfsInitDirectory( temp );