comparison bin/cek.pl @ 76:8fc79d327ccd

replaced crc calculator with perl version.
author Edho Prima Arief <me@myconan.net>
date Sun, 19 Jun 2011 01:56:31 +0700
parents
children fef22522dca3
comparison
equal deleted inserted replaced
75:f993714efa7b 76:8fc79d327ccd
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5 use Compress::Zlib;
6
7 sub get_file_crc32 {
8 my $block_size = 1*2**20;
9 my $crc32_digest = 0;
10 my $return_code = 0;
11 my $file_name = $_[0];
12 if (!$file_name) {
13 #internal error, empty file name
14 $return_code = 10;
15 } elsif (!-f $file_name) {
16 #input file name is not a file
17 $return_code = 11;
18 } elsif (!-r $file_name) {
19 #input file is not readable
20 $return_code = 12;
21 }
22 if ($return_code == 0) {
23 open(my $file_handle, '<', $file_name);
24 binmode($file_handle);
25 while(sysread($file_handle, $_, $block_size)) {
26 $crc32_digest = crc32($_, $crc32_digest);
27 }
28 close($file_handle);
29 }
30 return ($return_code, sprintf('%08X', $crc32_digest));
31 }
32
33 foreach (@ARGV) {
34 my $file_name = $_;
35 my @crc32 = get_file_crc32($file_name);
36 printf("%s %s", $file_name, $crc32[1]);
37 if ($crc32[0] > 0) {
38 printf(" [ERR%02d]", $crc32[0]);
39 }
40 printf("\n");
41 }