扫描密码文件和组文件中的所有记录。

来源:互联网 发布:局域网视频聊天软件 编辑:程序博客网 时间:2024/05/31 19:57

<strong><span style="color:#ff6666;">扫描密码文件和组文件中的所有记录</span></strong>    函数setpwent(),getpwent()和endpwent()的作用是按顺序扫描密码文件中的记录。       #include <sys/types.h>       #include <pwd.h>       struct passwd *getpwent(void);       void setpwent(void);       void endpwent(void);        getpwent()能够从密码文件中逐条返回记录,如果不再有返回记录或者出错时,则返回NULL。getpwent一经调用会自动打开密码文件。当密码文件处理完毕后,可调用endpwent()将其关闭。              The passwd structure is defined in <pwd.h> as follows:           struct passwd {               char   *pw_name;       /* username */               char   *pw_passwd;     /* user password */               uid_t   pw_uid;        /* user ID */               gid_t   pw_gid;        /* group ID */               char   *pw_gecos;      /* user information */               char   *pw_dir;        /* home directory */               char   *pw_shell;      /* shell program */           };  1 #include <stdio.h>  2 #include <stdlib.h>  3 #include <unistd.h>  4 #include <pwd.h>  5 int main()  6 {  7     struct passwd *pwd;  8     while((pwd = getpwent())!=NULL)  9     { 10         printf("%-8s  %5ld\n",pwd->pw_name,(long)pwd->pw_uid); 11     } 12     endpwent(); 13 }songxl@ubuntu:~/ds/day04$ a.outroot          0daemon        1bin           2sys           3sync          4games         5man           6lp            7mail          8news          9uucp         10proxy        13www-data     33backup       34list         38irc          39gnats        41nobody    65534libuuid     100

0 0
原创粉丝点击