Implement -G support for id(1)
This commit is contained in:
		
							
								
								
									
										6
									
								
								id.1
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								id.1
									
									
									
									
									
								
							| @@ -3,10 +3,16 @@ | |||||||
| id \- print real and effective user and group IDs | id \- print real and effective user and group IDs | ||||||
| .SH SYNOPSIS | .SH SYNOPSIS | ||||||
| .B id | .B id | ||||||
|  | .RB [ -G ] | ||||||
| .RB [ user | uid ] | .RB [ user | uid ] | ||||||
| .SH DESCRIPTION | .SH DESCRIPTION | ||||||
| Print user and group information of the calling process to standard output. | 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 | If a login name or uid is specified, the user and group information of that | ||||||
| user is displayed. | user is displayed. | ||||||
|  | .SH OPTIONS | ||||||
|  | .TP | ||||||
|  | .B \-G | ||||||
|  | Display group information as whitespace separated numbers, in no particular | ||||||
|  | order. | ||||||
| .SH SEE ALSO | .SH SEE ALSO | ||||||
| .IR who(1) | .IR who(1) | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								id.c
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								id.c
									
									
									
									
									
								
							| @@ -10,6 +10,7 @@ | |||||||
| #include <ctype.h> | #include <ctype.h> | ||||||
| #include "util.h" | #include "util.h" | ||||||
|  |  | ||||||
|  | static void groupid(struct passwd *pw); | ||||||
| static void user(struct passwd *pw); | static void user(struct passwd *pw); | ||||||
| static void userid(uid_t id); | static void userid(uid_t id); | ||||||
| static void usernam(const char *nam); | static void usernam(const char *nam); | ||||||
| @@ -17,13 +18,18 @@ static void usernam(const char *nam); | |||||||
| static void | static void | ||||||
| usage(void) | usage(void) | ||||||
| { | { | ||||||
| 	eprintf("usage: %s [user | uid]\n", argv0); | 	eprintf("usage: %s [-G] [user | uid]\n", argv0); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | static int Gflag = 0; | ||||||
|  |  | ||||||
| int | int | ||||||
| main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||||
| { | { | ||||||
| 	ARGBEGIN { | 	ARGBEGIN { | ||||||
|  | 	case 'G': | ||||||
|  | 		Gflag = 1; | ||||||
|  | 		break; | ||||||
| 	default: | 	default: | ||||||
| 		usage(); | 		usage(); | ||||||
| 	} ARGEND; | 	} ARGEND; | ||||||
| @@ -47,7 +53,26 @@ main(int argc, char *argv[]) | |||||||
| 	return EXIT_SUCCESS; | 	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('\n'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static void | ||||||
|  | usernam(const char *nam) | ||||||
| { | { | ||||||
| 	struct passwd *pw; | 	struct passwd *pw; | ||||||
|  |  | ||||||
| @@ -57,10 +82,14 @@ static void usernam(const char *nam) | |||||||
| 		eprintf("getpwnam %s:", nam); | 		eprintf("getpwnam %s:", nam); | ||||||
| 	else if (!pw) | 	else if (!pw) | ||||||
| 		eprintf("getpwnam %s: no such user\n", nam); | 		eprintf("getpwnam %s: no such user\n", 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; | 	struct passwd *pw; | ||||||
|  |  | ||||||
| @@ -70,7 +99,10 @@ static void userid(uid_t id) | |||||||
| 		eprintf("getpwuid %d:", id); | 		eprintf("getpwuid %d:", id); | ||||||
| 	else if (!pw) | 	else if (!pw) | ||||||
| 		eprintf("getpwuid %d: no such user\n", id); | 		eprintf("getpwuid %d: no such user\n", id); | ||||||
| 	user(pw); | 	if (Gflag) | ||||||
|  | 		groupid(pw); | ||||||
|  | 	else | ||||||
|  | 		user(pw); | ||||||
| } | } | ||||||
|  |  | ||||||
| static void | static void | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user