[hackers] [sbase] Implement -G support for id(1) || sin
commit 5534e607038648e7692b1e232d5825d966219028
Author: sin <sin_AT_2f30.org>
Date: Sun Dec 1 17:29:20 2013 +0000
Implement -G support for id(1)
diff --git a/id.1 b/id.1
index 6bfa06b..967022c 100644
--- a/id.1
+++ b/id.1
_AT_@ -3,10 +3,16 @@
id \- print real and effective user and group IDs
.SH SYNOPSIS
.B id
+.RB [ -G ]
.RB [ user | uid ]
.SH DESCRIPTION
Print user and group information of the calling process to standard output.
If a login name or uid is specified, the user and group information of that
user is displayed.
+.SH OPTIONS
+.TP
+.B \-G
+Display group information as whitespace separated numbers, in no particular
+order.
.SH SEE ALSO
.IR who(1)
diff --git a/id.c b/id.c
index 78fa71d..225c254 100644
--- a/id.c
+++ b/id.c
_AT_@ -10,6 +10,7 @@
#include <ctype.h>
#include "util.h"
+static void groupid(struct passwd *pw);
static void user(struct passwd *pw);
static void userid(uid_t id);
static void usernam(const char *nam);
_AT_@ -17,13 +18,18 @@ static void usernam(const char *nam);
static void
usage(void)
{
- eprintf("usage: %s [user | uid]
", argv0);
+ eprintf("usage: %s [-G] [user | uid]
", argv0);
}
+static int Gflag = 0;
+
int
main(int argc, char *argv[])
{
ARGBEGIN {
+ case 'G':
+ Gflag = 1;
+ break;
default:
usage();
} ARGEND;
_AT_@ -47,7 +53,26 @@ main(int argc, char *argv[])
return EXIT_SUCCESS;
}
-static void usernam(const char *nam)
+static void
+groupid(struct passwd *pw)
+{
+ gid_t gid, groups[NGROUPS_MAX];
+ int ngroups;
+ int i;
+
+ ngroups = NGROUPS_MAX;
+ getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
+ for (i = 0; i < ngroups; i++) {
+ gid = groups[i];
+ printf("%u", gid);
+ if (i < ngroups - 1)
+ putchar(' ');
+ }
+ putchar('
');
+}
+
+static void
+usernam(const char *nam)
{
struct passwd *pw;
_AT_@ -57,10 +82,14 @@ static void usernam(const char *nam)
eprintf("getpwnam %s:", nam);
else if (!pw)
eprintf("getpwnam %s: no such user
", nam);
- user(pw);
+ if (Gflag)
+ groupid(pw);
+ else
+ user(pw);
}
-static void userid(uid_t id)
+static void
+userid(uid_t id)
{
struct passwd *pw;
_AT_@ -70,7 +99,10 @@ static void userid(uid_t id)
eprintf("getpwuid %d:", id);
else if (!pw)
eprintf("getpwuid %d: no such user
", id);
- user(pw);
+ if (Gflag)
+ groupid(pw);
+ else
+ user(pw);
}
static void
Received on Sun Dec 01 2013 - 18:30:04 CET
This archive was generated by hypermail 2.3.0
: Sun Dec 01 2013 - 18:36:14 CET