| 1 |
|
| #include <EXTERN.h> |
| 2 |
|
| #include <perl.h> |
| 3 |
|
| #include <XSUB.h> |
| 4 |
|
| |
| 5 |
|
| #include <perliol.h> |
| 6 |
|
| |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #define CanRead(f) (PerlIOBase(f)->flags & PERLIO_F_CANREAD) |
| 10 |
|
| |
| 11 |
|
| #define undef (&PL_sv_undef) |
| 12 |
|
| |
| 13 |
|
| MODULE = IO::Handle::unread PACKAGE = IO::Handle |
| 14 |
|
| |
| 15 |
|
| PROTOTYPES: DISABLE |
| 16 |
|
| |
| 17 |
|
| IV |
| 18 |
|
| unread(filehandle, string, length = undef) |
| 19 |
|
| SV* filehandle |
| 20 |
|
| SV* string |
| 21 |
|
| SV* length |
| 22 |
|
| PREINIT: |
| 23 |
|
| PerlIO* f; |
| 24 |
12 |
| IO* io = sv_2io(filehandle); /* doesn't return NULL */ |
| 25 |
|
| INIT: |
| 26 |
12 |
| RETVAL = -1; |
| 27 |
|
| CODE: |
| 28 |
20 |
▶ | if((f = IoIFP(io)) && PerlIOValid(f) && CanRead(f)){ |
Branch: 4 branches
| Branch | True | False | Total |
| if((f = IoIFP(io)) && PerlIOValid(f) && CanRead(f)){ |
10 |
2 |
12 |
| if((f = IoIFP(io)) && PerlIOValid(f) && CanRead(f)){ |
10 |
0 |
10 |
| if((f = IoIFP(io)) && PerlIOValid(f) && CanRead(f)){ |
10 |
0 |
10 |
| if((f = IoIFP(io)) && PerlIOValid(f) && CanRead(f)){ |
9 |
1 |
10 |
|
| 29 |
|
| STRLEN len; |
| 30 |
9 |
| const char* pv = SvPV_const(string, len); |
| 31 |
|
| |
| 32 |
9 |
▶ | if(SvOK(length)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvOK(length)){ |
3 |
6 |
9 |
|
| 33 |
|
| UV uv; |
| 34 |
3 |
▶ | if(looks_like_number(length) && SvIV(length) < 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(looks_like_number(length) && SvIV(length) < 0) |
3 |
0 |
3 |
| if(looks_like_number(length) && SvIV(length) < 0) |
1 |
2 |
3 |
|
| 35 |
1 |
| Perl_croak(aTHX_ "Negative length"); |
| 36 |
|
| |
| 37 |
2 |
| uv = SvUV(length); |
| 38 |
2 |
▶ | if(uv < len){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(uv < len){ |
1 |
1 |
2 |
|
| 39 |
1 |
| len = uv; |
| 40 |
|
| } |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
8 |
| RETVAL = PerlIO_unread(f, pv, len); |
| 44 |
|
| } |
| 45 |
|
| else { |
| 46 |
3 |
▶ | if(ckWARN(WARN_IO)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(ckWARN(WARN_IO)){ |
3 |
0 |
3 |
|
| 47 |
3 |
| const char* msg = |
| 48 |
1 |
▶ | (PerlIOValid(f) && !CanRead(f)) ? "FileHandle opened only for output" |
Branch: 2 branches
| Branch | True | False | Total |
| (PerlIOValid(f) && !CanRead(f)) ? "FileHandle opened only for output" |
1 |
0 |
1 |
| (PerlIOValid(f) && !CanRead(f)) ? "FileHandle opened only for output" |
0 |
1 |
1 |
|
| 49 |
4 |
▶ | : IoTYPE(io) == IoTYPE_CLOSED ? "unread() on closed filehandle" |
Branch: 2 branches
| Branch | True | False | Total |
| : IoTYPE(io) == IoTYPE_CLOSED ? "unread() on closed filehandle" |
1 |
2 |
3 |
| : IoTYPE(io) == IoTYPE_CLOSED ? "unread() on closed filehandle" |
1 |
1 |
2 |
|
| 50 |
|
| : "unread() on unopened filehandle"; |
| 51 |
|
| |
| 52 |
3 |
| Perl_warner(aTHX_ packWARN(WARN_IO), msg); |
| 53 |
|
| } |
| 54 |
0 |
| SETERRNO(EBADF,RMS_IFI); |
| 55 |
0 |
| XSRETURN_EMPTY; |
| 56 |
|
| } |
| 57 |
|
| OUTPUT: |
| 58 |
|
| RETVAL |