changeset 796:50052f1368b9

id: make "id -Gn root" print root's groups instead of current user (and without leading space), enforce max 1 argument and at most one of Ggu.
author Rob Landley <rob@landley.net>
date Thu, 31 Jan 2013 04:05:02 -0600
parents 8e3b60814ad7
children 36b43eafab38
files toys/posix/id.c
diffstat 1 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/id.c	Thu Jan 31 01:56:57 2013 -0600
+++ b/toys/posix/id.c	Thu Jan 31 04:05:02 2013 -0600
@@ -6,7 +6,7 @@
  *
  * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
 
-USE_ID(NEWTOY(id, "nGgru", TOYFLAG_BIN))
+USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
 
 config ID
   bool "id"
@@ -97,14 +97,16 @@
   }
 
   groups = (gid_t *)toybuf;
-  if (0 >= (ngroups = getgroups(sizeof(toybuf)/sizeof(gid_t), groups)))
-    perror_exit(0);
+  i = sizeof(toybuf)/sizeof(gid_t);
+  ngroups = *toys.optargs ? getgrouplist(*toys.optargs, gid, groups, &i) : getgroups(i, groups);
+  if (0 >= ngroups) perror_exit(0);
 
-  for (i = 0; i < ngroups; i++) {
-    xputc(' ');
+  for (i = 0;;) {
     if (!(grp = getgrgid(groups[i]))) perror_msg(0);
     else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
     else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
+    if (++i >= ngroups) break;
+    xputc(' ');
   }
   xputc('\n');
 }