This removes the dependency on a TTY and now does what is expected: gets the (possibly impersonated) username of the current process/thread. Implementation and tests updated, still uses reentrant versions where possible.