| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include "globaldefs.h" |
| 28 |
|
| 29 |
#if !GDEF_OS_WINDOWS |
| 30 |
#include <unistd.h> |
| 31 |
#include <pwd.h> |
| 32 |
#include <limits.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
#include "cmdlib.h" |
| 37 |
#include "inout.h" |
| 38 |
|
| 39 |
|
| 40 |
#define MAX_BASE_PATHS 10 |
| 41 |
#define MAX_GAME_PATHS 10 |
| 42 |
|
| 43 |
char *homePath; |
| 44 |
char installPath[ MAX_OS_PATH ]; |
| 45 |
|
| 46 |
int numBasePaths; |
| 47 |
char *basePaths[ MAX_BASE_PATHS ]; |
| 48 |
int numGamePaths; |
| 49 |
char *gamePaths[ MAX_GAME_PATHS ]; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
char *LokiGetHomeDir( void ){ |
| 62 |
#if GDEF_OS_WINDOWS |
| 63 |
return NULL; |
| 64 |
#else |
| 65 |
char *home; |
| 66 |
uid_t id; |
| 67 |
struct passwd *pwd; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
home = getenv( "HOME" ); |
| 72 |
if ( home == NULL ) { |
| 73 |
|
| 74 |
id = getuid(); |
| 75 |
setpwent(); |
| 76 |
while ( ( pwd = getpwent() ) != NULL ) |
| 77 |
{ |
| 78 |
if ( pwd->pw_uid == id ) { |
| 79 |
home = pwd->pw_dir; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
endpwent(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
return home; |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void LokiInitPaths( char *argv0 ){ |
| 99 |
#if GDEF_OS_WINDOWS |
| 100 |
|
| 101 |
strcpy( installPath, "../" ); |
| 102 |
#else |
| 103 |
char temp[ MAX_OS_PATH ]; |
| 104 |
char *home; |
| 105 |
char *path; |
| 106 |
char *last; |
| 107 |
qboolean found; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
home = LokiGetHomeDir(); |
| 112 |
if ( home == NULL ) { |
| 113 |
home = "."; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
strcpy( temp, argv0 ); |
| 118 |
if ( strrchr( temp, '/' ) ) { |
| 119 |
argv0 = strrchr( argv0, '/' ) + 1; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
|
| 124 |
path = getenv( "PATH" ); |
| 125 |
|
| 126 |
|
| 127 |
last[ 0 ] = path[ 0 ]; |
| 128 |
last[ 1 ] = '\0'; |
| 129 |
found = false; |
| 130 |
|
| 131 |
|
| 132 |
while ( last[ 0 ] != '\0' && found == false ) |
| 133 |
{ |
| 134 |
|
| 135 |
temp[ 0 ] = '\0'; |
| 136 |
|
| 137 |
|
| 138 |
last = strchr( path, ':' ); |
| 139 |
if ( last == NULL ) { |
| 140 |
last = path + strlen( path ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
if ( *path == '~' ) { |
| 145 |
strcpy( temp, home ); |
| 146 |
path++; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if ( last > ( path + 1 ) ) { |
| 151 |
strncat( temp, path, ( last - path ) ); |
| 152 |
strcat( temp, "/" ); |
| 153 |
} |
| 154 |
strcat( temp, "./" ); |
| 155 |
strcat( temp, argv0 ); |
| 156 |
|
| 157 |
|
| 158 |
if ( access( temp, X_OK ) == 0 ) { |
| 159 |
found++; |
| 160 |
} |
| 161 |
path = last + 1; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if ( realpath( temp, installPath ) ) { |
| 167 |
|
| 168 |
*( strrchr( installPath, '/' ) ) = '\0'; |
| 169 |
*( strrchr( installPath, '/' ) + 1 ) = '\0'; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
homePath = home; |
| 174 |
#endif |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
void CleanPath( char *path ){ |
| 185 |
while ( *path ) |
| 186 |
{ |
| 187 |
if ( *path == '\\' ) { |
| 188 |
*path = '/'; |
| 189 |
} |
| 190 |
path++; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
void AddBasePath( char *path ){ |
| 200 |
|
| 201 |
if ( path == NULL || path[ 0 ] == '\0' || numBasePaths >= MAX_BASE_PATHS ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 ); |
| 207 |
strcpy( basePaths[ numBasePaths ], path ); |
| 208 |
CleanPath( basePaths[ numBasePaths ] ); |
| 209 |
numBasePaths++; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
void AddHomeBasePath( char *path ){ |
| 220 |
#if !GDEF_OS_WINDOWS |
| 221 |
int i; |
| 222 |
char temp[ MAX_OS_PATH ]; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if ( path == NULL || path[ 0 ] == '\0' ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
for ( i = 0; i < ( MAX_BASE_PATHS - 1 ); i++ ) |
| 232 |
basePaths[ i + 1 ] = basePaths[ i ]; |
| 233 |
|
| 234 |
|
| 235 |
sprintf( temp, "%s/%s", homePath, path ); |
| 236 |
|
| 237 |
|
| 238 |
basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 ); |
| 239 |
strcpy( basePaths[ 0 ], temp ); |
| 240 |
CleanPath( basePaths[ 0 ] ); |
| 241 |
numBasePaths++; |
| 242 |
#endif |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
void AddGamePath( char *path ){ |
| 253 |
|
| 254 |
if ( path == NULL || path[ 0 ] == '\0' || numGamePaths >= MAX_GAME_PATHS ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 ); |
| 260 |
strcpy( gamePaths[ numGamePaths ], path ); |
| 261 |
CleanPath( gamePaths[ numGamePaths ] ); |
| 262 |
numGamePaths++; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
void InitPaths( int *argc, char **argv ){ |
| 275 |
int i, j, k, len, len2; |
| 276 |
char temp[ MAX_OS_PATH ]; |
| 277 |
char gamePath[MAX_OS_PATH], homeBasePath[MAX_OS_PATH], game_magic[10]; |
| 278 |
|
| 279 |
strcpy( gamePath, "baseq2" ); |
| 280 |
strcpy( game_magic, "quake" ); |
| 281 |
strcpy( homeBasePath, ".quake2" ); |
| 282 |
|
| 283 |
|
| 284 |
Sys_FPrintf( SYS_VRB, "--- InitPaths ---\n" ); |
| 285 |
|
| 286 |
|
| 287 |
LokiInitPaths( argv[ 0 ] ); |
| 288 |
|
| 289 |
|
| 290 |
numBasePaths = 0; |
| 291 |
numGamePaths = 0; |
| 292 |
|
| 293 |
|
| 294 |
for ( i = 0; i < *argc; i++ ) |
| 295 |
{ |
| 296 |
|
| 297 |
if ( argv[ i ] == NULL ) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
if ( strcmp( argv[ i ], "-fs_basepath" ) == 0 ) { |
| 303 |
if ( ++i >= *argc ) { |
| 304 |
Error( "Out of arguments: No path specified after %s.", argv[ i - 1 ] ); |
| 305 |
} |
| 306 |
argv[ i - 1 ] = NULL; |
| 307 |
AddBasePath( argv[ i ] ); |
| 308 |
argv[ i ] = NULL; |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
for ( i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++ ) |
| 315 |
{ |
| 316 |
for ( j; j < *argc && argv[ j ] == NULL; j++ ) ; |
| 317 |
argv[ i ] = argv[ j ]; |
| 318 |
if ( argv[ i ] != NULL ) { |
| 319 |
k++; |
| 320 |
} |
| 321 |
} |
| 322 |
*argc = k; |
| 323 |
|
| 324 |
|
| 325 |
AddGamePath( gamePath ); |
| 326 |
|
| 327 |
|
| 328 |
if ( numBasePaths == 0 ) { |
| 329 |
|
| 330 |
len2 = strlen( game_magic ); |
| 331 |
for ( i = 0; i < *argc && numBasePaths == 0; i++ ) |
| 332 |
{ |
| 333 |
|
| 334 |
strcpy( temp, argv[ i ] ); |
| 335 |
CleanPath( temp ); |
| 336 |
len = strlen( temp ); |
| 337 |
Sys_FPrintf( SYS_VRB, "Searching for \"%s\" in \"%s\" (%d)...\n", game_magic, temp, i ); |
| 338 |
|
| 339 |
|
| 340 |
for ( j = 0; j < ( len - len2 ); j++ ) |
| 341 |
{ |
| 342 |
|
| 343 |
if ( Q_strncasecmp( &temp[ j ], game_magic, len2 ) == 0 ) { |
| 344 |
|
| 345 |
while ( temp[ ++j ] != '/' && temp[ j ] != '\0' ) ; |
| 346 |
temp[ j ] = '\0'; |
| 347 |
|
| 348 |
|
| 349 |
AddBasePath( temp ); |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
if ( numBasePaths == 0 ) { |
| 357 |
AddBasePath( installPath ); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
if ( numBasePaths == 0 ) { |
| 362 |
Error( "Failed to find a valid base path." ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
AddHomeBasePath( homeBasePath ); |
| 368 |
|
| 369 |
|
| 370 |
if ( numBasePaths > MAX_BASE_PATHS ) { |
| 371 |
numBasePaths = MAX_BASE_PATHS; |
| 372 |
} |
| 373 |
if ( numGamePaths > MAX_GAME_PATHS ) { |
| 374 |
numGamePaths = MAX_GAME_PATHS; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
Sys_Printf( "\n" ); |
| 391 |
} |