Posted on 2009-05-06 09:36
Prayer 阅读(2337)
评论(0) 编辑 收藏 引用 所属分类:
LINUX/UNIX/AIX
如果知道一个用户的用户ID或者登录名,可以通过getpwuid或getpwnam函数获得用户的登录信息.
函数原型为:
#include <pwd.h>
#include <sys/types.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
The passwd structure is defined in <pwd.h> as follows:
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 <pwd.h>
#include <sys/types.h>
#include <stdio.h>
main()
{
uid_t my_uid;
my_uid = 500;
struct passwd *my_info;
my_info = getpwuid( my_uid );
//my_info = getpwnam( "fflg" );
printf( "my name = [%s]\n", my_info->pw_name );
printf( "my passwd = [%s]\n", my_info->pw_passwd );
printf( "my uid = [%d]\n", my_info->pw_uid );
printf( "my gid = [%d]\n", my_info->pw_gid );
printf( "my gecos = [%s]\n", my_info->pw_gecos );
printf( "my dir = [%s]\n", my_info->pw_dir );
printf( "my shell = [%s]\n", my_info->pw_shell );
}
~