This commit is contained in:
Your Name 2023-11-15 00:17:49 +03:00
parent 2f26cc0a82
commit e40d161b9c
1 changed files with 16 additions and 9 deletions

View File

@ -15,15 +15,21 @@ unsigned int n_flag;
unsigned int r_flag;
unsigned int u_flag;
int print_groups(char *fmt, int flag) {
gid_t groups[NGROUPS_MAX + 1];
int count = getgroups(sizeof(groups), groups);
if (count < 0) {
fprintf(stderr, "id: %s\n", strerror(errno));
int print_groups(const struct passwd *pwd, const char *fmt, const int flag) {
int ngroups = 0;
getgrouplist(pwd->pw_name, pwd->pw_gid, NULL, &ngroups);
if (ngroups <= 0)
return 1;
gid_t *groups = malloc(ngroups * sizeof(gid_t *));
if (groups == NULL) {
fprintf(stderr, "id: malloc failed\n");
return 1;
}
for (int i = 0; i < count; i++) {
getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups);
for (int i = 0; i < ngroups; i++) {
struct group *grp = getgrgid(groups[i]);
if (grp && !n_flag)
printf("%d", groups[i]);
@ -32,11 +38,12 @@ int print_groups(char *fmt, int flag) {
printf(fmt, grp->gr_name);
if (g_flag && i == 0)
return 0;
break;
putchar(' ');
}
free(groups);
return 0;
}
@ -50,7 +57,7 @@ int ids(uid_t uid, struct passwd *pwd) {
}
if (G_flag)
print_groups("%s", 0);
print_groups(pwd, "%s", 0);
putchar('\n');
return 0;
@ -63,7 +70,7 @@ int def_ids(uid_t uid, struct passwd *pwd) {
printf("(%s)", grp->gr_name);
printf(" groups=");
print_groups("(%s)", 1);
print_groups(pwd, "(%s)", 1);
putchar('\n');
return 0;