« auto/share/dist/App-RegexFileUtils/ppt/rm.pl
↑ Summary
auto/share/dist/App-RegexFileUtils/ppt/mv.pl »
| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: ln,v 1.2 2004/08/05 14:17:43 cwest Exp $ | ||
| 5 | # | ||
| 6 | # $Log: ln,v $ | ||
| 7 | # Revision 1.2 2004/08/05 14:17:43 cwest | ||
| 8 | # cleanup, new version number on website | ||
| 9 | # | ||
| 10 | # Revision 1.1 2004/07/23 20:10:07 cwest | ||
| 11 | # initial import | ||
| 12 | # | ||
| 13 | # Revision 1.1 1999/03/14 05:26:18 abigail | ||
| 14 | # Initial revision | ||
| 15 | # | ||
| 16 | # | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use Getopt::Std; | ||
| 20 | |||
| 21 | my ($VERSION) = '$Revision: 1.2 $' =~ /([.\d]+)/; | ||
| 22 | |||
| 23 | my $warnings = 0; | ||
| 24 | |||
| 25 | # Print a usuage message on a unknown option. | ||
| 26 | # Requires my patch to Getopt::Std of 25 Feb 1999. | ||
| 27 | $SIG {__WARN__} = sub { | ||
| 28 | if (substr ($_ [0], 0, 14) eq "Unknown option") {die "Usage"}; | ||
| 29 | require File::Basename; | ||
| 30 | $0 = File::Basename::basename ($0); | ||
| 31 | $warnings = 1; | ||
| 32 | warn "$0: @_"; | ||
| 33 | }; | ||
| 34 | |||
| 35 | $SIG {__DIE__} = sub { | ||
| 36 | require File::Basename; | ||
| 37 | $0 = File::Basename::basename ($0); | ||
| 38 | if (substr ($_ [0], 0, 5) eq "Usage") { | ||
| 39 | die <<EOF; | ||
| 40 | $0 (Perl bin utils) $VERSION | ||
| 41 | $0 [-sf] source_file [target_file | [source_files ...] target_directory] | ||
| 42 | EOF | ||
| 43 | } | ||
| 44 | die "$0: @_"; | ||
| 45 | }; | ||
| 46 | |||
| 47 | # Get the options. | ||
| 48 | # s => symbolic links. | ||
| 49 | # f => force. | ||
| 50 | getopts ('sf', \my %options); | ||
| 51 | |||
| 52 | die "Usage" unless @ARGV; | ||
| 53 | |||
| 54 | my $target; | ||
| 55 | if (@ARGV > 1) { | ||
| 56 | $target = pop @ARGV; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | require File::Basename; | ||
| 60 | $target = File::Basename::basename ($ARGV [0]); | ||
| 61 | die "$target: cannot overwrite directory" if -d $target; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Deal with the force option in that case of a "file" target. | ||
| 65 | unless (-d $target) { | ||
| 66 | if (exists $options {f} && -e $target) { | ||
| 67 | unlink $target or die "cannot unlink `$target': $!"; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | # Since creating symbolic links uses a different function than creating | ||
| 72 | # hard links, we stick it in a closure. | ||
| 73 | # Note that we can't do the obvious symlink (@_), as those bloody prototypes | ||
| 74 | # force use to spell out the arguments, and & can't be used on buildins. | ||
| 75 | # perlsub also hints it's possible to override the global link(), and | ||
| 76 | # hence have symlink() called if the 's' option is given. But perlsub | ||
| 77 | # cowardly calls Exporter -> export() instead of explaining it, and the | ||
| 78 | # author of Exporter.pm should be shot for the lack of comments. | ||
| 79 | # I don't feel like plowing through its code. | ||
| 80 | my $link = exists $options {s} ? sub {symlink ($_ [0], $_ [1])} | ||
| 81 | : sub { link ($_ [0], $_ [1])}; | ||
| 82 | |||
| 83 | foreach my $file (@ARGV) { | ||
| 84 | my $this_target; | ||
| 85 | if (-d $target) { | ||
| 86 | require File::Basename; | ||
| 87 | require File::Spec; | ||
| 88 | # Isn't this silly? Two File:: namespaces, one is procedurial, | ||
| 89 | # the other OO. And what's the point of the OO-ness of File::Spec | ||
| 90 | # anyway?? | ||
| 91 | my $this_file = File::Basename::basename ($file); | ||
| 92 | $this_target = File::Spec -> catfile ($target, $this_file); | ||
| 93 | # Deal with the force option. | ||
| 94 | if (exists $options {f} && -e $this_target) { | ||
| 95 | unlink $this_target or do { | ||
| 96 | warn "cannot unlink `$this_target': $!"; | ||
| 97 | next; | ||
| 98 | }; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $this_target = $target; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Now it's time to make the link. | ||
| 106 | $link -> ($file, $this_target) or | ||
| 107 | warn "failed to link $file to $this_target: $!"; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | exit $warnings; | ||
| 112 | |||
| 113 | __END__ |