Merge remote-tracking branch 'github/master'
[xonotic/netradiant.git] / tools / quake3 / q3map2 / path_init.c
index 2591c5bf45b54ba9e7467bd43a42e9f9f139ae28..07ebc31b1e154dc2ade01a871560ad49a3b1635a 100644 (file)
@@ -66,26 +66,21 @@ char *LokiGetHomeDir( void ){
        #ifndef Q_UNIX
        return NULL;
        #else
        #ifndef Q_UNIX
        return NULL;
        #else
+       static char     buf[ 4096 ];
+       struct passwd   pw, *pwp;
        char            *home;
        char            *home;
-       uid_t id;
-       struct passwd   *pwd;
        static char homeBuf[MAX_OS_PATH];
 
 
        /* get the home environment variable */
        home = getenv( "HOME" );
        static char homeBuf[MAX_OS_PATH];
 
 
        /* get the home environment variable */
        home = getenv( "HOME" );
-       if ( home == NULL ) {
-               /* do some more digging */
-               id = getuid();
-               setpwent();
-               while ( ( pwd = getpwent() ) != NULL )
-               {
-                       if ( pwd->pw_uid == id ) {
-                               home = pwd->pw_dir;
-                               break;
-                       }
+
+       /* look up home dir in password database */
+       if(!home)
+       {
+               if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
+                       return pw.pw_dir;
                }
                }
-               endpwent();
        }
 
        snprintf( homeBuf, sizeof( homeBuf ), "%s/.", home );
        }
 
        snprintf( homeBuf, sizeof( homeBuf ), "%s/.", home );
@@ -130,21 +125,16 @@ void LokiInitPaths( char *argv0 ){
        qboolean found;
 
 
        qboolean found;
 
 
+       path = getenv( "PATH" );
+
        /* do some path divining */
        /* do some path divining */
-       strcpy( temp, argv0 );
-       if ( strrchr( argv0, '/' ) ) {
+       Q_strncpyz( temp, argv0, sizeof( temp ) );
+       if ( strrchr( temp, '/' ) ) {
                argv0 = strrchr( argv0, '/' ) + 1;
        }
                argv0 = strrchr( argv0, '/' ) + 1;
        }
-       else
-       {
-               /* get path environment variable */
-               path = getenv( "PATH" );
-
-               /* minor setup */
-               last = last0;
-               last[ 0 ] = path[ 0 ];
-               last[ 1 ] = '\0';
+       else if ( path ) {
                found = qfalse;
                found = qfalse;
+               last = path;
 
                /* go through each : segment of path */
                while ( last[ 0 ] != '\0' && found == qfalse )
 
                /* go through each : segment of path */
                while ( last[ 0 ] != '\0' && found == qfalse )
@@ -160,17 +150,17 @@ void LokiInitPaths( char *argv0 ){
 
                        /* found home dir candidate */
                        if ( *path == '~' ) {
 
                        /* found home dir candidate */
                        if ( *path == '~' ) {
-                               strcpy( temp, home );
+                               Q_strncpyz( temp, home, sizeof( temp ) );
                                path++;
                        }
 
                        /* concatenate */
                        if ( last > ( path + 1 ) ) {
                                path++;
                        }
 
                        /* concatenate */
                        if ( last > ( path + 1 ) ) {
-                               strncat( temp, path, ( last - path ) );
-                               strcat( temp, "/" );
+                               Q_strncat( temp, sizeof( temp ), path, ( last - path ) );
+                               Q_strcat( temp, sizeof( temp ), "/" );
                        }
                        }
-                       strcat( temp, "./" );
-                       strcat( temp, argv0 );
+                       Q_strcat( temp, sizeof( temp ), "./" );
+                       Q_strcat( temp, sizeof( temp ), argv0 );
 
                        /* verify the path */
                        if ( access( temp, X_OK ) == 0 ) {
 
                        /* verify the path */
                        if ( access( temp, X_OK ) == 0 ) {