From b604a7d7c2306483697c5b8f2dea4d8a6ccbafd5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 29 Jan 2014 19:29:55 -0500 Subject: [PATCH] Respect HOME environment variable if set Signed-off-by: Anders Kaseorg --- src/frontend/mosh-server.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 0e611f0..7c971bc 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -776,17 +776,21 @@ static void print_motd( void ) static void chdir_homedir( void ) { - struct passwd *pw = getpwuid( geteuid() ); - if ( pw == NULL ) { - perror( "getpwuid" ); - return; /* non-fatal */ + 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" ); } }