C语言中操作密码文件的一些函数总结
C语言setpwent()函数:从头读取密码文件中的账号数据 头文件: #include <pwd.h> #include <sys/types.h> 定义函数: void setpwent(void); 函数说明:setpwent()用来将getpwent()的读写地址指回密码文件开头。 范例 #include <pwd.h> #include <sys/types.h> main() { struct passwd *user; int i; for(i = 0; i < 4; i++) { user = getpwent(); printf("%s :%d :%d :%s:%s:%sn",user->pw_name,user->pw_uid,user->pw_gid,user->pw_gecos,user->pw_dir,user->pw_shell); } setpwent(); user = getpwent(); printf("%s :%d :%d :%s:%s:%sn",user->pw_shell); endpwent(); } 执行结果: root:0:0:root:/root:/bin/bash bin:1:1:bin:/bin daemon:2:2:daemon:/sbin adm:3:4:adm:/var/adm root:0:0:root:/root:/bin/bash C语言getpwent()函数:从密码文件中取得账号的数据 头文件: #include <pwd.h> #include <sys/types.h> 定义函数: strcut passwd * getpwent(void); 函数说明:getpwent()用来从密码文件(/etc/passwd)中读取一项用户数据,该用户的数据以passwd 结构返回. 第一次调用时会取得第一位用户数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。 passwd 结构定义如下: struct passwd { char * pw_name; //用户账号 char * pw_passwd; //用户密码 uid_t pw_uid; //用户识别码 gid_t pw_gid; //组识别码 char * pw_gecos; //用户全名 char * pw_dir; //家目录 char * pw_shell; //所使用的shell 路径 }; 返回值:返回 passwd 结构数据,如果返回NULL 则表示已无数据,或有错误发生. 附加说明:getpwent()在第一次调用时会打开密码文件,读取数据完毕后可使用endpwent()来关闭该密码文件。 错误代码: 范例 #include <pwd.h> #include <sys/types.h> main() { struct passwd *user; while((user = getpwent()) != 0) { printf("%s:%d:%d:%s:%s:%sn",user->pw_shell); } endpwent(); } 执行: root:0:0:root:/root:/bin/bash bin:1:1:bin:/bin: daemon:2:2:daemon:/sbin: adm:3:4:adm:/var/adm: lp:4:7:lp:/var/spool/lpd: sync:5:0:sync:/sbin:/bin/sync shutdown:6:0:shutdown:/sbin:/sbin/shutdown halt:7:0:halt:/sbin:/sbin/halt mail:8:12:mail:/var/spool/mail: news:9:13:news:var/spool/news uucp:10:14:uucp:/var/spool/uucp: operator:11:0:operator :/root: games:12:100:games:/usr/games: gopher:13:30:gopher:/usr/lib/gopher-data: ftp:14:50:FTP User:/home/ftp: nobody:99:99:Nobody:/: xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false gdm:42:42:/home/gdm:/bin/bash kids:500:500: : /home/kids:/bin/bash C语言endpwent()函数:关闭文件(关闭密码文件) 头文件: #include <pwd.h> #include <sys/types.h> 定义函数: void endpwent(void); 函数说明:endpwent()用来关闭由getpwent()所打开的密码文件。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |