1 /* id.c - print real and effective user and group IDs
3 * Copyright 2012 Sony Network Entertainment, Inc.
5 * by Tim Bird <tim.bird@am.sony.com>
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
9 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
10 USE_ID_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
11 USE_ID_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN))
12 USE_ID_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN))
20 Print user and group ID.
22 -n print names instead of numeric IDs (to be used with -Ggu)
23 -G Show only the group IDs
24 -g Show only the effective group ID
25 -r Show real ID instead of effective ID
26 -u Show only the effective user ID
35 Print the groups a user is in.
44 Print the current user name.
51 static void s_or_u(char *s, unsigned u, int done)
53 if (toys.optflags & FLAG_n) printf("%s", s);
61 static void showid(char *header, unsigned u, char *s)
63 printf("%s%u(%s)", header, u, s);
66 void do_id(char *username)
68 int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
71 uid_t uid = getuid(), euid = geteuid();
72 gid_t gid = getgid(), egid = getegid(), *groups;
75 toys.optflags |= FLAG_G | FLAG_n;
77 flags = toys.optflags;
79 // check if a username is given
81 pw = xgetpwnam(username);
82 uid = euid = pw->pw_uid;
83 gid = egid = pw->pw_gid;
84 if (cmd_groups) printf("%s : ", pw->pw_name);
88 pw = xgetpwuid(i ? uid : euid);
89 if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
91 grp = xgetgrgid(i ? gid : egid);
92 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
94 if (!(flags & FLAG_G)) {
95 showid("uid=", pw->pw_uid, pw->pw_name);
96 showid(" gid=", grp->gr_gid, grp->gr_name);
100 pw = xgetpwuid(euid);
101 showid(" euid=", pw->pw_uid, pw->pw_name);
104 grp = xgetgrgid(egid);
105 showid(" egid=", grp->gr_gid, grp->gr_name);
109 showid(" groups=", grp->gr_gid, grp->gr_name);
112 groups = (gid_t *)toybuf;
113 i = sizeof(toybuf)/sizeof(gid_t);
114 ngroups = username ? getgrouplist(username, gid, groups, &i)
115 : getgroups(i, groups);
116 if (0 >= ngroups) perror_exit(0);
119 if (!(grp = getgrgid(groups[i]))) perror_msg(0);
120 else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
121 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
122 if (++i >= ngroups) break;
130 if (toys.which->name[0] > 'i') toys.optflags = (FLAG_u | FLAG_n);
131 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);