您的位置:首页 > 其它

文件2

2015-07-11 10:59 239 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq616478441/article/details/46840261

#Include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

1int stat(const char *path,struct stat *buf);

2int fstat(int fd,struct stat *buf);

3int lstat(const char *path,struct stat *buf);


#include <pwd.h>

4struct passwd *getpwuid(uid_t uid)//根据uid来获得用户信息

struct passwd

{

   char *pw_name;//usrname

   char *pw_passwd;//userword

   uid_t pw_uid;//  user ID

   gid_t pw_gid; //group ID

   char *pw_gecos; //user information

   char *pw_dir;//home diectory

   char *pw_shell;// shell

};


5struct group *getgrgid(gid_t gid)//根据组ID来获得信息

struct group

{

 char *gr_name;

 char *gr_passwd;

 gid_t  gr_gid;

 char **gr_mem;

};


例子:

#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
int main(int argc,char *argv[])
{
struct stat filestat;
lstat(argv[1],&filestat);
struct passwd *a=(struct passwd *)getpwuid(filestat.st_uid);
struct group *gp;
gp=getgrgid(filestat.st_gid);
printf("gid name is:%s\n",gp->gr_name);
printf("username :%s\n",a->pw_name);
printf("passwd :%s\n",a->pw_passwd);
printf("uid=%d\n",a->pw_uid);
printf("gid=%d\n",a->pw_gid);
printf("shell=%s\n",a->pw_shell);
printf("dir=%s\n",a->pw_dir);
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: