Skip to content

Commit

Permalink
q3map2: getpwent() result may not be persistent
Browse files Browse the repository at this point in the history
Use getpwuid_r() instead and store the path in a static buffer.
  • Loading branch information
bnoordhuis committed Mar 18, 2012
1 parent c2be26a commit d7e9dab
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions tools/quake3/q3map2/path_init.c
Expand Up @@ -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
}

Expand Down

0 comments on commit d7e9dab

Please sign in to comment.