Advertisement
Guest User

Untitled

a guest
Oct 14th, 2015
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. #
  4. # https://www.linux.org.ru/forum/development/12024408
  5. #
  6.  
  7. use strict;
  8. use warnings;
  9.  
  10.  
  11. my $positions_file = 'positions.txt';
  12.  
  13.  
  14. if ( @ARGV > 0 ) {
  15.     &restore_positions();
  16.     &compare();
  17.     &save_positions();
  18. } else {
  19.     &print_usage();
  20.     exit 1;
  21. }
  22.  
  23. exit 0;
  24.  
  25.  
  26. {
  27.     my %positions;
  28.  
  29.  
  30.     sub restore_positions() {
  31.         -e $positions_file or return;
  32.  
  33.         open my $fh, "<", $positions_file
  34.             or die "open $positions_file: $!";
  35.  
  36.         while ( <$fh> ) {
  37.             chomp;
  38.             my ( $file, $pos ) = split /:/;
  39.             $positions{ $file } = $pos;
  40.         }
  41.  
  42.         close $fh
  43.             or die "close $positions_file: $!";
  44.     }
  45.  
  46.     sub save_positions() {
  47.         open my $fh, ">", $positions_file
  48.             or die "open $positions_file: $!";
  49.  
  50.         while ( my ( $file, $pos ) = each %positions ) {
  51.             syswrite $fh, join( ':', $file, $pos ) . "\n";
  52.         }
  53.  
  54.         close $fh
  55.             or die "close $positions_file: !";
  56.     }
  57.  
  58.     sub get_position($) {
  59.         my ( $file ) = @_;
  60.  
  61.  
  62.         return ( exists $positions{ $file } )
  63.             ? $positions{ $file }
  64.             : 0;
  65.     }
  66.  
  67.     sub set_position($$) {
  68.         my ( $file, $pos ) = @_;
  69.  
  70.         $positions{ $file } = $pos;
  71.     }
  72. }
  73.  
  74.  
  75. sub compare() {
  76.     my @FHS;
  77.  
  78.     for ( my $filenum = 0; $filenum < @ARGV; $filenum++ ) {
  79.         my $file = $ARGV[ $filenum ];
  80.         open my $fh, "<", $file or die "open $file: $!";
  81.         push @FHS, $fh;
  82.     }
  83.  
  84.     my %uniq;
  85.  
  86.     IN: while ( my $in = <STDIN> ) {
  87.         if ( exists $uniq{ $in } ) {
  88.             delete $uniq{ $in };
  89.             next;
  90.         }
  91.  
  92.         for ( my $filenum = 0; $filenum < @ARGV; $filenum++ ) {
  93.             my $file = $ARGV[ $filenum ];
  94.             my $fh = $FHS[ $filenum ];
  95.             my $curpos = &get_position( $file );
  96.  
  97.             seek( $fh, $curpos, 0 ) or die "seek $file: $!";
  98.  
  99.             for ( $curpos = tell( $fh );
  100.                 my $line = <$fh>;
  101.                 $curpos = tell( $fh ) )
  102.             {
  103.                 if ( $line eq $in ) {
  104.                     &set_position( $file, $curpos );
  105.                     next IN;
  106.                 }
  107.  
  108.                 if ( not exists $uniq{ $line } ) {
  109.                     $uniq{ $line } = 1;
  110.                 }
  111.             }
  112.  
  113.             &set_position( $file, $curpos );
  114.         }
  115.  
  116.         print $in;
  117.     }
  118. }
  119.  
  120. sub print_usage {
  121.     print <<'EOF';
  122. Usage:
  123.     echo urls | xcmp.pl file1 [ file2 file 3 ... fileN ]
  124. EOF
  125. }
Advertisement
RAW Paste Data Copied
Advertisement