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
26 #include "globaldefs.h"
28 #if GDEF_OS_LINUX || GDEF_OS_MACOS
46 #define MAX_BASE_PATHS 10
47 #define MAX_GAME_PATHS 10
50 char installPath[ MAX_OS_PATH ];
53 char *basePaths[ MAX_BASE_PATHS ];
55 char *gamePaths[ MAX_GAME_PATHS ];
58 some of this code is based off the original q3map port from loki
59 and finds various paths. moved here from bsp.c for clarity.
64 gets the user's home dir (for ~/.q3a)
67 char *LokiGetHomeDir( void ){
76 /* get the home environment variable */
77 home = getenv( "HOME" );
79 /* do some more digging */
82 while ( ( pwd = getpwent() ) != NULL )
84 if ( pwd->pw_uid == id ) {
101 initializes some paths on linux/os x
104 void LokiInitPaths( char *argv0 ){
106 /* this is kinda crap, but hey */
107 strcpy( installPath, "../" );
109 char temp[ MAX_OS_PATH ];
117 home = LokiGetHomeDir();
118 if ( home == NULL ) {
122 /* do some path divining */
123 strcpy( temp, argv0 );
124 if ( strrchr( temp, '/' ) ) {
125 argv0 = strrchr( argv0, '/' ) + 1;
129 /* get path environment variable */
130 path = getenv( "PATH" );
133 last[ 0 ] = path[ 0 ];
137 /* go through each : segment of path */
138 while ( last[ 0 ] != '\0' && found == false )
143 /* find next chunk */
144 last = strchr( path, ':' );
145 if ( last == NULL ) {
146 last = path + strlen( path );
149 /* found home dir candidate */
150 if ( *path == '~' ) {
151 strcpy( temp, home );
156 if ( last > ( path + 1 ) ) {
157 strncat( temp, path, ( last - path ) );
160 strcat( temp, "./" );
161 strcat( temp, argv0 );
163 /* verify the path */
164 if ( access( temp, X_OK ) == 0 ) {
172 if ( realpath( temp, installPath ) ) {
173 /* q3map is in "tools/" */
174 *( strrchr( installPath, '/' ) ) = '\0';
175 *( strrchr( installPath, '/' ) + 1 ) = '\0';
187 cleans a dos path \ -> /
190 void CleanPath( char *path ){
193 if ( *path == '\\' ) {
201 AddBasePath() - ydnar
202 adds a base path to the list
205 void AddBasePath( char *path ){
207 if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
211 /* add it to the list */
212 basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
213 strcpy( basePaths[ numBasePaths ], path );
214 CleanPath( basePaths[ numBasePaths ] );
221 AddHomeBasePath() - ydnar
222 adds a base path to the beginning of the list, prefixed by ~/
225 void AddHomeBasePath( char *path ){
228 char temp[ MAX_OS_PATH ];
232 if ( path == NULL || path[ 0 ] == '\0' ) {
237 for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
238 basePaths[ i + 1 ] = basePaths[ i ];
240 /* concatenate home dir and path */
241 sprintf( temp, "%s/%s", homePath, path );
243 /* add it to the list */
244 basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
245 strcpy( basePaths[ 0 ], temp );
246 CleanPath( basePaths[ 0 ] );
254 AddGamePath() - ydnar
255 adds a game path to the list
258 void AddGamePath( char *path ){
260 if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
264 /* add it to the list */
265 gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
266 strcpy( gamePaths[ numGamePaths ], path );
267 CleanPath( gamePaths[ numGamePaths ] );
276 cleaned up some of the path initialization code from bsp.c
277 will remove any arguments it uses
280 void InitPaths( int *argc, char **argv ){
281 int i, j, k, len, len2;
282 char temp[ MAX_OS_PATH ];
283 char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10];
285 strcpy( gamePath, "baseq2" );
286 strcpy( game_magic, "quake" );
287 strcpy( homeBasePath, ".quake2" );
290 Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
292 /* get the install path for backup */
293 LokiInitPaths( argv[ 0 ] );
295 /* set game to default (q3a) */
299 /* parse through the arguments and extract those relevant to paths */
300 for ( i = 0; i < *argc; i++ )
303 if ( argv[ i ] == NULL ) {
308 if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
309 if ( ++i >= *argc ) {
310 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
312 argv[ i - 1 ] = NULL;
313 AddBasePath( argv[ i ] );
319 /* remove processed arguments */
320 for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
322 for ( j; j < *argc && argv[ j ] == NULL; j++ ) ;
323 argv[ i ] = argv[ j ];
324 if ( argv[ i ] != NULL ) {
330 /* add standard game path */
331 AddGamePath( gamePath );
333 /* if there is no base path set, figure it out */
334 if ( numBasePaths == 0 ) {
335 /* this is another crappy replacement for SetQdirFromPath() */
336 len2 = strlen( game_magic );
337 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
339 /* extract the arg */
340 strcpy( temp, argv[ i ] );
342 len = strlen( temp );
343 Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i );
345 /* this is slow, but only done once */
346 for ( j = 0; j < ( len - len2 ); j++ )
348 /* check for the game's magic word */
349 if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) {
350 /* now find the next slash and nuke everything after it */
351 while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
354 /* add this as a base path */
361 /* add install path */
362 if ( numBasePaths == 0 ) {
363 AddBasePath( installPath );
367 if ( numBasePaths == 0 ) {
368 Error( "Failed to find a valid base path." );
372 /* this only affects unix */
373 AddHomeBasePath( homeBasePath );
375 /* initialize vfs paths */
376 if ( numBasePaths > MAX_BASE_PATHS ) {
377 numBasePaths = MAX_BASE_PATHS;
379 if ( numGamePaths > MAX_GAME_PATHS ) {
380 numGamePaths = MAX_GAME_PATHS;
383 /* walk the list of game paths */
384 //for( j = 0; j < numGamePaths; j++ )
386 /* walk the list of base paths */
387 // for( i = 0; i < numBasePaths; i++ )
389 /* create a full path and initialize it */
390 // sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
391 // vfsInitDirectory( temp );