| 1 |
|
| #include <sys/types.h> |
| 2 |
|
| |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| #include <unistd.h> |
| 5 |
|
| |
| 6 |
|
| #include "EXTERN.h" |
| 7 |
|
| #include "perl.h" |
| 8 |
|
| #include "XSUB.h" |
| 9 |
|
| |
| 10 |
|
| #include "ppport.h" |
| 11 |
|
| |
| 12 |
|
| MODULE = File::ReplaceBytes PACKAGE = File::ReplaceBytes |
| 13 |
|
| |
| 14 |
|
| ssize_t |
| 15 |
|
| pread(PerlIO *fh, SV *buf, ...) |
| 16 |
|
| PROTOTYPE: $$$;$ |
| 17 |
|
| PREINIT: |
| 18 |
8 |
| off_t offset = 0; |
| 19 |
8 |
▶ | STRLEN len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN len = 0; |
8 |
0 |
8 |
|
| 20 |
|
| |
| 21 |
|
| CODE: |
| 22 |
7 |
▶ | if( items > 2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 2 ) { |
7 |
0 |
7 |
|
| 23 |
7 |
▶ | if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
7 |
0 |
7 |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
1 |
6 |
7 |
|
| 24 |
1 |
| errno = EINVAL; |
| 25 |
1 |
| XSRETURN_IV(-1); |
| 26 |
|
| } |
| 27 |
6 |
| len = SvIV(ST(2)); |
| 28 |
|
| } |
| 29 |
|
| /* emulate pread not complaining if nothing to read */ |
| 30 |
6 |
▶ | if (len == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) |
1 |
5 |
6 |
|
| 31 |
1 |
| XSRETURN_IV(0); |
| 32 |
5 |
▶ | if( items > 3 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 3 ) { |
5 |
0 |
5 |
|
| 33 |
5 |
▶ | if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
5 |
0 |
5 |
| if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
1 |
4 |
5 |
|
| 34 |
1 |
| errno = EINVAL; |
| 35 |
1 |
| XSRETURN_IV(-1); |
| 36 |
|
| } |
| 37 |
4 |
| offset = SvIV(ST(3)); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
4 |
▶ | if(!SvOK(buf)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(buf)) |
1 |
3 |
4 |
|
| 41 |
1 |
| sv_setpvs(buf, ""); |
| 42 |
|
| |
| 43 |
4 |
▶ | RETVAL = pread(PerlIO_fileno(fh), SvGROW(buf, len), len, offset); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = pread(PerlIO_fileno(fh), SvGROW(buf, len), len, offset); |
3 |
1 |
4 |
| RETVAL = pread(PerlIO_fileno(fh), SvGROW(buf, len), len, offset); |
0 |
3 |
3 |
|
| 44 |
4 |
▶ | if (RETVAL > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL > 0) { |
4 |
0 |
4 |
|
| 45 |
4 |
| SvCUR_set(buf, RETVAL); |
| 46 |
4 |
▶ | SvTAINTED_on(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| SvTAINTED_on(buf); |
0 |
4 |
4 |
|
| 47 |
|
| } else { |
| 48 |
0 |
| SvCUR_set(buf, 0); |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| OUTPUT: |
| 52 |
|
| buf |
| 53 |
|
| RETVAL |
| 54 |
|
| |
| 55 |
|
| ssize_t |
| 56 |
|
| pwrite(PerlIO *fh, SV *buf, ...) |
| 57 |
|
| PROTOTYPE: $$;$$ |
| 58 |
|
| PREINIT: |
| 59 |
|
| char *bp; |
| 60 |
6 |
| off_t offset = 0; |
| 61 |
6 |
| STRLEN len = 0; |
| 62 |
6 |
▶ | STRLEN userlen = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN userlen = 0; |
6 |
0 |
6 |
|
| 63 |
|
| |
| 64 |
|
| CODE: |
| 65 |
|
| /* pwrite(2) does not complain if nothing to write, so emulate that */ |
| 66 |
6 |
▶ | if(!SvOK(buf) || SvCUR(buf) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(!SvOK(buf) || SvCUR(buf) == 0) |
5 |
1 |
6 |
| if(!SvOK(buf) || SvCUR(buf) == 0) |
1 |
4 |
5 |
|
| 67 |
4 |
| XSRETURN_IV(0); |
| 68 |
|
| /* length, offset are optional, but offset demands that length also be |
| 69 |
|
| * set by the caller */ |
| 70 |
4 |
▶ | if( items > 2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 2 ) { |
4 |
0 |
4 |
|
| 71 |
4 |
▶ | if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
4 |
0 |
4 |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
1 |
3 |
4 |
|
| 72 |
1 |
| errno = EINVAL; |
| 73 |
1 |
| XSRETURN_IV(-1); |
| 74 |
|
| } |
| 75 |
3 |
| userlen = SvIV(ST(2)); |
| 76 |
|
| } |
| 77 |
3 |
▶ | if( items > 3 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 3 ) { |
3 |
0 |
3 |
|
| 78 |
3 |
▶ | if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
3 |
0 |
3 |
| if (!SvIOK(ST(3)) || SvIV(ST(3)) < 0) { |
1 |
2 |
3 |
|
| 79 |
1 |
| errno = EINVAL; |
| 80 |
1 |
| XSRETURN_IV(-1); |
| 81 |
|
| } |
| 82 |
2 |
| offset = SvIV(ST(3)); |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
2 |
| bp = SvPV(buf, len); |
| 86 |
2 |
▶ | if (userlen == 0 || userlen > len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (userlen == 0 || userlen > len) |
0 |
2 |
2 |
| if (userlen == 0 || userlen > len) |
0 |
0 |
0 |
|
| 87 |
2 |
| userlen = len; |
| 88 |
2 |
| RETVAL = pwrite(PerlIO_fileno(fh), bp, userlen, offset); |
| 89 |
|
| |
| 90 |
|
| OUTPUT: |
| 91 |
|
| RETVAL |
| 92 |
|
| |
| 93 |
|
| ssize_t |
| 94 |
|
| replacebytes(SV *filename, SV *buf, ...) |
| 95 |
|
| PROTOTYPE: $$;$ |
| 96 |
|
| PREINIT: |
| 97 |
|
| char *bp; |
| 98 |
|
| int fd, i; |
| 99 |
2 |
▶ | off_t offset = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| off_t offset = 0; |
2 |
0 |
2 |
|
| 100 |
|
| STRLEN len; |
| 101 |
|
| |
| 102 |
|
| CODE: |
| 103 |
2 |
▶ | if(!SvOK(buf) || SvCUR(buf) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if(!SvOK(buf) || SvCUR(buf) == 0) |
2 |
0 |
2 |
| if(!SvOK(buf) || SvCUR(buf) == 0) |
0 |
2 |
2 |
|
| 104 |
1 |
| XSRETURN_IV(0); |
| 105 |
2 |
▶ | if( items > 2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( items > 2 ) { |
1 |
1 |
2 |
|
| 106 |
1 |
▶ | if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
1 |
0 |
1 |
| if (!SvIOK(ST(2)) || SvIV(ST(2)) < 0) { |
0 |
1 |
1 |
|
| 107 |
0 |
| errno = EINVAL; |
| 108 |
0 |
| XSRETURN_IV(-1); |
| 109 |
|
| } |
| 110 |
1 |
| offset = SvIV(ST(2)); |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| /* as otherwise a filename of "bar\0foo" results in a "bar" file which |
| 114 |
|
| * is not the same as what was input */ |
| 115 |
2 |
| bp = SvPV(filename, len); |
| 116 |
8 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
7 |
1 |
8 |
|
| 117 |
7 |
▶ | if (bp[i] == '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bp[i] == '\0') { |
1 |
6 |
7 |
|
| 118 |
1 |
| errno = EINVAL; |
| 119 |
1 |
| XSRETURN_IV(-1); |
| 120 |
|
| } |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
1 |
▶ | if((fd = open(SvPV_nolen(filename), O_CREAT|O_WRONLY, 0666)) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if((fd = open(SvPV_nolen(filename), O_CREAT|O_WRONLY, 0666)) == -1) |
0 |
1 |
1 |
|
| 124 |
0 |
| XSRETURN_IV(-1); |
| 125 |
|
| |
| 126 |
1 |
| bp = SvPV(buf, len); |
| 127 |
1 |
| RETVAL = pwrite(fd, bp, len, offset); |
| 128 |
|
| |
| 129 |
1 |
| close(fd); |
| 130 |
|
| |
| 131 |
|
| OUTPUT: |
| 132 |
|
| RETVAL |