【问题标题】:C - Users belonging to the group - LinuxC - 属于该组的用户 - Linux
【发布时间】:2015-03-09 19:35:18
【问题描述】:

我试图解决显示属于指定为参数(optarg)的组的所有用户。

我编写了第一个案例,它显示了所有用户及其下的组,现在我想创建带有包含组名称的参数 optarg 的 b 案例。

我不能使用 gr_mem,因为我不是 root。

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();

【问题讨论】:

    标签: c linux administration ansi-c


    【解决方案1】:

    我不能使用 gr_mem,因为我不是 root。

    您无需成为 root 即可使用 gr_mem 的字段 struct group

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-29
      • 2018-04-28
      • 2021-11-30
      • 2020-02-24
      • 2021-12-12
      • 2012-12-26
      • 1970-01-01
      • 2011-05-26
      相关资源
      最近更新 更多