1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-1995, 1996 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)
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, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
24 #include <sys/types.h>
35 #else /* not _POSIX_VERSION */
36 struct passwd *getpwuid ();
37 struct group *getgrgid ();
42 #include <sys/param.h>
43 #endif /* not _POSIX_VERSION */
48 static void print_user __P ((int uid));
49 static void print_group __P ((int gid));
50 static void print_group_list __P ((char *username));
51 static void print_full_info __P ((char *username));
52 static void usage __P ((int status));
54 /* The name this program was run with. */
57 /* If nonzero, output only the group ID(s). -g */
58 static int just_group = 0;
60 /* If nonzero, output user/group name instead of ID number. -n */
61 static int use_name = 0;
63 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
64 static int use_real = 0;
66 /* If nonzero, output only the user ID(s). -u */
67 static int just_user = 0;
69 /* If nonzero, output only the supplementary groups. -G */
70 static int just_group_list = 0;
72 /* The real and effective IDs of the user to print. */
73 static uid_t ruid, euid;
74 static gid_t rgid, egid;
76 /* The number of errors encountered so far. */
77 static int problems = 0;
79 /* If nonzero, display usage information and exit. */
82 /* If nonzero, print the version on standard output and exit. */
83 static int show_version;
85 static struct option const longopts[] =
87 {"group", no_argument, NULL, 'g'},
88 {"groups", no_argument, NULL, 'G'},
89 {"help", no_argument, &show_help, 1},
90 {"name", no_argument, NULL, 'n'},
91 {"real", no_argument, NULL, 'r'},
92 {"user", no_argument, NULL, 'u'},
93 {"version", no_argument, &show_version, 1},
98 main (int argc, char **argv)
102 program_name = argv[0];
103 setlocale (LC_ALL, "");
104 bindtextdomain (PACKAGE, LOCALEDIR);
105 textdomain (PACKAGE);
107 while ((optc = getopt_long (argc, argv, "agnruG", longopts, NULL)) != -1)
114 /* Ignore -a, for compatibility with SVR4. */
138 printf ("id (%s) %s\n", GNU_PACKAGE, VERSION);
145 if (just_user + just_group + just_group_list > 1)
146 error (1, 0, _("cannot print only user and only group"));
148 if (just_user + just_group + just_group_list == 0 && (use_real || use_name))
149 error (1, 0, _("cannot print only names or real IDs in default format"));
151 if (argc - optind > 1)
154 if (argc - optind == 1)
156 struct passwd *pwd = getpwnam (argv[optind]);
158 error (1, 0, _("%s: No such user"), argv[optind]);
159 ruid = euid = pwd->pw_uid;
160 rgid = egid = pwd->pw_gid;
171 print_user (use_real ? ruid : euid);
173 print_group (use_real ? rgid : egid);
174 else if (just_group_list)
175 print_group_list (argv[optind]);
177 print_full_info (argv[optind]);
180 exit (problems != 0);
183 /* Print the name or value of user ID UID. */
188 struct passwd *pwd = NULL;
192 pwd = getpwuid (uid);
198 printf ("%u", (unsigned) uid);
200 printf ("%s", pwd->pw_name);
203 /* Print the name or value of group ID GID. */
206 print_group (int gid)
208 struct group *grp = NULL;
212 grp = getgrgid (gid);
218 printf ("%u", (unsigned) gid);
220 printf ("%s", grp->gr_name);
224 xgetgroups (const char *username, int *n_groups, GETGROUPS_T **groups)
232 max_n_groups = getgroups (0, NULL);
234 max_n_groups = getugroups (0, NULL, username);
236 /* Add 1 just in case max_n_groups is zero. */
237 g = (GETGROUPS_T *) xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
239 ng = getgroups (max_n_groups, g);
241 ng = getugroups (max_n_groups, g, username);
245 error (0, errno, _("cannot get supplemental group list"));
257 /* Print all of the distinct groups the user is in. */
260 print_group_list (char *username)
275 if (xgetgroups (username, &n_groups, &groups))
281 for (i = 0; i < n_groups; i++)
282 if (groups[i] != rgid && groups[i] != egid)
285 print_group (groups[i]);
292 /* Print all of the info about the user's user and group IDs. */
295 print_full_info (char *username)
300 printf ("uid=%u", (unsigned) ruid);
301 pwd = getpwuid (ruid);
305 printf ("(%s)", pwd->pw_name);
307 printf (" gid=%u", (unsigned) rgid);
308 grp = getgrgid (rgid);
312 printf ("(%s)", grp->gr_name);
316 printf (" euid=%u", (unsigned) euid);
317 pwd = getpwuid (euid);
321 printf ("(%s)", pwd->pw_name);
326 printf (" egid=%u", (unsigned) egid);
327 grp = getgrgid (egid);
331 printf ("(%s)", grp->gr_name);
340 if (xgetgroups (username, &n_groups, &groups))
347 fputs (_(" groups="), stdout);
348 for (i = 0; i < n_groups; i++)
352 printf ("%u", (unsigned) groups[i]);
353 grp = getgrgid (groups[i]);
357 printf ("(%s)", grp->gr_name);
368 fprintf (stderr, _("Try `%s --help' for more information.\n"),
372 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name);
374 Print information for USERNAME, or the current user.\n\
376 -a ignore, for compatibility with other versions\n\
377 -g, --group print only the group ID\n\
378 -G, --groups print only the supplementary groups\n\
379 -n, --name print a name instead of a number, for -ugG\n\
380 -r, --real print the real ID instead of effective ID, for -ugG\n\
381 -u, --user print only the user ID\n\
382 --help display this help and exit\n\
383 --version output version information and exit\n\
385 Without any OPTION, print some useful set of identified information.\n\
387 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));