#!/usr/bin/perl -s

# ingroup - list members of the given groups
# Steve Kinzler, steve@kinzler.com, Nov 95
# https://kinzler.com/me/home.html#unix

$usage = "usage: $0 [ -p ] [ -g ] group ...
	-p	list only group members from the passwd file
	-g	list only group members from the group file
With no arguments, group members are listed from either the passwd or
the group file.  If group is numeric, it's taken to be a gid instead of
a group name.\n";
die $usage if $h || ! @ARGV;

$p = $g = 1 unless $p || $g;

foreach $arg (@ARGV) {
	($gid, $members) =
		(($arg =~ /^-?\d+$/) ? getgrgid($arg) : getgrnam($arg))[2, 3];
	$gid = $arg if $gid eq '' && $arg =~ /^-?\d+$/;

	grep($out{$_}++ && 0, split(/\s+/, $members)) if $g;

	next if ! $p || $gid eq '';
	while (($user, $passwd, $uid, $ugid) = getpwent) {
		$out{$user}++ if $ugid == $gid;
	}
}

$, = "\n";
print sort(keys %out), '';
