Update.
[glibc.git] / posix / id.c
blob6c602b21b4b49238ff282390a3f5a11dbf82a48f
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. */
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <grp.h>
23 #include <pwd.h>
24 #include <limits.h>
25 #include <sys/types.h>
28 static void
29 print_grpname (id, parens)
30 gid_t id;
31 int parens;
33 const struct group *const g = getgrgid (id);
34 if (g == NULL)
36 if (parens)
37 return;
38 else
40 fprintf (stderr, _("Couldn't find name for group %d\n"), id);
41 exit (EXIT_FAILURE);
45 if (parens)
46 printf ("(%s)", g->gr_name);
47 else
48 puts (g->gr_name);
51 static void
52 print_pwdname (id, parens)
53 uid_t id;
54 int parens;
56 const struct passwd *const p = getpwuid (id);
57 if (p == NULL)
59 if (parens)
60 return;
61 else
63 fprintf (stderr, _("Couldn't find name for user %d\n"), (int) id);
64 exit (EXIT_FAILURE);
68 if (parens)
69 printf ("(%s)", p->pw_name);
70 else
71 puts (p->pw_name);
74 int
75 main (argc, argv)
76 int argc;
77 char **argv;
79 int print_gid = 1, print_uid = 1;
80 int real = 0, name = 0;
81 int error = 0;
82 int c;
84 uid_t ruid = getuid (), euid = geteuid ();
85 gid_t rgid = getgid (), egid = getegid ();
87 while ((c = getopt (argc, argv, "gurn")) != -1)
88 switch (c)
90 default:
91 error = 1;
92 break;
94 case 'g':
95 print_gid = 1;
96 print_uid = 0;
97 break;
99 case 'u':
100 print_uid = 1;
101 print_gid = 0;
102 break;
104 case 'r':
105 real = 1;
106 break;
108 case 'n':
109 name = 1;
110 break;
113 if (error || argc != optind)
115 fputs (_("Usage: id [-gurn]\n"), stderr);
116 exit (EXIT_FAILURE);
119 if (print_uid && !print_gid)
121 const uid_t uid = real ? ruid : euid;
122 if (name)
123 print_pwdname (uid, 0);
124 else
125 printf ("%d\n", (int) uid);
127 else if (print_gid && !print_uid)
129 const gid_t gid = real ? rgid : egid;
130 if (name)
131 print_grpname (gid, 0);
132 else
133 printf ("%d\n", (int) gid);
135 else
137 #if NGROUPS_MAX > 0
138 gid_t groups[NGROUPS_MAX];
139 int ngroups;
140 ngroups = getgroups (NGROUPS_MAX, groups);
141 #endif
143 printf ("uid=%d", (int) ruid);
144 print_pwdname (ruid, 1);
145 printf (" gid=%d", (int) rgid);
146 print_grpname (rgid, 1);
147 if (euid != ruid)
149 printf (" euid=%d", (int) euid);
150 print_pwdname (euid, 1);
152 if (egid != rgid)
154 printf (" egid=%d", (int) egid);
155 print_grpname (egid, 1);
158 #if NGROUPS > 0
159 if (ngroups > 0)
161 size_t i;
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);
170 #endif
172 putchar ('\n');
175 exit (EXIT_SUCCESS);