How can I get the actual "username" without using the environment (getenv, ...) in a program?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
The function
getlogin_r()
defined inunistd.h
returns the username. Seeman getlogin_r
for more information.Its signature is:
Needless to say, this function can just as easily be called in C or C++.
From http://www.unix.com/programming/21041-getting-username-c-program-unix.html :
Just take main lines and encapsulate it in class:
For C only: