Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the username in C/C++ in Linux?

Tags:

c++

c

linux

posix

How can I get the actual "username" without using the environment (getenv, ...) in a program? Environment is C/C++ with Linux.

like image 843
Zat42 Avatar asked Jan 21 '12 13:01

Zat42


People also ask

How can I find my UID by name?

Use `getpwnam` to get uid by user's login name - C Lang.


1 Answers

The function getlogin_r() defined in unistd.h returns the username. See man getlogin_r for more information.

Its signature is:

int getlogin_r(char *buf, size_t bufsize); 

Needless to say, this function can just as easily be called in C or C++.

like image 167
drrlvn Avatar answered Oct 02 '22 12:10

drrlvn