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.
8 #define PERLIO_NOT_STDIO 1
15 #if defined(I_FCNTL) || defined(HAS_FCNTL)
21 typedef PerlIO * InputStream;
22 typedef PerlIO * OutputStream;
24 #define PERLIO_IS_STDIO 1
26 typedef FILE * InputStream;
27 typedef FILE * OutputStream;
30 #include "patchlevel.h"
32 #if (PATCHLEVEL < 3) || ((PATCHLEVEL == 3) && (SUBVERSION < 22))
34 # define MY_start_subparse(fmt,flags) start_subparse()
36 # if (PATCHLEVEL == 3) && (SUBVERSION == 22)
38 # define MY_start_subparse(fmt,flags) start_subparse(flags)
40 /* 5.003_23 onwards */
41 # define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
46 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
53 croak("%s not implemented on this architecture", s);
59 * Define an XSUB that returns a constant scalar. The resulting structure is
60 * identical to that created by the parser when it parses code like :
64 * This allows the constants from the XSUB to be inlined.
66 * !!! THIS SHOULD BE ADDED INTO THE CORE CODE !!!!
71 newCONSTSUB(stash,name,sv)
80 HV *old_cop_stash = curcop->cop_stash;
81 HV *old_curstash = curstash;
82 line_t oldline = curcop->cop_line;
83 curcop->cop_line = copline;
85 hints &= ~HINT_BLOCK_SCOPE;
87 curstash = curcop->cop_stash = stash;
90 MY_start_subparse(FALSE, 0),
91 newSVOP(OP_CONST, 0, newSVpv(name,0)),
92 newSVOP(OP_CONST, 0, &sv_no), /* SvPV(&sv_no) == "" -- GMB */
93 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
97 curcop->cop_stash = old_cop_stash;
98 curstash = old_curstash;
99 curcop->cop_line = oldline;
104 #define PerlIO_fileno(f) fileno(f)
117 #if defined(HAS_FCNTL)
118 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
123 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
124 /* Ooops has O_NDELAY too - make sure we don't
125 * get SysV behaviour by mistake
127 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
129 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
131 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
132 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
136 else if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
138 mode &= ~(O_NONBLOCK | O_NDELAY);
139 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
145 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
147 if ((block == 0) && !(mode & O_NONBLOCK)) {
150 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
154 else if ((block > 0) && (mode & O_NONBLOCK)) {
157 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
163 /* Not POSIX - better have O_NDELAY or we can't cope.
164 * for BSD-ish machines this is an acceptable alternative
165 * for SysV we can't tell "would block" from EOF but that is
168 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
170 if ((block == 0) && !(mode & O_NDELAY)) {
173 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
177 else if ((block > 0) && (mode & O_NDELAY)) {
180 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
192 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
201 PerlIO_getpos(handle, &pos);
203 fgetpos(handle, &pos);
205 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
219 RETVAL = PerlIO_setpos(handle, (Fpos_t*)SvPVX(pos));
221 RETVAL = fsetpos(handle, (Fpos_t*)SvPVX(pos));
230 MODULE = IO PACKAGE = IO::File PREFIX = f
233 new_tmpfile(packname = "IO::File")
240 fp = PerlIO_tmpfile();
244 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
245 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
246 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
247 ST(0) = sv_2mortal(newRV((SV*)gv));
248 sv_bless(ST(0), gv_stashpv(packname, TRUE));
249 SvREFCNT_dec(gv); /* undo increment in newRV() */
256 MODULE = IO PACKAGE = IO::Poll
264 int nfd = (items - 1) / 2;
265 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
266 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
268 for(i=1, j=0 ; j < nfd ; j++) {
269 fds[j].fd = SvIV(ST(i));
271 fds[j].events = SvIV(ST(i));
275 if((ret = poll(fds,nfd,timeout)) >= 0) {
276 for(i=1, j=0 ; j < nfd ; j++) {
277 sv_setiv(ST(i), fds[j].fd); i++;
278 sv_setiv(ST(i), fds[j].revents); i++;
284 not_here("IO::Poll::poll");
288 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
291 io_blocking(handle,blk=-1)
297 int ret = io_blocking(handle, items == 1 ? -1 : blk ? 1 : 0);
304 MODULE = IO PACKAGE = IO::Handle PREFIX = f
314 RETVAL = PerlIO_ungetc(handle, c);
316 RETVAL = ungetc(c, handle);
331 RETVAL = PerlIO_error(handle);
333 RETVAL = ferror(handle);
348 PerlIO_clearerr(handle);
369 IoFLAGS(io) |= IOf_UNTAINT;
388 RETVAL = PerlIO_flush(handle);
390 RETVAL = Fflush(handle);
402 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
405 #ifdef PERLIO_IS_STDIO
408 not_here("IO::Handle::setbuf");
412 setvbuf(handle, buf, type, size)
414 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
418 /* Should check HAS_SETVBUF once Configure tests for that */
419 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF)
420 if (!handle) /* Try input stream. */
421 handle = IoIFP(sv_2io(ST(0)));
423 RETVAL = setvbuf(handle, buf, type, size);
429 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
441 RETVAL = fsync(PerlIO_fileno(handle));
447 RETVAL = (SysRet) not_here("IO::Handle::sync");
457 * constant subs for IO::Poll
459 stash = gv_stashpvn("IO::Poll", 8, TRUE);
461 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
464 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
467 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
470 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
473 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
476 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
479 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
482 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
485 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
488 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
491 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
494 * constant subs for IO::Handle
496 stash = gv_stashpvn("IO::Handle", 10, TRUE);
498 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
501 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
504 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
507 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
510 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
513 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
516 * constant subs for IO
518 stash = gv_stashpvn("IO", 2, TRUE);
520 newCONSTSUB(stash,"EINPROGRESS", newSViv(EINPROGRESS));