1 /* Copyright (C) 1991 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
26 #include <sys/types.h>
30 DEFUN(print_grpname, (id, parens),
31 gid_t id AND int 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 DEFUN(print_pwdname, (id, parens),
53 uid_t id AND int parens)
55 CONST struct passwd *CONST p = getpwuid(id);
62 fprintf(stderr, "Couldn't find name for user %d\n", (int) id);
68 printf("(%s)", p->pw_name);
74 DEFUN(main, (argc, argv), int argc AND char **argv)
76 int print_gid = 1, print_uid = 1;
77 int real = 0, name = 0;
81 uid_t ruid = getuid(), euid = geteuid();
82 gid_t rgid = getgid(), egid = getegid();
84 while ((c = getopt(argc, argv, "gurn")) != -1)
110 if (error || argc != optind)
112 fputs("Usage: id [-gurn]\n", stderr);
116 if (print_uid && !print_gid)
118 CONST uid_t uid = real ? ruid : euid;
120 print_pwdname(uid, 0);
122 printf("%d\n", (int) uid);
124 else if (print_gid && !print_uid)
126 CONST gid_t gid = real ? rgid : egid;
128 print_grpname(gid, 0);
130 printf("%d\n", (int) gid);
135 gid_t groups[NGROUPS_MAX];
137 ngroups = getgroups(NGROUPS_MAX, groups);
140 printf("uid=%d", (int) ruid);
141 print_pwdname(ruid, 1);
142 printf(" gid=%d", (int) rgid);
143 print_grpname(rgid, 1);
146 printf(" euid=%d", (int) euid);
147 print_pwdname(euid, 1);
151 printf(" egid=%d", (int) egid);
152 print_grpname(egid, 1);
159 printf(" groups=%d", (int) groups[0]);
160 print_grpname(groups[0], 1);
161 for (i = 1; i < ngroups; ++i)
163 printf(", %d", (int) groups[i]);
164 print_grpname(groups[i], 1);