File Coverage

lib/POSIX/1003/FdIO.pm
Criterion Covered Total %
statement 39 48 81.2
branch 4 14 28.5
condition n/a
subroutine 12 20 60.0
pod 13 13 100.0
total 68 95 71.5


line stmt bran cond sub pod time code
1             # Copyrights 2011-2015 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.01.
5 6     6   51901 use warnings;
  6         10  
  6         215  
6 6     6   27 use strict;
  6         6  
  6         245  
7              
8             package POSIX::1003::FdIO;
9 6     6   25 use vars '$VERSION';
  6         4  
  6         361  
10             $VERSION = '0.99_06';
11              
12 6     6   23 use base 'POSIX::1003::Module';
  6         5  
  6         1551  
13              
14             # Blocks resp from unistd.h, limits.h, and stdio.h
15             my (@constants, @seek, @mode);
16             my @functions = qw/closefd creatfd dupfd dup2fd openfd pipefd
17             readfd seekfd writefd tellfd truncfd fdopen/;
18              
19             our %EXPORT_TAGS =
20             ( constants => \@constants
21             , functions => \@functions
22             , seek => \@seek
23             , mode => \@mode
24             , tables => [ qw/%seek %mode/ ]
25             );
26              
27             my $fdio;
28             our (%fdio, %seek, %mode);
29              
30             BEGIN {
31 6     6   1521 $fdio = fdio_table;
32 6         65 push @constants, keys %$fdio;
33              
34             # initialize the :seek export tag
35 6         79 push @seek, grep /^SEEK_/, keys %$fdio;
36 6         11 my %seek_subset;
37 6         500 @seek_subset{@seek} = @{$fdio}{@seek};
  6         21  
38 6         32 tie %seek, 'POSIX::1003::ReadOnlyTable', \%seek_subset;
39              
40             # initialize the :mode export tag
41 6         79 push @mode, grep /^O_/, keys %$fdio;
42 6         11 my %mode_subset;
43 6         5 @mode_subset{@mode} = @{$fdio}{@mode};
  6         37  
44 6         16 tie %mode, 'POSIX::1003::ReadOnlyTable', \%mode_subset;
45             }
46              
47              
48 5     5 1 1059 sub seekfd($$$) { goto &POSIX::lseek }
49 4     4 1 583 sub openfd($$;$) { goto &POSIX::open }
50 5     5 1 2148 sub closefd($) { goto &POSIX::close }
51 1 50   1 1 5 sub readfd($$;$) { push @_, SSIZE_MAX() if @_==2; goto &POSIX::read }
  1         10  
52 0 0   0 1 0 sub writefd($$;$) { push @_, length $_[1] if @_==2; goto &POSIX::write }
  0         0  
53 0     0 1 0 sub pipefd() { goto &POSIX::pipe }
54 0     0 1 0 sub dupfd($) { goto &POSIX::dup }
55 0     0 1 0 sub dup2fd($$) { goto &POSIX::dup2 }
56 0     0 1 0 sub statfd($) { goto &POSIX::fstat }
57 0     0 1 0 sub creatfd($$) { openfd $_[0], O_WRONLY()|O_CREAT()|O_TRUNC(), $_[1] }
58              
59              
60             # This is implemented via CORE::open, because we need an Perl FH, not a
61             # FILE *.
62              
63             sub fdopen($$)
64 1     1 1 4 { my ($fd, $mode) = @_;
65            
66 1 50       9 $mode =~ m/^([rwa]\+?|\<|\>|\>>)$/
67             or die "illegal fdopen() mode '$mode'\n";
68              
69 1 0       6 my $m = $1 eq 'r' ? '<' : $1 eq 'w' ? '>' : $1 eq 'a' ? '>>' : $1;
    0          
    50          
70              
71 1 50       8 die "fdopen() mode '$mode' (both read and write) is not supported\n"
72             if substr($m,-1) eq '+';
73              
74 1         23 open my($fh), "$m&=", $fd;
75 1         3 $fh;
76             }
77              
78              
79             #------------------
80              
81 1     1 1 5 sub tellfd($) {seekfd $_[0], 0, SEEK_CUR() }
82 0     0 1 0 sub rewindfd() {seekfd $_[0], 0, SEEK_SET() }
83              
84              
85             sub _create_constant($)
86 50     50   49 { my ($class, $name) = @_;
87 50         52 my $val = $fdio->{$name};
88 50     0   265 sub() {$val};
  0            
89             }
90              
91             1;