(linux系统)用户数据信息获取getpwuid、getspnam

根据用户名区验证用户名密码是否正确

函数说明

The getpwnam() and getpwuid() functions return a pointer to a passwd structure, or NULL if the matching entry is not found or an error occurs. If an error occurs, errno is set appropriately. If one wantsto check errno after the call, it should be set to zero before call.

getpass()   --get a password,用于获取用户输入的密码,取消了输入的回显功能
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);

passwd结构体

 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 */
           };

验证用户密码代码

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<shadow.h>
#include<string.h>
#include<crypt.h>

#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
int main(int argc, char *argv[])
{
    struct spwd *shadowline;


    char *crypted_pass;

    char *input_pass;
    if(argc<2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }
    //口令获取
    input_pass=getpass("PassWord:");
    
    shadowline=getspnam(argv[1]);

    crypted_pass=crypt(input_pass,shadowline->sp_pwdp);

    if(strcmp(shadowline->sp_pwdp,crypted_pass)==0)
        puts("OK!");
    else
        puts("failer!");
    exit(0);
}
## 根据ID显示用户名代码

```c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<pwd.h>
int main(int argc,char *argv[])
{
    struct passwd *pwdline;
    if(argc<2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }
    pwdline=getpwuid(atoi(argv[1]));
    puts(pwdline->pw_name);
    exit(0);
}

调用示例

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值