打印本文 打印本文 关闭窗口 关闭窗口
Linux C 函数参考(用户组)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2879  更新时间:2009/4/22 23:08:51  文章录入:mintao  责任编辑:mintao
bin:x:1:root,bin,daemon,
daemon:x:2:root,bin,daemon,
sys:x:3:root,bin,adm,
adm:x:4:root,adm,daemon
tty:x:5
disk:x:6:root
lp:x:7:daemon,lp
mem:x:8
kmem:x:9:
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15:
games:x:20
gopher:x:30
dip:x:40
ftp:x:50
nobody:x:99
 


getgrgid(从组文件中取得指定gid 的数据)
相关函数 fgetgrent,getgrent,getgrnam
表头文件 #include<grp.h>
#include<sys/types.h>
定义函数 strcut group * getgrgid(gid_t gid);
函数说明 getgrgid()用来依参数gid指定的组识别码逐一搜索组文件,找到时便将该组的数据以group结构返回。group结构请参考getgrent()。
返回值 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。
范例 /* 取得gid=3的组数据*/
#include<grp.h>
#include<sys/types.h>
main()
{
strcut group *data;
int i=0;
data = getgrgid(3);
printf(“%s:%s:%d:”,data->gr_name,data->gr_passwd,data->gr_gid);
while(data->gr_mem[i])printf(“%s ,”,data->mem[i++]);
printf(“\n”);
}
执行 sys:x:3:root,bin,adm
 


getgrnam(从组文件中取得指定组的数据)
相关函数 fgetgrent,getrent,getgruid
表头文件 #include<grp.h>
#include<sys/types.h>
定义函数 strcut group * getgrnam(const char * name);
函数说明 getgrnam()用来逐一搜索参数那么指定的组名称,找到时便将该组的数据以group结构返回。group 结构请参考getgrent()。
返回值 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。
范例 /* 取得adm的组数据*/
#include<grp.h>
#include<sys/types.h>
main()
{
strcut group * data;
int i=0;
data = getgrnam(“adm”);
printf(“%s:%s:%d:”,data->gr_name,data->gr_passwd,data->gr_gid);
while(data->gr_mem[i])printf(“%s,”,data->gr_mem[i++]);
printf(“\n”);
}
执行 adm:x:4:root,adm,daemon
 


getgroups(取得组代码)
相关函数 initgroups,setgroup,getgid,setgid
表头文件 #include<unistd.h>
#include<sys/types.h>
定义函数 int getgroups(int size,gid_t list[]);
函数说明 getgroup()用来取得目前用户所属的组代码。参数size为list〔〕所能容纳的gid_t 数目。如果参数size 值为零,此函数仅会返回用户所属的组数。
返回值 返回组识别码,如有错误则返回-1。
错误代码 EFAULT 参数list数组地址不合法。EINVAL 参数size值不足以容纳所有的组。
范例 #include<unistd.h>
#include<sys/types.h>
main()
{
gid_t list[500];
int x,i;
x = getgroups(0.list);
getgroups(x,list);
for(i=0;i<x;i++)
printf(“%d:%d\n”,i,list[i]);
}
执行 0:00
1:01
2:02
3:03
4:04
5:06
6:10
 


getpw(取得指定用户的密码文件数据)
相关函数 getpwent
表头文件 #include<pwd.h>
#include<sys/types.h>
定义函数 int getpw(uid_t uid,char *buf);
函数说明 getpw()会从/etc/passwd中查找符合参数uid所指定的用户账号数据,找不到相关数据就返回-1。所返回的buf字符串格式如下:账号:密码:用户识别码(uid):组识别码(gid):全名:根目录:shell
返回值 返回0表示成功,有错误发生时返回-1。
附加说明 1. getpw()会有潜在的安全性问题,请尽量使用别的函数取代。
2. 使用shadow的系统已把用户密码抽出/etc/passwd,因此使用getpw()取得的密码将为“x”。
范例 #include<pwd.h>
#include<sys/types.h>
main()
{
char buffer[80];
getpw(0,buffer);
printf(“%s\n”,buffer);
}
执行 root:x:0:0:root:/root:/bin/bash
 


