Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

shell

unix

Please i am looking forward to learn how to print the current logged-in user and system name in Unix.

#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int argc, char **argv)   
{      
     //Print the current logged-in user / username.   
     //Print the name of the system / computer name.

     return 0;
}

I would be grateful if you can provide a line of code or two as demonstration. Thanks

like image 752
CompilingCyborg Avatar asked Dec 17 '22 05:12

CompilingCyborg


2 Answers

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

Machine name --> gethostname().

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

like image 196
fge Avatar answered Jan 11 '23 17:01

fge


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

like image 32
Basile Starynkevitch Avatar answered Jan 11 '23 15:01

Basile Starynkevitch