1 /* Copyright (C) 1991, 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
29 print_grpname (id, parens)
33 const struct group *const g = getgrgid (id);
40 fprintf (stderr, _("Couldn't find name for group %d\n"), id);
46 printf ("(%s)", g->gr_name);
52 print_pwdname (id, parens)
56 const struct passwd *const p = getpwuid (id);
63 fprintf (stderr, _("Couldn't find name for user %d\n"), (int) id);
69 printf ("(%s)", p->pw_name);
79 int print_gid = 1, print_uid = 1;
80 int real = 0, name = 0;
84 uid_t ruid = getuid (), euid = geteuid ();
85 gid_t rgid = getgid (), egid = getegid ();
87 while ((c = getopt (argc, argv, "gurn")) != -1)
113 if (error || argc != optind)
115 fputs (_("Usage: id [-gurn]\n"), stderr);
119 if (print_uid && !print_gid)
121 const uid_t uid = real ? ruid : euid;
123 print_pwdname (uid, 0);
125 printf ("%d\n", (int) uid);
127 else if (print_gid && !print_uid)
129 const gid_t gid = real ? rgid : egid;
131 print_grpname (gid, 0);
133 printf ("%d\n", (int) gid);
138 gid_t groups[NGROUPS_MAX];
140 ngroups = getgroups (NGROUPS_MAX, groups);
143 printf ("uid=%d", (int) ruid);
144 print_pwdname (ruid, 1);
145 printf (" gid=%d", (int) rgid);
146 print_grpname (rgid, 1);
149 printf (" euid=%d", (int) euid);
150 print_pwdname (euid, 1);
154 printf (" egid=%d", (int) egid);
155 print_grpname (egid, 1);
162 printf (" groups=%d", (int) groups[0]);
163 print_grpname (groups[0], 1);
164 for (i = 1; i < ngroups; ++i)
166 printf (", %d", (int) groups[i]);
167 print_grpname (groups[i], 1);