FD.xs

filter: Statementstmt 97.6% 41 / 42 Branchbran 75.0% 18 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.3% 59 / 66 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-xs.inc ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include "const-c.inc"
9  
10 // Platform include files are injected here.
11 // See Makefile.PL for details
12 //
13 #include "platform.h"
14  
15  
16 //Read from an fd until eof or error condition
17 //Returns SV containing all the data
18 // AKA "slurp";
19 3 SV * slurp(pTHX_ int fd, int read_size){
20 SV* buffer;
21 char *buf;
22 int ret;
23 3 int len=0;
24 3 buffer=newSV(read_size);
25
26 do {
27 4 ▶ SvGROW(buffer, len+read_size); //Grow the buffer if required
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(buffer, len+read_size); //Grow the buffer if required 4 0 4
SvGROW(buffer, len+read_size); //Grow the buffer if required 1 3 4
28 4 buf=SvPVX(buffer); //Get pointer to memory..
29 4 ret=read(fd, buf+len, read_size); //Do the read,offset to current traked len
30  
31 4 ▶ if(ret>=0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret>=0){ 3 1 4
32 3 len+=ret; //track total length
33 3 buf=SvPVX(buffer);
34 3 buf[len]='\0'; //Add null for shits and giggles
35 }
36 else{
37 //break;
38 //fprintf(stderr, "ERROR IN slurp\n");
39 1 return &PL_sv_undef;
40 }
41  
42 }
43 3 ▶ while(ret>0);
Branch: 1 branch
BranchTrueFalseTotal
while(ret>0); 1 2 3
44  
45 2 SvPOK_on(buffer); //Make it a string
46 2 SvCUR_set(buffer,len); //Set the length
47 2 sv_2mortal(buffer); //Decrement ref count
48 2 return buffer;
49 }
50  
51  
52 SV *accept_multiple_next_addr;
53 struct sockaddr *accept_multiple_next_buf;
54  
55 SV *max_file_desc;
56  
57 #if defined(IO_FD_OS_DARWIN)|| defined(IO_FD_OS_BSD)
58 #define IO_FD_ATIME atime=buf.st_atimespec.tv_sec+buf.st_atimespec.tv_nsec*1e-9;
59 #define IO_FD_MTIME mtime=buf.st_mtimespec.tv_sec+buf.st_mtimespec.tv_nsec*1e-9;
60 #define IO_FD_CTIME ctime=buf.st_ctimespec.tv_sec+buf.st_ctimespec.tv_nsec*1e-9;
61 #endif
62  
63 #if defined(IO_FD_OS_LINUX)
64 #define IO_FD_ATIME atime=buf.st_atim.tv_sec+buf.st_atim.tv_nsec*1e-9;
65 #define IO_FD_MTIME mtime=buf.st_mtim.tv_sec+buf.st_mtim.tv_nsec*1e-9;
66 #define IO_FD_CTIME ctime=buf.st_ctim.tv_sec+buf.st_ctim.tv_nsec*1e-9;
67 #endif
68  
69 #if defined(IO_FD_OS_DARWIN)
70 #define KEVENT kevent64
71 #define KEVENT_S struct kevent64_s
72 #endif
73 #if defined(IO_FD_OS_BSD)
74 #define KEVENT kevent
75 #define KEVENT_S struct kevent
76 #endif
77  
78 #define ADJUST_SOCKADDR_SIZE(addr) \
79 struct sockaddr * a=(struct sockaddr *)SvPVX(addr);\
80 switch(a->sa_family){\
81 case AF_INET:\
82 SvCUR_set(addr, sizeof(struct sockaddr_in));\
83 break;\
84 case AF_INET6:\
85 SvCUR_set(addr, sizeof(struct sockaddr_in6));\
86 break;\
87 case AF_UNIX:\
88 SvCUR_set(addr, sizeof(struct sockaddr_un));\
89 break;\
90 default:\
91 break;\
92 }\
93  
94  
95  
96  
97 MODULE = IO::FD PACKAGE = IO::FD
98  
99 INCLUDE: const-xs.inc
100  
101 BOOT:
102  
103 //boot strap the mutliple accept buffer
104 21 accept_multiple_next_addr=newSV(sizeof(struct sockaddr_storage));
105 21 accept_multiple_next_buf=(struct sockaddr *)SvPVX(accept_multiple_next_addr);
106  
107 //locate the $^F variable
108 //max_file_desc=get_sv("^F",0);
109  
110 INCLUDE_COMMAND: cat "xs-include/socket.c"
111 INCLUDE_COMMAND: $^X "xs-include/sendfile.pl"
112 INCLUDE_COMMAND: cat "xs-include/mkfifo.c"
113 INCLUDE_COMMAND: $^X "xs-include/mkfifoat.pl"
114 INCLUDE_COMMAND: cat "xs-include/file.c"
115 INCLUDE_COMMAND: cat "xs-include/experimental.c"
116 INCLUDE_COMMAND: cat "xs-include/temp.c"
117 INCLUDE_COMMAND: cat "xs-include/send-recv.c"
118  
119  
120  
121 void
122 readline(fd)
123 SV *fd
124
125 INIT:
126 SV *irs;
127 int ret;
128 int count;
129 SV* buffer;
130 char *buf;
131 9 int do_loop=1;
132  
133 int tmp;
134 PPCODE:
135 9 ▶ if(SvOK(fd)&& SvIOK(fd)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(fd)&& SvIOK(fd)){ 9 0 9
if(SvOK(fd)&& SvIOK(fd)){ 9 0 9
136 9 irs=get_sv("/",0);
137 9 ▶ if(irs){
Branch: 1 branch
BranchTrueFalseTotal
if(irs){ 9 0 9
138 9 ▶ #Found variable. Read records
Branch: 1 branch
BranchTrueFalseTotal
#Found variable. Read records 6 3 9
139 6 ▶ if(SvOK(irs)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(irs)){ 5 1 6
140 if(SvROK(irs)){
141 //fprintf(stderr, "DOING RECORD READ\n");
142 //SLURP RECORDS
143 5  
144 5 SV* v=SvRV(irs); //Dereference to get SV
145 5 tmp=SvIV(v); //The integer value of the sv
146 5 buffer=newSV(tmp); //Allocate buffer at record size
147 5 buf=SvPVX(buffer); //Get the pointer we need
148 ret=read(SvIV(fd), buf, tmp); //Do the read into buffer
149 5 //fprintf(stderr, "read return: %d\n", ret);
150 5 ▶ SvPOK_on(buffer); //Make a string
Branch: 1 branch
BranchTrueFalseTotal
SvPOK_on(buffer); //Make a string 4 1 5
151 4 if(ret>=0){
152 4 buf[ret]='\0'; //Set null just in case
153 4 ▶ SvCUR_set(buffer,ret); //Set the length of the string
Branch: 1 branch
BranchTrueFalseTotal
SvCUR_set(buffer,ret); //Set the length of the string 0 4 4
154 4 EXTEND(SP,1); //Extend stack
155 4 mPUSHs(buffer); //Push record
156 XSRETURN(1);
157 }
158 1 else {
159 XSRETURN_UNDEF;
160 }
161  
162 }
163 1 else {
164 Perl_croak( aTHX_ "IO::FD::readline does not split lines");
165 }
166 }
167 else{
168  
169 //fprintf(stderr, "DOING SLURP READ\n");
170 3 ▶ //SLURP entire file
Branch: 1 branch
BranchTrueFalseTotal
//SLURP entire file 0 3 3
171 3 EXTEND(SP,1);
172 3 PUSHs(slurp(aTHX_ SvIV(fd), 4096));
173 XSRETURN(1);
174 }
175 }
176 else {
177 //not found.. this isn't good
178  
179 }
180 }
181 0 else{
182 XSRETURN_UNDEF;
183 }
184  
185  
186 #Naming
187  
188 #TODO:
189 # TODO ioctl
190 # poll
191 # select ... perl compatiable version
192 # dir ... not normally on FDs?
193 # readline?
194  
195 # Add IPC::Open2 and IPC::Open3 emulations
196  
« const-xs.inc ↑ Summary