integrate cfgperl changes#6174..6203 into mainline (first of several)
[p5sagit/p5-mst-13.2.git] / ext / IO / IO.xs
CommitLineData
cf7fe8a2 1/*
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.
5 */
6
c5be433b 7#define PERL_NO_GET_CONTEXT
8add82fc 8#include "EXTERN.h"
760ac839 9#define PERLIO_NOT_STDIO 1
8add82fc 10#include "perl.h"
11#include "XSUB.h"
cf7fe8a2 12#include "poll.h"
8add82fc 13#ifdef I_UNISTD
14# include <unistd.h>
15#endif
cf7fe8a2 16#if defined(I_FCNTL) || defined(HAS_FCNTL)
760ac839 17# include <fcntl.h>
18#endif
8add82fc 19
2a0cf753 20#ifdef PerlIO
8add82fc 21typedef int SysRet;
760ac839 22typedef PerlIO * InputStream;
23typedef PerlIO * OutputStream;
2a0cf753 24#else
25#define PERLIO_IS_STDIO 1
26typedef int SysRet;
27typedef FILE * InputStream;
28typedef FILE * OutputStream;
29#endif
8add82fc 30
cceca5ed 31#define MY_start_subparse(fmt,flags) start_subparse(fmt,flags)
cf7fe8a2 32
33#ifndef gv_stashpvn
34#define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
35#endif
36
8add82fc 37static int
a1ea39dc 38not_here(char *s)
8add82fc 39{
40 croak("%s not implemented on this architecture", s);
41 return -1;
42}
43
cf7fe8a2 44
45#ifndef PerlIO
46#define PerlIO_fileno(f) fileno(f)
8add82fc 47#endif
cf7fe8a2 48
49static int
a1ea39dc 50io_blocking(InputStream f, int block)
cf7fe8a2 51{
52 int RETVAL;
53 if(!f) {
54 errno = EBADF;
55 return -1;
56 }
57#if defined(HAS_FCNTL)
58 RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
59 if (RETVAL >= 0) {
60 int mode = RETVAL;
61#ifdef O_NONBLOCK
62 /* POSIX style */
63#if defined(O_NDELAY) && O_NDELAY != O_NONBLOCK
64 /* Ooops has O_NDELAY too - make sure we don't
6fd254a4 65 * get SysV behaviour by mistake. */
cf7fe8a2 66
6fd254a4 67 /* E.g. In UNICOS and UNICOS/mk a F_GETFL returns an O_NDELAY
68 * after a successful F_SETFL of an O_NONBLOCK. */
69 RETVAL = RETVAL & (O_NONBLOCK | O_NDELAY) ? 0 : 1;
70
71 if (block >= 0) {
72 if ((mode & O_NDELAY) || ((block == 0) && !(mode & O_NONBLOCK))) {
73 int ret;
74 mode = (mode & ~O_NDELAY) | O_NONBLOCK;
75 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
76 if(ret < 0)
77 RETVAL = ret;
78 }
79 else
80 if ((mode & O_NDELAY) || ((block > 0) && (mode & O_NONBLOCK))) {
81 int ret;
82 mode &= ~(O_NONBLOCK | O_NDELAY);
83 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
84 if(ret < 0)
85 RETVAL = ret;
86 }
cf7fe8a2 87 }
8add82fc 88#else
cf7fe8a2 89 /* Standard POSIX */
90 RETVAL = RETVAL & O_NONBLOCK ? 0 : 1;
91
92 if ((block == 0) && !(mode & O_NONBLOCK)) {
93 int ret;
94 mode |= O_NONBLOCK;
95 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
96 if(ret < 0)
97 RETVAL = ret;
98 }
99 else if ((block > 0) && (mode & O_NONBLOCK)) {
100 int ret;
101 mode &= ~O_NONBLOCK;
102 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
103 if(ret < 0)
104 RETVAL = ret;
105 }
106#endif
8add82fc 107#else
cf7fe8a2 108 /* Not POSIX - better have O_NDELAY or we can't cope.
109 * for BSD-ish machines this is an acceptable alternative
110 * for SysV we can't tell "would block" from EOF but that is
111 * the way SysV is...
112 */
113 RETVAL = RETVAL & O_NDELAY ? 0 : 1;
114
115 if ((block == 0) && !(mode & O_NDELAY)) {
116 int ret;
117 mode |= O_NDELAY;
118 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
119 if(ret < 0)
120 RETVAL = ret;
121 }
122 else if ((block > 0) && (mode & O_NDELAY)) {
123 int ret;
124 mode &= ~O_NDELAY;
125 ret = fcntl(PerlIO_fileno(f),F_SETFL,mode);
126 if(ret < 0)
127 RETVAL = ret;
128 }
8add82fc 129#endif
cf7fe8a2 130 }
131 return RETVAL;
8add82fc 132#else
cf7fe8a2 133 return -1;
8add82fc 134#endif
8add82fc 135}
136
8add82fc 137MODULE = IO PACKAGE = IO::Seekable PREFIX = f
138
139SV *
140fgetpos(handle)
141 InputStream handle
142 CODE:
8add82fc 143 if (handle) {
144 Fpos_t pos;
2a0cf753 145#ifdef PerlIO
760ac839 146 PerlIO_getpos(handle, &pos);
2a0cf753 147#else
148 fgetpos(handle, &pos);
149#endif
8add82fc 150 ST(0) = sv_2mortal(newSVpv((char*)&pos, sizeof(Fpos_t)));
151 }
152 else {
a1ea39dc 153 ST(0) = &PL_sv_undef;
8add82fc 154 errno = EINVAL;
155 }
8add82fc 156
157SysRet
158fsetpos(handle, pos)
159 InputStream handle
160 SV * pos
161 CODE:
a1ea39dc 162 char *p;
163 STRLEN len;
164 if (handle && (p = SvPV(pos,len)) && len == sizeof(Fpos_t))
2a0cf753 165#ifdef PerlIO
a1ea39dc 166 RETVAL = PerlIO_setpos(handle, (Fpos_t*)p);
2a0cf753 167#else
a1ea39dc 168 RETVAL = fsetpos(handle, (Fpos_t*)p);
2a0cf753 169#endif
8add82fc 170 else {
171 RETVAL = -1;
172 errno = EINVAL;
173 }
8add82fc 174 OUTPUT:
175 RETVAL
176
177MODULE = IO PACKAGE = IO::File PREFIX = f
178
a375a877 179SV *
8add82fc 180new_tmpfile(packname = "IO::File")
181 char * packname
a375a877 182 PREINIT:
183 OutputStream fp;
184 GV *gv;
8add82fc 185 CODE:
2a0cf753 186#ifdef PerlIO
a375a877 187 fp = PerlIO_tmpfile();
2a0cf753 188#else
a375a877 189 fp = tmpfile();
2a0cf753 190#endif
a375a877 191 gv = (GV*)SvREFCNT_inc(newGVgen(packname));
192 hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);
193 if (do_open(gv, "+>&", 3, FALSE, 0, 0, fp)) {
6d5cdeed 194 ST(0) = sv_2mortal(newRV((SV*)gv));
a375a877 195 sv_bless(ST(0), gv_stashpv(packname, TRUE));
cf7fe8a2 196 SvREFCNT_dec(gv); /* undo increment in newRV() */
a375a877 197 }
198 else {
a1ea39dc 199 ST(0) = &PL_sv_undef;
a375a877 200 SvREFCNT_dec(gv);
201 }
8add82fc 202
cf7fe8a2 203MODULE = IO PACKAGE = IO::Poll
204
205void
206_poll(timeout,...)
207 int timeout;
208PPCODE:
209{
210#ifdef HAS_POLL
211 int nfd = (items - 1) / 2;
212 SV *tmpsv = NEWSV(999,nfd * sizeof(struct pollfd));
213 struct pollfd *fds = (struct pollfd *)SvPVX(tmpsv);
214 int i,j,ret;
215 for(i=1, j=0 ; j < nfd ; j++) {
216 fds[j].fd = SvIV(ST(i));
217 i++;
218 fds[j].events = SvIV(ST(i));
219 i++;
220 fds[j].revents = 0;
221 }
222 if((ret = poll(fds,nfd,timeout)) >= 0) {
223 for(i=1, j=0 ; j < nfd ; j++) {
224 sv_setiv(ST(i), fds[j].fd); i++;
225 sv_setiv(ST(i), fds[j].revents); i++;
226 }
227 }
228 SvREFCNT_dec(tmpsv);
229 XSRETURN_IV(ret);
230#else
231 not_here("IO::Poll::poll");
232#endif
233}
234
235MODULE = IO PACKAGE = IO::Handle PREFIX = io_
236
237void
238io_blocking(handle,blk=-1)
239 InputStream handle
240 int blk
241PROTOTYPE: $;$
242CODE:
243{
244 int ret = io_blocking(handle, items == 1 ? -1 : blk ? 1 : 0);
245 if(ret >= 0)
246 XSRETURN_IV(ret);
247 else
248 XSRETURN_UNDEF;
249}
250
8add82fc 251MODULE = IO PACKAGE = IO::Handle PREFIX = f
252
8add82fc 253
254int
255ungetc(handle, c)
256 InputStream handle
257 int c
258 CODE:
259 if (handle)
2a0cf753 260#ifdef PerlIO
760ac839 261 RETVAL = PerlIO_ungetc(handle, c);
2a0cf753 262#else
263 RETVAL = ungetc(c, handle);
264#endif
8add82fc 265 else {
266 RETVAL = -1;
267 errno = EINVAL;
268 }
269 OUTPUT:
270 RETVAL
271
272int
273ferror(handle)
274 InputStream handle
275 CODE:
276 if (handle)
2a0cf753 277#ifdef PerlIO
760ac839 278 RETVAL = PerlIO_error(handle);
2a0cf753 279#else
280 RETVAL = ferror(handle);
281#endif
282 else {
283 RETVAL = -1;
284 errno = EINVAL;
285 }
286 OUTPUT:
287 RETVAL
288
289int
290clearerr(handle)
291 InputStream handle
292 CODE:
293 if (handle) {
294#ifdef PerlIO
295 PerlIO_clearerr(handle);
296#else
297 clearerr(handle);
298#endif
299 RETVAL = 0;
300 }
8add82fc 301 else {
302 RETVAL = -1;
59629a13 303 errno = EINVAL;
304 }
305 OUTPUT:
306 RETVAL
307
308int
309untaint(handle)
310 SV * handle
311 CODE:
7a4c00b4 312#ifdef IOf_UNTAINT
59629a13 313 IO * io;
314 io = sv_2io(handle);
315 if (io) {
316 IoFLAGS(io) |= IOf_UNTAINT;
317 RETVAL = 0;
318 }
319 else {
7a4c00b4 320#endif
59629a13 321 RETVAL = -1;
8add82fc 322 errno = EINVAL;
7a4c00b4 323#ifdef IOf_UNTAINT
8add82fc 324 }
7a4c00b4 325#endif
8add82fc 326 OUTPUT:
327 RETVAL
328
329SysRet
330fflush(handle)
331 OutputStream handle
332 CODE:
333 if (handle)
2a0cf753 334#ifdef PerlIO
760ac839 335 RETVAL = PerlIO_flush(handle);
2a0cf753 336#else
337 RETVAL = Fflush(handle);
338#endif
8add82fc 339 else {
340 RETVAL = -1;
341 errno = EINVAL;
342 }
343 OUTPUT:
344 RETVAL
345
346void
347setbuf(handle, buf)
348 OutputStream handle
349 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), BUFSIZ) : 0;
350 CODE:
351 if (handle)
760ac839 352#ifdef PERLIO_IS_STDIO
8add82fc 353 setbuf(handle, buf);
760ac839 354#else
355 not_here("IO::Handle::setbuf");
356#endif
8add82fc 357
358SysRet
359setvbuf(handle, buf, type, size)
360 OutputStream handle
361 char * buf = SvPOK(ST(1)) ? sv_grow(ST(1), SvIV(ST(3))) : 0;
362 int type
363 int size
364 CODE:
1eeb0f31 365#if defined(PERLIO_IS_STDIO) && defined(_IOFBF) && defined(HAS_SETVBUF)
d924de76 366 if (!handle) /* Try input stream. */
367 handle = IoIFP(sv_2io(ST(0)));
8add82fc 368 if (handle)
369 RETVAL = setvbuf(handle, buf, type, size);
370 else {
371 RETVAL = -1;
372 errno = EINVAL;
373 }
374#else
61839fa9 375 RETVAL = (SysRet) not_here("IO::Handle::setvbuf");
760ac839 376#endif
8add82fc 377 OUTPUT:
378 RETVAL
379
380
cf7fe8a2 381SysRet
382fsync(handle)
383 OutputStream handle
384 CODE:
385#ifdef HAS_FSYNC
386 if(handle)
387 RETVAL = fsync(PerlIO_fileno(handle));
388 else {
389 RETVAL = -1;
390 errno = EINVAL;
391 }
392#else
393 RETVAL = (SysRet) not_here("IO::Handle::sync");
394#endif
395 OUTPUT:
396 RETVAL
397
398
399BOOT:
400{
401 HV *stash;
402 /*
403 * constant subs for IO::Poll
404 */
405 stash = gv_stashpvn("IO::Poll", 8, TRUE);
406#ifdef POLLIN
407 newCONSTSUB(stash,"POLLIN",newSViv(POLLIN));
408#endif
409#ifdef POLLPRI
410 newCONSTSUB(stash,"POLLPRI", newSViv(POLLPRI));
411#endif
412#ifdef POLLOUT
413 newCONSTSUB(stash,"POLLOUT", newSViv(POLLOUT));
414#endif
415#ifdef POLLRDNORM
416 newCONSTSUB(stash,"POLLRDNORM", newSViv(POLLRDNORM));
417#endif
418#ifdef POLLWRNORM
419 newCONSTSUB(stash,"POLLWRNORM", newSViv(POLLWRNORM));
420#endif
421#ifdef POLLRDBAND
422 newCONSTSUB(stash,"POLLRDBAND", newSViv(POLLRDBAND));
423#endif
424#ifdef POLLWRBAND
425 newCONSTSUB(stash,"POLLWRBAND", newSViv(POLLWRBAND));
426#endif
427#ifdef POLLNORM
428 newCONSTSUB(stash,"POLLNORM", newSViv(POLLNORM));
429#endif
430#ifdef POLLERR
431 newCONSTSUB(stash,"POLLERR", newSViv(POLLERR));
432#endif
433#ifdef POLLHUP
434 newCONSTSUB(stash,"POLLHUP", newSViv(POLLHUP));
435#endif
436#ifdef POLLNVAL
437 newCONSTSUB(stash,"POLLNVAL", newSViv(POLLNVAL));
438#endif
439 /*
440 * constant subs for IO::Handle
441 */
442 stash = gv_stashpvn("IO::Handle", 10, TRUE);
443#ifdef _IOFBF
444 newCONSTSUB(stash,"_IOFBF", newSViv(_IOFBF));
445#endif
446#ifdef _IOLBF
447 newCONSTSUB(stash,"_IOLBF", newSViv(_IOLBF));
448#endif
449#ifdef _IONBF
450 newCONSTSUB(stash,"_IONBF", newSViv(_IONBF));
451#endif
452#ifdef SEEK_SET
453 newCONSTSUB(stash,"SEEK_SET", newSViv(SEEK_SET));
454#endif
455#ifdef SEEK_CUR
456 newCONSTSUB(stash,"SEEK_CUR", newSViv(SEEK_CUR));
457#endif
458#ifdef SEEK_END
459 newCONSTSUB(stash,"SEEK_END", newSViv(SEEK_END));
460#endif
cf7fe8a2 461}