getspnam、getspent、setspent和endspent
头文件
#include <shadow.h>
函数原型
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);
spwd结构
struct spwd { char *sp_namp; /* 用户登录名 */
char *sp_pwdp; /* 加密口令 */
long int sp_lstchg; /* 上次更改口令以来的时间 */
long int sp_min; /* 进过多少天后可以更改 */
long int sp_max; /* 要求更改的剩余天数 */
long int sp_warn; /* 到期警告的天数 */
long int sp_inact; /* 账户不活动之前尚余天数 */
long int sp_expire; /* 账户到期天数 */
};unsigned long int sp_flag; /* 保留 */
功能
getspnam函数可以访问shadow口令,返回值是spwd结构的指针。
getspent函数是访问阴影口令的接口,返回值是spwd的指针。
第一次调用时会取得第一项组数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。
读取数据完毕后可使用endspent()来关闭该组文件。而setspent函数用来打开文件。
例子
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <shadow.h>
int main(int argc, char * argv[])
{
struct spwd *sp;
char buf[80];
setpwent();
while(gets(buf) != NULL)
{
if((sp = getspnam(buf)) != (struct spwd *) 0 )
{
printf("Vaild login name is:%s\n",sp->sp_namp);
}
else
{
setspent();
while((sp = getspent()) != (struct spwd *)0)
{
printf("%s\n", sp->sp_namp);
}
}
}
endspent();
return(EXIT_SUCCESS);
}