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.
9 #define PERL_NO_GET_CONTEXT
11 #define PERLIO_NOT_STDIO 1
18 #if defined(I_FCNTL) || defined(HAS_FCNTL)
24 # include <sys/sockio.h>
30 typedef PerlIO * InputStream;
31 typedef PerlIO * OutputStream;
33 #define PERLIO_IS_STDIO 1
35 typedef FILE * InputStream;
36 typedef FILE * OutputStream;
39 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
42 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
48 croak("%s not implemented on this architecture", s);
54 #define PerlIO_fileno(f) fileno(f)
58 io_blocking(InputStream f, int block)
65 #if defined(HAS_FCNTL)
66 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
71 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
72 /* Ooops has O_NDELAY too - make sure we don't
73 * get SysV behaviour by mistake. */
75 /* E.g. In UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
76 * after a successful F_SETFL of an O_NONBLOCK. */
77 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
80 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
82 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
83 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
88 if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
90 mode &= ~(O_NONBLOCK | O_NDELAY);
91 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
98 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
100 if ((block == 0) && !(mode & O_NONBLOCK)) {
103 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
107 else if ((block > 0) && (mode & O_NONBLOCK)) {
110 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
116 /* Not POSIX - better have O_NDELAY or we can't cope.
117 * for BSD-ish machines this is an acceptable alternative
118 * for SysV we can't tell "would block" from EOF but that is
121 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
123 if ((block == 0) && !(mode & O_NDELAY)) {
126 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
130 else if ((block > 0) && (mode & O_NDELAY)) {
133 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
145 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
153 ST(0) = sv_2mortal(newSV(0));
154 if (PerlIO_getpos(handle, ST(0)) != 0) {
155 ST(0) = &PL_sv_undef;
158 if (fgetpos(handle, &pos)) {
159 ST(0) = &PL_sv_undef;
161 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
166 ST(0) = &PL_sv_undef;
177 RETVAL = PerlIO_setpos(handle, pos);
181 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
182 RETVAL = fsetpos(handle, (Fpos_t*)p);
197 MODULE = IO PACKAGE = IO::File PREFIX = f
200 new_tmpfile(packname = "IO::File")
207 fp = PerlIO_tmpfile();
211 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
212 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
213 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
214 ST(0) = sv_2mortal(newRV((SV*)gv));
215 sv_bless(ST(0), gv_stashpv(packname, TRUE));
216 SvREFCNT_dec(gv); /* undo increment in newRV() */
219 ST(0) = &PL_sv_undef;
223 MODULE = IO PACKAGE = IO::Poll
231 int nfd = (items - 1) / 2;
232 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
233 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
235 for(i=1, j=0 ; j < nfd ; j++) {
236 fds[j].fd = SvIV(ST(i));
238 fds[j].events = SvIV(ST(i));
242 if((ret = poll(fds,nfd,timeout)) >= 0) {
243 for(i=1, j=0 ; j < nfd ; j++) {
244 sv_setiv(ST(i), fds[j].fd); i++;
245 sv_setiv(ST(i), fds[j].revents); i++;
251 not_here("IO::Poll::poll");
255 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
258 io_blocking(handle,blk=-1)
264 int ret = io_blocking(handle, items == 1 ? -1 : blk ? 1 : 0);
271 MODULE = IO PACKAGE = IO::Handle PREFIX = f
280 RETVAL = PerlIO_ungetc(handle, c);
282 RETVAL = ungetc(c, handle);
297 RETVAL = PerlIO_error(handle);
299 RETVAL = ferror(handle);
314 PerlIO_clearerr(handle);
335 IoFLAGS(io) |= IOf_UNTAINT;
354 RETVAL = PerlIO_flush(handle);
356 RETVAL = Fflush(handle);
370 #ifdef PERLIO_IS_STDIO
372 char *buf = items == 2 && SvPOK(ST(1)) ?
373 sv_grow(ST(1), BUFSIZ) : 0;
377 not_here("IO::Handle::setbuf");
384 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
385 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
387 OutputStream handle = 0;
388 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
393 handle = IoOFP(sv_2io(ST(0)));
394 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
395 type = (int)SvIV(ST(2));
396 size = (int)SvIV(ST(3));
398 if (!handle) /* Try input stream. */
399 handle = IoIFP(sv_2io(ST(0)));
400 if (items == 4 && handle)
401 RETVAL = setvbuf(handle, buf, type, size);
408 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
420 RETVAL = fsync(PerlIO_fileno(handle));
426 RETVAL = (SysRet) not_here("IO::Handle::sync");
432 MODULE = IO PACKAGE = IO::Socket
442 fd = PerlIO_fileno(sock);
443 #ifdef HAS_SOCKATMARK
444 RETVAL = sockatmark(fd);
449 # if defined(NETWARE) || defined(WIN32)
450 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
452 if (ioctl(fd, SIOCATMARK, &flag) != 0)
456 not_here("IO::Socket::atmark");
469 * constant subs for IO::Poll
471 stash = gv_stashpvn("IO::Poll", 8, TRUE);
473 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
476 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
479 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
482 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
485 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
488 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
491 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
494 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
497 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
500 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
503 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
506 * constant subs for IO::Handle
508 stash = gv_stashpvn("IO::Handle", 10, TRUE);
510 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
513 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
516 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
519 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
522 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
525 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));