File Coverage

FD.xs
Criterion Covered Total %
statement 41 42 97.6
branch 18 24 75.0
condition n/a
subroutine n/a
pod n/a
total 59 66 89.3


line stmt bran cond sub pod time code
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 50         SvGROW(buffer, len+read_size); //Grow the buffer if required
    100          
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 100         if(ret>=0){
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 100         while(ret>0);
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              
58             #if defined(IO_FD_OS_DARWIN)
59             #define KEVENT kevent64
60             #define KEVENT_S struct kevent64_s
61             #endif
62             #if defined(IO_FD_OS_BSD)
63             #define KEVENT kevent
64             #define KEVENT_S struct kevent
65             #endif
66              
67             #define ADJUST_SOCKADDR_SIZE(addr) \
68             struct sockaddr * a=(struct sockaddr *)SvPVX(addr);\
69             switch(a->sa_family){\
70             case AF_INET:\
71             SvCUR_set(addr, sizeof(struct sockaddr_in));\
72             break;\
73             case AF_INET6:\
74             SvCUR_set(addr, sizeof(struct sockaddr_in6));\
75             break;\
76             case AF_UNIX:\
77             SvCUR_set(addr, sizeof(struct sockaddr_un));\
78             break;\
79             default:\
80             break;\
81             }\
82              
83              
84              
85              
86             MODULE = IO::FD PACKAGE = IO::FD
87              
88             INCLUDE: const-xs.inc
89              
90             BOOT:
91              
92             //boot strap the mutliple accept buffer
93 22           accept_multiple_next_addr=newSV(sizeof(struct sockaddr_storage));
94 22           accept_multiple_next_buf=(struct sockaddr *)SvPVX(accept_multiple_next_addr);
95              
96             //locate the $^F variable
97             //max_file_desc=get_sv("^F",0);
98              
99             INCLUDE_COMMAND: cat "xs-include/socket.c"
100             INCLUDE_COMMAND: $^X "xs-include/sendfile.pl"
101             INCLUDE_COMMAND: cat "xs-include/mkfifo.c"
102             INCLUDE_COMMAND: $^X "xs-include/mkfifoat.pl"
103             INCLUDE_COMMAND: cat "xs-include/file.c"
104             INCLUDE_COMMAND: cat "xs-include/experimental.c"
105             INCLUDE_COMMAND: cat "xs-include/temp.c"
106             INCLUDE_COMMAND: cat "xs-include/send-recv.c"
107              
108              
109              
110             void
111             readline(fd)
112             SV *fd
113            
114             INIT:
115             SV *irs;
116             int ret;
117             int count;
118             SV* buffer;
119             char *buf;
120 9           int do_loop=1;
121              
122             int tmp;
123             PPCODE:
124 9 50         if(SvOK(fd)&& SvIOK(fd)){
    50          
125 9           irs=get_sv("/",0);
126 9 50         if(irs){
127 9 100         #Found variable. Read records
128 6 100         if(SvOK(irs)){
129             if(SvROK(irs)){
130             //fprintf(stderr, "DOING RECORD READ\n");
131             //SLURP RECORDS
132 5            
133 5           SV* v=SvRV(irs); //Dereference to get SV
134 5           tmp=SvIV(v); //The integer value of the sv
135 5           buffer=newSV(tmp); //Allocate buffer at record size
136 5           buf=SvPVX(buffer); //Get the pointer we need
137             ret=read(SvIV(fd), buf, tmp); //Do the read into buffer
138 5           //fprintf(stderr, "read return: %d\n", ret);
139 5 100         SvPOK_on(buffer); //Make a string
140 4           if(ret>=0){
141 4           buf[ret]='\0'; //Set null just in case
142 4 50         SvCUR_set(buffer,ret); //Set the length of the string
143 4           EXTEND(SP,1); //Extend stack
144 4           mPUSHs(buffer); //Push record
145             XSRETURN(1);
146             }
147 1           else {
148             XSRETURN_UNDEF;
149             }
150              
151             }
152 1           else {
153             Perl_croak( aTHX_ "IO::FD::readline does not split lines");
154             }
155             }
156             else{
157              
158             //fprintf(stderr, "DOING SLURP READ\n");
159 3 50         //SLURP entire file
160 3           EXTEND(SP,1);
161 3           PUSHs(slurp(aTHX_ SvIV(fd), 4096));
162             XSRETURN(1);
163             }
164             }
165             else {
166             //not found.. this isn't good
167              
168             }
169             }
170 0           else{
171             XSRETURN_UNDEF;
172             }
173              
174              
175             #Naming
176              
177             #TODO:
178             # TODO ioctl
179             # poll
180             # select ... perl compatiable version
181             # dir ... not normally on FDs?
182             # readline?
183              
184             # Add IPC::Open2 and IPC::Open3 emulations
185