getpwent(从密码文件中取得账号的数据)
相关函数 getpw,fgetpwent,getpwnam,getpwuid,setpwent,endpwent
表头文件 #include<pwd.h>
#include<sys/types.h>
定义函数 strcut passwd * getpwent(void);
函数说明 getpwent()用来从密码文件(/etc/passwd)中读取一项用户数据,该用户的数据以passwd 结构返回。第一次调用时会取得第一位用户数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。
passwd 结构定义如下
struct passwd{
char * pw_name; /*用户账号*/
char * pw_passwd; /*用户密码*/
uid_t pw_uid; /*用户识别码*/
gid_t pw_gid; /*组识别码*/
char * pw_gecos; /*用户全名*/
char * pw_dir; /*家目录*/
char * pw_shell; /* 所使用的shell路径*/
};
返回值 返回passwd 结构数据,如果返回NULL 则表示已无数据,或有错误发生。
附加说明 getpwent()在第一次调用时会打开密码文件,读取数据完毕后可使用endpwent()来关闭该密码文件。错误代码ENOMEM 内存不足,无法配置passwd结构。
范例 #include<pwd.h>
#include<sys/types.h>
main()
{
struct passwd *user;
while((user = getpwent())!=0){
printf(“%s:%d:%d:%s:%s:%s\n”,user->pw_name,user->pw_uid,user->pw_gid,
user->pw_gecos,user->pw_dir,user->pw_shell);
}
endpwent();
}
执行 root:0:0:root:/root:/bin/bash
bin:1:1:bin:/bin:
daemon:2:2:daemon:/sbin:
adm:3:4:adm:/var/adm:
lp:4:7:lp:/var/spool/lpd:
sync:5:0:sync:/sbin:/bin/sync
shutdown:6:0:shutdown:/sbin:/sbin/shutdown
halt:7:0:halt:/sbin:/sbin/halt
mail:8:12:mail:/var/spool/mail:
news:9:13:news:var/spool/news
uucp:10:14:uucp:/var/spool/uucp:
operator:11:0:operator :/root:
games:12:100:games:/usr/games:
gopher:13:30:gopher:/usr/lib/gopher-data:
ftp:14:50:FTP User:/home/ftp:
nobody:99:99:Nobody:/:
xfs:100:101:X Font Server: /etc/Xll/fs:/bin/false
gdm:42:42:/home/gdm:/bin/bash
kids:500:500: : /home/kids:/bin/bash
 


getpwnam(从密码文件中取得指定账号的数据)
相关函数 getpw,fgetpwent,getpwent,getpwuid
表头文件 #include<pwd.h>
#include<sys/types.h>
定义函数 struct passwd * getpwnam(const char * name);
函数说明 getpwnam()用来逐一搜索参数name 指定的账号名称,找到时便将该用户的数据以passwd结构返回。passwd结构请参考getpwent()。
返回值 返回passwd 结构数据,如果返回NULL 则表示已无数据,或有错误发生。
范例 /*取得root账号的识别码和根目录*/
#include<pwd.h>
#include<sys/types.h>
main()
{
struct passwd *user;
user = getpwnam(“root”);
printf(“name:%s\n”,user->pw_name);
printf(“uid:%d\n”,user->pw_uid);
printf(“home:%s\n”,user->pw_dir);
}
执行 name:root
uid:0
home:/root
 


getpwuid(从密码文件中取得指定uid 的数据)
相关函数 getpw,fgetpwent,getpwent,getpwnam
表头文件 #include<pwd.h>
#include<sys/types.h>
定义函数 struct passwd * getpwuid(uid_t uid);
函数说明 getpwuid()用来逐一搜索参数uid 指定的用户识别码,找到时便将该用户的数据以结构返回结构请参考将该用户的数据以passwd 结构返回。passwd 结构请参考getpwent()。
返回值 返回passwd 结构数据,如果返回NULL 则表示已无数据,或者有错误发生。
范例 #include<pwd.h>
#include<sys/types.h>
main()
{
struct passwd *user;
user= getpwuid(6);
printf(“name:%s\n”,user->pw_name);
printf(“uid:%d\n”,user->pw_uid);
printf(“home:%s\n”,user->pw_dir);
}
执行 name:shutdown
uid:6
home:/sbin
 


getuid(取得真实的用户识别码)
相关函数 geteui

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口