endgrent();
}
执行 root:x:0:root,
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
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。