分类: LINUX
2008-12-03 11:29:08
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。