How to retrieve the user name from the user ID

前端 未结 2 1047
故里飘歌
故里飘歌 2020-12-11 16:22

I am implementing the (ls) command on Unix while learning from a book. During the coding part of my implementation of the (ls) command with

相关标签:
2条回答
  • 2020-12-11 16:44

    check my code for username:

    #include <unistd.h>
    #include <sys/types.h>
    #include <pwd.h>
    
    string getUser(uid_t uid)
    {
        struct passwd *pws;
        pws = getpwuid(uid);
            return pws->pw_name;
    }
    

    for groupname you can use getgrgid.

    0 讨论(0)
  • 2020-12-11 16:55

    You use getpwuid to look up the password file entry for a particular UID (which includes the user name, but now not the password itself) and getgrgid to look up the group file entry for a particular GID.

    0 讨论(0)
提交回复
热议问题