Respect HOME environment variable if set

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2014-01-29 19:29:55 -05:00
committed by John Hood
parent 9019f62bf6
commit b604a7d7c2
+6 -2
View File
@@ -776,17 +776,21 @@ static void print_motd( void )
static void chdir_homedir( void )
{
const char *home = getenv( "HOME" );
if ( home == NULL ) {
struct passwd *pw = getpwuid( geteuid() );
if ( pw == NULL ) {
perror( "getpwuid" );
return; /* non-fatal */
}
home = pw->pw_dir;
}
if ( chdir( pw->pw_dir ) < 0 ) {
if ( chdir( home ) < 0 ) {
perror( "chdir" );
}
if ( setenv( "PWD", pw->pw_dir, 1 ) < 0 ) {
if ( setenv( "PWD", home, 1 ) < 0 ) {
perror( "setenv" );
}
}