On Mon, 2002-11-18 at 22:47, Eric Gillespie wrote:
Jon Steinhart <jon(_at_)fourwinds(_dot_)com> writes:
3. If the $HOME environment variable is set, mypath is copied from the
getenv return. Why? It's never changed.
4. If the $HOME environment variable is not set, mypath is copied from the
pw_dir member of the returned passwd structure. Now, I understand that
this is a static structure, but getpwuid is never called again so I
don't see why the copy is needed.
4. If the $HOME environment variable is not set, the pw_dir member of the
passwd structure returned by getpwuid() is checked for a NULL pointer.
This can never happen in a non-error return, which is already checked.
So why the superfluous check?
These checks are not superflous, they are for maintainability.
Two years from now someone will add a second getenv(3) call and
waste their (probably volunteer) time trying to figure out how
they busted the home dir variable.
The return value of getenv() is a pointer into the environment; a future
getenv() call will not overwrite it. So it's safe not to copy it unless
you anticipate a putenv(). (And I think it's safe even in the face of a
putenv(), actually.) A judgement call.
Your argument definitely holds for copying the getpwuid() value.
Checking the pw_dir element for NULL is superfluous unless you feel like
second-guessing the kernel and libc at every turn, a practice which
doesn't have much value.