File Coverage

blib/lib/BSD/Getfsent.pm
Criterion Covered Total %
statement 56 56 100.0
branch 10 12 83.3
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 78 80 97.5


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