我试图解决显示属于指定为参数(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("%s\n", 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("%s\n", gr->gr_name);
}
}
endpwent();
return 0;
}
}
setpwent();
while ((p=getpwent()) != NULL)
printf("%s\n", p->pw_name);
endpwent();