#!/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#    chog [user [group]]                                                      #
#                                                                             #
# This program does a recursive descent starting at  the  current  directory, #
# and  changes the ownership of every file found to the given user/group.  If #
# omitted, the default is jc/mail for my convenience.  This program  is  also #
# useful  as  a  prototype  for  a  task  that  does a recursive descent of a #
# directory tree.  Note that, on most Unix systems, you will  need  to  be  a #
# super-user to run this program successfully.                                #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#
#AUTHOR
# Copyright (c) 1992 by John Chambers <jc@trillian.mit.edu>

$ARGV[0] = "jc"   if ($#ARGV < 1);
$ARGV[1] = "mail" if ($#ARGV < 2);
$usr = $ARGV[0];
$grp = $ARGV[1];
#
# Get the numeric ids for the user and group.
#
($unam,$upwd,$uid) = getpwnam($usr);
($gnam,$gpwd,$gid) = getgrnam($grp);
if (!$unam) {die "Can't get uid for \"$usr\"\n";}
if (!$gnam) {die "Can't get gid for \"$grp\"\n";}
#
# Start the recursive processing from the current directory.
#
&dodir('.',$uid,$gid);
exit 0;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Do one directory.  Note that we process the '.' and '..'  entries,  but  we
# don't  recurse  down them.  Other variants of this program may want to move
# the processing to after the "next" command.
#
sub dodir {
	local($d,$u,$g) = @_;

	if (! opendir(DIR,$d)) {
		print STDERR  "Can't read \"$d\"\n";
		return;
	}
	local(@files) = readdir(DIR);
	closedir(DIR);

	for $f (@files) {	# Run thru the files in the directory.
		$p = "$d/$f";	# Path relative to $cwd.
		chown($u,$g,$p);	# Do it to one file.
		next if ($f eq '.' || $f eq '..');
		&dodir($p,$u,$g)	# Recursion,
			if -d $p;		# whenever we hit a subdirectory.
	}
}
