getpwent系统调用
NAME
getpwent, setpwent, endpwent - get password file entry
SYNOPSIS
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);
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 */
};
测试代码:
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#define DEBUG_INFO(...) \
do{\
printf("%s:%d -- ", __func__, __LINE__);\
printf(__VA_ARGS__); \
printf("\n"); \
}while(0)
int main(int argc, char **argv)
{
struct passwd *p;
int count = 0;
do{
p = getpwent();
if(!p){
perror("getpwent");
break;
}
// DEBUG_INFO("pw_name=%s,pw_passwd=%s,pw_uid=%d,pw_gid=%d,"
// "pw_gecos=%s,pw_dir=%s,pw_shell=%s",
// p->pw_name,
// p->pw_passwd,
// p->pw_uid,
// p->pw_gid,
// p->pw_gecos,
// p->pw_dir,
// p->pw_shell);
DEBUG_INFO("%s:%s:%d:%d:%s:%s:%s",
p->pw_name,
p->pw_passwd,
p->pw_uid,
p->pw_gid,
p->pw_gecos,
p->pw_dir,
p->pw_shell);
count++;
}while(1);
DEBUG_INFO("count: %d\n", count);
return 0;
}
执行结果:
main:39 -- root:x:0:0:root:/root:/bin/bash
main:39 -- daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
/*略*/
getpwent: Success
main:44 -- count: 56
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)