toybox

changeset 1503:96bdf979d9e6

Untangle id/groups/logname so single.sh can build each one standalone.
author Rob Landley <rob@landley.net>
date Sat, 27 Sep 2014 20:28:33 -0500
parents 84922376d15c
children c86e27992c4a
files toys/posix/id.c
diffstat 1 files changed, 33 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/toys/posix/id.c	Sat Sep 27 19:59:28 2014 -0500
     1.2 +++ b/toys/posix/id.c	Sat Sep 27 20:28:33 2014 -0500
     1.3 @@ -7,9 +7,9 @@
     1.4   * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
     1.5  
     1.6  USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
     1.7 -USE_ID_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
     1.8 -USE_ID_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN))
     1.9 -USE_ID_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN))
    1.10 +USE_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
    1.11 +USE_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN))
    1.12 +USE_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN))
    1.13  
    1.14  config ID
    1.15    bool "id"
    1.16 @@ -25,32 +25,41 @@
    1.17      -r	Show real ID instead of effective ID
    1.18      -u	Show only the effective user ID
    1.19  
    1.20 -config ID_GROUPS
    1.21 +config GROUPS
    1.22    bool "groups"
    1.23    default y
    1.24 -  depends on ID
    1.25    help
    1.26      usage: groups [user]
    1.27  
    1.28      Print the groups a user is in.
    1.29  
    1.30 -config ID_LOGNAME
    1.31 +config LOGNAME
    1.32    bool "logname"
    1.33    default y
    1.34 -  depends on ID
    1.35    help
    1.36      usage: logname
    1.37  
    1.38      Print the current user name.
    1.39  
    1.40 +config WHOAMI
    1.41 +  bool "whoami"
    1.42 +  default y
    1.43 +  help
    1.44 +    usage: whoami
    1.45 +
    1.46 +    Print the current user name.
    1.47  */
    1.48  
    1.49  #define FOR_id
    1.50  #include "toys.h"
    1.51  
    1.52 +GLOBALS(
    1.53 +  int do_u, do_n, do_G, is_groups;
    1.54 +)
    1.55 +
    1.56  static void s_or_u(char *s, unsigned u, int done)
    1.57  {
    1.58 -  if (toys.optflags & FLAG_n) printf("%s", s);
    1.59 +  if (TT.do_n) printf("%s", s);
    1.60    else printf("%u", u);
    1.61    if (done) {
    1.62      xputc('\n');
    1.63 @@ -65,15 +74,12 @@
    1.64  
    1.65  void do_id(char *username)
    1.66  {
    1.67 -  int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
    1.68 +  int flags, i, ngroups;
    1.69    struct passwd *pw;
    1.70    struct group *grp;
    1.71    uid_t uid = getuid(), euid = geteuid();
    1.72    gid_t gid = getgid(), egid = getegid(), *groups;
    1.73  
    1.74 -  if (cmd_groups)
    1.75 -      toys.optflags |= FLAG_G | FLAG_n;
    1.76 -
    1.77    flags = toys.optflags;
    1.78  
    1.79    // check if a username is given
    1.80 @@ -81,17 +87,17 @@
    1.81      pw = xgetpwnam(username);
    1.82      uid = euid = pw->pw_uid;
    1.83      gid = egid = pw->pw_gid;
    1.84 -    if (cmd_groups) printf("%s : ", pw->pw_name);
    1.85 +    if (TT.is_groups) printf("%s : ", pw->pw_name);
    1.86    }
    1.87  
    1.88    i = flags & FLAG_r;
    1.89    pw = xgetpwuid(i ? uid : euid);
    1.90 -  if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
    1.91 +  if (TT.do_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
    1.92  
    1.93    grp = xgetgrgid(i ? gid : egid);
    1.94    if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
    1.95  
    1.96 -  if (!(flags & FLAG_G)) {
    1.97 +  if (!TT.do_G) {
    1.98      showid("uid=", pw->pw_uid, pw->pw_name);
    1.99      showid(" gid=", grp->gr_gid, grp->gr_name);
   1.100  
   1.101 @@ -116,9 +122,9 @@
   1.102    if (ngroups<0) perror_exit(0);
   1.103  
   1.104    for (i = 0; i<ngroups; i++) {
   1.105 -    if (i) xputc(' ');
   1.106 +    if (i || !TT.do_G) xputc(' ');
   1.107      if (!(grp = getgrgid(groups[i]))) perror_msg(0);
   1.108 -    else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
   1.109 +    else if (TT.do_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
   1.110      else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
   1.111    }
   1.112    xputc('\n');
   1.113 @@ -126,7 +132,16 @@
   1.114  
   1.115  void id_main(void)
   1.116  {
   1.117 -  if (toys.which->name[0] > 'i') toys.optflags = (FLAG_u | FLAG_n);
   1.118 +  // FLAG macros can be 0 if "id" command enabled, so snapshot them here.
   1.119 +  if (FLAG_u) TT.do_u = toys.optflags & FLAG_u;
   1.120 +  if (FLAG_n) TT.do_n = toys.optflags & FLAG_n;
   1.121 +  if (FLAG_G) TT.do_G = toys.optflags & FLAG_G;
   1.122 +
   1.123 +  // And set the variables for non-id commands.
   1.124 +  TT.is_groups = toys.which->name[0] == 'g';
   1.125 +  if (TT.is_groups) TT.do_G = TT.do_n = 1;
   1.126 +  else if (toys.which->name[0] != 'i') TT.do_u = TT.do_n = 1;
   1.127 +
   1.128    if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
   1.129    else do_id(NULL);
   1.130  }