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
65 # define OpSIBLING(o) (o)->op_sibling
68 static int not_here(const char *s) __attribute__noreturn__;
70 not_here(const char *s)
72 croak("%s not implemented on this architecture", s);
73 NORETURN_FUNCTION_END;
76 #ifndef UVCHR_IS_INVARIANT /* For use with Perls without this macro */
78 # define UVCHR_IS_INVARIANT(cp) ((cp) < 128)
79 # elif (defined(NATIVE_IS_INVARIANT)) /* EBCDIC on old Perl */
80 # define UVCHR_IS_INVARIANT(cp) ((cp) < 256 && NATIVE_IS_INVARIANT(cp))
81 # elif defined(isASCII) /* EBCDIC on very old Perl */
82 /* In EBCDIC, the invariants are the code points corresponding to ASCII,
83 * plus all the controls. All but one EBCDIC control is below SPACE; it
84 * varies depending on the code page, determined by the ord of '^' */
85 # define UVCHR_IS_INVARIANT(cp) (isASCII(cp) \
87 || (('^' == 106) /* POSIX-BC */ \
89 : (cp) == 0xFF)) /* 1047 or 037 */
90 # else /* EBCDIC on very very old Perl */
91 /* This assumes isascii() is available, but that could be fixed by
92 * having the macro test for each printable ASCII char */
93 # define UVCHR_IS_INVARIANT(cp) (isascii(cp) \
95 || (('^' == 106) /* POSIX-BC */ \
97 : (cp) == 0xFF)) /* 1047 or 037 */
103 #define PerlIO_fileno(f) fileno(f)
107 io_blocking(pTHX_ InputStream f, int block)
110 #if defined(HAS_FCNTL)
116 fd = PerlIO_fileno(f);
121 RETVAL = fcntl(fd, F_GETFL, 0);
129 # define O_NDELAY O_NONBLOCK
131 /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
132 * after a successful F_SETFL of an O_NONBLOCK. */
133 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
136 newmode &= ~O_NDELAY;
137 newmode |= O_NONBLOCK;
138 } else if (block > 0) {
139 newmode &= ~(O_NDELAY|O_NONBLOCK);
142 /* Not POSIX - better have O_NDELAY or we can't cope.
143 * for BSD-ish machines this is an acceptable alternative
144 * for SysV we can't tell "would block" from EOF but that is
147 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
151 } else if (block > 0) {
152 newmode &= ~O_NDELAY;
155 if (newmode != mode) {
156 const int ret = fcntl(fd, F_SETFL, newmode);
165 unsigned long flags = !block;
166 /* ioctl claims to take char* but really needs a u_long sized buffer */
167 const int ret = ioctl(fd, FIONBIO, (char*)&flags);
170 /* Win32 has no way to get the current blocking status of a socket.
171 * However, we don't want to just return undef, because there's no way
172 * to tell that the ioctl succeeded.
176 /* TODO: Perhaps set $! to ENOTSUP? */
185 io_pp_nextstate(pTHX)
188 COP *old_curcop = PL_curcop;
189 OP *next = PL_ppaddr[PL_op->op_type](aTHX);
190 PL_curcop = old_curcop;
195 io_ck_lineseq(pTHX_ OP *o)
197 OP *kid = cBINOPo->op_first;
198 for (; kid; kid = OpSIBLING(kid))
199 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
200 kid->op_ppaddr = io_pp_nextstate;
205 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
215 ST(0) = sv_newmortal();
216 if (PerlIO_getpos(handle, &pos) != 0) {
217 ST(0) = &PL_sv_undef;
220 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
223 ST(0) = sv_newmortal();
224 if (PerlIO_getpos(handle, ST(0)) != 0) {
225 ST(0) = &PL_sv_undef;
230 if (fgetpos(handle, &pos)) {
231 ST(0) = &PL_sv_undef;
233 # if PERL_VERSION >= 11
234 ST(0) = newSVpvn_flags((char*)&pos, sizeof(Fpos_t), SVs_TEMP);
236 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
243 ST(0) = &PL_sv_undef;
256 if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
257 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
264 RETVAL = PerlIO_setpos(handle, pos);
269 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
270 RETVAL = fsetpos(handle, (Fpos_t*)p);
285 MODULE = IO PACKAGE = IO::File PREFIX = f
288 new_tmpfile(packname = "IO::File")
289 const char * packname
295 fp = PerlIO_tmpfile();
299 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
301 (void) hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
302 if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
303 ST(0) = sv_2mortal(newRV((SV*)gv));
304 sv_bless(ST(0), gv_stashpv(packname, TRUE));
305 SvREFCNT_dec(gv); /* undo increment in newRV() */
308 ST(0) = &PL_sv_undef;
312 MODULE = IO PACKAGE = IO::Poll
320 const int nfd = (items - 1) / 2;
321 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
322 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
324 for(i=1, j=0 ; j < nfd ; j++) {
325 fds[j].fd = SvIV(ST(i));
327 fds[j].events = (short)SvIV(ST(i));
331 if((ret = poll(fds,nfd,timeout)) >= 0) {
332 for(i=1, j=0 ; j < nfd ; j++) {
333 sv_setiv(ST(i), fds[j].fd); i++;
334 sv_setiv(ST(i), fds[j].revents); i++;
340 not_here("IO::Poll::poll");
344 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
347 io_blocking(handle,blk=-1)
353 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
360 MODULE = IO PACKAGE = IO::Handle PREFIX = f
371 if ((SvIOK_notUV(c) && SvIV(c) < 0) || (SvNOK(c) && SvNV(c) < 0.0))
372 croak("Negative character number in ungetc()");
375 if (UVCHR_IS_INVARIANT(v) || (v <= 0xFF && !PerlIO_isutf8(handle)))
376 RETVAL = PerlIO_ungetc(handle, (int)v);
378 U8 buf[UTF8_MAXBYTES + 1], *end;
381 if (!PerlIO_isutf8(handle))
382 croak("Wide character number in ungetc()");
384 /* This doesn't warn for non-chars, surrogate, and
386 end = uvchr_to_utf8_flags(buf, v, 0);
388 if ((Size_t)PerlIO_unread(handle, &buf, len) == len)
394 RETVAL = ungetc((int)SvIV(c), handle);
410 RETVAL = PerlIO_error(handle);
412 RETVAL = ferror(handle);
427 PerlIO_clearerr(handle);
448 IoFLAGS(io) |= IOf_UNTAINT;
467 RETVAL = PerlIO_flush(handle);
469 RETVAL = Fflush(handle);
483 #ifdef PERLIO_IS_STDIO
485 char *buf = items == 2 && SvPOK(ST(1)) ?
486 sv_grow(ST(1), BUFSIZ) : 0;
490 not_here("IO::Handle::setbuf");
497 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
498 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
500 OutputStream handle = 0;
501 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
506 handle = IoOFP(sv_2io(ST(0)));
507 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
508 type = (int)SvIV(ST(2));
509 size = (int)SvIV(ST(3));
511 if (!handle) /* Try input stream. */
512 handle = IoIFP(sv_2io(ST(0)));
513 if (items == 4 && handle)
514 RETVAL = setvbuf(handle, buf, type, size);
521 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
531 OutputStream handle = NULL;
534 handle = IoOFP(sv_2io(arg));
536 handle = IoIFP(sv_2io(arg));
538 int fd = PerlIO_fileno(handle);
550 RETVAL = (SysRet) not_here("IO::Handle::sync");
556 _create_getline_subs(const char *code)
558 OP *(*io_old_ck_lineseq)(pTHX_ OP *) = PL_check[OP_LINESEQ];
559 PL_check[OP_LINESEQ] = io_ck_lineseq;
560 RETVAL = SvREFCNT_inc(eval_pv(code,FALSE));
561 PL_check[OP_LINESEQ] = io_old_ck_lineseq;
566 MODULE = IO PACKAGE = IO::Socket
575 fd = PerlIO_fileno(sock);
580 #ifdef HAS_SOCKATMARK
582 RETVAL = sockatmark(fd);
588 # if defined(NETWARE) || defined(WIN32)
589 if (ioctl(fd, SIOCATMARK, (char*)&flag) != 0)
591 if (ioctl(fd, SIOCATMARK, &flag) != 0)
595 not_here("IO::Socket::atmark");
607 * constant subs for IO::Poll
609 stash = gv_stashpvn("IO::Poll", 8, TRUE);
611 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
614 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
617 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
620 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
623 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
626 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
629 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
632 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
635 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
638 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
641 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
644 * constant subs for IO::Handle
646 stash = gv_stashpvn("IO::Handle", 10, TRUE);
648 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
651 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
654 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
657 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
660 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
663 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));