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