Linux getspnam、getspent、setspent和endspent函数

本文详细介绍了Linux系统中用于处理 shadow 文件的函数,包括getspnam用于获取指定用户名的shadow信息,getspent遍历shadow文件,setspent开启遍历,以及endspent结束遍历。通过这些函数,开发者可以方便地访问和操作用户的密码和账户过期等安全信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getspnam、getspent、setspent和endspent


头文件

#include <shadow.h>

函数原型

struct spwd *getspnam(const char *name);
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);
}

ps:请使用sudo运行,此例子来自百度百科。(莫黑


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值