q3map2: getpwent() result may not be persistent
authorBen Noordhuis <info@bnoordhuis.nl>
Sat, 17 Mar 2012 23:26:28 +0000 (00:26 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Sun, 18 Mar 2012 01:17:09 +0000 (02:17 +0100)
Use getpwuid_r() instead and store the path in a static buffer.

tools/quake3/q3map2/path_init.c

index 6bd061350d7327034afa11167f5fcb89af4139f1..ac8fa328f8501f2369d9302260eeb117264873e6 100644 (file)
@@ -66,29 +66,23 @@ char *LokiGetHomeDir( void ){
        #ifndef Q_UNIX
        return NULL;
        #else
+       static char     buf[ 4096 ];
+       struct passwd   pw, *pwp;
        char            *home;
-       uid_t id;
-       struct passwd   *pwd;
 
 
        /* 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;
-                       }
-               }
-               endpwent();
+       if ( home ) {
+               return Q_strncpyz( buf, home, sizeof( buf ) );
        }
 
-       /* return it */
-       return home;
+       /* look up home dir in password database */
+       if ( getpwuid_r( getuid(), &pw, buf, sizeof( buf ), &pwp ) == 0 ) {
+               return pw.pw_dir;
+       }
+
+       return NULL;
        #endif
 }