FD.xs

filter: Statementstmt 59.0% 254 / 430 Branchbran 36.5% 95 / 260 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 50.5% 349 / 690 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 #include <fcntl.h>
10 #include <sys/socket.h>
11 #include <sys/un.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <poll.h>
15  
16 #include <sys/time.h>
17  
18 #if defined(IO_FD_OS_DARWIN) || defined(IO_FD_OS_BSD)
19 #include <sys/event.h>
20 #endif
21  
22 #include <sys/stat.h>
23  
24 //Read from an fd until eof or error condition
25 //Returns SV containing all the data
26 // AKA "slurp";
27 3 SV * slurp(pTHX_ int fd, int read_size){
28 SV* buffer;
29 char *buf;
30 int ret;
31 3 int len=0;
32 3 buffer=newSV(read_size);
33
34 do {
35 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
36 4 buf=SvPVX(buffer); //Get pointer to memory..
37 4 ret=read(fd, buf+len, read_size); //Do the read,offset to current traked len
38  
39 4 ▶ if(ret>=0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret>=0){ 3 1 4
40 3 len+=ret; //track total length
41 3 buf=SvPVX(buffer);
42 3 buf[len]='\0'; //Add null for shits and giggles
43 }
44 else{
45 //break;
46 //fprintf(stderr, "ERROR IN slurp\n");
47 1 return &PL_sv_undef;
48 }
49  
50 }
51 3 ▶ while(ret>0);
Branch: 1 branch
BranchTrueFalseTotal
while(ret>0); 1 2 3
52  
53 2 SvPOK_on(buffer); //Make it a string
54 2 SvCUR_set(buffer,len); //Set the length
55 2 sv_2mortal(buffer); //Decrement ref count
56 2 return buffer;
57 }
58  
59  
60  
61 #if defined(IO_FD_OS_DARWIN)|| defined(IO_FD_OS_BSD)
62 #define IO_FD_ATIME atime=buf.st_atimespec.tv_sec+buf.st_atimespec.tv_nsec*1e-9;
63 #define IO_FD_MTIME mtime=buf.st_mtimespec.tv_sec+buf.st_mtimespec.tv_nsec*1e-9;
64 #define IO_FD_CTIME ctime=buf.st_ctimespec.tv_sec+buf.st_ctimespec.tv_nsec*1e-9;
65 #endif
66  
67 #if defined(IO_FD_OS_LINUX)
68 #define IO_FD_ATIME atime=buf.st_atim.tv_sec+buf.st_atim.tv_nsec*1e-9;
69 #define IO_FD_MTIME mtime=buf.st_mtim.tv_sec+buf.st_mtim.tv_nsec*1e-9;
70 #define IO_FD_CTIME ctime=buf.st_ctim.tv_sec+buf.st_ctim.tv_nsec*1e-9;
71 #endif
72  
73 #if defined(IO_FD_OS_DARWIN)
74 #define KEVENT kevent64
75 #define KEVENT_S struct kevent64_s
76 #endif
77 #if defined(IO_FD_OS_BSD)
78 #define KEVENT kevent
79 #define KEVENT_S struct kevent
80 #endif
81  
82  
83  
84 MODULE = IO::FD PACKAGE = IO::FD
85  
86 INCLUDE: const-xs.inc
87  
88 #SOCKET
89 #######
90  
91 #TODO:
92 #Allow a string af, which is actually sockaddr structure
93 #Extract out the family, Saves a step
94 SV*
95 socket(sock,af,type,proto)
96 SV* sock;
97 int af
98 int type
99 int proto
100  
101 PREINIT:
102 int fd;
103 int s;
104 CODE:
105 3 s=socket(af, type, proto);
106  
107 //Set error variable...
108 3 ▶ if(s<0){
Branch: 1 branch
BranchTrueFalseTotal
if(s<0){ 0 3 3
109
110 0 RETVAL=&PL_sv_undef;
111 #need to set error code here
112 }
113 3 else{
114 3 ▶ RETVAL=newSViv(s);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL=newSViv(s); 0 3 3
115 0 if(SvOK(sock)){
116 sv_setiv(sock,s);
117 }
118 3 else {
119 sock=newSViv(s);
120 }
121 }
122  
123 OUTPUT:
124 RETVAL
125 sock
126  
127 #LISTEN
128 #######
129  
130  
131 SV *
132 listen(listener,backlog)
133 int listener;
134 int backlog;
135  
136  
137 INIT:
138 int ret;
139  
140 CODE:
141  
142 1 ret=listen(listener, backlog);
143  
144 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 1 1
145 0 RETVAL=&PL_sv_undef;
146 }
147 else{
148 1 RETVAL=newSViv(ret+1);
149 }
150  
151 OUTPUT:
152 RETVAL
153  
154 #ACCEPT
155 #######
156  
157 SV*
158 accept(new_fd, listen_fd)
159 SV* new_fd
160 int listen_fd
161  
162 PREINIT:
163 struct sockaddr *packed_addr;
164 int ret;
165 1 SV *addr=newSV(sizeof(struct sockaddr));
166 1 struct sockaddr *buf=(struct sockaddr *)SvPVX(addr);
167 1 unsigned int len=sizeof(struct sockaddr);
168  
169
170  
171 CODE:
172 1 ret=accept(listen_fd, buf, &len);
173 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 1 1
174 0 RETVAL=&PL_sv_undef;
175 }
176 else {
177 1 SvPOK_on(addr);
178 1 SvCUR_set(addr,sizeof(struct sockaddr));
179 1 RETVAL=addr;
180 1 ▶ if(SvOK(new_fd)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(new_fd)){ 0 1 1
181 0 sv_setiv(new_fd, ret);
182 }
183 else{
184 1 new_fd=newSViv(ret);
185 }
186 }
187
188
189 OUTPUT:
190 RETVAL
191 new_fd
192 #CONNECT
193 ########
194  
195 SV*
196 connect(fd, address)
197 SV* fd
198 SV *address
199  
200 PREINIT:
201 int ret;
202 1 ▶ int len=SvOK(address)?SvCUR(address):0;
Branch: 1 branch
BranchTrueFalseTotal
int len=SvOK(address)?SvCUR(address):0; 1 0 1
203 1 struct sockaddr *addr=(struct sockaddr *)SvPVX(address);
204  
205 CODE:
206  
207 1 ret=connect(SvIVX(fd),addr,len);
208 //fprintf(stderr,"CONNECT: %d\n",ret);
209 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 1 0 1
210 1 RETVAL=&PL_sv_undef;
211 }
212 else{
213 0 RETVAL=newSViv(ret+1);
214 }
215 OUTPUT:
216 RETVAL
217
218 #SOCKATMARK
219 ##########
220 SV*
221 sockatmark(fd)
222 SV *fd;
223 INIT:
224 int ret;
225 PPCODE:
226 0 ▶ if(SvOK(fd)&&SvIOK(fd)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(fd)&&SvIOK(fd)){ 0 0 0
if(SvOK(fd)&&SvIOK(fd)){ 0 0 0
227 0 ret=sockatmark(SvIV(fd));
228 0 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 0 0
229 0 XSRETURN_UNDEF;
230 }
231 else{
232 0 XSRETURN_IV(ret);
233 }
234  
235 }
236 else{
237 0 XSRETURN_UNDEF;
238 }
239
240 #SYSOPEN
241 ########
242  
243 SV*
244 sysopen(fd, path, mode, ... )
245 SV* fd
246 char *path
247 int mode
248  
249 PREINIT:
250 int f;
251 2 int permissions=0666; //Default if not provided
252  
253 CODE:
254 2 ▶ if(items==4){
Branch: 1 branch
BranchTrueFalseTotal
if(items==4){ 1 1 2
255 1 permissions=SvIV(ST(3));
256 }
257 2 f=open(path, mode, permissions);
258 2 ▶ if(f<0){
Branch: 1 branch
BranchTrueFalseTotal
if(f<0){ 0 2 2
259 0 RETVAL=&PL_sv_undef;
260 }
261 else{
262 2 RETVAL=newSViv(f);
263 2 ▶ if(SvOK(fd)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(fd)){ 0 2 2
264 0 sv_setiv(fd,f);
265 }
266 else {
267 2 fd= newSViv(f);
268 }
269 }
270  
271 OUTPUT:
272 RETVAL
273 fd
274  
275  
276 SV*
277 sysopen4(fd, path, mode, permissions)
278 int fd
279 char *path
280 int mode
281 int permissions
282  
283 PREINIT:
284 int f;
285  
286 CODE:
287 0 fd=open(path, mode, permissions);
288 0 ▶ if(fd<0){
Branch: 1 branch
BranchTrueFalseTotal
if(fd<0){ 0 0 0
289 0 RETVAL=&PL_sv_undef;
290 }
291 else{
292 0 RETVAL=newSViv(fd);
293 }
294  
295 OUTPUT:
296 RETVAL
297 fd
298 #CLOSE
299 ######
300  
301 SV*
302 close(fd)
303 int fd;
304  
305 INIT:
306 int ret;
307  
308 CODE:
309 16 ret=close(fd);
310 16 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 1 15 16
311 1 RETVAL=&PL_sv_undef;
312 }
313 else{
314 15 #close returns 0 on success.. which is false in perl
315 #so increment
316 RETVAL=newSViv(ret+1);
317 }
318 OUTPUT:
319 RETVAL
320  
321  
322  
323 #SYSREAD
324 ########
325  
326 SV*
327 sysread(fd, data, len, ...)
328 int fd;
329 SV* data
330 int len
331 INIT:
332 int ret;
333 char *buf;
334 14 int offset=0;
335  
336 CODE:
337 //TODO: allow unspecified len and offset
338  
339 //grow scalar to fit potental read
340  
341 14 ▶ if(items >=4 ){
Branch: 1 branch
BranchTrueFalseTotal
if(items >=4 ){ 0 14 14
342 //len=SvIOK(ST(2))?SvIV(ST(2)):0;
343 0 ▶ offset=SvIOK(ST(3))?SvIV(ST(3)):0;
Branch: 1 branch
BranchTrueFalseTotal
offset=SvIOK(ST(3))?SvIV(ST(3)):0; 0 0 0
344 }
345
346 14 int data_len=sv_len(data);
347 int request_len;
348 14 ▶ if(offset<0){
Branch: 1 branch
BranchTrueFalseTotal
if(offset<0){ 0 14 14
349 0 offset=data_len-offset;
350 }
351 else{
352  
353 }
354 14 request_len=len+offset;
355  
356 #fprintf(stderr, "Length of buffer is: %d\n", data_len);
357 14 ▶ #fprintf(stderr, "Length of request is: %d\n", request_len);
Branch: 3 branches
BranchTrueFalseTotal
#fprintf(stderr, "Length of request is: %d\n", request_len); 14 0 14
#fprintf(stderr, "Length of request is: %d\n", request_len); 10 4 14
#fprintf(stderr, "Length of request is: %d\n", request_len); 0 10 10
358  
359 14 buf = SvPOK(data) ? SvGROW(data, request_len+1) : 0;
360  
361 14 data_len=sv_len(data);
362 #fprintf(stderr, "Length of buffer is: %d\n", data_len);
363 14 #TODO: fill with nulls if offset past end of original data
364 14 ▶
Branch: 1 branch
BranchTrueFalseTotal
0 14 14
365 buf+=offset;
366 0  
367 ret=read(fd, buf, len);
368 if(ret<0){
369 14  
370 14 RETVAL=&PL_sv_undef;
371 14 }
372 else {
373 buf[ret]='\0';
374 SvCUR_set(data,ret+offset);
375 RETVAL=newSViv(ret);
376 }
377  
378 OUTPUT:
379 RETVAL
380  
381 SV*
382 sysread3(fd, data, len)
383 int fd;
384 SV* data
385 int len
386  
387 INIT:
388 int ret;
389 char *buf;
390 int offset;
391  
392 CODE:
393 0 int data_len=SvCUR(data);
394  
395 #fprintf(stderr, "Length of buffer is: %d\n", data_len);
396 0 ▶ #fprintf(stderr, "Length of request is: %d\n",len);
Branch: 3 branches
BranchTrueFalseTotal
#fprintf(stderr, "Length of request is: %d\n",len); 0 0 0
#fprintf(stderr, "Length of request is: %d\n",len); 0 0 0
#fprintf(stderr, "Length of request is: %d\n",len); 0 0 0
397  
398 buf = SvPOK(data) ? SvGROW(data,len+1) : 0;
399  
400 //data_len=SvPVX(data);
401 0 #fprintf(stderr, "Length of buffer is: %d\n", data_len);
402 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
403  
404 0 ret=read(fd, buf, len);
405 if(ret<0){
406  
407 0 RETVAL=&PL_sv_undef;
408 0 }
409 0 else {
410 buf[ret]='\0';
411 SvCUR_set(data,ret);
412 RETVAL=newSViv(ret);
413 }
414  
415 OUTPUT:
416 RETVAL
417  
418 SV*
419 sysread4(fd, data, len, offset)
420 int fd;
421 SV* data
422 int len
423 int offset
424  
425 INIT:
426 int ret;
427 char *buf;
428  
429 CODE:
430 #TODO: allow unspecified len and offset
431 0  
432 #grow scalar to fit potental read
433 0 ▶ int data_len=sv_len(data);
Branch: 1 branch
BranchTrueFalseTotal
int data_len=sv_len(data); 0 0 0
434 0 int request_len;
435 if(offset<0){
436 offset=data_len-offset;
437 }
438 else{
439 0  
440 }
441 request_len=len+offset;
442 0 ▶ 
Branch: 3 branches
BranchTrueFalseTotal
0 0 0
0 0 0
0 0 0
443 #fprintf(stderr, "Length of buffer is: %d\n", data_len);
444 0 #fprintf(stderr, "Length of request is: %d\n", request_len);
445  
446 0 buf = SvPOK(data) ? SvGROW(data, request_len+1) : 0;
447  
448 0 data_len=sv_len(data);
449 0 ▶ #fprintf(stderr, "Length of buffer is: %d\n", data_len);
Branch: 1 branch
BranchTrueFalseTotal
#fprintf(stderr, "Length of buffer is: %d\n", data_len); 0 0 0
450 #TODO: fill with nulls if offset past end of original data
451 0
452 buf+=offset;
453  
454 0 ret=read(fd, buf, len);
455 0 if(ret<0){
456 0  
457 RETVAL=&PL_sv_undef;
458 }
459 else {
460 buf[ret]='\0';
461 SvCUR_set(data,ret+offset);
462 RETVAL=newSViv(ret);
463 }
464  
465 OUTPUT:
466 RETVAL
467  
468 #SYSWRITE
469 ##########
470  
471 SV*
472 syswrite(fd,data,...)
473 int fd
474 SV* data
475  
476 INIT:
477 int ret;
478 char *buf;
479 17 STRLEN max=SvCUR(data);
480 int len;
481 int offset;
482 CODE:
483 17 ▶ if(items >=4 ){
Branch: 1 branch
BranchTrueFalseTotal
if(items >=4 ){ 0 17 17
484 //length and Offset provided
485 0 ▶ len=SvIOK(ST(2))?SvIV(ST(2)):0;
Branch: 1 branch
BranchTrueFalseTotal
len=SvIOK(ST(2))?SvIV(ST(2)):0; 0 0 0
486 0 ▶ offset=SvIOK(ST(3))?SvIV(ST(3)):0;
Branch: 1 branch
BranchTrueFalseTotal
offset=SvIOK(ST(3))?SvIV(ST(3)):0; 0 0 0
487
488 }
489 17 ▶ else if(items == 3){
Branch: 1 branch
BranchTrueFalseTotal
else if(items == 3){ 0 17 17
490 //length provided
491 0 ▶ len=SvIOK(ST(2))?SvIV(ST(2)):0;
Branch: 1 branch
BranchTrueFalseTotal
len=SvIOK(ST(2))?SvIV(ST(2)):0; 0 0 0
492 0 offset=0;
493 }
494 else{
495 //no length or offset
496 17 len=SvCUR(data);
497 17 offset=0;
498 }
499  
500 #TODO: fix negative offset processing
501 17 #TODO: allow unspecified len and offset
502  
503 #fprintf(stderr,"Input size: %zu\n",SvCUR(data));
504 17 offset=
505 offset>max
506 17 ▶ ?max
Branch: 1 branch
BranchTrueFalseTotal
?max 0 17 17
507 0 :offset;
508  
509 if((offset+len)>max){
510 17 len=max-offset;
511 17 }
512 17
513 17 ▶ buf=SvPVX(data);
Branch: 1 branch
BranchTrueFalseTotal
buf=SvPVX(data); 0 17 17
514 0 buf+=offset;
515 ret=write(fd, buf, len);
516 #fprintf(stderr, "write consumed %d bytes\n", ret);
517 17 if(ret<0){
518 RETVAL=&PL_sv_undef;
519 }
520 else{
521 RETVAL=newSViv(ret);
522 }
523  
524 OUTPUT:
525 RETVAL
526  
527 SV*
528 syswrite2(fd,data)
529 int fd
530 SV* data
531  
532 INIT:
533 int ret;
534 char *buf;
535 int len;
536 CODE:
537  
538 0 ▶ len=SvPOK(data)?SvCUR(data):0;
Branch: 1 branch
BranchTrueFalseTotal
len=SvPOK(data)?SvCUR(data):0; 0 0 0
539 #TODO: fix negative offset processing
540 #TODO: allow unspecified len and offset
541  
542 0 #fprintf(stderr,"Input size: %zu\n",SvCUR(data));
543 0  
544 0 ▶
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
545 0 buf=SvPVX(data);
546 ret=write(fd, buf, len);
547 if(ret<0){
548 0 RETVAL=&PL_sv_undef;
549 }
550 else{
551 RETVAL=newSViv(ret);
552 }
553  
554 OUTPUT:
555 RETVAL
556  
557 SV*
558 syswrite3(fd,data,len)
559 int fd
560 SV* data
561 int len
562  
563 INIT:
564 int ret;
565 char *buf;
566 0 STRLEN max=SvCUR(data);
567 0 int offset=0;
568 CODE:
569  
570 #TODO: fix negative offset processing
571 #TODO: allow unspecified len and offset
572 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
573 0 #fprintf(stderr,"Input size: %zu\n",SvCUR(data));
574  
575 if(len>max){
576 0 len=max;
577 0 }
578 0 ▶
Branch: 1 branch
BranchTrueFalseTotal
0 0 0
579 0 buf=SvPVX(data);
580 ret=write(fd,buf,len);
581 #fprintf(stderr, "write consumed %d bytes\n", ret);
582 0 if(ret<0){
583 RETVAL=&PL_sv_undef;
584 }
585 else{
586 RETVAL=newSViv(ret);
587 }
588  
589 OUTPUT:
590 RETVAL
591  
592  
593 SV*
594 syswrite4(fd,data,len,offset)
595 int fd
596 SV* data
597 int len
598 int offset
599  
600 INIT:
601 int ret;
602 char *buf;
603 0 STRLEN max=SvCUR(data);
604 CODE:
605  
606 #TODO: fix negative offset processing
607 0 #TODO: allow unspecified len and offset
608  
609 #fprintf(stderr,"Input size: %zu\n",SvCUR(data));
610 0 offset=
611 offset>max
612 0 ▶ ?max
Branch: 1 branch
BranchTrueFalseTotal
?max 0 0 0
613 0 :offset;
614  
615 if((offset+len)>max){
616 0 len=max-offset;
617 0 }
618 0
619 0 ▶ buf=SvPVX(data);
Branch: 1 branch
BranchTrueFalseTotal
buf=SvPVX(data); 0 0 0
620 0 buf+=offset;
621 ret=write(fd,buf,len);
622 #fprintf(stderr, "write consumed %d bytes\n", ret);
623 0 if(ret<0){
624 RETVAL=&PL_sv_undef;
625 }
626 else{
627 RETVAL=newSViv(ret);
628 }
629  
630 OUTPUT:
631 RETVAL
632  
633  
634 #PIPE
635 ######
636  
637 SV*
638 pipe(read_end,write_end)
639 SV* read_end
640 SV* write_end
641  
642 ALIAS: syspipe=1
643  
644  
645 INIT:
646 int ret;
647 int fds[2];
648  
649 CODE:
650 3 ret=pipe(fds);
651  
652 3 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 3 3
653 0 RETVAL=&PL_sv_undef;
654 }
655 else{
656 3 #pipe returns 0 on success...
657 3 ▶ RETVAL=newSViv(ret+1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL=newSViv(ret+1); 0 3 3
658 0 if(SvOK(read_end)){
659 sv_setiv(read_end, fds[0]);
660 }
661 3 else {
662 read_end=newSViv(fds[0]);
663 }
664 3 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
0 3 3
665 0 if(SvOK(write_end)){
666 sv_setiv(write_end,fds[1]);
667 }
668 3 else {
669 write_end=newSViv(fds[1]);
670 }
671 }
672 OUTPUT:
673 RETVAL
674 read_end
675 write_end
676  
677 #BIND
678 #####
679  
680 SV*
681 bind(fd, address)
682 int fd
683 SV*address
684
685 ALIAS: sysbind=1
686  
687 INIT:
688 int ret;
689 2 ▶ int len=SvOK(address)?SvCUR(address):0;
Branch: 1 branch
BranchTrueFalseTotal
int len=SvOK(address)?SvCUR(address):0; 2 0 2
690 2 struct sockaddr *addr=(struct sockaddr *)SvPVX(address);
691 CODE:
692 //fprintf(stderr, "bind fd: %d\n",fd);
693 //fprintf(stderr, "bind len: %d\n",len);
694 2 ret=bind(fd, addr, len);
695 2 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 2 2
696 0 RETVAL=&PL_sv_undef;
697 }
698 else{
699 2 RETVAL=newSViv(ret+1);
700 }
701
702 OUTPUT:
703 RETVAL
704 #SOCKETPAIR
705 ###########
706 # TODO:
707 # How to through an exception like perl when syscall not implemented?
708  
709 SV*
710 socketpair(fd1,fd2, domain, type, protocol)
711 int fd1
712 int fd2
713 int domain
714 int type
715 int protocol
716  
717 INIT:
718  
719 int ret;
720 int fds[2];
721  
722 CODE:
723 3 #TODO need to emulate via tcp to localhost for non unix
724 3 ▶ ret=socketpair(domain, type, protocol, fds);
Branch: 1 branch
BranchTrueFalseTotal
ret=socketpair(domain, type, protocol, fds); 0 3 3
725 0 if(ret<0){
726 RETVAL=&PL_sv_undef;
727 }
728 3 else{
729 3 RETVAL=newSViv(ret+1);
730 3 fd1=fds[0];
731 fd2=fds[1];
732 }
733 OUTPUT:
734 RETVAL
735 fd1
736 fd2
737  
738 #SYSSEEK
739 ########
740  
741 SV*
742 sysseek(fd,offset,whence)
743 int fd;
744 int offset;
745 int whence;
746  
747 INIT:
748 int ret;
749  
750 CODE:
751  
752 3 ret=lseek(fd, offset,whence);
753 3 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 3 3
754 0 RETVAL=&PL_sv_undef;
755 }
756 else{
757 3 RETVAL=newSViv(ret);
758 }
759  
760 OUTPUT:
761 RETVAL
762  
763 #DUP
764 ####
765  
766 SV*
767 dup(fd)
768 int fd;
769  
770 INIT:
771 int ret;
772  
773 CODE:
774 0 ret=dup(fd);
775 0 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 0 0
776 0 RETVAL=&PL_sv_undef;
777 }
778 else{
779 0 RETVAL=newSViv(ret);
780 }
781  
782 OUTPUT:
783 RETVAL
784  
785  
786 #DUP2
787 #####
788  
789 SV*
790 dup2(fd1,fd2)
791 int fd1
792 int fd2
793  
794 INIT:
795 int ret;
796  
797 CODE:
798 0 ret=dup2(fd1,fd2);
799 0 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 0 0
800 0 RETVAL=&PL_sv_undef;
801 }
802 else{
803 0 RETVAL=newSViv(ret);
804 }
805  
806 OUTPUT:
807  
808 RETVAL
809 #FCNTL
810 ######
811  
812 SV*
813 fcntl(fd, cmd, arg)
814 int fd
815 int cmd
816 SV* arg
817  
818 #TODO: everything
819  
820 ALIAS: sysfctrl=1
821 INIT:
822 int ret;
823 CODE:
824 12 ▶ #if arg is numeric, call with iv
Branch: 1 branch
BranchTrueFalseTotal
#if arg is numeric, call with iv 12 0 12
825 12 ▶ #otherwise we pass pointers and hope for the best
Branch: 1 branch
BranchTrueFalseTotal
#otherwise we pass pointers and hope for the best 12 0 12
826 12 if(SvOK(arg)){
827 0 ▶ if(SvIOK(arg)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK(arg)){ 0 0 0
828 0 #fprintf(stderr, "PROCESSING ARG AS NUMBER\n");
829 ret=fcntl(fd,cmd, SvIV(arg));
830 }else if(SvPOK(arg)){
831 0 #fprintf(stderr, "PROCESSING ARG AS STRING\n");
832 ret=fcntl(fd,cmd,SvPVX(arg));
833 12 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 12 12
834 0 else {
835 #error
836 #fprintf(stderr, "PROCESSING ARG AS UNKOWN\n");
837 12 ret=-1;
838 }
839 if(ret==-1){
840 RETVAL=&PL_sv_undef;
841 }
842 else {
843 RETVAL=newSViv(ret);
844 }
845 }
846  
847 OUTPUT:
848 RETVAL
849  
850  
851 #IOCTL
852 ######
853  
854 SV*
855 ioctl(fd, request, arg)
856 int fd
857 int request
858 int arg
859  
860 ALIAS: sysioctl=1
861 INIT:
862  
863 CODE:
864 0 RETVAL=&PL_sv_undef;
865  
866 OUTPUT:
867 RETVAL
868  
869 #GETSOCKOPT
870 ############
871 SV*
872 getsockopt(fd, level, option)
873 int fd
874 int level
875 int option
876  
877 INIT:
878 int ret;
879 char * buf;
880 unsigned int len;
881 SV *buffer;
882  
883 CODE:
884 3 buffer=newSV(257);
885 3 SvPOK_on(buffer);
886 3 buf=SvPVX(buffer);
887 3 len=256;
888  
889 3 ret=getsockopt(fd,level, option, buf, &len);
890  
891 3 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 3 3
892 0 RETVAL=&PL_sv_undef;
893 }
894 else {
895 3 SvCUR_set(buffer, len);
896 3 *SvEND(buffer)='\0';
897 3 RETVAL=buffer;
898 }
899  
900  
901 OUTPUT:
902 RETVAL
903  
904  
905 #SETSOCKOPT
906 ###########
907 SV*
908 setsockopt(fd, level, option, buffer)
909 int fd
910 int level
911 int option
912 SV* buffer;
913  
914 INIT:
915 int ret;
916 char *buf;
917 unsigned int len;
918  
919 SV *_buffer;
920  
921  
922 CODE:
923 2 ▶ if(SvOK(buffer)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(buffer)){ 2 0 2
924 2 ▶ if(SvIOK(buffer)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvIOK(buffer)){ 2 0 2
925 #fprintf(stderr, "SET SOCKOPT as integer\n");
926 2 //Do what perl does and convert integers
927 2 _buffer=newSV(sizeof(int));
928 len=sizeof(int);
929 2
930 2 SvPOK_on(_buffer);
931 SvCUR_set(_buffer, len);
932 2 //*SvEND(_buffer)='\0';
933 2 buf=SvPVX(_buffer);
934 *((int *)buf)=SvIVX(buffer);
935  
936 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
937 0 else if(SvPOK(buffer)){
938 #fprintf(stderr, "SET SOCKOPT as NON integer\n");
939 _buffer=buffer;
940  
941 2 }
942 2  
943 2 len=SvCUR(_buffer);
944 2 ▶ buf=SvPVX(_buffer);
Branch: 1 branch
BranchTrueFalseTotal
buf=SvPVX(_buffer); 0 2 2
945 ret=setsockopt(fd,level,option,buf, len);
946 0 if(ret<0){
947 //fprintf(stderr, "call failed\n");
948 RETVAL=&PL_sv_undef;
949 }
950 2 else{
951 //fprintf(stderr, "call succeeded\n");
952 RETVAL=newSViv(ret+1);
953 }
954
955 }
956 0 else{
957 //fprintf(stderr, "no buffer");
958 RETVAL=&PL_sv_undef;
959 }
960  
961  
962
963  
964 OUTPUT:
965 RETVAL
966  
967 ##########################################################
968 # #FILENO #
969 # ####### #
970 # #
971 # SV * #
972 # fileno (fd_fh) #
973 # SV *fd_fh; #
974 # #
975 # INIT: #
976 # int ret; #
977 # #
978 # CODE: #
979 # if(!SvOK(fd_fh)){ #
980 # RETVAL=&PL_sv_undef; #
981 # } #
982 # else{ #
983 # if(SvIOK(fd_fh)){ #
984 # //treat as integer fd #
985 # RETVAL=newSVsv(fd_fh); #
986 # } #
987 # else{ #
988 # //assume glob #
989 # REVAL=GvIO #
990 # } #
991 # } #
992 # #
993 # #
994 # OUTPUT: #
995 # RETVAL #
996 # #
997 ##########################################################
998  
999  
1000 #SELECT
1001 #######
1002 SV*
1003 select(readvec, writevec, errorvec, tout)
1004 SV* readvec
1005 SV* writevec
1006 SV* errorvec
1007 #Perl timeout is in fractional seconds
1008 SV* tout
1009  
1010  
1011 INIT:
1012 fd_set *r;
1013 fd_set *w;
1014 fd_set *e;
1015 struct timeval timeout;
1016 0 int size=sizeof(fd_set)+1;
1017 double tval;
1018 int ret;
1019 0 int nfds=0;
1020 0 int bit_string_size=129;
1021 int tmp;
1022  
1023 CODE:
1024 //Ensure the vector can fit a fd_set
1025 //TODO: Need to make sure its null filled too
1026 //
1027 0 ▶ if(SvOK(readvec)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(readvec)){ 0 0 0
1028 0 ▶ r=(fd_set *)SvGROW(readvec,bit_string_size);
Branch: 2 branches
BranchTrueFalseTotal
r=(fd_set *)SvGROW(readvec,bit_string_size); 0 0 0
r=(fd_set *)SvGROW(readvec,bit_string_size); 0 0 0
1029 0 tmp=SvCUR(readvec);
1030 0 Zero(((char *)r)+tmp,bit_string_size-tmp,1);
1031 0 nfds=tmp;
1032 }
1033 else {
1034 0 r=NULL;
1035 }
1036  
1037 0 ▶ if(SvOK(writevec)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(writevec)){ 0 0 0
1038 0 ▶ w=(fd_set *)SvGROW(writevec,bit_string_size);
Branch: 2 branches
BranchTrueFalseTotal
w=(fd_set *)SvGROW(writevec,bit_string_size); 0 0 0
w=(fd_set *)SvGROW(writevec,bit_string_size); 0 0 0
1039 0 tmp=SvCUR(writevec);
1040 0 Zero(((char *)w)+tmp,bit_string_size-tmp,1);
1041 0 nfds=tmp>nfds?tmp:nfds;
1042 }
1043 else {
1044 0 w=NULL;
1045 }
1046  
1047 0 ▶ if(SvOK(errorvec)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(errorvec)){ 0 0 0
1048 0 ▶ e=(fd_set *)SvGROW(errorvec,bit_string_size);
Branch: 2 branches
BranchTrueFalseTotal
e=(fd_set *)SvGROW(errorvec,bit_string_size); 0 0 0
e=(fd_set *)SvGROW(errorvec,bit_string_size); 0 0 0
1049 0 tmp=SvCUR(errorvec);
1050 0 Zero(((char *)e)+tmp, bit_string_size-tmp,1);
1051 0 nfds=tmp>nfds?tmp:nfds;
1052 }
1053 else {
1054 0 e=NULL;
1055 }
1056  
1057 0 nfds*=8; //convert string (byte) length to bit length
1058 //This length has an implicit +1 for select call
1059  
1060 //Clamp the nfds to 1024
1061 0 ▶ if(nfds>1024){
Branch: 1 branch
BranchTrueFalseTotal
if(nfds>1024){ 0 0 0
1062 //TODO: Need to show an error here?
1063 0 nfds=1024;
1064 }
1065 //TODO: handle EAGAIN and EINT
1066 //nfds++;
1067 //fprintf(stderr, "Number of fds for select: %d\n", nfds);
1068 0 ▶ if(SvOK(tout) && (SvNOK(tout) || SvIOK(tout))){
Branch: 3 branches
BranchTrueFalseTotal
if(SvOK(tout) && (SvNOK(tout) || SvIOK(tout))){ 0 0 0
if(SvOK(tout) && (SvNOK(tout) || SvIOK(tout))){ 0 0 0
if(SvOK(tout) && (SvNOK(tout) || SvIOK(tout))){ 0 0 0
1069 //Timeout value provided in fractional seconds
1070 0 tval=SvNV(tout);
1071 0 timeout.tv_sec=(unsigned int ) tval;
1072 0 tval-=timeout.tv_sec;
1073 0 tval*=1000000;
1074 0 timeout.tv_usec=(unsigned int) tval;
1075  
1076 //fprintf(stderr, "select with %fs timeout....\n", tval);
1077 0 ret=select(nfds,r,w,e,&timeout);
1078 //fprintf(stderr, "Returned number of fds %d\n", ret);
1079 }
1080  
1081 else{
1082 //Timeout is not a number
1083 //printf(stderr, "select with null timeout....\n");
1084 0 ret=select(nfds,r,w,e, NULL);
1085 }
1086 0 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 0 0
1087 //Undef on error
1088 0 RETVAL=&PL_sv_undef;
1089 }
1090 else{
1091 //0 on timeout expired
1092 //>0 number of found fds to test
1093 0 RETVAL=newSViv(ret);
1094 }
1095  
1096  
1097 OUTPUT:
1098  
1099 RETVAL
1100 readvec
1101 writevec
1102 errorvec
1103  
1104 #POLL
1105 #####
1106 SV*
1107 poll (poll_list, s_timeout)
1108 SV* poll_list;
1109 double s_timeout;
1110 INIT:
1111  
1112 11 int sz=sizeof(struct pollfd);
1113 int count;
1114 int ret;
1115 CODE:
1116 11 ▶ if(SvOK(poll_list) && SvPOK(poll_list)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(poll_list) && SvPOK(poll_list)){ 11 0 11
if(SvOK(poll_list) && SvPOK(poll_list)){ 11 0 11
1117 11 count=SvCUR(poll_list)/sz; //Number of items in array
1118 //TODO: croak if not fully divisible
1119 11 ret=poll((struct pollfd *)SvPVX(poll_list), count, (int)s_timeout*1000);
1120 }
1121 else {
1122 //Used for timeout only
1123 0 ret=poll(NULL,0,(int)s_timeout*1000);
1124 }
1125 //TODO: need to process EAGAIN and INT somehow
1126 11 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 11 11
1127 0 RETVAL=&PL_sv_undef;
1128 }
1129 else{
1130 11 RETVAL=newSViv(ret);
1131  
1132 }
1133
1134
1135 //No length of list is required as we use the smallest multiple of sizeof(struct pollfd) which will fit in the poll list
1136  
1137 OUTPUT:
1138  
1139 RETVAL
1140 poll_list
1141  
1142  
1143  
1144  
1145 #MKSTEMP
1146 ########
1147  
1148 SV*
1149 mkstemp(template)
1150 char *template
1151  
1152 INIT:
1153 int ret;
1154 SV *path_sv;
1155 char *path;
1156 2 int len=0;
1157 2 int min_ok=1;
1158 PPCODE:
1159 2 len=strlen(template);
1160 2 ▶ if(len<6){
Branch: 1 branch
BranchTrueFalseTotal
if(len<6){ 0 2 2
1161 0 Perl_croak(aTHX_ "The template must end with at least 6 'X' characters");
1162 }
1163  
1164 14 ▶ for(int i=1; i<=6; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(int i=1; i<=6; i++){ 12 2 14
1165 12 ▶ min_ok=min_ok&&(template[len-i]=='X');
Branch: 2 branches
BranchTrueFalseTotal
min_ok=min_ok&&(template[len-i]=='X'); 12 0 12
min_ok=min_ok&&(template[len-i]=='X'); 12 0 12
1166 }
1167 2 ▶ if(!min_ok){
Branch: 1 branch
BranchTrueFalseTotal
if(!min_ok){ 0 2 2
1168 0 Perl_croak(aTHX_ "The template must end with at least 6 'X' characters");
1169 }
1170  
1171  
1172 2 ret=mkstemp(template);
1173 2 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 2 2
1174 0 Perl_croak(aTHX_ "Error creating temp file");
1175 //mXPUSHs(&PL_sv_undef);
1176 }
1177 else{
1178 2 switch(GIMME_V){
1179 2 case G_SCALAR:
1180 2 ▶ mXPUSHs(newSViv(ret));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(ret)); 0 2 2
1181 2 XSRETURN(1);
1182 break;
1183 0 case G_ARRAY:
1184 //path_sv=newSV(MAXPATHLEN);
1185 //path=SvPVX(path_sv);
1186 //fcntl(ret, F_GETPATH, path);
1187 //SvCUR_set(path_sv, strlen(path));
1188  
1189 0 ▶ EXTEND(SP,2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,2); 0 0 0
1190 0 mPUSHs(newSViv(ret));
1191 //mPUSHs(newSVpv(path,0));
1192 0 mPUSHs(&PL_sv_undef);
1193 0 XSRETURN(2);
1194 break;
1195  
1196 0 default:
1197 0 XSRETURN_EMPTY;
1198 break;
1199
1200 }
1201 }
1202  
1203 #MKTEMP
1204 #######
1205  
1206 SV*
1207 mktemp(template)
1208 char *template
1209  
1210 INIT:
1211 char *ret;
1212 char *buf;
1213 2 int len=0;
1214 2 int min_ok=1;
1215 PPCODE:
1216 2 len=strlen(template);
1217 2 ▶ if(len<6){
Branch: 1 branch
BranchTrueFalseTotal
if(len<6){ 0 2 2
1218 0 Perl_croak(aTHX_ "The template must end with at least 6 'X' characters");
1219 }
1220  
1221 14 ▶ for(int i=1; i<=6; i++){
Branch: 1 branch
BranchTrueFalseTotal
for(int i=1; i<=6; i++){ 12 2 14
1222 12 ▶ min_ok=min_ok&&(template[len-i]=='X');
Branch: 2 branches
BranchTrueFalseTotal
min_ok=min_ok&&(template[len-i]=='X'); 12 0 12
min_ok=min_ok&&(template[len-i]=='X'); 12 0 12
1223 }
1224  
1225 2 ▶ if(!min_ok){
Branch: 1 branch
BranchTrueFalseTotal
if(!min_ok){ 0 2 2
1226 0 Perl_croak(aTHX_ "The template must end with at least 6 'X' characters");
1227 }
1228  
1229 2 ret=mktemp(template);
1230 2 ▶ if(ret==NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(ret==NULL){ 0 2 2
1231 0 Perl_croak(aTHX_ "Error creating temp file");
1232 }
1233 else{
1234 2 ▶ mXPUSHs(newSVpv(ret, 0));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpv(ret, 0)); 0 2 2
1235 2 XSRETURN(1);
1236 }
1237  
1238  
1239 SV*
1240 recv(fd, data, len, flags)
1241 int fd
1242 SV *data
1243 int len
1244 int flags
1245  
1246 INIT:
1247 int ret;
1248 SV* peer; //Return addr like perl
1249 struct sockaddr *peer_buf;
1250 unsigned int addr_len;
1251 char *buf;
1252  
1253 CODE:
1254 //Makesure the buffer exists and is large enough to recv
1255 2 ▶ if(!SvOK(data)){
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(data)){ 0 2 2
1256 0 data=newSV(len);
1257 }
1258 2 ▶ buf = SvPOK(data) ? SvGROW(data,len+1) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
buf = SvPOK(data) ? SvGROW(data,len+1) : NULL; 2 0 2
buf = SvPOK(data) ? SvGROW(data,len+1) : NULL; 1 1 2
buf = SvPOK(data) ? SvGROW(data,len+1) : NULL; 0 1 1
1259  
1260 2 peer=newSV(sizeof(struct sockaddr));
1261 2 peer_buf=(struct sockaddr *)SvPVX(peer);
1262  
1263 2 addr_len=sizeof(struct sockaddr);
1264 2 ret=recvfrom(fd, buf, len, flags, peer_buf, &addr_len);
1265  
1266 2 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 2 2
1267 0 RETVAL=&PL_sv_undef;
1268 }
1269 else{
1270 2 SvCUR_set(data,ret);
1271 2 SvPOK_on(peer);
1272 2 SvCUR_set(peer, addr_len);
1273 2 RETVAL=peer;
1274 }
1275 OUTPUT:
1276 RETVAL
1277  
1278 SV*
1279 send(fd,data,flags, ...)
1280 int fd
1281 SV* data
1282 int flags
1283  
1284 INIT:
1285  
1286 char *buf;
1287 int len;
1288  
1289 struct sockaddr *dest;
1290 int ret;
1291  
1292
1293 CODE:
1294 2 ▶ if(SvOK(data) && SvPOK(data)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(data) && SvPOK(data)){ 2 0 2
if(SvOK(data) && SvPOK(data)){ 2 0 2
1295 2 ▶ if((items == 4) && SvOK(ST(3)) && SvPOK(ST(3))){
Branch: 3 branches
BranchTrueFalseTotal
if((items == 4) && SvOK(ST(3)) && SvPOK(ST(3))){ 0 2 2
if((items == 4) && SvOK(ST(3)) && SvPOK(ST(3))){ 0 0 0
if((items == 4) && SvOK(ST(3)) && SvPOK(ST(3))){ 0 0 0
1296 //Do sendto
1297 0 len=SvCUR(data);
1298 0 buf=SvPVX(data);
1299  
1300 0 dest=(struct sockaddr *)SvPVX(ST(3));
1301  
1302 0 ret=sendto(fd, buf, len, flags, dest, SvCUR(ST(3)));
1303 }
1304 else {
1305 //Regular send
1306 2 len=SvCUR(data);
1307 2 buf=SvPVX(data);
1308 2 ret=send(fd, buf, len, flags);
1309 }
1310 }
1311 2 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 2 2
1312  
1313 0 RETVAL=&PL_sv_undef;
1314 }
1315 else{
1316 2 RETVAL=newSViv(ret);
1317 }
1318 OUTPUT:
1319 RETVAL
1320  
1321  
1322 SV*
1323 getpeername(fd)
1324 int fd;
1325
1326 INIT:
1327 int ret;
1328 1 SV *addr=newSV(sizeof(struct sockaddr)+1);
1329 1 struct sockaddr *buf=(struct sockaddr *)SvPVX(addr);
1330 1 unsigned int len=sizeof(struct sockaddr);
1331  
1332 CODE:
1333
1334 1 ret=getpeername(fd,buf,&len);
1335 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 1 1
1336 0 RETVAL=&PL_sv_undef;
1337 }
1338 else {
1339 1 SvCUR_set(addr,len);
1340 1 SvPOK_on(addr);
1341  
1342 1 RETVAL=addr;
1343 }
1344
1345 OUTPUT:
1346 RETVAL
1347  
1348 SV*
1349 getsockname(fd)
1350 int fd;
1351
1352 INIT:
1353 int ret;
1354 2 SV *addr=newSV(sizeof(struct sockaddr)+1);
1355 2 struct sockaddr *buf=(struct sockaddr *)SvPVX(addr);
1356 2 unsigned int len=sizeof(struct sockaddr);
1357  
1358 CODE:
1359
1360 2 ret=getsockname(fd,buf,&len);
1361 2 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 2 2
1362 0 RETVAL=&PL_sv_undef;
1363 }
1364 else {
1365 2 SvCUR_set(addr,len);
1366 2 SvPOK_on(addr);
1367  
1368 2 RETVAL=addr;
1369 }
1370
1371 OUTPUT:
1372 RETVAL
1373  
1374 void
1375 shutdown(fd, how)
1376 int fd
1377 int how
1378  
1379 INIT:
1380  
1381 int ret;
1382 PPCODE:
1383 1 ret=shutdown(fd, how);
1384  
1385 1 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 1 1
1386 0 XSRETURN_UNDEF;
1387 }
1388 else{
1389 1 ▶ mXPUSHs(newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(1)); 0 1 1
1390 1 XSRETURN(1);
1391 }
1392  
1393 void
1394 stat(target)
1395 SV *target;
1396  
1397 ALIAS:
1398 IO::FD::stat = 1
1399 IO::FD::lstat = 2
1400 INIT:
1401  
1402 8 int ret=-1;
1403 char *path;
1404 struct stat buf;
1405 int len;
1406 IV atime;
1407 IV mtime;
1408 IV ctime;
1409 PPCODE:
1410  
1411 8 ▶ if(SvOK(target) && SvIOK(target)){
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(target) && SvIOK(target)){ 8 0 8
if(SvOK(target) && SvIOK(target)){ 2 6 8
1412 //Integer => always an fstat
1413 2 ret=fstat(SvIV(target), &buf);
1414 }
1415 6 ▶ else if(SvOK(target)&& SvPOK(target)){
Branch: 2 branches
BranchTrueFalseTotal
else if(SvOK(target)&& SvPOK(target)){ 6 0 6
else if(SvOK(target)&& SvPOK(target)){ 6 0 6
1416 //String => stat OR lstat
1417
1418 6 len=SvCUR(target);
1419 6 Newx(path, len+1, char); //Allocate plus null
1420 6 Copy(SvPV_nolen(target), path, len, char); //Copy
1421 6 *(path+len)='\0'; //set null
1422 6 switch(ix){
1423 4 case 1:
1424 4 ret=stat(path, &buf);
1425 4 break;
1426 2 case 2:
1427 2 ret=lstat(path, &buf);
1428 2 break;
1429  
1430 0 default:
1431 0 break;
1432 }
1433 6 Safefree(path);
1434  
1435
1436 }
1437 else {
1438 //Unkown
1439 }
1440  
1441 8 ▶ if(ret>=0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret>=0){ 8 0 8
1442 8 switch(GIMME_V){
1443 4 case G_ARRAY:
1444 //fprintf(stderr , "ARRAY CONTEXT. no error\n");
1445  
1446  
1447 //atime=buf.st_atimespec.tv_sec+buf.st_atimespec.tv_nsec*1e-9;
1448 //mtime=buf.st_mtimespec.tv_sec+buf.st_mtimespec.tv_nsec*1e-9;
1449 //ctime=buf.st_ctimespec.tv_sec+buf.st_ctimespec.tv_nsec*1e-9;
1450 4 IO_FD_ATIME
1451 4 IO_FD_MTIME
1452 4 IO_FD_CTIME
1453  
1454  
1455  
1456  
1457  
1458 //Work through the items in the struct
1459 //dSP;
1460 4 ▶ EXTEND(SP, 13);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 13); 0 4 4
1461 4 mPUSHs(newSViv(buf.st_dev));
1462 4 mPUSHs(newSVuv(buf.st_ino));
1463 4 mPUSHs(newSVuv(buf.st_mode));
1464 4 mPUSHs(newSViv(buf.st_nlink));
1465 4 mPUSHs(newSViv(buf.st_uid));
1466 4 mPUSHs(newSViv(buf.st_gid));
1467 4 mPUSHs(newSViv(buf.st_rdev));
1468 4 mPUSHs(newSViv(buf.st_size));
1469 4 mPUSHs(newSViv(atime));
1470 4 mPUSHs(newSViv(mtime));
1471 4 mPUSHs(newSViv(ctime));
1472 4 mPUSHs(newSViv(buf.st_blksize));
1473 4 mPUSHs(newSViv(buf.st_blocks));
1474 4 XSRETURN(13);
1475 break;
1476 0 case G_VOID:
1477 0 XSRETURN_EMPTY;
1478 break;
1479 4 case G_SCALAR:
1480 default:
1481 //fprintf(stderr , "SCALAR CONTEXT. no error\n");
1482 4 ▶ mXPUSHs(newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSViv(1)); 0 4 4
1483 4 XSRETURN(1);
1484 break;
1485 }
1486  
1487  
1488  
1489 }
1490 0 ▶ switch(GIMME_V){
Branch: 1 branch
BranchTrueFalseTotal
switch(GIMME_V){ 0 0 0
1491 0 case G_SCALAR:
1492 0 ▶ mXPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(&PL_sv_undef); 0 0 0
1493 0 XSRETURN(1);
1494 break;
1495 0 case G_VOID:
1496 case G_ARRAY:
1497 default:
1498 0 XSRETURN_EMPTY;
1499 break;
1500 }
1501  
1502  
1503  
1504  
1505  
1506 #if defined(IO_FD_OS_DARWIN) || defined(IO_FD_OS_BSD)
1507 SV *
1508 kqueue()
1509  
1510 INIT:
1511 int ret;
1512 CODE:
1513 ret=kqueue();
1514 if(ret<0){
1515 RETVAL=&PL_sv_undef;
1516 }
1517 else{
1518 RETVAL=newSViv(ret);
1519 }
1520 OUTPUT:
1521 RETVAL
1522  
1523 SV *
1524 kevent(kq, change_list, event_list, timeout)
1525 int kq;
1526 SV * change_list;
1527 SV * event_list;
1528 SV * timeout;
1529
1530 INIT:
1531 int ret;
1532 int ncl, nel;
1533 KEVENT_S *cl, *el;
1534 struct timespec tspec;
1535 double tout;
1536  
1537 CODE:
1538 //Calcuate from current length
1539 ncl=SvCUR(change_list)/sizeof(KEVENT_S);
1540  
1541 //Calculate from available length
1542 nel=SvLEN(event_list)/sizeof(KEVENT_S);
1543  
1544 cl=(KEVENT_S *)SvPVX(change_list);
1545 el=(KEVENT_S *)SvPVX(event_list);
1546  
1547 //fprintf(stderr, "change list len: %d, event list available: %d\n", ncl, nel);
1548 if(SvOK(timeout)&& SvNIOK(timeout)){
1549 tout=SvNV(timeout);
1550 tspec.tv_sec=tout;
1551 tspec.tv_nsec=1e9*(tout-tspec.tv_sec);
1552 #if defined(IO_FD_OS_DARWIN)
1553 ret=KEVENT (kq, cl, ncl, el, nel, 0, &tspec);
1554 #endif
1555 #if defined(IO_FD_OS_BSD)
1556 ret=KEVENT(kq, cl, ncl, el, nel, &tspec);
1557 //}
1558 #endif
1559 }
1560 else {
1561 #if defined(IO_FD_OS_DARWIN)
1562 ret=KEVENT(kq,cl,ncl, el, nel,0, NULL);
1563 #endif
1564 #if defined(IO_FD_OS_BSD)
1565 ret=KEVENT(kq,cl,ncl, el, nel, NULL);
1566 #endif
1567 }
1568  
1569 if(ret<0){
1570 SvCUR_set(event_list,0);
1571 RETVAL=&PL_sv_undef;
1572 }
1573 else {
1574 SvCUR_set(event_list,sizeof(KEVENT_S)*ret);
1575 RETVAL=newSViv(ret);
1576 }
1577  
1578 OUTPUT:
1579 RETVAL
1580 event_list
1581  
1582
1583 SV *
1584 pack_kevent(ident,filter,flags,fflags,data,udata, ...)
1585 unsigned long ident;
1586 I16 filter;
1587 U16 flags;
1588 U32 fflags;
1589 long data;
1590 SV *udata;
1591  
1592 INIT:
1593 KEVENT_S *e;
1594  
1595 CODE:
1596 RETVAL=newSV(sizeof(KEVENT_S));
1597 e=(KEVENT_S *)SvPVX(RETVAL);
1598 e->ident=ident;
1599 e->filter=filter;
1600 e->flags=flags;
1601 e->fflags=fflags;
1602 e->data=data;
1603 e->udata=SvRV(udata);
1604 SvCUR_set(RETVAL,sizeof(KEVENT_S));
1605 SvPOK_on(RETVAL);
1606 //Pack
1607  
1608 OUTPUT:
1609 RETVAL
1610  
1611 #endif
1612  
1613 SV *
1614 clock_gettime_monotonic()
1615  
1616 INIT:
1617 struct timespec tp;
1618 int ret;
1619  
1620 CODE:
1621 0 ret=clock_gettime(CLOCK_MONOTONIC, &tp);
1622 0 ▶ if(ret<0){
Branch: 1 branch
BranchTrueFalseTotal
if(ret<0){ 0 0 0
1623 0 RETVAL=&PL_sv_undef;
1624 }
1625 else{
1626 0 RETVAL=newSVnv(tp.tv_sec + tp.tv_nsec * 1e-9);
1627 }
1628  
1629  
1630 OUTPUT:
1631 RETVAL
1632  
1633 long
1634 sv_to_pointer(sv)
1635 SV *sv
1636  
1637 INIT:
1638  
1639 CODE:
1640 //TODO: Increase the ref count of input sv
1641 0 ▶ RETVAL=(long)SvRV(sv);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL=(long)SvRV(sv); 0 0 0
1642 OUTPUT:
1643  
1644 RETVAL
1645  
1646 SV *
1647 pointer_to_sv(pointer)
1648 long pointer;
1649  
1650 INIT:
1651 CODE:
1652 //TODO: check valid. decrement ref count;
1653 0 RETVAL=newRV((SV*)pointer);
1654  
1655 OUTPUT:
1656 RETVAL
1657  
1658  
1659 SV *
1660 SV(size)
1661 int size
1662  
1663 CODE:
1664 0 RETVAL=newSV(size);
1665
1666 //SvPOK_on(RETVAL);
1667 0 SvPVCLEAR(RETVAL);
1668 OUTPUT:
1669 RETVAL
1670  
1671 #TODO
1672  
1673 #readline
1674 #readinput based on $\ seperator. use get_sv function??
1675 #in list or scalar context
1676  
1677 void
1678 readline(fd)
1679 int fd
1680
1681 INIT:
1682 SV *irs;
1683 int ret;
1684 int count;
1685 SV* buffer;
1686 char *buf;
1687 9 int do_loop=1;
1688  
1689 int tmp;
1690 PPCODE:
1691 9 irs=get_sv("/",0);
1692 9 ▶ if(irs){
Branch: 1 branch
BranchTrueFalseTotal
if(irs){ 9 0 9
1693 9 ▶ #Found variable. Read records
Branch: 1 branch
BranchTrueFalseTotal
#Found variable. Read records 6 3 9
1694 6 ▶ if(SvOK(irs)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(irs)){ 5 1 6
1695 if(SvROK(irs)){
1696 //fprintf(stderr, "DOING RECORD READ\n");
1697 //SLURP RECORDS
1698 5  
1699 5 SV* v=SvRV(irs); //Dereference to get SV
1700 5 tmp=SvIV(v); //The integer value of the sv
1701 5 buffer=newSV(tmp); //Allocate buffer at record size
1702 5 buf=SvPVX(buffer); //Get the pointer we need
1703 ret=read(fd, buf, tmp); //Do the read into buffer
1704 5 //fprintf(stderr, "read return: %d\n", ret);
1705 5 ▶ SvPOK_on(buffer); //Make a string
Branch: 1 branch
BranchTrueFalseTotal
SvPOK_on(buffer); //Make a string 4 1 5
1706 4 if(ret>=0){
1707 4 buf[ret]='\0'; //Set null just in case
1708 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
1709 4 EXTEND(SP,1); //Extend stack
1710 4 mPUSHs(buffer); //Push record
1711 XSRETURN(1);
1712 }
1713 1 else {
1714 XSRETURN_UNDEF;
1715 }
1716  
1717 }
1718 1 else {
1719 Perl_croak( aTHX_ "IO::FD::readline does not split lines");
1720 }
1721 }
1722 else{
1723  
1724 //fprintf(stderr, "DOING SLURP READ\n");
1725 3 ▶ //SLURP entire file
Branch: 1 branch
BranchTrueFalseTotal
//SLURP entire file 0 3 3
1726 3 EXTEND(SP,1);
1727 3 PUSHs(slurp(aTHX_ fd, 4096));
1728 XSRETURN(1);
1729 }
1730 }
1731 else {
1732 #not found.. this isn't good
1733  
1734 }
1735  
1736  
1737 #Naming
1738  
1739 #TODO:
1740 # TODO ioctl
1741 # poll
1742 # select ... perl compatiable version
1743 # dir ... not normally on FDs?
1744 # readline?
1745  
1746 # Add IPC::Open2 and IPC::Open3 emulations
1747  
« const-xs.inc ↑ Summary