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
64 static int not_here(const char *s) __attribute__noreturn__;
66 not_here(const char *s)
68 croak("%s not implemented on this architecture", s);
69 NORETURN_FUNCTION_END;
74 #define PerlIO_fileno(f) fileno(f)
78 io_blocking(pTHX_ InputStream f, int block)
80 #if defined(HAS_FCNTL)
86 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
94 # define O_NDELAY O_NONBLOCK
96 /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
97 * after a successful F_SETFL of an O_NONBLOCK. */
98 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
101 newmode &= ~O_NDELAY;
102 newmode |= O_NONBLOCK;
103 } else if (block > 0) {
104 newmode &= ~(O_NDELAY|O_NONBLOCK);
107 /* Not POSIX - better have O_NDELAY or we can't cope.
108 * for BSD-ish machines this is an acceptable alternative
109 * for SysV we can't tell "would block" from EOF but that is
112 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
116 } else if (block > 0) {
117 newmode &= ~O_NDELAY;
120 if (newmode != mode) {
121 const int ret = fcntl(PerlIO_fileno(f),F_SETFL,newmode);
130 unsigned long flags = !block;
131 /* ioctl claims to take char* but really needs a u_long sized buffer */
132 const int ret = ioctl(PerlIO_fileno(f), FIONBIO, (char*)&flags);
135 /* Win32 has no way to get the current blocking status of a socket.
136 * However, we don't want to just return undef, because there's no way
137 * to tell that the ioctl succeeded.
141 /* TODO: Perhaps set $! to ENOTSUP? */
150 io_pp_nextstate(pTHX)
153 COP *old_curcop = PL_curcop;
154 OP *next = PL_ppaddr[PL_op->op_type](aTHX);
155 PL_curcop = old_curcop;
160 io_ck_lineseq(pTHX_ OP *o)
162 OP *kid = cBINOPo->op_first;
163 for (; kid; kid = kid->op_sibling)
164 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
165 kid->op_ppaddr = io_pp_nextstate;
170 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
180 ST(0) = sv_newmortal();
181 if (PerlIO_getpos(handle, &pos) != 0) {
182 ST(0) = &PL_sv_undef;
185 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
188 ST(0) = sv_newmortal();
189 if (PerlIO_getpos(handle, ST(0)) != 0) {
190 ST(0) = &PL_sv_undef;
195 if (fgetpos(handle, &pos)) {
196 ST(0) = &PL_sv_undef;
198 # if PERL_VERSION >= 11
199 ST(0) = newSVpvn_flags((char*)&pos, sizeof(Fpos_t), SVs_TEMP);
201 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
208 ST(0) = &PL_sv_undef;
221 if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
222 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
229 RETVAL = PerlIO_setpos(handle, pos);
234 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
235 RETVAL = fsetpos(handle, (Fpos_t*)p);
250 MODULE = IO PACKAGE = IO::File PREFIX = f
253 new_tmpfile(packname = "IO::File")
254 const char * packname
260 fp = PerlIO_tmpfile();
264 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
266 (void) hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
267 if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
268 ST(0) = sv_2mortal(newRV((SV*)gv));
269 sv_bless(ST(0), gv_stashpv(packname, TRUE));
270 SvREFCNT_dec(gv); /* undo increment in newRV() */
273 ST(0) = &PL_sv_undef;
277 MODULE = IO PACKAGE = IO::Poll
285 const int nfd = (items - 1) / 2;
286 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
287 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
289 for(i=1, j=0 ; j < nfd ; j++) {
290 fds[j].fd = SvIV(ST(i));
292 fds[j].events = (short)SvIV(ST(i));
296 if((ret = poll(fds,nfd,timeout)) >= 0) {
297 for(i=1, j=0 ; j < nfd ; j++) {
298 sv_setiv(ST(i), fds[j].fd); i++;
299 sv_setiv(ST(i), fds[j].revents); i++;
305 not_here("IO::Poll::poll");
309 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
312 io_blocking(handle,blk=-1)
318 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
325 MODULE = IO PACKAGE = IO::Handle PREFIX = f
336 if ((SvIOK_notUV(c) && SvIV(c) < 0) || (SvNOK(c) && SvNV(c) < 0.0))
337 croak("Negative character number in ungetc()");
340 if (NATIVE_IS_INVARIANT(v) || (v <= 0xFF && !PerlIO_isutf8(handle)))
341 RETVAL = PerlIO_ungetc(handle, (int)v);
343 U8 buf[UTF8_MAXBYTES + 1], *end;
346 if (!PerlIO_isutf8(handle))
347 croak("Wide character number in ungetc()");
349 /* This doesn't warn for non-chars, surrogate, and
351 end = uvchr_to_utf8_flags(buf, v, 0);
353 if (PerlIO_unread(handle, &buf, len) == len)
359 RETVAL = ungetc((int)SvIV(c), handle);
375 RETVAL = PerlIO_error(handle);
377 RETVAL = ferror(handle);
392 PerlIO_clearerr(handle);
413 IoFLAGS(io) |= IOf_UNTAINT;
432 RETVAL = PerlIO_flush(handle);
434 RETVAL = Fflush(handle);
448 #ifdef PERLIO_IS_STDIO
450 char *buf = items == 2 && SvPOK(ST(1)) ?
451 sv_grow(ST(1), BUFSIZ) : 0;
455 not_here("IO::Handle::setbuf");
462 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
463 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
465 OutputStream handle = 0;
466 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
471 handle = IoOFP(sv_2io(ST(0)));
472 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
473 type = (int)SvIV(ST(2));
474 size = (int)SvIV(ST(3));
476 if (!handle) /* Try input stream. */
477 handle = IoIFP(sv_2io(ST(0)));
478 if (items == 4 && handle)
479 RETVAL = setvbuf(handle, buf, type, size);
486 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
496 OutputStream handle = NULL;
499 handle = IoOFP(sv_2io(arg));
501 handle = IoIFP(sv_2io(arg));
503 RETVAL = fsync(PerlIO_fileno(handle));
509 RETVAL = (SysRet) not_here("IO::Handle::sync");
515 _create_getline_subs(const char *code)
517 OP *(*io_old_ck_lineseq)(pTHX_ OP *) = PL_check[OP_LINESEQ];
518 PL_check[OP_LINESEQ] = io_ck_lineseq;
519 RETVAL = SvREFCNT_inc(eval_pv(code,FALSE));
520 PL_check[OP_LINESEQ] = io_old_ck_lineseq;
525 MODULE = IO PACKAGE = IO::Socket
535 fd = PerlIO_fileno(sock);
536 #ifdef HAS_SOCKATMARK
537 RETVAL = sockatmark(fd);
542 # if defined(NETWARE) || defined(WIN32)
543 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
545 if (ioctl(fd, SIOCATMARK, &flag) != 0)
549 not_here("IO::Socket::atmark");
562 * constant subs for IO::Poll
564 stash = gv_stashpvn("IO::Poll", 8, TRUE);
566 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
569 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
572 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
575 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
578 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
581 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
584 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
587 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
590 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
593 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
596 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
599 * constant subs for IO::Handle
601 stash = gv_stashpvn("IO::Handle", 10, TRUE);
603 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
606 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
609 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
612 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
615 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
618 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));