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 static int not_here(const char *s) __attribute__noreturn__;
54 not_here(const char *s)
56 croak("%s not implemented on this architecture", s);
62 #define PerlIO_fileno(f) fileno(f)
66 io_blocking(pTHX_ InputStream f, int block)
68 #if defined(HAS_FCNTL)
74 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
82 # define O_NDELAY O_NONBLOCK
84 /* Note: UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
85 * after a successful F_SETFL of an O_NONBLOCK. */
86 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
90 newmode |= O_NONBLOCK;
91 } else if (block > 0) {
92 newmode &= ~(O_NDELAY|O_NONBLOCK);
95 /* Not POSIX - better have O_NDELAY or we can't cope.
96 * for BSD-ish machines this is an acceptable alternative
97 * for SysV we can't tell "would block" from EOF but that is
100 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
104 } else if (block > 0) {
105 newmode &= ~O_NDELAY;
108 if (newmode != mode) {
109 const int ret = fcntl(PerlIO_fileno(f),F_SETFL,newmode);
120 MODULE = IO PACKAGE = IO::Seekable PREFIX = f
128 ST(0) = sv_2mortal(newSV(0));
129 if (PerlIO_getpos(handle, ST(0)) != 0) {
130 ST(0) = &PL_sv_undef;
133 if (fgetpos(handle, &pos)) {
134 ST(0) = &PL_sv_undef;
136 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
141 ST(0) = &PL_sv_undef;
152 RETVAL = PerlIO_setpos(handle, pos);
156 if ((p = SvPV(pos,len)) && len == sizeof(Fpos_t)) {
157 RETVAL = fsetpos(handle, (Fpos_t*)p);
172 MODULE = IO PACKAGE = IO::File PREFIX = f
175 new_tmpfile(packname = "IO::File")
176 const char * packname
182 fp = PerlIO_tmpfile();
186 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
187 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
188 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
189 ST(0) = sv_2mortal(newRV((SV*)gv));
190 sv_bless(ST(0), gv_stashpv(packname, TRUE));
191 SvREFCNT_dec(gv); /* undo increment in newRV() */
194 ST(0) = &PL_sv_undef;
198 MODULE = IO PACKAGE = IO::Poll
206 const int nfd = (items - 1) / 2;
207 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
208 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
210 for(i=1, j=0 ; j < nfd ; j++) {
211 fds[j].fd = SvIV(ST(i));
213 fds[j].events = (short)SvIV(ST(i));
217 if((ret = poll(fds,nfd,timeout)) >= 0) {
218 for(i=1, j=0 ; j < nfd ; j++) {
219 sv_setiv(ST(i), fds[j].fd); i++;
220 sv_setiv(ST(i), fds[j].revents); i++;
226 not_here("IO::Poll::poll");
230 MODULE = IO PACKAGE = IO::Handle PREFIX = io_
233 io_blocking(handle,blk=-1)
239 const int ret = io_blocking(aTHX_ handle, items == 1 ? -1 : blk ? 1 : 0);
246 MODULE = IO PACKAGE = IO::Handle PREFIX = f
255 RETVAL = PerlIO_ungetc(handle, c);
257 RETVAL = ungetc(c, handle);
272 RETVAL = PerlIO_error(handle);
274 RETVAL = ferror(handle);
289 PerlIO_clearerr(handle);
310 IoFLAGS(io) |= IOf_UNTAINT;
329 RETVAL = PerlIO_flush(handle);
331 RETVAL = Fflush(handle);
345 #ifdef PERLIO_IS_STDIO
347 char *buf = items == 2 && SvPOK(ST(1)) ?
348 sv_grow(ST(1), BUFSIZ) : 0;
352 not_here("IO::Handle::setbuf");
359 Perl_croak(aTHX_ "Usage: IO::Handle::setvbuf(handle, buf, type, size)");
360 #if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
362 OutputStream handle = 0;
363 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
368 handle = IoOFP(sv_2io(ST(0)));
369 buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
370 type = (int)SvIV(ST(2));
371 size = (int)SvIV(ST(3));
373 if (!handle) /* Try input stream. */
374 handle = IoIFP(sv_2io(ST(0)));
375 if (items == 4 && handle)
376 RETVAL = setvbuf(handle, buf, type, size);
383 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
395 RETVAL = fsync(PerlIO_fileno(handle));
401 RETVAL = (SysRet) not_here("IO::Handle::sync");
407 MODULE = IO PACKAGE = IO::Socket
417 fd = PerlIO_fileno(sock);
418 #ifdef HAS_SOCKATMARK
419 RETVAL = sockatmark(fd);
424 # if defined(NETWARE) || defined(WIN32)
425 if (ioctl(fd, SIOCATMARK, (void*)&flag) != 0)
427 if (ioctl(fd, SIOCATMARK, &flag) != 0)
431 not_here("IO::Socket::atmark");
444 * constant subs for IO::Poll
446 stash = gv_stashpvn("IO::Poll", 8, TRUE);
448 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
451 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
454 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
457 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
460 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
463 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
466 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
469 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
472 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
475 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
478 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
481 * constant subs for IO::Handle
483 stash = gv_stashpvn("IO::Handle", 10, TRUE);
485 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
488 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
491 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
494 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
497 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
500 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));