如何从用户ID检索用户名 [英] How to retrieve the user name from the user ID

查看:109
本文介绍了如何从用户ID检索用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Unix的同时在Unix上实现(ls)命令.在使用(-l)标志实现(ls)命令的编码部分期间,我看到我必须提示文件的用户名和组名.到目前为止,我已经从以下几行获得了用户ID和组ID:

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 the (-l) flag, I see that I have to prompt the user and group names of the file. So far I have the user and group IDs from the following lines:

struct stat statBuf;

statBuf.st_uid; //For the user id. 
statBuf.st_gid; //For the group id. 

在Unix上默认的(ls)命令中,文件信息的打印方式是显示用户名而不是用户ID.

In the default (ls) command on Unix, the information of the file is printed in such a way that the user name is shown instead of the user id.

有人可以帮助我找到正确的方法来从其ID中检索用户名和组名吗?

Can anyone help me to find the correct methodology to retrieve the user and group names from their IDs?

推荐答案

您使用 getpwuid 查找特定UID(包括用户名,但现在不包括密码本身)和

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.

这篇关于如何从用户ID检索用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