pwd.h中的函数

看手册:

The <pwd.h> header shall provide a definition for struct passwd, which shall include at least the following members:

char    *pw_name   User's login name. 
uid_t    pw_uid    Numerical user ID. 
gid_t    pw_gid    Numerical group ID. 
char    *pw_dir    Initial working directory. 
char    *pw_shell  Program to use as shell. 

测试:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 
 5 #include <pwd.h>
 6 #include <sys/types.h>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     
11     struct passwd *my_info;
12     if(argc!=2)
13     {
14         printf("usage: %s username\n",argv[0]);
15         exit(0);
16     }
17     my_info = getpwnam(argv[1]);
18     if(my_info==NULL)
19     {
20         printf("could not find user '%s'\n",argv[1]);
21         exit(1);
22     }
23     printf( "name = %s\n", my_info->pw_name );
24     printf( "uid = %d\n", my_info->pw_uid );
25     printf( "gid = %d\n", my_info->pw_gid );
26     printf( "dir = %s\n", my_info->pw_dir );
27     printf( "shell = %s\n", my_info->pw_shell );
28     return 0;
29 }

 另外一个是getpwuid函数,使用起来是差不多的。

 

posted @ 2020-05-19 20:09  castor_xu  阅读(1337)  评论(0编辑  收藏  举报