@rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S %0 %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #!perl #line 15 use strict; use warnings; use IO::Handle qw( ); use Getopt::Long qw( ); my $opt_a; sub usage { print STDERR (<<'__EOI__'); usage: tee [] [--] [file [...]] Use tee --help for help. __EOI__ exit(1); } sub help { print(<<'__EOI__'); usage: tee [] [--] [file [...]] Use tee --help for help. Options: -a Append the output to the files rather than overwriting them. -i Ignore the SIGINT signal. __EOI__ exit(0); } sub process_args { Getopt::Long::Configure('posix_default'); Getopt::Long::GetOptions( "a" => \$opt_a, "help|h|?" => \&help, ) or usage(); } { process_args(); my $mode = ($opt_a ? '>>' : '>' ); my $mode_text = ($opt_a ? 'open' : 'create'); my @fhs; my %fh_names; { my $fh = \*STDOUT; push @fhs, $fh; $fh_names{$fh} = 'STDOUT'; } foreach (@ARGV) { my $fh; if (!open($fh, $mode, $_)) { die("Unable to open file \"$_\": $!\n"); } push @fhs, $fh; $fh_names{$fh} = "file \"$_\""; } binmode(STDIN); foreach my $fh (@fhs) { $fh->autoflush(1); binmode($fh); } for (;;) { my $buf = ''; my $rv = sysread(STDIN, $buf, 4096); if (not defined $rv) { die("Unable to read from STDIN: $!\n"); } if (not $rv) { last; } foreach my $fh (@fhs) { if (!$fh->print($buf)) { die("Unable to write to $fh_names{$fh}: $!\n"); } } } } __END__ :endofperl