调用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