Sat Nov 25 02:48:47 1995 Ulrich Drepper <drepper@gnu.ai.mit.edu>
[platform/upstream/glibc.git] / posix / id.c
1 /* Copyright (C) 1991, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
19 #include <ansidecl.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <grp.h>
24 #include <pwd.h>
25 #include <limits.h>
26 #include <sys/types.h>
27
28
29 static void
30 DEFUN(print_grpname, (id, parens),
31       gid_t id AND int parens)
32 {
33   CONST struct group *CONST g = getgrgid(id);
34   if (g == NULL)
35     {
36       if (parens)
37         return;
38       else
39         {
40           fprintf(stderr, _("Couldn't find name for group %d\n"), id);
41           exit(EXIT_FAILURE);
42         }
43     }
44
45   if (parens)
46     printf("(%s)", g->gr_name);
47   else
48     puts(g->gr_name);
49 }
50
51 static void
52 DEFUN(print_pwdname, (id, parens),
53       uid_t id AND int parens)
54 {
55   CONST struct passwd *CONST p = getpwuid(id);
56   if (p == NULL)
57     {
58       if (parens)
59         return;
60       else
61         {
62           fprintf(stderr, _("Couldn't find name for user %d\n"), (int) id);
63           exit(EXIT_FAILURE);
64         }
65     }
66
67   if (parens)
68     printf("(%s)", p->pw_name);
69   else
70     puts(p->pw_name);
71 }
72 \f
73 int
74 DEFUN(main, (argc, argv), int argc AND char **argv)
75 {
76   int print_gid = 1, print_uid = 1;
77   int real = 0, name = 0;
78   int error = 0;
79   register int c;
80
81   uid_t ruid = getuid(), euid = geteuid();
82   gid_t rgid = getgid(), egid = getegid();
83
84   while ((c = getopt(argc, argv, "gurn")) != -1)
85     switch (c)
86       {
87       default:
88         error = 1;
89         break;
90
91       case 'g':
92         print_gid = 1;
93         print_uid = 0;
94         break;
95
96       case 'u':
97         print_uid = 1;
98         print_gid = 0;
99         break;
100
101       case 'r':
102         real = 1;
103         break;
104
105       case 'n':
106         name = 1;
107         break;
108       }
109
110   if (error || argc != optind)
111     {
112       fputs(_("Usage: id [-gurn]\n"), stderr);
113       exit(EXIT_FAILURE);
114     }
115
116   if (print_uid && !print_gid)
117     {
118       CONST uid_t uid = real ? ruid : euid;
119       if (name)
120         print_pwdname(uid, 0);
121       else
122         printf("%d\n", (int) uid);
123     }
124   else if (print_gid && !print_uid)
125     {
126       CONST gid_t gid = real ? rgid : egid;
127       if (name)
128         print_grpname(gid, 0);
129       else
130         printf("%d\n", (int) gid);
131     }
132   else
133     {
134 #if     NGROUPS_MAX > 0
135       gid_t groups[NGROUPS_MAX];
136       int ngroups;
137       ngroups = getgroups(NGROUPS_MAX, groups);
138 #endif
139
140       printf("uid=%d", (int) ruid);
141       print_pwdname(ruid, 1);
142       printf(" gid=%d", (int) rgid);
143       print_grpname(rgid, 1);
144       if (euid != ruid)
145         {
146           printf(" euid=%d", (int) euid);
147           print_pwdname(euid, 1);
148         }
149       if (egid != rgid)
150         {
151           printf(" egid=%d", (int) egid);
152           print_grpname(egid, 1);
153         }
154
155 #if     NGROUPS > 0
156       if (ngroups > 0)
157         {
158           register size_t i;
159           printf(" groups=%d", (int) groups[0]);
160           print_grpname(groups[0], 1);
161           for (i = 1; i < ngroups; ++i)
162             {
163               printf(", %d", (int) groups[i]);
164               print_grpname(groups[i], 1);
165             }
166         }
167 #endif
168
169       putchar('\n');
170     }
171
172   exit(EXIT_SUCCESS);
173 }