OSDN Git Service

logname and whoami are the same as id -un, so merge them.
[android-x86/external-toybox.git] / toys / posix / id.c
1 /* id.c - print real and effective user and group IDs
2  *
3  * Copyright 2012 Sony Network Entertainment, Inc.
4  *
5  * by Tim Bird <tim.bird@am.sony.com>
6  *
7  * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
8
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))
13
14 config ID
15   bool "id"
16   default y
17   help
18     usage: id [-nGgru]
19
20     Print user and group ID.
21
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
27
28 config ID_GROUPS
29   bool "groups"
30   default y
31   depends on ID
32   help
33     usage: groups [user]
34
35     Print the groups a user is in.
36
37 config ID_LOGNAME
38   bool "logname"
39   default y
40   depends on ID
41   help
42     usage: logname
43
44     Print the current user name.
45
46 */
47
48 #define FOR_id
49 #include "toys.h"
50
51 static void s_or_u(char *s, unsigned u, int done)
52 {
53   if (toys.optflags & FLAG_n) printf("%s", s);
54   else printf("%u", u);
55   if (done) {
56     xputc('\n');
57     exit(0);
58   }
59 }
60
61 static void showid(char *header, unsigned u, char *s)
62 {
63   printf("%s%u(%s)", header, u, s);
64 }
65
66 void do_id(char *username)
67 {
68   int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
69   struct passwd *pw;
70   struct group *grp;
71   uid_t uid = getuid(), euid = geteuid();
72   gid_t gid = getgid(), egid = getegid(), *groups;
73
74   if (cmd_groups)
75       toys.optflags |= FLAG_G | FLAG_n;
76
77   flags = toys.optflags;
78
79   // check if a username is given
80   if (username) {
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);
85   }
86
87   i = flags & FLAG_r;
88   pw = xgetpwuid(i ? uid : euid);
89   if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
90
91   grp = xgetgrgid(i ? gid : egid);
92   if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
93
94   if (!(flags & FLAG_G)) {
95     showid("uid=", pw->pw_uid, pw->pw_name);
96     showid(" gid=", grp->gr_gid, grp->gr_name);
97
98     if (!i) {
99       if (uid != euid) {
100         pw = xgetpwuid(euid);
101         showid(" euid=", pw->pw_uid, pw->pw_name);
102       }
103       if (gid != egid) {
104         grp = xgetgrgid(egid);
105         showid(" egid=", grp->gr_gid, grp->gr_name);
106       }
107     }
108
109     showid(" groups=", grp->gr_gid, grp->gr_name);
110   }
111
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);
117
118   for (i = 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;
123     xputc(' ');
124   }
125   xputc('\n');
126 }
127
128 void id_main(void)
129 {
130   if (toys.which->name[0] > 'i') toys.optflags = (FLAG_u | FLAG_n);
131   if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
132   else do_id(NULL);
133 }