From cc0d4ab069d79510c387ffa53ca351a0acb4b61b Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 12 Mar 2012 20:56:56 -0500 Subject: Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says. --- toys/id.c | 113 ++++++++++++++++++-------------------------------------------- 1 file changed, 33 insertions(+), 80 deletions(-) (limited to 'toys/id.c') diff --git a/toys/id.c b/toys/id.c index aaea3e37..7436a75a 100644 --- a/toys/id.c +++ b/toys/id.c @@ -36,42 +36,30 @@ config ID void pretty_print(struct passwd *pw, struct group *grp, struct group **grps, int n) { - int i; - printf("uid= %d(%s) gid= %d(%s)", pw->pw_uid, pw->pw_name, - grp->gr_gid, grp->gr_name); - if (n) { - printf(" groups= "); - } - for (i = 0; i < n; i++) { - printf("%d(%s)%s", grps[i]->gr_gid, grps[i]->gr_name, - (i < n-1) ? ",": ""); + int i = 0; + + printf("uid=%u(%s) gid=%u(%s)", pw->pw_uid, pw->pw_name, + grp->gr_gid, grp->gr_name); + if (n) printf(" groups="); + while (i < n) { + printf("%d(%s)", grps[i]->gr_gid, grps[i]->gr_name); + if (++ipw_uid; gid = pw->pw_gid; } else { @@ -85,78 +73,43 @@ void id_main(void) } } - pw = getpwuid(uid); - if (!pw) { - perror("id"); - toys.exitval = 1; - return; - } - - grp = getgrgid(pw->pw_gid); - if (!grp) { - perror("id"); - toys.exitval = 1; - return; - } + if (!(pw = getpwuid(uid)) || !(grp = getgrgid(gid))) + perror_exit(0); if (flags & FLAG_u) { - if (flags & FLAG_n) - printf("%s\n", pw->pw_name); - else - printf("%d\n", pw->pw_uid); + if (flags & FLAG_n) xputs(pw->pw_name); + else printf("%d\n", pw->pw_uid); return; } if (flags & FLAG_g) { - if (flags & FLAG_n) - printf("%s\n", grp->gr_name); - else - printf("%d\n", grp->gr_gid); + if (flags & FLAG_n) xputs(grp->gr_name); + else printf("%d\n", grp->gr_gid); return; } - - if (flags & FLAG_r) { - printf("-r can only be used in combination with -u or -g\n"); - toys.exitval = 1; - return; - } ngroups = sysconf(_SC_NGROUPS_MAX); - /* fallback for number of groups to 32 */ - if (ngroups < 0) - ngroups = 32; - groups = malloc(ngroups * sizeof(gid_t)); - if (!groups) { - perror("id"); - toys.exitval = 1; - return; - } - if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) < 0) { - perror("id"); - toys.exitval = 1; - return; - } - grps = malloc(ngroups * sizeof(struct group *)); + if (ngroups<1) ngroups = 32; + groups = xmalloc(ngroups * sizeof(gid_t)); + if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) < 0) + perror_exit(0); + + grps = xmalloc(ngroups * sizeof(struct group *)); for (i = 0; i < ngroups; i++) { struct group *tmp; - grps[i] = malloc(sizeof(struct group)); + grps[i] = xmalloc(sizeof(struct group)); size_t f = sysconf(_SC_GETGR_R_SIZE_MAX); - char *buf = malloc(f); - if (getgrgid_r(groups[i], grps[i], buf, f, &tmp) < 0) { - perror("id"); - continue; - } - if (tmp == NULL) { - perror("id"); + char *buf = xmalloc(f); + if (getgrgid_r(groups[i], grps[i], buf, f, &tmp) < 0 || !tmp) { + perror_msg(0); continue; } } if (flags & FLAG_G) { for (i = 0; i < ngroups; i++) { - if (flags & FLAG_n) - printf("%s%s", !i ? "" : " ", grps[i]->gr_name); - else - printf("%s%d", !i ? "" : " ", grps[i]->gr_gid); + if (i) xputc(' '); + if (flags & FLAG_n) printf("%s", grps[i]->gr_name); + else printf("%d", grps[i]->gr_gid); } printf("\n"); return; -- cgit v1.2.3