我试图解决显示属于指定为参数(optarg)的组的所有用户的问题。
我写了第一个案例,它显示了所有用户及其下方的组,现在我想使用包含组名称的参数optarg创建b案例。
我不能使用gr_mem因为我不是根。
struct passwd *p;
gid_t *groups = NULL;
int ng = 0;
struct group *gr;
int i, opt;
while((opt = getopt (argc, argv, "a")) != -1){
switch(opt){
case 'a':
setpwent();
while ((p=getpwent()) != NULL){
printf("%sn", p->pw_name);
if(getgrouplist(p->pw_name, p->pw_gid, groups, &ng) < 0){
groups = (gid_t*) malloc(ng * sizeof (gid_t));
getgrouplist(p->pw_name, p->pw_gid, groups, &ng);
}
for(i = 0; i<ng; i++){
gr = getgrgid(groups[i]);
printf("%sn", gr->gr_name);
}
}
endpwent();
return 0;
}
}
setpwent();
while ((p=getpwent()) != NULL)
printf("%sn", p->pw_name);
endpwent();
我不能使用gr_mem因为我不是根。
您无需是根即可使用字段gr_mem
struct group
。