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