changeset 899:7b2e8f9db29b

add groups implementation to id.c
author Ivo van poorten <ivopvp@gmail.com>
date Tue, 14 May 2013 00:03:26 -0500
parents f6db08574875
children edd8e6fd418d
files toys/posix/id.c
diffstat 1 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/id.c	Sun May 12 21:09:16 2013 -0500
+++ b/toys/posix/id.c	Tue May 14 00:03:26 2013 -0500
@@ -7,6 +7,7 @@
  * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
 
 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
+USE_ID_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
 
 config ID
   bool "id"
@@ -21,6 +22,16 @@
     -g	Show only the effective group ID
     -r	Show real ID instead of effective ID
     -u	Show only the effective user ID
+
+config ID_GROUPS
+  bool "groups"
+  default y
+  depends on ID
+  help
+    usage: groups [user]
+
+    Print the groups a user is in.
+
 */
 
 #define FOR_id
@@ -57,21 +68,28 @@
 
 void id_main(void)
 {
-  int flags = toys.optflags, i, ngroups;
+  int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
   struct passwd *pw;
   struct group *grp;
   uid_t uid = getuid(), euid = geteuid();
   gid_t gid = getgid(), egid = getegid(), *groups;
 
+  if (cmd_groups)
+      toys.optflags |= FLAG_G | FLAG_n;
+
+  flags = toys.optflags;
+
   // check if a username is given
   if (*toys.optargs) {
     if (!(pw = getpwnam(*toys.optargs)))
       error_exit("no such user '%s'", *toys.optargs);
     uid = euid = pw->pw_uid;
     gid = egid = pw->pw_gid;
+    if (cmd_groups)
+      printf("%s : ", pw->pw_name);
   }
 
-  i = toys.optflags & FLAG_r;
+  i = flags & FLAG_r;
   pw = xgetpwuid(i ? uid : euid);
   if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);