Fix declaration-after-statement problem
[p5sagit/p5-mst-13.2.git] / ext / IO / IO.xs
1 /*
2  * Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
3  * This program is free software; you can redistribute it and/or
4  * modify it under the same terms as Perl itself.
5  */
6
7 #define PERL_EXT_IO
8
9 #define PERL_NO_GET_CONTEXT
10 #include "EXTERN.h"
11 #define PERLIO_NOT_STDIO 1
12 #include "perl.h"
13 #include "XSUB.h"
14 #include "poll.h"
15 #ifdef I_UNISTD
16 #  include <unistd.h>
17 #endif
18 #if defined(I_FCNTL) || defined(HAS_FCNTL)
19 #  include <fcntl.h>
20 #endif
21
22 #ifndef SIOCATMARK
23 #   ifdef I_SYS_SOCKIO
24 #       include <sys/sockio.h>
25 #   endif
26 #endif
27
28 #ifdef PerlIO
29 #if defined(MACOS_TRADITIONAL) && defined(USE_SFIO)
30 #define PERLIO_IS_STDIO 1
31 #undef setbuf
32 #undef setvbuf
33 #define setvbuf         _stdsetvbuf
34 #define setbuf(f,b)     ( __sf_setbuf(f,b) )
35 #endif
36 typedef int SysRet;
37 typedef PerlIO * InputStream;
38 typedef PerlIO * OutputStream;
39 #else
40 #define PERLIO_IS_STDIO 1
41 typedef int SysRet;
42 typedef FILE * InputStream;
43 typedef FILE * OutputStream;
44 #endif
45
46 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
47
48 #ifndef gv_stashpvn
49 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
50 #endif
51
52 #ifndef __attribute__noreturn__
53 #  define __attribute__noreturn__
54 #endif
55
56 #ifndef NORETURN_FUNCTION_END
57 # define NORETURN_FUNCTION_END /* NOT REACHED */ return 0
58 #endif
59
60 static int not_here(const char *s) __attribute__noreturn__;
61 static int
62 not_here(const char *s)
63 {
64     croak("%s not implemented on this architecture", s);
65     NORETURN_FUNCTION_END;
66 }
67
68
69 #ifndef PerlIO
70 #define PerlIO_fileno(f) fileno(f)
71 #endif
72
73 static int
74 io_blocking(pTHX_ InputStream f, int block)
75 {
76 #if defined(HAS_FCNTL)
77     int RETVAL;
78     if(!f) {
79         errno = EBADF;
80         return -1;
81     }
82     RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
83     if (RETVAL >= 0) {
84         int mode = RETVAL;
85         int newmode = mode;
86 #ifdef O_NONBLOCK
87         /* POSIX style */
88
89 # ifndef O_NDELAY
90 #  define O_NDELAY O_NONBLOCK
91 # endif
92         /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
93          * after a successful F_SETFL of an O_NONBLOCK. */
94         RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
95
96         if (block == 0) {
97             newmode &= ~O_NDELAY;
98             newmode |= O_NONBLOCK;
99         } else if (block > 0) {
100             newmode &= ~(O_NDELAY|O_NONBLOCK);
101         }
102 #else
103         /* Not POSIX - better have O_NDELAY or we can't cope.
104          * for BSD-ish machines this is an acceptable alternative
105          * for SysV we can't tell "would block" from EOF but that is
106          * the way SysV is...
107          */
108         RETVAL = RETVAL & O_NDELAY ? 0 : 1;
109
110         if (block == 0) {
111             newmode |= O_NDELAY;
112         } else if (block > 0) {
113             newmode &= ~O_NDELAY;
114         }
115 #endif
116         if (newmode != mode) {
117             const int ret = fcntl(PerlIO_fileno(f),F_SETFL,newmode);
118             if (ret < 0)
119                 RETVAL = ret;
120         }
121     }
122     return RETVAL;
123 #else
124     return -1;
125 #endif
126 }
127
128 MODULE = IO     PACKAGE = IO::Seekable  PREFIX = f
129
130 void
131 fgetpos(handle)
132         InputStream     handle
133     CODE:
134         if (handle) {
135             Fpos_t pos;
136 #ifdef PerlIO
137             ST(0) = sv_newmortal();
138 #if PERL_VERSION < 8
139             if (PerlIO_getpos(handle, &pos) != 0) {
140                 ST(0) = &PL_sv_undef;
141             }
142             else {
143                 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
144             }
145 #else
146             if (PerlIO_getpos(handle, ST(0)) != 0) {
147                 ST(0) = &PL_sv_undef;
148             }
149 #endif
150 #else
151             Fpos_t pos;
152             if (fgetpos(handle, &pos)) {
153                 ST(0) = &PL_sv_undef;
154             } else {
155                 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
156             }
157 #endif
158         }
159         else {
160             errno = EINVAL;
161             ST(0) = &PL_sv_undef;
162         }
163
164 SysRet
165 fsetpos(handle, pos)
166         InputStream     handle
167         SV *            pos
168     CODE:
169         if (handle) {
170 #ifdef PerlIO
171 #if PERL_VERSION < 8
172             char *p;
173             STRLEN len;
174             if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
175                 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
176             }
177             else {
178                 RETVAL = -1;
179                 errno = EINVAL;
180             }
181 #else
182             RETVAL = PerlIO_setpos(handle, pos);
183 #endif
184 #else
185             char *p;
186             STRLEN len;
187             if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
188                 RETVAL = fsetpos(handle, (Fpos_t*)p);
189             }
190             else {
191                 RETVAL = -1;
192                 errno = EINVAL;
193             }
194 #endif
195         }
196         else {
197             RETVAL = -1;
198             errno = EINVAL;
199         }
200     OUTPUT:
201         RETVAL
202
203 MODULE = IO     PACKAGE = IO::File      PREFIX = f
204
205 void
206 new_tmpfile(packname = "IO::File")
207     char *      packname
208     PREINIT:
209         OutputStream fp;
210         GV *gv;
211     CODE:
212 #ifdef PerlIO
213         fp = PerlIO_tmpfile();
214 #else
215         fp = tmpfile();
216 #endif
217         gv = (GV*)SvREFCNT_inc(newGVgen(packname));
218         if (gv)
219             hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
220         if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
221             ST(0) = sv_2mortal(newRV((SV*)gv));
222             sv_bless(ST(0), gv_stashpv(packname, TRUE));
223             SvREFCNT_dec(gv);   /* undo increment in newRV() */
224         }
225         else {
226             ST(0) = &PL_sv_undef;
227             SvREFCNT_dec(gv);
228         }
229
230 MODULE = IO     PACKAGE = IO::Poll
231
232 void
233 _poll(timeout,...)
234         int timeout;
235 PPCODE:
236 {
237 #ifdef HAS_POLL
238     const int nfd = (items - 1) / 2;
239     SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
240     struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
241     int i,j,ret;
242     for(i=1, j=0  ; j < nfd ; j++) {
243         fds[j].fd = SvIV(ST(i));
244         i++;
245         fds[j].events = (short)SvIV(ST(i));
246         i++;
247         fds[j].revents = 0;
248     }
249     if((ret = poll(fds,nfd,timeout)) >= 0) {
250         for(i=1, j=0 ; j < nfd ; j++) {
251             sv_setiv(ST(i), fds[j].fd); i++;
252             sv_setiv(ST(i), fds[j].revents); i++;
253         }
254     }
255     SvREFCNT_dec(tmpsv);
256     XSRETURN_IV(ret);
257 #else
258         not_here("IO::Poll::poll");
259 #endif
260 }
261
262 MODULE = IO     PACKAGE = IO::Handle    PREFIX = io_
263
264 void
265 io_blocking(handle,blk=-1)
266         InputStream     handle
267         int             blk
268 PROTOTYPE: $;$
269 CODE:
270 {
271     const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
272     if(ret >= 0)
273         XSRETURN_IV(ret);
274     else
275         XSRETURN_UNDEF;
276 }
277
278 MODULE = IO     PACKAGE = IO::Handle    PREFIX = f
279
280 int
281 ungetc(handle, c)
282         InputStream     handle
283         int             c
284     CODE:
285         if (handle)
286 #ifdef PerlIO
287             RETVAL = PerlIO_ungetc(handle, c);
288 #else
289             RETVAL = ungetc(c, handle);
290 #endif
291         else {
292             RETVAL = -1;
293             errno = EINVAL;
294         }
295     OUTPUT:
296         RETVAL
297
298 int
299 ferror(handle)
300         InputStream     handle
301     CODE:
302         if (handle)
303 #ifdef PerlIO
304             RETVAL = PerlIO_error(handle);
305 #else
306             RETVAL = ferror(handle);
307 #endif
308         else {
309             RETVAL = -1;
310             errno = EINVAL;
311         }
312     OUTPUT:
313         RETVAL
314
315 int
316 clearerr(handle)
317         InputStream     handle
318     CODE:
319         if (handle) {
320 #ifdef PerlIO
321             PerlIO_clearerr(handle);
322 #else
323             clearerr(handle);
324 #endif
325             RETVAL = 0;
326         }
327         else {
328             RETVAL = -1;
329             errno = EINVAL;
330         }
331     OUTPUT:
332         RETVAL
333
334 int
335 untaint(handle)
336        SV *     handle
337     CODE:
338 #ifdef IOf_UNTAINT
339         IO * io;
340         io = sv_2io(handle);
341         if (io) {
342             IoFLAGS(io) |= IOf_UNTAINT;
343             RETVAL = 0;
344         }
345         else {
346 #endif
347             RETVAL = -1;
348             errno = EINVAL;
349 #ifdef IOf_UNTAINT
350         }
351 #endif
352     OUTPUT:
353         RETVAL
354
355 SysRet
356 fflush(handle)
357         OutputStream    handle
358     CODE:
359         if (handle)
360 #ifdef PerlIO
361             RETVAL = PerlIO_flush(handle);
362 #else
363             RETVAL = Fflush(handle);
364 #endif
365         else {
366             RETVAL = -1;
367             errno = EINVAL;
368         }
369     OUTPUT:
370         RETVAL
371
372 void
373 setbuf(handle, ...)
374         OutputStream    handle
375     CODE:
376         if (handle)
377 #ifdef PERLIO_IS_STDIO
378         {
379             char *buf = items == 2 && SvPOK(ST(1)) ?
380               sv_grow(ST(1), BUFSIZ) : 0;
381             setbuf(handle, buf);
382         }
383 #else
384             not_here("IO::Handle::setbuf");
385 #endif
386
387 SysRet
388 setvbuf(...)
389     CODE:
390         if (items != 4)
391             Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
392 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
393     {
394         OutputStream    handle = 0;
395         char *          buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
396         int             type;
397         int             size;
398
399         if (items == 4) {
400             handle = IoOFP(sv_2io(ST(0)));
401             buf    = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
402             type   = (int)SvIV(ST(2));
403             size   = (int)SvIV(ST(3));
404         }
405         if (!handle)                    /* Try input stream. */
406             handle = IoIFP(sv_2io(ST(0)));
407         if (items == 4 && handle)
408             RETVAL = setvbuf(handle, buf, type, size);
409         else {
410             RETVAL = -1;
411             errno = EINVAL;
412         }
413     }
414 #else
415         RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
416 #endif
417     OUTPUT:
418         RETVAL
419
420
421 SysRet
422 fsync(handle)
423         OutputStream handle
424     CODE:
425 #ifdef HAS_FSYNC
426         if(handle)
427             RETVAL = fsync(PerlIO_fileno(handle));
428         else {
429             RETVAL = -1;
430             errno = EINVAL;
431         }
432 #else
433         RETVAL = (SysRet) not_here("IO::Handle::sync");
434 #endif
435     OUTPUT:
436         RETVAL
437
438
439 MODULE = IO     PACKAGE = IO::Socket
440
441 SysRet
442 sockatmark (sock)
443    InputStream sock
444    PROTOTYPE: $
445    PREINIT:
446      int fd;
447    CODE:
448    {
449      fd = PerlIO_fileno(sock);
450 #ifdef HAS_SOCKATMARK
451      RETVAL = sockatmark(fd);
452 #else
453      {
454        int flag = 0;
455 #   ifdef SIOCATMARK
456 #     if defined(NETWARE) || defined(WIN32)
457        if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
458 #     else
459        if (ioctl(fd, SIOCATMARK, &flag) != 0)
460 #     endif
461          XSRETURN_UNDEF;
462 #   else
463        not_here("IO::Socket::atmark");
464 #   endif
465        RETVAL = flag;
466      }
467 #endif
468    }
469    OUTPUT:
470      RETVAL
471
472 BOOT:
473 {
474     HV *stash;
475     /*
476      * constant subs for IO::Poll
477      */
478     stash = gv_stashpvn("IO::Poll", 8, TRUE);
479 #ifdef  POLLIN
480         newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
481 #endif
482 #ifdef  POLLPRI
483         newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
484 #endif
485 #ifdef  POLLOUT
486         newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
487 #endif
488 #ifdef  POLLRDNORM
489         newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
490 #endif
491 #ifdef  POLLWRNORM
492         newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
493 #endif
494 #ifdef  POLLRDBAND
495         newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
496 #endif
497 #ifdef  POLLWRBAND
498         newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
499 #endif
500 #ifdef  POLLNORM
501         newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
502 #endif
503 #ifdef  POLLERR
504         newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
505 #endif
506 #ifdef  POLLHUP
507         newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
508 #endif
509 #ifdef  POLLNVAL
510         newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
511 #endif
512     /*
513      * constant subs for IO::Handle
514      */
515     stash = gv_stashpvn("IO::Handle", 10, TRUE);
516 #ifdef _IOFBF
517         newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
518 #endif
519 #ifdef _IOLBF
520         newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
521 #endif
522 #ifdef _IONBF
523         newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
524 #endif
525 #ifdef SEEK_SET
526         newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
527 #endif
528 #ifdef SEEK_CUR
529         newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
530 #endif
531 #ifdef SEEK_END
532         newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
533 #endif
534 }
535