pwd.h中的函数

时间:2020-05-19
本文章向大家介绍pwd.h中的函数,主要包括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函数,使用起来是差不多的。

原文地址:https://www.cnblogs.com/castor-xu/p/12919151.html