File Coverage

bin/uudecode
Criterion Covered Total %
statement 41 55 74.5
branch 16 26 61.5
condition 1 3 33.3
subroutine 5 6 83.3
pod n/a
total 63 90 70.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             =begin metadata
4              
5             Name: uudecode
6             Description: decode a binary file
7             Author: Nick Ing-Simmons, nick@ni-s.u-net.com
8             Author: Tom Christiansen, tchrist@perl.com
9             Author: brian d foy, brian.d.foy@gmail.com
10             License: perl
11              
12             =end metadata
13              
14             =cut
15              
16              
17 4     4   18269 use strict;
  4         7  
  4         174  
18              
19 4     4   17 use File::Basename qw(basename);
  4         5  
  4         358  
20 4     4   5861 use Getopt::Std qw(getopts);
  4         8302  
  4         351  
21              
22 4     4   23 use constant EX_SUCCESS => 0;
  4         10  
  4         387  
23 4     4   17 use constant EX_FAILURE => 1;
  4         7  
  4         7619  
24              
25 4         556523 my $Program = basename($0);
26              
27 4         11 my %opt;
28 4 50       27 getopts('io:', \%opt) or usage();
29 4         164 my $output_file = $opt{'o'};
30              
31             FILESPEC :
32 4         458 while (<>) {
33 4         15 my( $mode, $header_name );
34 4 50       40 next FILESPEC unless ($mode, $header_name) = /^begin\s+(\d+)\s+(\S+)/;
35 4 100       14 $output_file = $header_name unless defined $output_file;
36              
37 4         16 my $out;
38 4 100       15 if ($output_file eq '-') {
39 2         8 $out = *STDOUT;
40             }
41             else {
42 2 50 33     8 if ($opt{'i'} && -e $output_file) {
43 0         0 warn "$Program: won't clobber file '$output_file'\n";
44 0         0 exit EX_FAILURE;
45             }
46 2 50       240 unless (open $out, '>', $output_file) {
47 0         0 warn "$Program: can't create '$output_file': $!\n";
48 0         0 exit EX_FAILURE;
49             }
50             # Quickly protect file before data is written.
51             # XXX: Does this break on sub-Unix systems, like if
52             # it's a mode 400 or 000 file? If so, then we must
53             # wait until after the close.
54 2 50       62 unless (chmod oct($mode), $output_file) {
55 0         0 warn "$Program: can't chmod '$output_file' to mode '$mode': $!\n";
56 0         0 exit EX_FAILURE;
57             }
58             }
59              
60 4         15 binmode($out); # winsop
61 4         12 my $ended = 0;
62              
63             LINE:
64 4         43 while (<>) {
65 12 100       52 if (/^end$/) {
66 4         7 $ended = 1;
67 4         11 last LINE;
68             }
69 8 50       38 next LINE if /[a-z]/;
70 8 50       41 next LINE unless int((((ord() - 32) & 077) + 2) / 3)
71             == int(length() / 4);
72 8 50       73 unless (print $out unpack("u", $_)) {
73 0         0 warn "$Program: can't write '$output_file': $!\n";
74 0         0 exit EX_FAILURE;
75             }
76             }
77 4 50       164 unless (close $out) {
78 0         0 warn "$Program: can't close '$output_file': $!\n";
79 0         0 exit EX_FAILURE;
80             }
81 4 50       81 unless ($ended) {
82 0         0 warn "$Program: missing end; '$output_file' may be truncated\n";
83 0         0 exit EX_FAILURE;
84             }
85             }
86 4         0 exit EX_SUCCESS;
87              
88             sub usage {
89 0     0     require Pod::Usage;
90 0           Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
91             }
92              
93             __END__