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 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
33 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
39 croak("%s not implemented on this architecture", s);
45 #define PerlIO_fileno(f) fileno(f)
49 io_blocking(InputStream f, int block)
56 #if defined(HAS_FCNTL)
57 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
62 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
63 /* Ooops has O_NDELAY too - make sure we don't
64 * get SysV behaviour by mistake
66 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
68 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
70 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
71 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
75 else if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
77 mode &= ~(O_NONBLOCK | O_NDELAY);
78 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
84 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
86 if ((block == 0) && !(mode & O_NONBLOCK)) {
89 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
93 else if ((block > 0) && (mode & O_NONBLOCK)) {
96 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
102 /* Not POSIX - better have O_NDELAY or we can't cope.
103 * for BSD-ish machines this is an acceptable alternative
104 * for SysV we can't tell "would block" from EOF but that is
107 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
109 if ((block == 0) && !(mode & O_NDELAY)) {
112 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
116 else if ((block > 0) && (mode & O_NDELAY)) {
119 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
131 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
140 PerlIO_getpos(handle, &pos);
142 fgetpos(handle, &pos);
144 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
147 ST(0) = &PL_sv_undef;
158 if (handle && (p = SvPV(pos,len)) && len == sizeof(Fpos_t))
160 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
162 RETVAL = fsetpos(handle, (Fpos_t*)p);
171 MODULE = IO PACKAGE = IO::File PREFIX = f
174 new_tmpfile(packname = "IO::File")
181 fp = PerlIO_tmpfile();
185 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
186 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
187 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
188 ST(0) = sv_2mortal(newRV((SV*)gv));
189 sv_bless(ST(0), gv_stashpv(packname, TRUE));
190 SvREFCNT_dec(gv); /* undo increment in newRV() */
193 ST(0) = &PL_sv_undef;
197 MODULE = IO PACKAGE = IO::Poll
205 int nfd = (items - 1) / 2;
206 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
207 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
209 for(i=1, j=0 ; j < nfd ; j++) {
210 fds[j].fd = SvIV(ST(i));
212 fds[j].events = SvIV(ST(i));
216 if((ret = poll(fds,nfd,timeout)) >= 0) {
217 for(i=1, j=0 ; j < nfd ; j++) {
218 sv_setiv(ST(i), fds[j].fd); i++;
219 sv_setiv(ST(i), fds[j].revents); i++;
225 not_here("IO::Poll::poll");
229 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
232 io_blocking(handle,blk=-1)
238 int ret = io_blocking(handle, items == 1 ? -1 : blk ? 1 : 0);
245 MODULE = IO PACKAGE = IO::Handle PREFIX = f
255 RETVAL = PerlIO_ungetc(handle, c);
257 RETVAL = ungetc(c, handle);
272 RETVAL = PerlIO_error(handle);
274 RETVAL = ferror(handle);
289 PerlIO_clearerr(handle);
310 IoFLAGS(io) |= IOf_UNTAINT;
329 RETVAL = PerlIO_flush(handle);
331 RETVAL = Fflush(handle);
343 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
346 #ifdef PERLIO_IS_STDIO
349 not_here("IO::Handle::setbuf");
353 setvbuf(handle, buf, type, size)
355 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
359 /* Should check HAS_SETVBUF once Configure tests for that */
360 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF)
361 if (!handle) /* Try input stream. */
362 handle = IoIFP(sv_2io(ST(0)));
364 RETVAL = setvbuf(handle, buf, type, size);
370 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
382 RETVAL = fsync(PerlIO_fileno(handle));
388 RETVAL = (SysRet) not_here("IO::Handle::sync");
398 * constant subs for IO::Poll
400 stash = gv_stashpvn("IO::Poll", 8, TRUE);
402 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
405 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
408 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
411 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
414 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
417 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
420 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
423 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
426 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
429 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
432 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
435 * constant subs for IO::Handle
437 stash = gv_stashpvn("IO::Handle", 10, TRUE);
439 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
442 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
445 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
448 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
451 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
454 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
457 * constant subs for IO
459 stash = gv_stashpvn("IO", 2, TRUE);
461 newCONSTSUB(stash,"EINPROGRESS", newSViv(EINPROGRESS));