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.
7 #define PERL_NO_GET_CONTEXT
9 #define PERLIO_NOT_STDIO 1
16 #if defined(I_FCNTL) || defined(HAS_FCNTL)
22 typedef PerlIO * InputStream;
23 typedef PerlIO * OutputStream;
25 #define PERLIO_IS_STDIO 1
27 typedef FILE * InputStream;
28 typedef FILE * OutputStream;
31 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
34 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
40 croak("%s not implemented on this architecture", s);
46 #define PerlIO_fileno(f) fileno(f)
50 io_blocking(InputStream f, int block)
57 #if defined(HAS_FCNTL)
58 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
63 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
64 /* Ooops has O_NDELAY too - make sure we don't
65 * get SysV behaviour by mistake
67 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
69 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
71 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
72 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
76 else if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
78 mode &= ~(O_NONBLOCK | O_NDELAY);
79 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
85 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
87 if ((block == 0) && !(mode & O_NONBLOCK)) {
90 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
94 else if ((block > 0) && (mode & O_NONBLOCK)) {
97 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
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
108 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
110 if ((block == 0) && !(mode & O_NDELAY)) {
113 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
117 else if ((block > 0) && (mode & O_NDELAY)) {
120 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
132 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
141 PerlIO_getpos(handle, &pos);
143 fgetpos(handle, &pos);
145 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
148 ST(0) = &PL_sv_undef;
159 if (handle && (p = SvPV(pos,len)) && len == sizeof(Fpos_t))
161 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
163 RETVAL = fsetpos(handle, (Fpos_t*)p);
172 MODULE = IO PACKAGE = IO::File PREFIX = f
175 new_tmpfile(packname = "IO::File")
182 fp = PerlIO_tmpfile();
186 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
187 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
188 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
189 ST(0) = sv_2mortal(newRV((SV*)gv));
190 sv_bless(ST(0), gv_stashpv(packname, TRUE));
191 SvREFCNT_dec(gv); /* undo increment in newRV() */
194 ST(0) = &PL_sv_undef;
198 MODULE = IO PACKAGE = IO::Poll
206 int nfd = (items - 1) / 2;
207 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
208 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
210 for(i=1, j=0 ; j < nfd ; j++) {
211 fds[j].fd = SvIV(ST(i));
213 fds[j].events = SvIV(ST(i));
217 if((ret = poll(fds,nfd,timeout)) >= 0) {
218 for(i=1, j=0 ; j < nfd ; j++) {
219 sv_setiv(ST(i), fds[j].fd); i++;
220 sv_setiv(ST(i), fds[j].revents); i++;
226 not_here("IO::Poll::poll");
230 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
233 io_blocking(handle,blk=-1)
239 int ret = io_blocking(handle, items == 1 ? -1 : blk ? 1 : 0);
246 MODULE = IO PACKAGE = IO::Handle PREFIX = f
256 RETVAL = PerlIO_ungetc(handle, c);
258 RETVAL = ungetc(c, handle);
273 RETVAL = PerlIO_error(handle);
275 RETVAL = ferror(handle);
290 PerlIO_clearerr(handle);
311 IoFLAGS(io) |= IOf_UNTAINT;
330 RETVAL = PerlIO_flush(handle);
332 RETVAL = Fflush(handle);
344 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
347 #ifdef PERLIO_IS_STDIO
350 not_here("IO::Handle::setbuf");
354 setvbuf(handle, buf, type, size)
356 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
360 /* Should check HAS_SETVBUF once Configure tests for that */
361 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF)
362 if (!handle) /* Try input stream. */
363 handle = IoIFP(sv_2io(ST(0)));
365 RETVAL = setvbuf(handle, buf, type, size);
371 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
383 RETVAL = fsync(PerlIO_fileno(handle));
389 RETVAL = (SysRet) not_here("IO::Handle::sync");
399 * constant subs for IO::Poll
401 stash = gv_stashpvn("IO::Poll", 8, TRUE);
403 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
406 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
409 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
412 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
415 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
418 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
421 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
424 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
427 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
430 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
433 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
436 * constant subs for IO::Handle
438 stash = gv_stashpvn("IO::Handle", 10, TRUE);
440 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
443 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
446 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
449 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
452 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
455 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
458 * constant subs for IO
460 stash = gv_stashpvn("IO", 2, TRUE);
462 newCONSTSUB(stash,"EINPROGRESS", newSViv(EINPROGRESS));