diff --git a/bin/chmod b/bin/chmod index e963661e..7a068e73 100755 --- a/bin/chmod +++ b/bin/chmod @@ -15,28 +15,13 @@ use strict; use File::Basename qw(basename); +use constant EX_SUCCESS => 0; +use constant EX_FAILURE => 1; + my $Program = basename($0); -my $VERSION = '1.3'; - -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"}; - $warnings = 1; - warn "$Program: @_"; -}; - -$SIG {__DIE__} = sub { - if (substr ($_ [0], 0, 5) eq "Usage") { - die < 1; - my $mode = shift; +usage() unless @ARGV; my $symbolic = 0; if ($mode =~ /[^0-7]/) { $symbolic = 1; } elsif ($mode !~ /^[0-7]{1,4}$/) { - die "invalid mode: $mode\n" + warn "$Program: invalid mode: '$mode'\n"; + exit EX_FAILURE; } my %ARGV; @@ -86,6 +71,12 @@ else { modify_file $file; } } +exit $rc; + +sub usage { + print "usage: $Program [-R [-H | -L | -P]] mode file...\n"; + exit EX_FAILURE; +} # File::Find is weird. If called with a directory, it will call # the sub with "." as file name, while having chdir()ed to the @@ -108,19 +99,23 @@ sub modify_file { return; } unless (-e $file) { - warn "$file does not exist\n"; + warn "$Program: '$file' does not exist\n"; + $rc = EX_FAILURE; return; } my $realmode = $mode; if ($symbolic) { - $realmode = mod($mode, $file) or - die "invalid mode: $mode\n"; + $realmode = mod($mode, $file) or do { + warn "$Program: invalid mode: '$mode'\n"; + exit EX_FAILURE; + }; + } + unless (chmod oct($realmode), $file) { + warn "$Program: failed to change mode for '$file': $!\n"; + $rc = EX_FAILURE; } - chmod oct ($realmode), $file or warn "failed to change mode for '$file': $!\n"; } -exit $warnings; - # # $Id: SymbolicMode.pm,v 1.1 2004/07/23 20:10:01 cwest Exp $ # @@ -333,7 +328,7 @@ chmod - change permissions of files =head1 SYNOPSIS -B [B<-R> [B<-H> | B<-L> | B<-P>]] I I [I ...] +B [B<-R> [B<-H> | B<-L> | B<-P>]] I I... =head1 DESCRIPTION