1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Arnold Robbins, arnold@audiofax.com.
19 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu. */
24 #include <sys/types.h>
36 #else /* not _POSIX_VERSION */
37 struct passwd
*getpwuid ();
38 struct group
*getgrgid ();
43 #include <sys/param.h>
44 #endif /* not _POSIX_VERSION */
49 static void print_user
__P ((int uid
));
50 static void print_group
__P ((int gid
));
51 static void print_group_list
__P ((char *username
));
52 static void print_full_info
__P ((char *username
));
53 static void usage
__P ((int status
));
55 /* The name this program was run with. */
58 /* If nonzero, output only the group ID(s). -g */
59 static int just_group
= 0;
61 /* If nonzero, output user/group name instead of ID number. -n */
62 static int use_name
= 0;
64 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
65 static int use_real
= 0;
67 /* If nonzero, output only the user ID(s). -u */
68 static int just_user
= 0;
70 /* If nonzero, output only the supplementary groups. -G */
71 static int just_group_list
= 0;
73 /* The real and effective IDs of the user to print. */
74 static uid_t ruid
, euid
;
75 static gid_t rgid
, egid
;
77 /* The number of errors encountered so far. */
78 static int problems
= 0;
80 /* If nonzero, display usage information and exit. */
83 /* If nonzero, print the version on standard output and exit. */
84 static int show_version
;
86 static struct option
const longopts
[] =
88 {"group", no_argument
, NULL
, 'g'},
89 {"groups", no_argument
, NULL
, 'G'},
90 {"help", no_argument
, &show_help
, 1},
91 {"name", no_argument
, NULL
, 'n'},
92 {"real", no_argument
, NULL
, 'r'},
93 {"user", no_argument
, NULL
, 'u'},
94 {"version", no_argument
, &show_version
, 1},
99 main (int argc
, char **argv
)
103 program_name
= argv
[0];
104 setlocale (LC_ALL
, "");
105 bindtextdomain (PACKAGE
, LOCALEDIR
);
106 textdomain (PACKAGE
);
108 while ((optc
= getopt_long (argc
, argv
, "gnruG", longopts
, (int *) 0))
137 printf ("id - %s\n", version_string
);
144 if (just_user
+ just_group
+ just_group_list
> 1)
145 error (1, 0, _("cannot print only user and only group"));
147 if (just_user
+ just_group
+ just_group_list
== 0 && (use_real
|| use_name
))
148 error (1, 0, _("cannot print only names or real IDs in default format"));
150 if (argc
- optind
> 1)
153 if (argc
- optind
== 1)
155 struct passwd
*pwd
= getpwnam (argv
[optind
]);
157 error (1, 0, _("%s: No such user"), argv
[optind
]);
158 ruid
= euid
= pwd
->pw_uid
;
159 rgid
= egid
= pwd
->pw_gid
;
170 print_user (use_real
? ruid
: euid
);
172 print_group (use_real
? rgid
: egid
);
173 else if (just_group_list
)
174 print_group_list (argv
[optind
]);
176 print_full_info (argv
[optind
]);
179 exit (problems
!= 0);
182 /* Print the name or value of user ID UID. */
187 struct passwd
*pwd
= NULL
;
191 pwd
= getpwuid (uid
);
197 printf ("%u", (unsigned) uid
);
199 printf ("%s", pwd
->pw_name
);
202 /* Print the name or value of group ID GID. */
205 print_group (int gid
)
207 struct group
*grp
= NULL
;
211 grp
= getgrgid (gid
);
217 printf ("%u", (unsigned) gid
);
219 printf ("%s", grp
->gr_name
);
222 /* Print all of the distinct groups the user is in . */
225 print_group_list (char *username
)
240 n_groups
= getgroups (0, NULL
);
241 /* Add 1 just in case n_groups is zero. */
242 groups
= (GETGROUPS_T
*) xmalloc (n_groups
* sizeof (GETGROUPS_T
) + 1);
244 ng
= getgroups (n_groups
, groups
);
246 ng
= getugroups (n_groups
, groups
, username
);
250 error (0, errno
, _("cannot get supplemental group list"));
256 for (i
= 0; i
< n_groups
; i
++)
257 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
260 print_group (groups
[i
]);
267 /* Print all of the info about the user's user and group IDs. */
270 print_full_info (char *username
)
275 printf ("uid=%u", (unsigned) ruid
);
276 pwd
= getpwuid (ruid
);
280 printf ("(%s)", pwd
->pw_name
);
282 printf (" gid=%u", (unsigned) rgid
);
283 grp
= getgrgid (rgid
);
287 printf ("(%s)", grp
->gr_name
);
291 printf (" euid=%u", (unsigned) euid
);
292 pwd
= getpwuid (euid
);
296 printf ("(%s)", pwd
->pw_name
);
301 printf (" egid=%u", (unsigned) egid
);
302 grp
= getgrgid (egid
);
306 printf ("(%s)", grp
->gr_name
);
315 n_groups
= getgroups (0, NULL
);
316 /* Add 1 just in case n_groups is zero. */
317 groups
= (GETGROUPS_T
*) xmalloc (n_groups
* sizeof (GETGROUPS_T
) + 1);
319 ng
= getgroups (n_groups
, groups
);
321 ng
= getugroups (n_groups
, groups
, username
);
324 error (0, errno
, _("cannot get supplemental group list"));
331 fputs (_(" groups="), stdout
);
332 for (i
= 0; i
< n_groups
; i
++)
336 printf ("%u", (unsigned) groups
[i
]);
337 grp
= getgrgid (groups
[i
]);
341 printf ("(%s)", grp
->gr_name
);
352 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
356 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name
);
358 Print information for USERNAME, or the current user.\n\
360 -g, --group print only the group ID\n\
361 -G, --groups print only the supplementary groups\n\
362 -n, --name print a name instead of a number, for -ugG\n\
363 -r, --real print the real ID instead of effective ID, for -ugG\n\
364 -u, --user print only the user ID\n\
365 --help display this help and exit\n\
366 --version output version information and exit\n\
368 Without any OPTION, print some useful set of identified information.\n\