File Coverage

Pipe.xs
Criterion Covered Total %
statement 19 20 95.0
total 19 20 95.0


line stmt code
1   #define PERL_NO_GET_CONTEXT
2    
3   #include "EXTERN.h"
4   #include "perl.h"
5   #include "XSUB.h"
6    
7   #include
8   #include
9    
10   #include
11    
12   // CopSTASH isn’t documented as part of the API but is heavily used
13   // on CPAN. There is PL_curstash, but that doesn’t work with, e.g.,
14   // “perl -e'package Foo; _print_pl_curstash()'”, whereas PL_curcop does.
15   #define SP_CUR_STASH ( (HV*)CopSTASH(PL_curcop) )
16    
17   #define SP_CUR_PKGNAME HvNAME( SP_CUR_STASH )
18    
19   //#include "ppport.h"
20    
21   //----------------------------------------------------------------------
22    
23 2 static inline void _fd2sv( pTHX_ int fd, bool is_read, SV* sv ) {
24 2 PerlIO *pio = PerlIO_fdopen(fd, is_read ? "r" : "w");
25    
26 2 GV* gv = newGVgen( SP_CUR_PKGNAME );
27 2 IO* io = GvIOn(gv);
28    
29 2 SvUPGRADE(sv, SVt_IV);
30 2 SvROK_on(sv);
31 2 SvRV_set(sv, (SV*)gv);
32    
33 2 IoTYPE(io) = is_read ? '<' : '>';
34 2 IoIFP(io) = pio;
35 2 IoOFP(io) = pio;
36 2 }
37    
38 1 int _sp_pipe( pTHX_ SV* infh, SV* outfh, int flags ) {
39   int fds[2];
40    
41   // This macro comes from Makefile.PL:
42   #ifdef SP_HAS_PIPE2
43 1 int ret = pipe2(fds, flags);
44   #else
45   if (flags != 0) {
46   croak("This system lacks pipe2 support, so pipe() cannot accept flags.");
47   }
48    
49   int ret = pipe(fds);
50   #endif
51    
52 1 if (!ret) {
53    
54   // These don’t seem to be available to extensions,
55   // but apparently they’re unneeded anyway.
56   //
57   // Perl_setfd_cloexec_for_nonsysfd(fds[0]);
58   // Perl_setfd_cloexec_for_nonsysfd(fds[1]);
59    
60 1 _fd2sv( aTHX_ fds[0], true, infh );
61 1 _fd2sv( aTHX_ fds[1], false, outfh );
62   }
63    
64 1 return ret;
65   }
66    
67   //----------------------------------------------------------------------
68   //----------------------------------------------------------------------
69    
70   MODULE = Sys::Pipe PACKAGE = Sys::Pipe
71    
72   PROTOTYPES: DISABLE
73    
74   SV*
75   pipe( SV *infh, SV *outfh, int flags = 0 )
76   CODE:
77 1 if (_sp_pipe(aTHX_ infh, outfh, flags)) {
78 0 RETVAL = &PL_sv_undef;
79   }
80   else {
81 1 RETVAL = newSVuv(1);
82   }
83    
84   OUTPUT:
85   RETVAL
86