id: use getgroups() in "id -a"

This commit is contained in:
Pierre Pronchery 2020-12-08 15:06:04 +01:00
parent 1e07855e5b
commit 5846c72611

View File

@ -187,7 +187,9 @@ static int _id_all(char const * user)
struct passwd * pw; struct passwd * pw;
struct group * gr; struct group * gr;
char * u; char * u;
char ** p; int n;
gid_t * groups;
int i;
if(user == NULL) if(user == NULL)
{ {
@ -234,15 +236,34 @@ static int _id_all(char const * user)
return _id_error(gr->gr_name, 1); return _id_error(gr->gr_name, 1);
} }
} }
printf("%s%u(%s)", " groups=", (unsigned)pw->pw_gid, u); if((n = getgroups(0, NULL)) < 0)
setgrent(); {
for(gr = getgrent(); gr != NULL; gr = getgrent()) putchar('\n');
for(p = gr->gr_mem; p != NULL && *p != NULL; p++) return _id_error("getgroups", 1);
if(strcmp(u, *p) == 0) }
printf(",%u(%s)", (unsigned)gr->gr_gid, if(n == 0)
gr->gr_name); groups = NULL;
else if((groups = malloc(sizeof(*groups) * n)) == NULL)
{
putchar('\n');
return _id_error("getgroups", 1);
}
else if((n = getgroups(n, groups)) < 0)
{
putchar('\n');
free(groups);
return _id_error("getgroups", 1);
}
printf("%s", " groups=");
for(i = 0; i < n; i++)
if((gr = getgrgid(groups[i])) == NULL)
printf("%s%u", (i == 0) ? "" : ",",
(unsigned)gr->gr_gid);
else
printf("%s%u(%s)", (i == 0) ? "" : ",",
(unsigned)gr->gr_gid, gr->gr_name);
putchar('\n'); putchar('\n');
endgrent(); free(groups);
free(u); free(u);
return 0; return 0;
} }