#!/usr/bin/perl -w use strict; # FIXME commonize with parr. # # Get the program name. my $progname = shift @ARGV; my @flags; if ($progname =~ /\s/) { # Program name contains spaces, so it's probably a string like # 'grep -i pattern' or something. We assume that the other # arguments must be filenames for this command (or whatever), so # there are no other flags. # ($progname, @flags) = split /\s+/, $progname; } else { # Try to pick out the flags. my $arg; while (defined($arg = shift @ARGV)) { if ($arg =~ /^-/) { push @flags, $arg; } else { # End of flags processing, they must all be at the front. unshift @ARGV, $arg; last; } } } foreach (@ARGV) { my $out = "$$.$_.tmp"; die "$out already exists" if -e $out; local *OLDOUT; open(OLDOUT, '>&STDOUT') or die "cannot dup stdout: $!"; open(STDOUT, ">$out") or die "cannot write to $out: $!"; my @cmd = ($progname, @flags, $_); if (system(@cmd)) { my ($status, $sig, $core) = ($? >> 8, $? & 127, $? & 128); if ($sig) { die "@cmd killed by signal $sig, aborting"; } die "@cmd failed: $status, $sig, $core"; } close(STDOUT) or die "cannot close $out: $!"; open(STDOUT, ">&OLDOUT") or die "cannot dup stdout back again: $!"; unlink $_ or die "cannot unlink $_: $!"; rename $out => $_ or die "cannot rename $out to $_: $!"; }