Unix高级环境编程之获取用户口令信息getpwuid函数

本文介绍如何通过Unix/Linux系统函数getpwuid获取当前用户的详细信息,包括用户名、用户ID、组ID、真实姓名、家目录及默认shell等。

调用Unix/Linux系统函数getpwuid函数,其原型如下:

struct passwd *getpwuid(uid_t uid);

而struct passwd的结构体如下:

The passwd structure isdefined in<pwd.h>asfollows:
struct passwd {
char*pw_name; /*user name */
char*pw_passwd; /*user password */
uid_t pw_uid; /*user id */
gid_t pw_gid; /*group id */
char*pw_gecos; /*real name */
char*pw_dir; /*home directory */
char*pw_shell; /*shell program */
};

测试代码如下:

#include<stdio.h>
#include<unistd.h>
#include<pwd.h>

int main()
{
        uid_t uid;
        struct passwd *pw;

        uid =getuid();

        printf("User uid=%d\n", uid);

        if ((pw = getpwuid(getuid()))) {
                printf("Username =%s\n"
                        "Passwd=%s\n"
                        "UserID=%d\n"
                        "GroupID=%d\n"
                        "RealName=%s\n"
                        "Home=%s\n"
                        "Shell=%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
                                        }

        return 0;
}

运行:

[yuesichiu@localhost ~]$ ./getpwuid
User uid=1002
Username =apue
Passwd=x
UserID=1002
GroupID=1002
RealName=
Home=/home/apue
Shell=/bin/bash



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值