.
[coreutils.git] / src / id.c
bloba03ba6e6898b73e82c840de8bd1a7e6974c86a48
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <getopt.h>
29 #include "version.h"
30 #include "system.h"
31 #include "error.h"
33 #ifdef _POSIX_VERSION
34 #include <limits.h>
36 #else /* not _POSIX_VERSION */
37 struct passwd *getpwuid ();
38 struct group *getgrgid ();
39 uid_t getuid ();
40 gid_t getgid ();
41 uid_t geteuid ();
42 gid_t getegid ();
43 #include <sys/param.h>
44 #endif /* not _POSIX_VERSION */
46 char *xmalloc ();
47 int getugroups ();
49 static void print_user __P ((int uid));
50 static void print_group __P ((int gid));
51 static void print_group_list __P ((char *username));
52 static void print_full_info __P ((char *username));
53 static void usage __P ((int status));
55 /* The name this program was run with. */
56 char *program_name;
58 /* If nonzero, output only the group ID(s). -g */
59 static int just_group = 0;
61 /* If nonzero, output user/group name instead of ID number. -n */
62 static int use_name = 0;
64 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
65 static int use_real = 0;
67 /* If nonzero, output only the user ID(s). -u */
68 static int just_user = 0;
70 /* If nonzero, output only the supplementary groups. -G */
71 static int just_group_list = 0;
73 /* The real and effective IDs of the user to print. */
74 static uid_t ruid, euid;
75 static gid_t rgid, egid;
77 /* The number of errors encountered so far. */
78 static int problems = 0;
80 /* If nonzero, display usage information and exit. */
81 static int show_help;
83 /* If nonzero, print the version on standard output and exit. */
84 static int show_version;
86 static struct option const longopts[] =
88 {"group", no_argument, NULL, 'g'},
89 {"groups", no_argument, NULL, 'G'},
90 {"help", no_argument, &show_help, 1},
91 {"name", no_argument, NULL, 'n'},
92 {"real", no_argument, NULL, 'r'},
93 {"user", no_argument, NULL, 'u'},
94 {"version", no_argument, &show_version, 1},
95 {NULL, 0, NULL, 0}
98 void
99 main (int argc, char **argv)
101 int optc;
103 program_name = argv[0];
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 while ((optc = getopt_long (argc, argv, "gnruG", longopts, (int *) 0))
109 != EOF)
111 switch (optc)
113 case 0:
114 break;
115 case 'g':
116 just_group = 1;
117 break;
118 case 'n':
119 use_name = 1;
120 break;
121 case 'r':
122 use_real = 1;
123 break;
124 case 'u':
125 just_user = 1;
126 break;
127 case 'G':
128 just_group_list = 1;
129 break;
130 default:
131 usage (1);
135 if (show_version)
137 printf ("id - %s\n", version_string);
138 exit (0);
141 if (show_help)
142 usage (0);
144 if (just_user + just_group + just_group_list > 1)
145 error (1, 0, _("cannot print only user and only group"));
147 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
148 error (1, 0, _("cannot print only names or real IDs in default format"));
150 if (argc - optind > 1)
151 usage (1);
153 if (argc - optind == 1)
155 struct passwd *pwd = getpwnam (argv[optind]);
156 if (pwd == NULL)
157 error (1, 0, _("%s: No such user"), argv[optind]);
158 ruid = euid = pwd->pw_uid;
159 rgid = egid = pwd->pw_gid;
161 else
163 euid = geteuid ();
164 ruid = getuid ();
165 egid = getegid ();
166 rgid = getgid ();
169 if (just_user)
170 print_user (use_real ? ruid : euid);
171 else if (just_group)
172 print_group (use_real ? rgid : egid);
173 else if (just_group_list)
174 print_group_list (argv[optind]);
175 else
176 print_full_info (argv[optind]);
177 putchar ('\n');
179 exit (problems != 0);
182 /* Print the name or value of user ID UID. */
184 static void
185 print_user (int uid)
187 struct passwd *pwd = NULL;
189 if (use_name)
191 pwd = getpwuid (uid);
192 if (pwd == NULL)
193 problems++;
196 if (pwd == NULL)
197 printf ("%u", (unsigned) uid);
198 else
199 printf ("%s", pwd->pw_name);
202 /* Print the name or value of group ID GID. */
204 static void
205 print_group (int gid)
207 struct group *grp = NULL;
209 if (use_name)
211 grp = getgrgid (gid);
212 if (grp == NULL)
213 problems++;
216 if (grp == NULL)
217 printf ("%u", (unsigned) gid);
218 else
219 printf ("%s", grp->gr_name);
222 /* Print all of the distinct groups the user is in . */
224 static void
225 print_group_list (char *username)
227 print_group (rgid);
228 if (egid != rgid)
230 putchar (' ');
231 print_group (egid);
234 #if HAVE_GETGROUPS
236 int ng, n_groups;
237 GETGROUPS_T *groups;
238 register int i;
240 n_groups = getgroups (0, NULL);
241 /* Add 1 just in case n_groups is zero. */
242 groups = (GETGROUPS_T *) xmalloc (n_groups * sizeof (GETGROUPS_T) + 1);
243 if (username == 0)
244 ng = getgroups (n_groups, groups);
245 else
246 ng = getugroups (n_groups, groups, username);
248 if (ng < 0)
250 error (0, errno, _("cannot get supplemental group list"));
251 ++problems;
252 free (groups);
253 return;
256 for (i = 0; i < n_groups; i++)
257 if (groups[i] != rgid && groups[i] != egid)
259 putchar (' ');
260 print_group (groups[i]);
262 free (groups);
264 #endif
267 /* Print all of the info about the user's user and group IDs. */
269 static void
270 print_full_info (char *username)
272 struct passwd *pwd;
273 struct group *grp;
275 printf ("uid=%u", (unsigned) ruid);
276 pwd = getpwuid (ruid);
277 if (pwd == NULL)
278 problems++;
279 else
280 printf ("(%s)", pwd->pw_name);
282 printf (" gid=%u", (unsigned) rgid);
283 grp = getgrgid (rgid);
284 if (grp == NULL)
285 problems++;
286 else
287 printf ("(%s)", grp->gr_name);
289 if (euid != ruid)
291 printf (" euid=%u", (unsigned) euid);
292 pwd = getpwuid (euid);
293 if (pwd == NULL)
294 problems++;
295 else
296 printf ("(%s)", pwd->pw_name);
299 if (egid != rgid)
301 printf (" egid=%u", (unsigned) egid);
302 grp = getgrgid (egid);
303 if (grp == NULL)
304 problems++;
305 else
306 printf ("(%s)", grp->gr_name);
309 #if HAVE_GETGROUPS
311 int ng, n_groups;
312 GETGROUPS_T *groups;
313 register int i;
315 n_groups = getgroups (0, NULL);
316 /* Add 1 just in case n_groups is zero. */
317 groups = (GETGROUPS_T *) xmalloc (n_groups * sizeof (GETGROUPS_T) + 1);
318 if (username == 0)
319 ng = getgroups (n_groups, groups);
320 else
321 ng = getugroups (n_groups, groups, username);
322 if (ng < 0)
324 error (0, errno, _("cannot get supplemental group list"));
325 problems++;
326 free (groups);
327 return;
330 if (n_groups > 0)
331 fputs (_(" groups="), stdout);
332 for (i = 0; i < n_groups; i++)
334 if (i > 0)
335 putchar (',');
336 printf ("%u", (unsigned) groups[i]);
337 grp = getgrgid (groups[i]);
338 if (grp == NULL)
339 problems++;
340 else
341 printf ("(%s)", grp->gr_name);
343 free (groups);
345 #endif
348 static void
349 usage (int status)
351 if (status != 0)
352 fprintf (stderr, _("Try `%s --help' for more information.\n"),
353 program_name);
354 else
356 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
357 printf (_("\
358 Print information for USERNAME, or the current user.\n\
360 -g, --group print only the group ID\n\
361 -G, --groups print only the supplementary groups\n\
362 -n, --name print a name instead of a number, for -ugG\n\
363 -r, --real print the real ID instead of effective ID, for -ugG\n\
364 -u, --user print only the user ID\n\
365 --help display this help and exit\n\
366 --version output version information and exit\n\
368 Without any OPTION, print some useful set of identified information.\n\
369 "));
371 exit (status);