Avoid spurious failure on x86 solaris2.9 when using c89.
[coreutils.git] / src / id.c
blobb86667e7c30f2ed20da5e9c119d655407cfd14bf
1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-2004 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Arnold Robbins.
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 "system.h"
30 #include "error.h"
31 #include "quote.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "id"
36 #define AUTHORS "Arnold Robbins", "David MacKenzie"
38 #ifndef _POSIX_VERSION
39 struct passwd *getpwuid ();
40 struct group *getgrgid ();
41 uid_t getuid ();
42 gid_t getgid ();
43 uid_t geteuid ();
44 gid_t getegid ();
45 #endif /* not _POSIX_VERSION */
47 int getugroups ();
49 static void print_user (uid_t uid);
50 static void print_group (gid_t gid);
51 static void print_group_list (const char *username);
52 static void print_full_info (const char *username);
54 /* The name this program was run with. */
55 char *program_name;
57 /* If true, output user/group name instead of ID number. -n */
58 static bool use_name = false;
60 /* The real and effective IDs of the user to print. */
61 static uid_t ruid, euid;
62 static gid_t rgid, egid;
64 /* True unless errors have been encountered. */
65 static bool ok = true;
67 static struct option const longopts[] =
69 {"group", no_argument, NULL, 'g'},
70 {"groups", no_argument, NULL, 'G'},
71 {"name", no_argument, NULL, 'n'},
72 {"real", no_argument, NULL, 'r'},
73 {"user", no_argument, NULL, 'u'},
74 {GETOPT_HELP_OPTION_DECL},
75 {GETOPT_VERSION_OPTION_DECL},
76 {NULL, 0, NULL, 0}
79 void
80 usage (int status)
82 if (status != EXIT_SUCCESS)
83 fprintf (stderr, _("Try `%s --help' for more information.\n"),
84 program_name);
85 else
87 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
88 fputs (_("\
89 Print information for USERNAME, or the current user.\n\
90 \n\
91 -a ignore, for compatibility with other versions\n\
92 -g, --group print only the effective group ID\n\
93 -G, --groups print all group IDs\n\
94 -n, --name print a name instead of a number, for -ugG\n\
95 -r, --real print the real ID instead of the effective ID, with -ugG\n\
96 -u, --user print only the effective user ID\n\
97 "), stdout);
98 fputs (HELP_OPTION_DESCRIPTION, stdout);
99 fputs (VERSION_OPTION_DESCRIPTION, stdout);
100 fputs (_("\
102 Without any OPTION, print some useful set of identified information.\n\
103 "), stdout);
104 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
106 exit (status);
110 main (int argc, char **argv)
112 int optc;
114 /* If true, output the list of all group IDs. -G */
115 bool just_group_list = false;
116 /* If true, output only the group ID(s). -g */
117 bool just_group = false;
118 /* If true, output real UID/GID instead of default effective UID/GID. -r */
119 bool use_real = false;
120 /* If true, output only the user ID(s). -u */
121 bool just_user = false;
123 initialize_main (&argc, &argv);
124 program_name = argv[0];
125 setlocale (LC_ALL, "");
126 bindtextdomain (PACKAGE, LOCALEDIR);
127 textdomain (PACKAGE);
129 atexit (close_stdout);
131 while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
133 switch (optc)
135 case 'a':
136 /* Ignore -a, for compatibility with SVR4. */
137 break;
138 case 'g':
139 just_group = true;
140 break;
141 case 'n':
142 use_name = true;
143 break;
144 case 'r':
145 use_real = true;
146 break;
147 case 'u':
148 just_user = true;
149 break;
150 case 'G':
151 just_group_list = true;
152 break;
153 case_GETOPT_HELP_CHAR;
154 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
155 default:
156 usage (EXIT_FAILURE);
160 if (just_user + just_group + just_group_list > 1)
161 error (EXIT_FAILURE, 0, _("cannot print only user and only group"));
163 if (just_user + just_group + just_group_list == 0 && (use_real | use_name))
164 error (EXIT_FAILURE, 0,
165 _("cannot print only names or real IDs in default format"));
167 if (argc - optind > 1)
169 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
170 usage (EXIT_FAILURE);
173 if (argc - optind == 1)
175 struct passwd *pwd = getpwnam (argv[optind]);
176 if (pwd == NULL)
177 error (EXIT_FAILURE, 0, _("%s: No such user"), argv[optind]);
178 ruid = euid = pwd->pw_uid;
179 rgid = egid = pwd->pw_gid;
181 else
183 euid = geteuid ();
184 ruid = getuid ();
185 egid = getegid ();
186 rgid = getgid ();
189 if (just_user)
190 print_user (use_real ? ruid : euid);
191 else if (just_group)
192 print_group (use_real ? rgid : egid);
193 else if (just_group_list)
194 print_group_list (argv[optind]);
195 else
196 print_full_info (argv[optind]);
197 putchar ('\n');
199 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
202 /* Print the name or value of user ID UID. */
204 static void
205 print_user (uid_t uid)
207 struct passwd *pwd = NULL;
209 if (use_name)
211 pwd = getpwuid (uid);
212 if (pwd == NULL)
214 error (0, 0, _("cannot find name for user ID %u"), uid);
215 ok = false;
219 if (pwd == NULL)
220 printf ("%lu", (unsigned long int) uid);
221 else
222 printf ("%s", pwd->pw_name);
225 /* Print the name or value of group ID GID. */
227 static void
228 print_group (gid_t gid)
230 struct group *grp = NULL;
232 if (use_name)
234 grp = getgrgid (gid);
235 if (grp == NULL)
237 error (0, 0, _("cannot find name for group ID %u"), gid);
238 ok = false;
242 if (grp == NULL)
243 printf ("%lu", (unsigned long int) gid);
244 else
245 printf ("%s", grp->gr_name);
248 #if HAVE_GETGROUPS
250 /* FIXME: document */
252 static bool
253 xgetgroups (const char *username, gid_t gid, int *n_groups,
254 GETGROUPS_T **groups)
256 int max_n_groups;
257 int ng;
258 GETGROUPS_T *g = NULL;
260 if (!username)
261 max_n_groups = getgroups (0, NULL);
262 else
263 max_n_groups = getugroups (0, NULL, username, gid);
265 if (max_n_groups < 0)
266 ng = -1;
267 else
269 g = xnmalloc (max_n_groups, sizeof *g);
270 if (!username)
271 ng = getgroups (max_n_groups, g);
272 else
273 ng = getugroups (max_n_groups, g, username, gid);
276 if (ng < 0)
278 error (0, errno, _("cannot get supplemental group list"));
279 free (g);
280 return false;
282 else
284 *n_groups = ng;
285 *groups = g;
286 return true;
290 #endif /* HAVE_GETGROUPS */
292 /* Print all of the distinct groups the user is in. */
294 static void
295 print_group_list (const char *username)
297 struct passwd *pwd;
299 pwd = getpwuid (ruid);
300 if (pwd == NULL)
301 ok = false;
303 print_group (rgid);
304 if (egid != rgid)
306 putchar (' ');
307 print_group (egid);
310 #if HAVE_GETGROUPS
312 int n_groups;
313 GETGROUPS_T *groups;
314 register int i;
316 if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
317 &n_groups, &groups))
319 ok = false;
320 return;
323 for (i = 0; i < n_groups; i++)
324 if (groups[i] != rgid && groups[i] != egid)
326 putchar (' ');
327 print_group (groups[i]);
329 free (groups);
331 #endif /* HAVE_GETGROUPS */
334 /* Print all of the info about the user's user and group IDs. */
336 static void
337 print_full_info (const char *username)
339 struct passwd *pwd;
340 struct group *grp;
342 printf ("uid=%lu", (unsigned long int) ruid);
343 pwd = getpwuid (ruid);
344 if (pwd)
345 printf ("(%s)", pwd->pw_name);
347 printf (" gid=%lu", (unsigned long int) rgid);
348 grp = getgrgid (rgid);
349 if (grp)
350 printf ("(%s)", grp->gr_name);
352 if (euid != ruid)
354 printf (" euid=%lu", (unsigned long int) euid);
355 pwd = getpwuid (euid);
356 if (pwd)
357 printf ("(%s)", pwd->pw_name);
360 if (egid != rgid)
362 printf (" egid=%lu", (unsigned long int) egid);
363 grp = getgrgid (egid);
364 if (grp)
365 printf ("(%s)", grp->gr_name);
368 #if HAVE_GETGROUPS
370 int n_groups;
371 GETGROUPS_T *groups;
372 register int i;
374 if (! xgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
375 &n_groups, &groups))
377 ok = false;
378 return;
381 if (n_groups > 0)
382 fputs (_(" groups="), stdout);
383 for (i = 0; i < n_groups; i++)
385 if (i > 0)
386 putchar (',');
387 printf ("%lu", (unsigned long int) groups[i]);
388 grp = getgrgid (groups[i]);
389 if (grp)
390 printf ("(%s)", grp->gr_name);
392 free (groups);
394 #endif /* HAVE_GETGROUPS */