UNIX环境高级编程学习之第六章系统数据文件和信息-实现uid to name
[code lang="cpp"]#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <pwd.h>
char * getuidtoname(unsigned int uid)
{
struct passwd *ptr;
setpwent();//实现打开/etc/passwd文件
while((ptr = getpwent()) != NULL)//读取一条记录放到ptr中
{
if(uid == ptr->pw_uid)
{
break;
}
}
endpwent();
return ptr->pw_name;
}
char * getpwname_my(uid_t uid)
{
struct passwd *ptr;
ptr = getpwuid(uid);
return ptr->pw_name;
}
int main()
{
char * name;
name = getuidtoname(1000);
printf("name = %s/n", name);
name = getpwname_my(1000);
printf("name = %s/n", name);
return 0;
}
[/code]
文章评论