id: code shrink

function                                             old     new   delta
id_main                                              494     462     -32
This commit is contained in:
Denis Vlasenko
2008-09-13 12:51:10 +00:00
parent 0ee1cb0084
commit 4f3209b9d4
2 changed files with 21 additions and 20 deletions

View File

@ -45,7 +45,7 @@ int id_main(int argc UNUSED_PARAM, char **argv)
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
gid_t *groups; gid_t *groups;
int grp; int n;
unsigned long flags; unsigned long flags;
short status; short status;
#if ENABLE_SELINUX #if ENABLE_SELINUX
@ -72,17 +72,17 @@ int id_main(int argc UNUSED_PARAM, char **argv)
/* in this case PRINT_REAL is the same */ /* in this case PRINT_REAL is the same */
} }
grp = getgroups(0, 0); n = getgroups(0, NULL);
groups = (gid_t *)xmalloc(sizeof(gid_t) * grp); groups = xmalloc(sizeof(groups[0]) * n);
getgroups(grp, (gid_t *)groups); getgroups(n, groups);
if (flags & (JUST_ALL_GROUPS)) { if (flags & JUST_ALL_GROUPS) {
while (grp--) { while (n--) {
if (flags & NAME_NOT_NUMBER) if (flags & NAME_NOT_NUMBER)
printf("%s", bb_getgrgid(NULL, 0, *groups++)); printf("%s", bb_getgrgid(NULL, 0, *groups++));
else else
printf("%d", *groups++); printf("%d", (int) *groups++);
bb_putchar((grp > 0) ? ' ' : '\n'); bb_putchar((n > 0) ? ' ' : '\n');
} }
/* exit */ /* exit */
fflush_stdout_and_exit(EXIT_SUCCESS); fflush_stdout_and_exit(EXIT_SUCCESS);
@ -105,7 +105,7 @@ int id_main(int argc UNUSED_PARAM, char **argv)
#if ENABLE_SELINUX #if ENABLE_SELINUX
if (flags & JUST_CONTEXT) { if (flags & JUST_CONTEXT) {
selinux_or_die(); selinux_or_die();
if (argc - optind == 1) { if (argv[optind]) {
bb_error_msg_and_die("user name can't be passed with -Z"); bb_error_msg_and_die("user name can't be passed with -Z");
} }
@ -122,16 +122,17 @@ int id_main(int argc UNUSED_PARAM, char **argv)
/* Print full info like GNU id */ /* Print full info like GNU id */
/* bb_getpwuid(0) doesn't exit on failure (returns NULL) */ /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid="); status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid=");
bb_putchar(' '); status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid=");
status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), "gid="); {
printf(" groups="); const char *msg = " groups=";
while (grp--) { while (n--) {
status |= printf_full(*groups, bb_getgrgid(NULL, 0, *groups), ""); status |= printf_full(*groups, bb_getgrgid(NULL, 0, *groups), msg);
if (grp > 0) msg = ",";
bb_putchar(','); groups++;
groups++; }
} }
/* Don't free groups */ /* we leak groups vector... */
#if ENABLE_SELINUX #if ENABLE_SELINUX
if (is_selinux_enabled()) { if (is_selinux_enabled()) {
security_context_t mysid; security_context_t mysid;

View File

@ -1602,9 +1602,9 @@
USE_SELINUX( \ USE_SELINUX( \
"\n -Z Print the security context" \ "\n -Z Print the security context" \
) \ ) \
"\n -G Print all group IDs" \
"\n -g Print group ID" \
"\n -u Print user ID" \ "\n -u Print user ID" \
"\n -g Print group ID" \
"\n -G Print supplementary group IDs" \
"\n -n Print name instead of a number" \ "\n -n Print name instead of a number" \
"\n -r Print real user ID instead of effective ID" \ "\n -r Print real user ID instead of effective ID" \