In a C program on Linux, I get UID and GID of a file, but I want to print the username and group name. So how to map UID/GID to username/group name?
Use the getpwuid
function to get the username associated with a uid:
struct passwd *getpwuid(uid_t uid);
The name is returned in passwd.pw_name
For groups, use getgrgid
:
struct group *getgrgid(gid_t gid);
The name is returned in group.gr_name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.