Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19733785
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2008-12-03 11:29:08

§4.5  用户信息

       Uid一般为短整型。

#include

#include

uid_t getuid(void);

char *getlogin(void);

 

#include

#include

struct passwd *getpwuid(uid_t uid);

struct passwd *getpwnam(const char *name);

 

passwd的结构在pwd.h

passwd Member Description

char *pw_name The user’s login name

uid_t pw_uid The UID number

gid_t pw_gid The GID number

char *pw_dir The user’s home directory

char *pw_gecos The user’s full name

char *pw_shell The user’s default shell

 

实例:

# cat user.c

#include

#include

#include

#include

#include

 

int main()

{

    uid_t uid;

    gid_t gid;

    struct passwd *pw;

 

    uid = getuid();

    gid = getgid();

 

    printf("User is %s\n", getlogin());

 

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

 

    pw = getpwuid(uid);

    printf("UID passwd entry:\n name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",

        pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

 

    pw = getpwnam("root");

    printf("root passwd entry:\n");

    printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",

        pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

    exit(0);

}

运行结果:

# ./user

User is root

User IDs: uid=0, gid=0

UID passwd entry:

 name=root, uid=0, gid=0, home=/root, shell=/bin/bash

root passwd entry:

name=root, uid=0, gid=0, home=/root, shell=/bin/bash

 

扫描password文件信息可以使用getpwent

#include

#include

void endpwent(void);

struct passwd *getpwent(void

 

用户和组信息:

#include

#include

uid_t geteuid(void);

gid_t getgid(void);

gid_t getegid(void);

int setuid(uid_t uid);

int setgid(gid_t gid);

只有超级用户能使用setuid and setgid

阅读(3014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~