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);
125 unsigned long flags = block;
126 return ioctl(PerlIO_fileno(f), FIONBIO, &flags);
133 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
143 ST(0) = sv_newmortal();
144 if (PerlIO_getpos(handle, &pos) != 0) {
145 ST(0) = &PL_sv_undef;
148 sv_setpvn(ST(0), (char *)&pos, sizeof(Fpos_t));
151 ST(0) = sv_newmortal();
152 if (PerlIO_getpos(handle, ST(0)) != 0) {
153 ST(0) = &PL_sv_undef;
158 if (fgetpos(handle, &pos)) {
159 ST(0) = &PL_sv_undef;
161 ST(0) = sv_2mortal(newSVpvn((char*)&pos, sizeof(Fpos_t)));
167 ST(0) = &PL_sv_undef;
180 if (SvOK(pos) && (p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
181 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
188 RETVAL = PerlIO_setpos(handle, pos);
193 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
194 RETVAL = fsetpos(handle, (Fpos_t*)p);
209 MODULE = IO PACKAGE = IO::File PREFIX = f
212 new_tmpfile(packname = "IO::File")
213 const char * packname
219 fp = PerlIO_tmpfile();
223 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
225 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
226 if (gv && do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
227 ST(0) = sv_2mortal(newRV((SV*)gv));
228 sv_bless(ST(0), gv_stashpv(packname, TRUE));
229 SvREFCNT_dec(gv); /* undo increment in newRV() */
232 ST(0) = &PL_sv_undef;
236 MODULE = IO PACKAGE = IO::Poll
244 const int nfd = (items - 1) / 2;
245 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
246 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
248 for(i=1, j=0 ; j < nfd ; j++) {
249 fds[j].fd = SvIV(ST(i));
251 fds[j].events = (short)SvIV(ST(i));
255 if((ret = poll(fds,nfd,timeout)) >= 0) {
256 for(i=1, j=0 ; j < nfd ; j++) {
257 sv_setiv(ST(i), fds[j].fd); i++;
258 sv_setiv(ST(i), fds[j].revents); i++;
264 not_here("IO::Poll::poll");
268 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
271 io_blocking(handle,blk=-1)
277 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
284 MODULE = IO PACKAGE = IO::Handle PREFIX = f
293 RETVAL = PerlIO_ungetc(handle, c);
295 RETVAL = ungetc(c, handle);
310 RETVAL = PerlIO_error(handle);
312 RETVAL = ferror(handle);
327 PerlIO_clearerr(handle);
348 IoFLAGS(io) |= IOf_UNTAINT;
367 RETVAL = PerlIO_flush(handle);
369 RETVAL = Fflush(handle);
383 #ifdef PERLIO_IS_STDIO
385 char *buf = items == 2 && SvPOK(ST(1)) ?
386 sv_grow(ST(1), BUFSIZ) : 0;
390 not_here("IO::Handle::setbuf");
397 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
398 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
400 OutputStream handle = 0;
401 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
406 handle = IoOFP(sv_2io(ST(0)));
407 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
408 type = (int)SvIV(ST(2));
409 size = (int)SvIV(ST(3));
411 if (!handle) /* Try input stream. */
412 handle = IoIFP(sv_2io(ST(0)));
413 if (items == 4 && handle)
414 RETVAL = setvbuf(handle, buf, type, size);
421 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
433 RETVAL = fsync(PerlIO_fileno(handle));
439 RETVAL = (SysRet) not_here("IO::Handle::sync");
445 MODULE = IO PACKAGE = IO::Socket
455 fd = PerlIO_fileno(sock);
456 #ifdef HAS_SOCKATMARK
457 RETVAL = sockatmark(fd);
462 # if defined(NETWARE) || defined(WIN32)
463 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
465 if (ioctl(fd, SIOCATMARK, &flag) != 0)
469 not_here("IO::Socket::atmark");
482 * constant subs for IO::Poll
484 stash = gv_stashpvn("IO::Poll", 8, TRUE);
486 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
489 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
492 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
495 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
498 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
501 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
504 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
507 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
510 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
513 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
516 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
519 * constant subs for IO::Handle
521 stash = gv_stashpvn("IO::Handle", 10, TRUE);
523 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
526 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
529 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
532 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
535 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
538 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));