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>
29 #ifdef MACOS_TRADITIONAL
30 #define PERLIO_IS_STDIO 1
33 #define setvbuf _stdsetvbuf
34 #define setbuf(f,b) ( __sf_setbuf(f,b) )
37 typedef PerlIO * InputStream;
38 typedef PerlIO * OutputStream;
40 #define PERLIO_IS_STDIO 1
42 typedef FILE * InputStream;
43 typedef FILE * OutputStream;
46 #define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
49 #define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
55 croak("%s not implemented on this architecture", s);
61 #define PerlIO_fileno(f) fileno(f)
65 io_blocking(pTHX_ InputStream f, int block)
72 #if defined(HAS_FCNTL)
73 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
78 #if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
79 /* Ooops has O_NDELAY too - make sure we don't
80 * get SysV behaviour by mistake. */
82 /* E.g. In UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
83 * after a successful F_SETFL of an O_NONBLOCK. */
84 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
87 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
89 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
90 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
95 if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
97 mode &= ~(O_NONBLOCK | O_NDELAY);
98 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
105 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
107 if ((block == 0) && !(mode & O_NONBLOCK)) {
110 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
114 else if ((block > 0) && (mode & O_NONBLOCK)) {
117 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
123 /* Not POSIX - better have O_NDELAY or we can't cope.
124 * for BSD-ish machines this is an acceptable alternative
125 * for SysV we can't tell "would block" from EOF but that is
128 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
130 if ((block == 0) && !(mode & O_NDELAY)) {
133 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
137 else if ((block > 0) && (mode & O_NDELAY)) {
140 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
152 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
160 ST(0) = sv_2mortal(newSV(0));
161 if (PerlIO_getpos(handle, ST(0)) != 0) {
162 ST(0) = &PL_sv_undef;
165 if (fgetpos(handle, &pos)) {
166 ST(0) = &PL_sv_undef;
168 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
173 ST(0) = &PL_sv_undef;
184 RETVAL = PerlIO_setpos(handle, pos);
188 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
189 RETVAL = fsetpos(handle, (Fpos_t*)p);
204 MODULE = IO PACKAGE = IO::File PREFIX = f
207 new_tmpfile(packname = "IO::File")
214 fp = PerlIO_tmpfile();
218 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
219 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
220 if (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() */
226 ST(0) = &PL_sv_undef;
230 MODULE = IO PACKAGE = IO::Poll
238 int nfd = (items - 1) / 2;
239 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
240 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
242 for(i=1, j=0 ; j < nfd ; j++) {
243 fds[j].fd = SvIV(ST(i));
245 fds[j].events = SvIV(ST(i));
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++;
258 not_here("IO::Poll::poll");
262 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
265 io_blocking(handle,blk=-1)
271 int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
278 MODULE = IO PACKAGE = IO::Handle PREFIX = f
287 RETVAL = PerlIO_ungetc(handle, c);
289 RETVAL = ungetc(c, handle);
304 RETVAL = PerlIO_error(handle);
306 RETVAL = ferror(handle);
321 PerlIO_clearerr(handle);
342 IoFLAGS(io) |= IOf_UNTAINT;
361 RETVAL = PerlIO_flush(handle);
363 RETVAL = Fflush(handle);
377 #ifdef PERLIO_IS_STDIO
379 char *buf = items == 2 && SvPOK(ST(1)) ?
380 sv_grow(ST(1), BUFSIZ) : 0;
384 not_here("IO::Handle::setbuf");
391 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
392 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
394 OutputStream handle = 0;
395 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
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));
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);
415 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
427 RETVAL = fsync(PerlIO_fileno(handle));
433 RETVAL = (SysRet) not_here("IO::Handle::sync");
439 MODULE = IO PACKAGE = IO::Socket
449 fd = PerlIO_fileno(sock);
450 #ifdef HAS_SOCKATMARK
451 RETVAL = sockatmark(fd);
456 # if defined(NETWARE) || defined(WIN32)
457 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
459 if (ioctl(fd, SIOCATMARK, &flag) != 0)
463 not_here("IO::Socket::atmark");
476 * constant subs for IO::Poll
478 stash = gv_stashpvn("IO::Poll", 8, TRUE);
480 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
483 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
486 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
489 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
492 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
495 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
498 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
501 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
504 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
507 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
510 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
513 * constant subs for IO::Handle
515 stash = gv_stashpvn("IO::Handle", 10, TRUE);
517 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
520 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
523 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
526 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
529 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
532 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));