]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/path_init.c
q3map2: getpwent() result may not be persistent
[xonotic/netradiant.git] / tools / quake3 / q3map2 / path_init.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define PATH_INIT_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* path support */
42 #define MAX_BASE_PATHS  10
43 #define MAX_GAME_PATHS  10
44
45 char                    *homePath;
46 char installPath[ MAX_OS_PATH ];
47
48 int numBasePaths;
49 char                    *basePaths[ MAX_BASE_PATHS ];
50 int numGamePaths;
51 char                    *gamePaths[ MAX_GAME_PATHS ];
52
53
54
55 /*
56    some of this code is based off the original q3map port from loki
57    and finds various paths. moved here from bsp.c for clarity.
58  */
59
60 /*
61    PathLokiGetHomeDir()
62    gets the user's home dir (for ~/.q3a)
63  */
64
65 char *LokiGetHomeDir( void ){
66         #ifndef Q_UNIX
67         return NULL;
68         #else
69         static char     buf[ 4096 ];
70         struct passwd   pw, *pwp;
71         char            *home;
72
73
74         /* get the home environment variable */
75         home = getenv( "HOME" );
76         if ( home ) {
77                 return Q_strncpyz( buf, home, sizeof( buf ) );
78         }
79
80         /* look up home dir in password database */
81         if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
82                 return pw.pw_dir;
83         }
84
85         return NULL;
86         #endif
87 }
88
89
90
91 /*
92    PathLokiInitPaths()
93    initializes some paths on linux/os x
94  */
95
96 void LokiInitPaths( char *argv0 ){
97         #ifndef Q_UNIX
98         /* this is kinda crap, but hey */
99         strcpy( installPath, "../" );
100         #else
101         char temp[ MAX_OS_PATH ];
102         char        *home;
103         char        *path;
104         char        *last;
105         qboolean found;
106
107
108         /* get home dir */
109         home = LokiGetHomeDir();
110         if ( home == NULL ) {
111                 home = ".";
112         }
113
114         /* do some path divining */
115         strcpy( temp, argv0 );
116         if ( strrchr( temp, '/' ) ) {
117                 argv0 = strrchr( argv0, '/' ) + 1;
118         }
119         else
120         {
121                 /* get path environment variable */
122                 path = getenv( "PATH" );
123
124                 /* minor setup */
125                 last[ 0 ] = path[ 0 ];
126                 last[ 1 ] = '\0';
127                 found = qfalse;
128
129                 /* go through each : segment of path */
130                 while ( last[ 0 ] != '\0' && found == qfalse )
131                 {
132                         /* null out temp */
133                         temp[ 0 ] = '\0';
134
135                         /* find next chunk */
136                         last = strchr( path, ':' );
137                         if ( last == NULL ) {
138                                 last = path + strlen( path );
139                         }
140
141                         /* found home dir candidate */
142                         if ( *path == '~' ) {
143                                 strcpy( temp, home );
144                                 path++;
145                         }
146
147                         /* concatenate */
148                         if ( last > ( path + 1 ) ) {
149                                 strncat( temp, path, ( last - path ) );
150                                 strcat( temp, "/" );
151                         }
152                         strcat( temp, "./" );
153                         strcat( temp, argv0 );
154
155                         /* verify the path */
156                         if ( access( temp, X_OK ) == 0 ) {
157                                 found++;
158                         }
159                         path = last + 1;
160                 }
161         }
162
163         /* flake */
164         if ( realpath( temp, installPath ) ) {
165                 /* q3map is in "tools/" */
166                 *( strrchr( installPath, '/' ) ) = '\0';
167                 *( strrchr( installPath, '/' ) + 1 ) = '\0';
168         }
169
170         /* set home path */
171         homePath = home;
172         #endif
173 }
174
175
176
177 /*
178    CleanPath() - ydnar
179    cleans a dos path \ -> /
180  */
181
182 void CleanPath( char *path ){
183         while ( *path )
184         {
185                 if ( *path == '\\' ) {
186                         *path = '/';
187                 }
188                 path++;
189         }
190 }
191
192
193
194 /*
195    GetGame() - ydnar
196    gets the game_t based on a -game argument
197    returns NULL if no match found
198  */
199
200 game_t *GetGame( char *arg ){
201         int i;
202
203
204         /* dummy check */
205         if ( arg == NULL || arg[ 0 ] == '\0' ) {
206                 return NULL;
207         }
208
209         /* joke */
210         if ( !Q_stricmp( arg, "quake1" ) ||
211                  !Q_stricmp( arg, "quake2" ) ||
212                  !Q_stricmp( arg, "unreal" ) ||
213                  !Q_stricmp( arg, "ut2k3" ) ||
214                  !Q_stricmp( arg, "dn3d" ) ||
215                  !Q_stricmp( arg, "dnf" ) ||
216                  !Q_stricmp( arg, "hl" ) ) {
217                 Sys_Printf( "April fools, silly rabbit!\n" );
218                 exit( 0 );
219         }
220
221         /* test it */
222         i = 0;
223         while ( games[ i ].arg != NULL )
224         {
225                 if ( Q_stricmp( arg, games[ i ].arg ) == 0 ) {
226                         return &games[ i ];
227                 }
228                 i++;
229         }
230
231         /* no matching game */
232         return NULL;
233 }
234
235
236
237 /*
238    AddBasePath() - ydnar
239    adds a base path to the list
240  */
241
242 void AddBasePath( char *path ){
243         /* dummy check */
244         if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) {
245                 return;
246         }
247
248         /* add it to the list */
249         basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
250         strcpy( basePaths[ numBasePaths ], path );
251         CleanPath( basePaths[ numBasePaths ] );
252         numBasePaths++;
253 }
254
255
256
257 /*
258    AddHomeBasePath() - ydnar
259    adds a base path to the beginning of the list, prefixed by ~/
260  */
261
262 void AddHomeBasePath( char *path ){
263         #ifdef Q_UNIX
264         int i;
265         char temp[ MAX_OS_PATH ];
266
267
268         /* dummy check */
269         if ( path == NULL || path[ 0 ] == '\0' ) {
270                 return;
271         }
272
273         /* make a hole */
274         for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ )
275                 basePaths[ i + 1 ] = basePaths[ i ];
276
277         /* concatenate home dir and path */
278         sprintf( temp, "%s/%s", homePath, path );
279
280         /* add it to the list */
281         basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
282         strcpy( basePaths[ 0 ], temp );
283         CleanPath( basePaths[ 0 ] );
284         numBasePaths++;
285         #endif
286 }
287
288
289
290 /*
291    AddGamePath() - ydnar
292    adds a game path to the list
293  */
294
295 void AddGamePath( char *path ){
296         int i;
297
298         /* dummy check */
299         if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) {
300                 return;
301         }
302
303         /* add it to the list */
304         gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
305         strcpy( gamePaths[ numGamePaths ], path );
306         CleanPath( gamePaths[ numGamePaths ] );
307         numGamePaths++;
308
309         /* don't add it if it's already there */
310         for ( i = 0; i < numGamePaths - 1; i++ )
311         {
312                 if ( strcmp( gamePaths[i], gamePaths[numGamePaths - 1] ) == 0 ) {
313                         free( gamePaths[numGamePaths - 1] );
314                         gamePaths[numGamePaths - 1] = NULL;
315                         numGamePaths--;
316                         break;
317                 }
318         }
319
320 }
321
322
323
324
325 /*
326    InitPaths() - ydnar
327    cleaned up some of the path initialization code from bsp.c
328    will remove any arguments it uses
329  */
330
331 void InitPaths( int *argc, char **argv ){
332         int i, j, k, len, len2;
333         char temp[ MAX_OS_PATH ];
334
335
336         /* note it */
337         Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" );
338
339         /* get the install path for backup */
340         LokiInitPaths( argv[ 0 ] );
341
342         /* set game to default (q3a) */
343         game = &games[ 0 ];
344         numBasePaths = 0;
345         numGamePaths = 0;
346
347         /* parse through the arguments and extract those relevant to paths */
348         for ( i = 0; i < *argc; i++ )
349         {
350                 /* check for null */
351                 if ( argv[ i ] == NULL ) {
352                         continue;
353                 }
354
355                 /* -game */
356                 if ( strcmp( argv[ i ], "-game" ) == 0 ) {
357                         if ( ++i >= *argc ) {
358                                 Error( "Out of arguments: No game specified after %s", argv[ i - 1 ] );
359                         }
360                         argv[ i - 1 ] = NULL;
361                         game = GetGame( argv[ i ] );
362                         if ( game == NULL ) {
363                                 game = &games[ 0 ];
364                         }
365                         argv[ i ] = NULL;
366                 }
367
368                 /* -fs_basepath */
369                 else if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) {
370                         if ( ++i >= *argc ) {
371                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
372                         }
373                         argv[ i - 1 ] = NULL;
374                         AddBasePath( argv[ i ] );
375                         argv[ i ] = NULL;
376                 }
377
378                 /* -fs_game */
379                 else if ( strcmp( argv[ i ], "-fs_game" ) == 0 ) {
380                         if ( ++i >= *argc ) {
381                                 Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] );
382                         }
383                         argv[ i - 1 ] = NULL;
384                         AddGamePath( argv[ i ] );
385                         argv[ i ] = NULL;
386                 }
387         }
388
389         /* remove processed arguments */
390         for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ )
391         {
392                 for ( ; j < *argc && argv[ j ] == NULL; j++ ) ;
393                 argv[ i ] = argv[ j ];
394                 if ( argv[ i ] != NULL ) {
395                         k++;
396                 }
397         }
398         *argc = k;
399
400         /* add standard game path */
401         AddGamePath( game->gamePath );
402
403         /* if there is no base path set, figure it out */
404         if ( numBasePaths == 0 ) {
405                 /* this is another crappy replacement for SetQdirFromPath() */
406                 len2 = strlen( game->magic );
407                 for ( i = 0; i < *argc && numBasePaths == 0; i++ )
408                 {
409                         /* extract the arg */
410                         strcpy( temp, argv[ i ] );
411                         CleanPath( temp );
412                         len = strlen( temp );
413                         Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game->magic, temp, i );
414
415                         /* this is slow, but only done once */
416                         for ( j = 0; j < ( len - len2 ); j++ )
417                         {
418                                 /* check for the game's magic word */
419                                 if ( Q_strncasecmp( &temp[ j ], game->magic, len2 ) == 0 ) {
420                                         /* now find the next slash and nuke everything after it */
421                                         while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ;
422                                         temp[ j ] = '\0';
423
424                                         /* add this as a base path */
425                                         AddBasePath( temp );
426                                         break;
427                                 }
428                         }
429                 }
430
431                 /* add install path */
432                 if ( numBasePaths == 0 ) {
433                         AddBasePath( installPath );
434                 }
435
436                 /* check again */
437                 if ( numBasePaths == 0 ) {
438                         Error( "Failed to find a valid base path." );
439                 }
440         }
441
442         /* this only affects unix */
443         AddHomeBasePath( game->homeBasePath );
444
445         /* initialize vfs paths */
446         if ( numBasePaths > MAX_BASE_PATHS ) {
447                 numBasePaths = MAX_BASE_PATHS;
448         }
449         if ( numGamePaths > MAX_GAME_PATHS ) {
450                 numGamePaths = MAX_GAME_PATHS;
451         }
452
453         /* walk the list of game paths */
454         for ( j = 0; j < numGamePaths; j++ )
455         {
456                 /* walk the list of base paths */
457                 for ( i = 0; i < numBasePaths; i++ )
458                 {
459                         /* create a full path and initialize it */
460                         sprintf( temp, "%s/%s/", basePaths[ i ], gamePaths[ j ] );
461                         vfsInitDirectory( temp );
462                 }
463         }
464
465         /* done */
466         Sys_Printf( "\n" );
467 }