1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 89, 90, 91, 92, 93, 94, 95, 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
, "gnruG", longopts
, (int *) 0))
136 printf ("id - %s\n", PACKAGE_VERSION
);
143 if (just_user
+ just_group
+ just_group_list
> 1)
144 error (1, 0, _("cannot print only user and only group"));
146 if (just_user
+ just_group
+ just_group_list
== 0 && (use_real
|| use_name
))
147 error (1, 0, _("cannot print only names or real IDs in default format"));
149 if (argc
- optind
> 1)
152 if (argc
- optind
== 1)
154 struct passwd
*pwd
= getpwnam (argv
[optind
]);
156 error (1, 0, _("%s: No such user"), argv
[optind
]);
157 ruid
= euid
= pwd
->pw_uid
;
158 rgid
= egid
= pwd
->pw_gid
;
169 print_user (use_real
? ruid
: euid
);
171 print_group (use_real
? rgid
: egid
);
172 else if (just_group_list
)
173 print_group_list (argv
[optind
]);
175 print_full_info (argv
[optind
]);
178 exit (problems
!= 0);
181 /* Print the name or value of user ID UID. */
186 struct passwd
*pwd
= NULL
;
190 pwd
= getpwuid (uid
);
196 printf ("%u", (unsigned) uid
);
198 printf ("%s", pwd
->pw_name
);
201 /* Print the name or value of group ID GID. */
204 print_group (int gid
)
206 struct group
*grp
= NULL
;
210 grp
= getgrgid (gid
);
216 printf ("%u", (unsigned) gid
);
218 printf ("%s", grp
->gr_name
);
222 xgetgroups (const char *username
, int *n_groups
, GETGROUPS_T
**groups
)
230 max_n_groups
= getgroups (0, NULL
);
232 max_n_groups
= getugroups (0, NULL
, username
);
234 /* Add 1 just in case max_n_groups is zero. */
235 g
= (GETGROUPS_T
*) xmalloc (max_n_groups
* sizeof (GETGROUPS_T
) + 1);
237 ng
= getgroups (max_n_groups
, g
);
239 ng
= getugroups (max_n_groups
, g
, username
);
243 error (0, errno
, _("cannot get supplemental group list"));
255 /* Print all of the distinct groups the user is in. */
258 print_group_list (char *username
)
273 if (xgetgroups (username
, &n_groups
, &groups
))
279 for (i
= 0; i
< n_groups
; i
++)
280 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
283 print_group (groups
[i
]);
290 /* Print all of the info about the user's user and group IDs. */
293 print_full_info (char *username
)
298 printf ("uid=%u", (unsigned) ruid
);
299 pwd
= getpwuid (ruid
);
303 printf ("(%s)", pwd
->pw_name
);
305 printf (" gid=%u", (unsigned) rgid
);
306 grp
= getgrgid (rgid
);
310 printf ("(%s)", grp
->gr_name
);
314 printf (" euid=%u", (unsigned) euid
);
315 pwd
= getpwuid (euid
);
319 printf ("(%s)", pwd
->pw_name
);
324 printf (" egid=%u", (unsigned) egid
);
325 grp
= getgrgid (egid
);
329 printf ("(%s)", grp
->gr_name
);
338 if (xgetgroups (username
, &n_groups
, &groups
))
345 fputs (_(" groups="), stdout
);
346 for (i
= 0; i
< n_groups
; i
++)
350 printf ("%u", (unsigned) groups
[i
]);
351 grp
= getgrgid (groups
[i
]);
355 printf ("(%s)", grp
->gr_name
);
366 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
370 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name
);
372 Print information for USERNAME, or the current user.\n\
374 -g, --group print only the group ID\n\
375 -G, --groups print only the supplementary groups\n\
376 -n, --name print a name instead of a number, for -ugG\n\
377 -r, --real print the real ID instead of effective ID, for -ugG\n\
378 -u, --user print only the user ID\n\
379 --help display this help and exit\n\
380 --version output version information and exit\n\
382 Without any OPTION, print some useful set of identified information.\n\
384 puts (_("\nReport bugs to bug-gnu-utils@gnu.ai.mit.edu"));