Should we simplify the code to demand $LOGNAME exists and use that?
You can use getlogin(3) or getlogin_r(3) as per ISO/IEC 9945-1:1996.
That seems worse. Linux's getlogin(3) says in Description that $LOGNAME
is often more useful, and its Bugs section is an amusing read.
getlogin(3p) from POSIX is also available for detail.
getlogin() copes with multiple usernames for the same ID and finds the
one used on this controlling terminal, checking FDs 0-2 until ‘success’
and crawling utmp. That's overkill for our purposes. $LOGNAME is in
our memory and a function call away, plus it's easy to document and
useful to override.