#!/usr/local/bin/perl -w eval 'exec /usr/local/bin/perl -w -S $0 ${1+"$@"}' if 0; # not running under some shell # # $Id: chgrp,v 1.1.1.1 2001/06/06 08:54:05 sdague Exp $ # # $Log: chgrp,v $ # Revision 1.1.1.1 2001/06/06 08:54:05 sdague # initial import # # Revision 1.1.1.1 2001/05/13 19:55:39 sdague # added initial import of PPT work # # Revision 1.1 1999/03/02 22:34:29 abigail # Initial revision # # use strict; my ($VERSION) = '$Revision: 1.1.1.1 $' =~ /([.\d]+)/; my $warnings = 0; # Print a usuage message on a unknown option. # Requires my patch to Getopt::Std of 25 Feb 1999. $SIG {__WARN__} = sub { if (substr ($_ [0], 0, 14) eq "Unknown option") {die "Usage"}; require File::Basename; $0 = File::Basename::basename ($0); $warnings = 1; warn "$0: @_"; }; $SIG {__DIE__} = sub { require File::Basename; $0 = File::Basename::basename ($0); if (substr ($_ [0], 0, 5) eq "Usage") { die < 1; my $group = shift; defined (my $gid = getgrnam $group) or die "$group is an invalid group\n"; my %ARGV; %ARGV = map {$_ => 1} @ARGV if $options {H}; sub modify_file; if (exists $options {R}) { # Recursion. require File::Find; File::Find::find (\&modify_file, @ARGV); } else { foreach my $file (@ARGV) { modify_file $file; } } # File::Find is weird. If called with a directory, it will call # the sub with "." as file name, while having chdir()ed to the # directory. But it doesn't do that in recursion, just the top # level ones. And it ain't true that $File::Find::name eq # "$File::Find::dir/$_" in all cases. # But it shouldn't matter in this case. sub modify_file { my $file = @_ ? shift : $_; # Now, if this is a symbolic link, it points somewhere, # *and* we are following symbolic links, we recurse. # This may never end as symlinks can form loops. if (-l $file && -e $file && ($options {L} || $options {H} && $ARGV {$file})) { # We don't want to recurse symlinks that just happen to # have the same name as one of the arguments, hence the local. # Remember that $file is relative to the current directory. local $ARGV {readlink $file} = 0; File::Find::find (\&modify_file, readlink $file); return; } unless (-e $file) { warn "$file does not exist\n"; return; } my $uid = (stat $file) [4] or do { warn "failed to stat $file: $!\n"; return; }; chown $uid, $gid, $file or warn "$!\n"; } exit $warnings; __END__ =pod =head1 NAME chgrp -- change group ownership of files. =head1 SYNOPSIS chgrp [-R [-H | -L | -P]] group file [files ...] =head1 DESCRIPTION I sets the group ownership of files. The first argument after the options is the new group. =head2 OPTIONS I accepts the options described below. The options I<-L>, I<-H> and I<-P> are mutally exclusive, and only the last given option will be honoured. All of I<-L>, I<-H> and I<-P> require the I<-R> option to be set first. =over 4 =item -R Recurse into directories. Any directories are recursively traversed, and all files and directories will change owner. =item -L Follow symbolic links. By default, I will not follow symbolic links. This is a potential dangerous option, as I will not check for cycles. Be careful. This option requires the I<-R> option to be set. =item -H Follow symbolic links of command line files/directories only. This option requires the I<-R> option to be set. =item -P Do not follow symbolic links at all. This option requires the I<-R> option to be set. =back =head1 ENVIRONMENT The working of I is not influenced by any environment variables. =head1 BUGS I can loop forever when symbolic links create cycles. I uses C to recurse. =head1 REVISION HISTORY $Log: chgrp,v $ Revision 1.1.1.1 2001/06/06 08:54:05 sdague initial import Revision 1.1.1.1 2001/05/13 19:55:39 sdague added initial import of PPT work Revision 1.1 1999/03/02 22:34:29 abigail Initial revision =head1 AUTHOR The Perl implementation of I was written by Abigail, I. =head1 COPYRIGHT and LICENSE This program is copyright by Abigail 1999. This program is free and open software. You may use, copy, modify, distribute and sell this program (and any modified variants) in any way you wish, provided you do not restrict others to do the same. =cut