#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage %s filename\n", argv[0]);
exit(1);
}
struct stat buf;
if (stat(argv[1], &buf) < 0) //获取该文件的元信息
{
perror("stat() ");
exit(1);
}
struct passwd *pd;
pd = getpwuid(buf.st_uid);//根据uid获得passwd文件中相应的条目
printf("%s \n", pd->pw_name);//passwd结构体中存放着uid对应的用户名
exit(0);
}
getpwuid:根据uid获取passwd中相应的一行
最新推荐文章于 2024-08-08 19:17:47 发布