1 /* id -- print real and effective UIDs and GIDs
2 Copyright (C) 1989-1999 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
32 #ifndef _POSIX_VERSION
33 struct passwd
*getpwuid ();
34 struct group
*getgrgid ();
39 #endif /* not _POSIX_VERSION */
43 static void print_user
PARAMS ((int uid
));
44 static void print_group
PARAMS ((int gid
));
45 static void print_group_list
PARAMS ((const char *username
));
46 static void print_full_info
PARAMS ((const char *username
));
48 /* The name this program was run with. */
51 /* If nonzero, output only the group ID(s). -g */
52 static int just_group
= 0;
54 /* If nonzero, output user/group name instead of ID number. -n */
55 static int use_name
= 0;
57 /* If nonzero, output real UID/GID instead of default effective UID/GID. -r */
58 static int use_real
= 0;
60 /* If nonzero, output only the user ID(s). -u */
61 static int just_user
= 0;
63 /* If nonzero, output only the supplementary groups. -G */
64 static int just_group_list
= 0;
66 /* The real and effective IDs of the user to print. */
67 static uid_t ruid
, euid
;
68 static gid_t rgid
, egid
;
70 /* The number of errors encountered so far. */
71 static int problems
= 0;
73 /* If nonzero, display usage information and exit. */
76 /* If nonzero, print the version on standard output and exit. */
77 static int show_version
;
79 static struct option
const longopts
[] =
81 {"group", no_argument
, NULL
, 'g'},
82 {"groups", no_argument
, NULL
, 'G'},
83 {"help", no_argument
, &show_help
, 1},
84 {"name", no_argument
, NULL
, 'n'},
85 {"real", no_argument
, NULL
, 'r'},
86 {"user", no_argument
, NULL
, 'u'},
87 {"version", no_argument
, &show_version
, 1},
95 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
99 printf (_("Usage: %s [OPTION]... [USERNAME]\n"), program_name
);
101 Print information for USERNAME, or the current user.\n\
103 -a ignore, for compatibility with other versions\n\
104 -g, --group print only the group ID\n\
105 -G, --groups print only the supplementary groups\n\
106 -n, --name print a name instead of a number, for -ugG\n\
107 -r, --real print the real ID instead of effective ID, for -ugG\n\
108 -u, --user print only the user ID\n\
109 --help display this help and exit\n\
110 --version output version information and exit\n\
112 Without any OPTION, print some useful set of identified information.\n\
114 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
120 main (int argc
, char **argv
)
124 program_name
= argv
[0];
125 setlocale (LC_ALL
, "");
126 bindtextdomain (PACKAGE
, LOCALEDIR
);
127 textdomain (PACKAGE
);
129 while ((optc
= getopt_long (argc
, argv
, "agnruG", longopts
, NULL
)) != -1)
136 /* Ignore -a, for compatibility with SVR4. */
160 printf ("id (%s) %s\n", GNU_PACKAGE
, VERSION
);
167 if (just_user
+ just_group
+ just_group_list
> 1)
168 error (1, 0, _("cannot print only user and only group"));
170 if (just_user
+ just_group
+ just_group_list
== 0 && (use_real
|| use_name
))
171 error (1, 0, _("cannot print only names or real IDs in default format"));
173 if (argc
- optind
> 1)
176 if (argc
- optind
== 1)
178 struct passwd
*pwd
= getpwnam (argv
[optind
]);
180 error (1, 0, _("%s: No such user"), argv
[optind
]);
181 ruid
= euid
= pwd
->pw_uid
;
182 rgid
= egid
= pwd
->pw_gid
;
193 print_user (use_real
? ruid
: euid
);
195 print_group (use_real
? rgid
: egid
);
196 else if (just_group_list
)
197 print_group_list (argv
[optind
]);
199 print_full_info (argv
[optind
]);
202 exit (problems
!= 0);
205 /* Print the name or value of user ID UID. */
210 struct passwd
*pwd
= NULL
;
214 pwd
= getpwuid (uid
);
220 printf ("%u", (unsigned) uid
);
222 printf ("%s", pwd
->pw_name
);
225 /* Print the name or value of group ID GID. */
228 print_group (int gid
)
230 struct group
*grp
= NULL
;
234 grp
= getgrgid (gid
);
240 printf ("%u", (unsigned) gid
);
242 printf ("%s", grp
->gr_name
);
248 xgetgroups (const char *username
, int *n_groups
, GETGROUPS_T
**groups
)
256 max_n_groups
= getgroups (0, NULL
);
258 max_n_groups
= getugroups (0, NULL
, username
);
260 /* Add 1 just in case max_n_groups is zero. */
261 g
= (GETGROUPS_T
*) xmalloc (max_n_groups
* sizeof (GETGROUPS_T
) + 1);
263 ng
= getgroups (max_n_groups
, g
);
265 ng
= getugroups (max_n_groups
, g
, username
);
269 error (0, errno
, _("cannot get supplemental group list"));
281 #endif /* HAVE_GETGROUPS */
283 /* Print all of the distinct groups the user is in. */
286 print_group_list (const char *username
)
301 if (xgetgroups (username
, &n_groups
, &groups
))
307 for (i
= 0; i
< n_groups
; i
++)
308 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
311 print_group (groups
[i
]);
315 #endif /* HAVE_GETGROUPS */
318 /* Print all of the info about the user's user and group IDs. */
321 print_full_info (const char *username
)
326 printf ("uid=%u", (unsigned) ruid
);
327 pwd
= getpwuid (ruid
);
331 printf ("(%s)", pwd
->pw_name
);
333 printf (" gid=%u", (unsigned) rgid
);
334 grp
= getgrgid (rgid
);
338 printf ("(%s)", grp
->gr_name
);
342 printf (" euid=%u", (unsigned) euid
);
343 pwd
= getpwuid (euid
);
347 printf ("(%s)", pwd
->pw_name
);
352 printf (" egid=%u", (unsigned) egid
);
353 grp
= getgrgid (egid
);
357 printf ("(%s)", grp
->gr_name
);
366 if (xgetgroups (username
, &n_groups
, &groups
))
373 fputs (_(" groups="), stdout
);
374 for (i
= 0; i
< n_groups
; i
++)
378 printf ("%u", (unsigned) groups
[i
]);
379 grp
= getgrgid (groups
[i
]);
383 printf ("(%s)", grp
->gr_name
);
387 #endif /* HAVE_GETGROUPS */