Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624380
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-09-08 18:02:29

如果知道一个用户的用户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>

int 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 );

    return 0;
}

阅读(1462) | 评论(0) | 转发(0) |
0

上一篇:Linux内核中的Min和Max函数

下一篇:EINTR错误

给主人留下些什么吧!~~