update from main archive 961005
[glibc.git] / posix / id.c
blobb08d2501fa4702b33f6d59d09bcd39c6fdf07aec
1 /* Copyright (C) 1991, 1995 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. */
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>
29 static void
30 DEFUN(print_grpname, (id, parens),
31 gid_t id AND 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 DEFUN(print_pwdname, (id, parens),
53 uid_t id AND int parens)
55 CONST struct passwd *CONST p = getpwuid(id);
56 if (p == NULL)
58 if (parens)
59 return;
60 else
62 fprintf(stderr, _("Couldn't find name for user %d\n"), (int) id);
63 exit(EXIT_FAILURE);
67 if (parens)
68 printf("(%s)", p->pw_name);
69 else
70 puts(p->pw_name);
73 int
74 DEFUN(main, (argc, argv), int argc AND char **argv)
76 int print_gid = 1, print_uid = 1;
77 int real = 0, name = 0;
78 int error = 0;
79 register int c;
81 uid_t ruid = getuid(), euid = geteuid();
82 gid_t rgid = getgid(), egid = getegid();
84 while ((c = getopt(argc, argv, "gurn")) != -1)
85 switch (c)
87 default:
88 error = 1;
89 break;
91 case 'g':
92 print_gid = 1;
93 print_uid = 0;
94 break;
96 case 'u':
97 print_uid = 1;
98 print_gid = 0;
99 break;
101 case 'r':
102 real = 1;
103 break;
105 case 'n':
106 name = 1;
107 break;
110 if (error || argc != optind)
112 fputs(_("Usage: id [-gurn]\n"), stderr);
113 exit(EXIT_FAILURE);
116 if (print_uid && !print_gid)
118 CONST uid_t uid = real ? ruid : euid;
119 if (name)
120 print_pwdname(uid, 0);
121 else
122 printf("%d\n", (int) uid);
124 else if (print_gid && !print_uid)
126 CONST gid_t gid = real ? rgid : egid;
127 if (name)
128 print_grpname(gid, 0);
129 else
130 printf("%d\n", (int) gid);
132 else
134 #if NGROUPS_MAX > 0
135 gid_t groups[NGROUPS_MAX];
136 int ngroups;
137 ngroups = getgroups(NGROUPS_MAX, groups);
138 #endif
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)
146 printf(" euid=%d", (int) euid);
147 print_pwdname(euid, 1);
149 if (egid != rgid)
151 printf(" egid=%d", (int) egid);
152 print_grpname(egid, 1);
155 #if NGROUPS > 0
156 if (ngroups > 0)
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)
163 printf(", %d", (int) groups[i]);
164 print_grpname(groups[i], 1);
167 #endif
169 putchar('\n');
172 exit(EXIT_SUCCESS);