home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / clones / tee < prev    next >
Encoding:
Text File  |  1991-06-04  |  1.1 KB  |  49 lines

  1. #!/usr/bin/perl
  2. #
  3. # tee clone that groks process tees (should work even with old perls)
  4. # Tom Christiansen <tchrist@convex.com>
  5. # 6 June 91
  6.  
  7. while ($ARGV[0] =~ /^-(.+)/ && (shift, ($_ = $1), 1)) {
  8.     next if /^$/;
  9.     s/i// && (++$ignore_ints, redo); 
  10.     s/a// && (++$append,      redo);
  11.     s/u// && (++$unbuffer,    redo);
  12.     die "usage tee [-aiu] [filenames] ...\n";
  13. if ($ignore_ints) {
  14.     for $sig ('INT', 'TERM', 'HUP', 'QUIT') { $SIG{$sig} = 'IGNORE'; } 
  15. }
  16. $SIG{'PIPE'} = 'PLUMBER';
  17. $mode = $append ? '>>' : '>';
  18. $fh = 'FH000';
  19. %fh = ('STDOUT', 'standard output'); # always go to stdout
  20. $| = 1 if $unbuffer;
  21.  
  22. for (@ARGV) {
  23.     if (!open($fh, (/^[^>|]/ && $mode) . $_)) {
  24.     warn "$0: cannot open $_: $!\n"; # like sun's; i prefer die
  25.     $status++;
  26.     next;
  27.     }
  28.     select((select($fh), $| = 1)[0]) if $unbuffer;
  29.     $fh{$fh++} = $_;
  30. while (<STDIN>) {
  31.     for $fh (keys %fh) {
  32.     print $fh $_;
  33.     } 
  34. for $fh (keys %fh) { 
  35.     next if close($fh) || !defined $fh{$fh};
  36.     warn "$0: couldn't close $fh{$fh}: $!\n";
  37.     $status++;
  38. }
  39. exit $status;
  40.  
  41. sub PLUMBER {
  42.     warn "$0: pipe to \"$fh{$fh}\" broke!\n";
  43.     $status++;
  44.     delete $fh{$fh};
  45.