How to print the current user and system name in Unix?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 13:34:30

getuid() gets the id not the username. To get the username you'll have to additionally use getpwuid():

struct passwd *passwd;
passwd = getpwuid ( getuid()); 

printf("The Login Name is %s ", passwd->pw_name);

See it

And for getting the hostname you can use the gethostname() function.

User --> getuid() (see also geteuid()).

Machine name --> gethostname().

That is pure C. I don't know whether C++ has other library calls for that.

You need to call the uname, gethostname, getuid (and perhaps getgid) system calls, and to convert the numerical uid with getpwent function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!