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 #if defined(MACOS_TRADITIONAL) && defined(USE_SFIO)
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)
52 #ifndef __attribute__noreturn__
53 # define __attribute__noreturn__
56 #ifndef NORETURN_FUNCTION_END
57 # define NORETURN_FUNCTION_END /* NOT REACHED */ return 0
60 static int not_here(const char *s) __attribute__noreturn__;
62 not_here(const char *s)
64 croak("%s not implemented on this architecture", s);
65 NORETURN_FUNCTION_END;
70 #define PerlIO_fileno(f) fileno(f)
74 io_blocking(pTHX_ InputStream f, int block)
76 #if defined(HAS_FCNTL)
82 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
90 # define O_NDELAY O_NONBLOCK
92 /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
93 * after a successful F_SETFL of an O_NONBLOCK. */
94 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
98 newmode |= O_NONBLOCK;
99 } else if (block > 0) {
100 newmode &= ~(O_NDELAY|O_NONBLOCK);
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;
112 } else if (block > 0) {
113 newmode &= ~O_NDELAY;
116 if (newmode != mode) {
117 const int ret = fcntl(PerlIO_fileno(f),F_SETFL,newmode);
128 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
138 ST(0) = sv_newmortal();
139 if (PerlIO_getpos(handle, &pos) != 0) {
140 ST(0) = &PL_sv_undef;
143 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
146 ST(0) = sv_newmortal();
147 if (PerlIO_getpos(handle, ST(0)) != 0) {
148 ST(0) = &PL_sv_undef;
153 if (fgetpos(handle, &pos)) {
154 ST(0) = &PL_sv_undef;
156 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
162 ST(0) = &PL_sv_undef;
175 if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
176 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
183 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));
220 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
221 if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
222 ST(0) = sv_2mortal(newRV((SV*)gv));
223 sv_bless(ST(0), gv_stashpv(packname, TRUE));
224 SvREFCNT_dec(gv); /* undo increment in newRV() */
227 ST(0) = &PL_sv_undef;
231 MODULE = IO PACKAGE = IO::Poll
239 const int nfd = (items - 1) / 2;
240 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
241 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
243 for(i=1, j=0 ; j < nfd ; j++) {
244 fds[j].fd = SvIV(ST(i));
246 fds[j].events = (short)SvIV(ST(i));
250 if((ret = poll(fds,nfd,timeout)) >= 0) {
251 for(i=1, j=0 ; j < nfd ; j++) {
252 sv_setiv(ST(i), fds[j].fd); i++;
253 sv_setiv(ST(i), fds[j].revents); i++;
259 not_here("IO::Poll::poll");
263 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
266 io_blocking(handle,blk=-1)
272 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
279 MODULE = IO PACKAGE = IO::Handle PREFIX = f
288 RETVAL = PerlIO_ungetc(handle, c);
290 RETVAL = ungetc(c, handle);
305 RETVAL = PerlIO_error(handle);
307 RETVAL = ferror(handle);
322 PerlIO_clearerr(handle);
343 IoFLAGS(io) |= IOf_UNTAINT;
362 RETVAL = PerlIO_flush(handle);
364 RETVAL = Fflush(handle);
378 #ifdef PERLIO_IS_STDIO
380 char *buf = items == 2 && SvPOK(ST(1)) ?
381 sv_grow(ST(1), BUFSIZ) : 0;
385 not_here("IO::Handle::setbuf");
392 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
393 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
395 OutputStream handle = 0;
396 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
401 handle = IoOFP(sv_2io(ST(0)));
402 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
403 type = (int)SvIV(ST(2));
404 size = (int)SvIV(ST(3));
406 if (!handle) /* Try input stream. */
407 handle = IoIFP(sv_2io(ST(0)));
408 if (items == 4 && handle)
409 RETVAL = setvbuf(handle, buf, type, size);
416 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
428 RETVAL = fsync(PerlIO_fileno(handle));
434 RETVAL = (SysRet) not_here("IO::Handle::sync");
440 MODULE = IO PACKAGE = IO::Socket
450 fd = PerlIO_fileno(sock);
451 #ifdef HAS_SOCKATMARK
452 RETVAL = sockatmark(fd);
457 # if defined(NETWARE) || defined(WIN32)
458 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
460 if (ioctl(fd, SIOCATMARK, &flag) != 0)
464 not_here("IO::Socket::atmark");
477 * constant subs for IO::Poll
479 stash = gv_stashpvn("IO::Poll", 8, TRUE);
481 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
484 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
487 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
490 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
493 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
496 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
499 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
502 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
505 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
508 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
511 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
514 * constant subs for IO::Handle
516 stash = gv_stashpvn("IO::Handle", 10, TRUE);
518 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
521 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
524 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
527 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
530 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
533 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));