File Coverage

blib/lib/BSD/Getfsent.pm
Criterion Covered Total %
statement 59 59 100.0
branch 10 12 83.3
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 82 84 97.6


line stmt bran cond sub pod time code
1             package BSD::Getfsent;
2              
3 2     2   69391 use strict;
  2         9  
  2         54  
4 2     2   10 use warnings;
  2         3  
  2         50  
5 2     2   10 use base qw(Exporter);
  2         2  
  2         314  
6 2     2   954 use boolean qw(true false);
  2         6739  
  2         9  
7              
8 2     2   160 use Carp qw(croak);
  2         3  
  2         104  
9 2     2   1008 use IO::File ();
  2         17187  
  2         164  
10              
11             our ($VERSION, @EXPORT_OK, $ENTRIES, $FSTAB);
12              
13             $VERSION = '0.19';
14             @EXPORT_OK = qw(getfsent);
15             $ENTRIES = __PACKAGE__ . '::_fsents';
16             $FSTAB = '/etc/fstab';
17              
18             sub getfsent
19             {
20 7 100   7 1 274 if (wantarray) {
21 2     2   19 no strict 'refs';
  2         4  
  2         951  
22              
23 6 100       6 unless (${$ENTRIES}) {
  6         59  
24 1         3 @{$ENTRIES} = @{_parse_entries()};
  1         5  
  1         5  
25 1         5 ${$ENTRIES} = true;
  1         4  
26             }
27              
28 6 100       44 if (@{$ENTRIES}) {
  6         16  
29 5         7 return @{shift @{$ENTRIES}};
  5         5  
  5         26  
30             }
31             else {
32 1         5 ${$ENTRIES} = false;
  1         4  
33 1         2 return ();
34             }
35             }
36 1         5 else { return _count_entries() }
37             }
38              
39             sub _parse_entries
40             {
41 1     1   2 my @entries;
42 1         2 my $fh = _open_fh();
43              
44 1         36 while (local $_ = <$fh>) {
45 5 50       11 next if /^\#/;
46              
47 5         9 chomp;
48 5         18 my @entry = split;
49              
50 5 100       17 if ($entry[3] !~ /,/) { # In case element 4, fs_type, doesn't
51 1         4 splice(@entry, 3, 1, '', $entry[3]); # contain fs_mntops, insert blank fs_mntops
52             } # at index 3 and move fs_type to index 4.
53             else { # In case element 4 contains fs_type and
54 4         14 splice(@entry, 3, 1, # fs_mntops, switch fs_mntops to index 3 and
55             (reverse split ',', $entry[3], 2)); # fs_type to index 4.
56             }
57              
58 5         32 push @entries, [ @entry ];
59             }
60              
61 1         5 _close_fh($fh);
62              
63 1         20 return \@entries;
64             }
65              
66             sub _count_entries
67             {
68 1     1   2 my $counted_entries;
69              
70 1         7 my $fh = _open_fh();
71 1         56 $counted_entries++ while <$fh>;
72 1         9 _close_fh($fh);
73              
74 1         38 return $counted_entries;
75             }
76              
77             sub _open_fh
78             {
79 2 50   2   15 my $fh = IO::File->new("<$FSTAB")
80             or croak "Cannot open $FSTAB: $!";
81              
82 2         227 return $fh;
83             }
84              
85             sub _close_fh
86             {
87 2     2   6 my ($fh) = @_;
88 2         16 $fh->close;
89             }
90              
91             1;
92             __END__