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 = sv_2mortal(NEWSV(999,nfd * sizeof(struct pollfd)));
322 /* We should pass _some_ valid pointer even if nfd is zero, but it
323 * doesn't matter what it is, since we're telling it to not check any fds.
325 struct pollfd *fds = nfd ? (struct pollfd *)SvPVX(tmpsv) : (struct pollfd *)tmpsv;
327 for(i=1, j=0 ; j < nfd ; j++) {
328 fds[j].fd = SvIV(ST(i));
330 fds[j].events = (short)SvIV(ST(i));
334 if((ret = poll(fds,nfd,timeout)) >= 0) {
335 for(i=1, j=0 ; j < nfd ; j++) {
336 sv_setiv(ST(i), fds[j].fd); i++;
337 sv_setiv(ST(i), fds[j].revents); i++;
342 not_here("IO::Poll::poll");
346 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
349 io_blocking(handle,blk=-1)
355 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
362 MODULE = IO PACKAGE = IO::Handle PREFIX = f
373 if ((SvIOK_notUV(c) && SvIV(c) < 0) || (SvNOK(c) && SvNV(c) < 0.0))
374 croak("Negative character number in ungetc()");
377 if (UVCHR_IS_INVARIANT(v) || (v <= 0xFF && !PerlIO_isutf8(handle)))
378 RETVAL = PerlIO_ungetc(handle, (int)v);
380 U8 buf[UTF8_MAXBYTES + 1], *end;
383 if (!PerlIO_isutf8(handle))
384 croak("Wide character number in ungetc()");
386 /* This doesn't warn for non-chars, surrogate, and
388 end = uvchr_to_utf8_flags(buf, v, 0);
390 if ((Size_t)PerlIO_unread(handle, &buf, len) == len)
396 RETVAL = ungetc((int)SvIV(c), handle);
412 RETVAL = PerlIO_error(handle);
414 RETVAL = ferror(handle);
429 PerlIO_clearerr(handle);
450 IoFLAGS(io) |= IOf_UNTAINT;
469 RETVAL = PerlIO_flush(handle);
471 RETVAL = Fflush(handle);
485 #ifdef PERLIO_IS_STDIO
487 char *buf = items == 2 && SvPOK(ST(1)) ?
488 sv_grow(ST(1), BUFSIZ) : 0;
492 not_here("IO::Handle::setbuf");
499 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
500 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
502 OutputStream handle = 0;
503 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
508 handle = IoOFP(sv_2io(ST(0)));
509 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
510 type = (int)SvIV(ST(2));
511 size = (int)SvIV(ST(3));
513 if (!handle) /* Try input stream. */
514 handle = IoIFP(sv_2io(ST(0)));
515 if (items == 4 && handle)
516 RETVAL = setvbuf(handle, buf, type, size);
523 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
533 OutputStream handle = NULL;
536 handle = IoOFP(sv_2io(arg));
538 handle = IoIFP(sv_2io(arg));
540 int fd = PerlIO_fileno(handle);
552 RETVAL = (SysRet) not_here("IO::Handle::sync");
558 _create_getline_subs(const char *code)
560 OP *(*io_old_ck_lineseq)(pTHX_ OP *) = PL_check[OP_LINESEQ];
561 PL_check[OP_LINESEQ] = io_ck_lineseq;
562 RETVAL = SvREFCNT_inc(eval_pv(code,FALSE));
563 PL_check[OP_LINESEQ] = io_old_ck_lineseq;
568 MODULE = IO PACKAGE = IO::Socket
577 fd = PerlIO_fileno(sock);
582 #ifdef HAS_SOCKATMARK
584 RETVAL = sockatmark(fd);
590 # if defined(NETWARE) || defined(WIN32)
591 if (ioctl(fd, SIOCATMARK, (char*)&flag) != 0)
593 if (ioctl(fd, SIOCATMARK, &flag) != 0)
597 not_here("IO::Socket::atmark");
609 * constant subs for IO::Poll
611 stash = gv_stashpvn("IO::Poll", 8, TRUE);
613 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
616 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
619 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
622 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
625 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
628 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
631 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
634 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
637 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
640 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
643 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
646 * constant subs for IO::Handle
648 stash = gv_stashpvn("IO::Handle", 10, TRUE);
650 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
653 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
656 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
659 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
662 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
665 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));