[GLLUG] ~ or $HOME ?

Dimitrios Siganos dimitris at siganos.org
Mon Mar 17 12:19:00 UTC 2014


On 17/03/14 12:03, Alain Williams wrote:
> I have just written a program that processes an xxxx.rc file:
> 
> * /etc/xxxx.rc
> 
> * ~/.xxxx.rc
> 
> I got a value for ~ from the password file - I was doing a getpwuid() anyway.
> 
> Should I instead use $HOME ? OK: $HOME and the value from the password entry
> will be the same most of the time, but *most*, not always.
> 
> Any opinions on what the 'right' thing to do ? Options:
> 
> a) $HOME
> 
> b) getpwuid() (or similar)
> 
> c) $HOME, but if that is not defined: getpwuid()

Hi,

The man page of getpwuid says you should $HOME.

"The pw_dir field contains the name of the initial working directory of
the user. Login programs use the value of this field to initialize the
HOME environment variable for the login shell. An application that wants
to determine its user's home directory should inspect the value of HOME
(rather than the value getpwuid(getuid())->pw_dir) since this allows the
user to modify their notion of "the home directory" during a login
session. To determine the (initial) home directory of another user, it
is necessary to use getpwnam("username")->pw_dir or similar."

Regards,
Dimitris






More information about the GLLUG mailing list