2 * id - POSIX.2 user identity
4 * (INCOMPLETE -- supplementary groups for other users not yet done)
6 * usage: id [-Ggu] [-nr] [user]
8 * The default output format looks something like:
9 * uid=xxx(chet) gid=xx groups=aa(aname), bb(bname), cc(cname)
14 #include "bashtypes.h"
22 # include <sys/param.h>
25 #if !defined (HAVE_GETPW_DECLS)
26 extern struct passwd
*getpwuid ();
28 extern struct group
*getgrgid ();
34 #include "bashgetopt.h"
36 #define ID_ALLGROUPS 0x001 /* -G */
37 #define ID_GIDONLY 0x002 /* -g */
38 #define ID_USENAME 0x004 /* -n */
39 #define ID_USEREAL 0x008 /* -r */
40 #define ID_USERONLY 0x010 /* -u */
42 #define ID_FLAGSET(s) ((id_flags & (s)) != 0)
46 static uid_t ruid
, euid
;
47 static gid_t rgid
, egid
;
51 static int inituser ();
53 static int id_pruser ();
54 static int id_prgrp ();
55 static int id_prgroups ();
56 static int id_prall ();
66 reset_internal_getopt ();
67 while ((opt
= internal_getopt (list
, "Ggnru")) != -1)
71 case 'G': id_flags
|= ID_ALLGROUPS
; break;
72 case 'g': id_flags
|= ID_GIDONLY
; break;
73 case 'n': id_flags
|= ID_USENAME
; break;
74 case 'r': id_flags
|= ID_USEREAL
; break;
75 case 'u': id_flags
|= ID_USERONLY
; break;
83 user
= list
? list
->word
->word
: (char *)NULL
;
85 /* Check for some invalid option combinations */
86 opt
= ID_FLAGSET (ID_ALLGROUPS
) + ID_FLAGSET (ID_GIDONLY
) + ID_FLAGSET (ID_USERONLY
);
87 if (opt
> 1 || (opt
== 0 && ((id_flags
& (ID_USEREAL
|ID_USENAME
)) != 0)))
93 if (list
&& list
->next
)
99 if (inituser (user
) < 0)
100 return (EXECUTION_FAILURE
);
103 if (id_flags
& ID_USERONLY
)
104 opt
+= id_pruser ((id_flags
& ID_USEREAL
) ? ruid
: euid
);
105 else if (id_flags
& ID_GIDONLY
)
106 opt
+= id_prgrp ((id_flags
& ID_USEREAL
) ? rgid
: egid
);
107 else if (id_flags
& ID_ALLGROUPS
)
108 opt
+= id_prgroups (user
);
110 opt
+= id_prall (user
);
114 return (opt
== 0 ? EXECUTION_SUCCESS
: EXECUTION_FAILURE
);
125 pwd
= getpwnam (uname
);
128 builtin_error ("%s: no such user", uname
);
131 ruid
= euid
= pwd
->pw_uid
;
132 rgid
= egid
= pwd
->pw_gid
;
136 ruid
= current_user
.uid
;
137 euid
= current_user
.euid
;
138 rgid
= current_user
.gid
;
139 egid
= current_user
.egid
;
144 /* Print the name or value of user ID UID. */
149 struct passwd
*pwd
= NULL
;
153 if (id_flags
& ID_USENAME
)
155 pwd
= getpwuid (uid
);
160 printf ("%s", pwd
->pw_name
);
162 printf ("%u", (unsigned) uid
);
167 /* Print the name or value of group ID GID. */
173 struct group
*grp
= NULL
;
177 if (id_flags
& ID_USENAME
)
179 grp
= getgrgid (gid
);
185 printf ("%s", grp
->gr_name
);
187 printf ("%u", (unsigned) gid
);
196 int *glist
, ng
, i
, r
;
208 builtin_error ("supplementary groups for other users not yet implemented");
214 glist
= get_group_array (&ng
);
216 for (i
= 0; i
< ng
; i
++)
217 if (glist
[i
] != rgid
&& glist
[i
] != egid
)
230 int r
, i
, ng
, *glist
;
235 printf ("uid=%u", (unsigned) ruid
);
236 pwd
= getpwuid (ruid
);
240 printf ("(%s)", pwd
->pw_name
);
242 printf (" gid=%u", (unsigned) rgid
);
243 grp
= getgrgid (rgid
);
247 printf ("(%s)", grp
->gr_name
);
251 printf (" euid=%u", (unsigned) euid
);
252 pwd
= getpwuid (euid
);
256 printf ("(%s)", pwd
->pw_name
);
261 printf (" egid=%u", (unsigned) egid
);
262 grp
= getgrgid (egid
);
266 printf ("(%s)", grp
->gr_name
);
271 builtin_error ("supplementary groups for other users not yet implemented");
277 glist
= get_group_array (&ng
);
281 for (i
= 0; i
< ng
; i
++)
285 printf ("%u", (unsigned) glist
[i
]);
286 grp
= getgrgid (glist
[i
]);
290 printf ("(%s)", grp
->gr_name
);
297 "return information about user identity",
301 struct builtin id_struct
= {
306 "id [user]\n\tid -G [-n] [user]\n\tid -g [-nr] [user]\n\tid -u [-nr] [user]",