This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In perldiag.pod add an entry for the error message that change 27155
[perl5.git] / perlio.c
CommitLineData
14a5cf38 1/*
acde74e1 2 * perlio.c Copyright (c) 1996-2006, Nick Ing-Simmons You may distribute
14a5cf38 3 * under the terms of either the GNU General Public License or the
71200d45 4 * Artistic License, as specified in the README file.
760ac839
LW
5 */
6
14a5cf38 7/*
d31a8517
AT
8 * Hour after hour for nearly three weary days he had jogged up and down,
9 * over passes, and through long dales, and across many streams.
10 */
11
166f8a29
DM
12/* This file contains the functions needed to implement PerlIO, which
13 * is Perl's private replacement for the C stdio library. This is used
14 * by default unless you compile with -Uuseperlio or run with
15 * PERLIO=:stdio (but don't do this unless you know what you're doing)
16 */
17
d31a8517 18/*
71200d45 19 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14a5cf38 20 * at the dispatch tables, even when we do not need it for other reasons.
71200d45 21 * Invent a dSYS macro to abstract this out
14a5cf38 22 */
7bcba3d4
NIS
23#ifdef PERL_IMPLICIT_SYS
24#define dSYS dTHX
25#else
26#define dSYS dNOOP
27#endif
28
760ac839 29#define VOIDUSED 1
12ae5dfc
JH
30#ifdef PERL_MICRO
31# include "uconfig.h"
32#else
33# include "config.h"
34#endif
760ac839 35
6f9d8c32 36#define PERLIO_NOT_STDIO 0
760ac839 37#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
14a5cf38 38/*
71200d45 39 * #define PerlIO FILE
14a5cf38 40 */
760ac839
LW
41#endif
42/*
6f9d8c32 43 * This file provides those parts of PerlIO abstraction
88b61e10 44 * which are not #defined in perlio.h.
6f9d8c32 45 * Which these are depends on various Configure #ifdef's
760ac839
LW
46 */
47
48#include "EXTERN.h"
864dbfa3 49#define PERL_IN_PERLIO_C
760ac839
LW
50#include "perl.h"
51
32af7c23
CL
52#ifdef PERL_IMPLICIT_CONTEXT
53#undef dSYS
54#define dSYS dTHX
55#endif
56
0c4f7ff0
NIS
57#include "XSUB.h"
58
9cffb111
OS
59#ifdef __Lynx__
60/* Missing proto on LynxOS */
61int mkstemp(char*);
62#endif
63
1b7a0411 64/* Call the callback or PerlIOBase, and return failure. */
b32dd47e 65#define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
1b7a0411 66 if (PerlIOValid(f)) { \
46c461b5 67 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
68 if (tab && tab->callback) \
69 return (*tab->callback) args; \
70 else \
71 return PerlIOBase_ ## base args; \
72 } \
73 else \
74 SETERRNO(EBADF, SS_IVCHAN); \
75 return failure
76
77/* Call the callback or fail, and return failure. */
b32dd47e 78#define Perl_PerlIO_or_fail(f, callback, failure, args) \
1b7a0411 79 if (PerlIOValid(f)) { \
46c461b5 80 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
81 if (tab && tab->callback) \
82 return (*tab->callback) args; \
83 SETERRNO(EINVAL, LIB_INVARG); \
84 } \
85 else \
86 SETERRNO(EBADF, SS_IVCHAN); \
87 return failure
88
89/* Call the callback or PerlIOBase, and be void. */
b32dd47e 90#define Perl_PerlIO_or_Base_void(f, callback, base, args) \
1b7a0411 91 if (PerlIOValid(f)) { \
46c461b5 92 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
93 if (tab && tab->callback) \
94 (*tab->callback) args; \
95 else \
96 PerlIOBase_ ## base args; \
1b7a0411
JH
97 } \
98 else \
99 SETERRNO(EBADF, SS_IVCHAN)
100
101/* Call the callback or fail, and be void. */
b32dd47e 102#define Perl_PerlIO_or_fail_void(f, callback, args) \
1b7a0411 103 if (PerlIOValid(f)) { \
46c461b5 104 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
105 if (tab && tab->callback) \
106 (*tab->callback) args; \
37725cdc
NIS
107 else \
108 SETERRNO(EINVAL, LIB_INVARG); \
1b7a0411
JH
109 } \
110 else \
111 SETERRNO(EBADF, SS_IVCHAN)
112
71ab4674
SP
113#ifndef USE_SFIO
114int
115perlsio_binmode(FILE *fp, int iotype, int mode)
116{
117 /*
118 * This used to be contents of do_binmode in doio.c
119 */
120#ifdef DOSISH
121# if defined(atarist) || defined(__MINT__)
122 if (!fflush(fp)) {
123 if (mode & O_BINARY)
124 ((FILE *) fp)->_flag |= _IOBIN;
125 else
126 ((FILE *) fp)->_flag &= ~_IOBIN;
127 return 1;
128 }
129 return 0;
130# else
131 dTHX;
132#ifdef NETWARE
133 if (PerlLIO_setmode(fp, mode) != -1) {
134#else
135 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
136#endif
137# if defined(WIN32) && defined(__BORLANDC__)
138 /*
139 * The translation mode of the stream is maintained independent
140of
141 * the translation mode of the fd in the Borland RTL (heavy
142 * digging through their runtime sources reveal). User has to
143set
144 * the mode explicitly for the stream (though they don't
145document
146 * this anywhere). GSAR 97-5-24
147 */
148 fseek(fp, 0L, 0);
149 if (mode & O_BINARY)
150 fp->flags |= _F_BIN;
151 else
152 fp->flags &= ~_F_BIN;
153# endif
154 return 1;
155 }
156 else
157 return 0;
158# endif
159#else
160# if defined(USEMYBINMODE)
161 dTHX;
162 if (my_binmode(fp, iotype, mode) != FALSE)
163 return 1;
164 else
165 return 0;
166# else
167 PERL_UNUSED_ARG(fp);
168 PERL_UNUSED_ARG(iotype);
169 PERL_UNUSED_ARG(mode);
170 return 1;
171# endif
172#endif
173}
174#endif /* sfio */
175
06c7082d 176#ifndef O_ACCMODE
22569500 177#define O_ACCMODE 3 /* Assume traditional implementation */
06c7082d
NIS
178#endif
179
180int
181PerlIO_intmode2str(int rawmode, char *mode, int *writing)
182{
de009b76 183 const int result = rawmode & O_ACCMODE;
06c7082d
NIS
184 int ix = 0;
185 int ptype;
186 switch (result) {
187 case O_RDONLY:
188 ptype = IoTYPE_RDONLY;
189 break;
190 case O_WRONLY:
191 ptype = IoTYPE_WRONLY;
192 break;
193 case O_RDWR:
194 default:
195 ptype = IoTYPE_RDWR;
196 break;
197 }
198 if (writing)
199 *writing = (result != O_RDONLY);
200
201 if (result == O_RDONLY) {
202 mode[ix++] = 'r';
203 }
204#ifdef O_APPEND
205 else if (rawmode & O_APPEND) {
206 mode[ix++] = 'a';
207 if (result != O_WRONLY)
208 mode[ix++] = '+';
209 }
210#endif
211 else {
212 if (result == O_WRONLY)
213 mode[ix++] = 'w';
214 else {
215 mode[ix++] = 'r';
216 mode[ix++] = '+';
217 }
218 }
219 if (rawmode & O_BINARY)
220 mode[ix++] = 'b';
221 mode[ix] = '\0';
222 return ptype;
223}
224
eb73beca
NIS
225#ifndef PERLIO_LAYERS
226int
227PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
228{
6874a2de
NIS
229 if (!names || !*names
230 || strEQ(names, ":crlf")
231 || strEQ(names, ":raw")
232 || strEQ(names, ":bytes")
233 ) {
14a5cf38
JH
234 return 0;
235 }
236 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
237 /*
71200d45 238 * NOTREACHED
14a5cf38
JH
239 */
240 return -1;
eb73beca
NIS
241}
242
13621cfb
NIS
243void
244PerlIO_destruct(pTHX)
245{
246}
247
f5b9d040
NIS
248int
249PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
250{
92bff44d 251#ifdef USE_SFIO
8772537c
AL
252 PERL_UNUSED_ARG(iotype);
253 PERL_UNUSED_ARG(mode);
254 PERL_UNUSED_ARG(names);
14a5cf38 255 return 1;
92bff44d 256#else
14a5cf38 257 return perlsio_binmode(fp, iotype, mode);
92bff44d 258#endif
f5b9d040 259}
60382766 260
e0fa5af2 261PerlIO *
ecdeb87c 262PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
e0fa5af2 263{
a0fd4948 264#if defined(PERL_MICRO) || defined(__SYMBIAN32__)
0553478e
NIS
265 return NULL;
266#else
267#ifdef PERL_IMPLICIT_SYS
22569500 268 return PerlSIO_fdupopen(f);
0553478e 269#else
30753f56
NIS
270#ifdef WIN32
271 return win32_fdupopen(f);
272#else
e0fa5af2 273 if (f) {
504618e9 274 const int fd = PerlLIO_dup(PerlIO_fileno(f));
e0fa5af2 275 if (fd >= 0) {
06c7082d 276 char mode[8];
a5936e02 277#ifdef DJGPP
dcda55fc
AL
278 const int omode = djgpp_get_stream_mode(f);
279#else
280 const int omode = fcntl(fd, F_GETFL);
a5936e02 281#endif
06c7082d 282 PerlIO_intmode2str(omode,mode,NULL);
e0fa5af2 283 /* the r+ is a hack */
06c7082d 284 return PerlIO_fdopen(fd, mode);
e0fa5af2
NIS
285 }
286 return NULL;
287 }
288 else {
93189314 289 SETERRNO(EBADF, SS_IVCHAN);
e0fa5af2 290 }
7114a2d2 291#endif
e0fa5af2 292 return NULL;
0553478e 293#endif
30753f56 294#endif
e0fa5af2
NIS
295}
296
297
14a5cf38 298/*
71200d45 299 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
14a5cf38 300 */
ee518936
NIS
301
302PerlIO *
14a5cf38
JH
303PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
304 int imode, int perm, PerlIO *old, int narg, SV **args)
305{
7cf31beb
NIS
306 if (narg) {
307 if (narg > 1) {
3b8752bb 308 Perl_croak(aTHX_ "More than one argument to open");
7cf31beb 309 }
14a5cf38
JH
310 if (*args == &PL_sv_undef)
311 return PerlIO_tmpfile();
312 else {
e62f0680 313 const char *name = SvPV_nolen_const(*args);
3b6c1aba 314 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
315 fd = PerlLIO_open3(name, imode, perm);
316 if (fd >= 0)
de009b76 317 return PerlIO_fdopen(fd, mode + 1);
14a5cf38
JH
318 }
319 else if (old) {
320 return PerlIO_reopen(name, mode, old);
321 }
322 else {
323 return PerlIO_open(name, mode);
324 }
325 }
326 }
327 else {
328 return PerlIO_fdopen(fd, (char *) mode);
329 }
330 return NULL;
ee518936
NIS
331}
332
0c4f7ff0
NIS
333XS(XS_PerlIO__Layer__find)
334{
14a5cf38
JH
335 dXSARGS;
336 if (items < 2)
337 Perl_croak(aTHX_ "Usage class->find(name[,load])");
338 else {
dcda55fc 339 const char * const name = SvPV_nolen_const(ST(1));
14a5cf38
JH
340 ST(0) = (strEQ(name, "crlf")
341 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
342 XSRETURN(1);
343 }
0c4f7ff0
NIS
344}
345
346
347void
348Perl_boot_core_PerlIO(pTHX)
349{
14a5cf38 350 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
0c4f7ff0
NIS
351}
352
ac27b0f5
NIS
353#endif
354
32e30700 355
6f9d8c32 356#ifdef PERLIO_IS_STDIO
760ac839
LW
357
358void
e8632036 359PerlIO_init(pTHX)
760ac839 360{
14a5cf38
JH
361 /*
362 * Does nothing (yet) except force this file to be included in perl
71200d45 363 * binary. That allows this file to force inclusion of other functions
14a5cf38 364 * that may be required by loadable extensions e.g. for
71200d45 365 * FileHandle::tmpfile
14a5cf38 366 */
760ac839
LW
367}
368
33dcbb9a
PP
369#undef PerlIO_tmpfile
370PerlIO *
8ac85365 371PerlIO_tmpfile(void)
33dcbb9a 372{
14a5cf38 373 return tmpfile();
33dcbb9a
PP
374}
375
22569500 376#else /* PERLIO_IS_STDIO */
760ac839
LW
377
378#ifdef USE_SFIO
379
380#undef HAS_FSETPOS
381#undef HAS_FGETPOS
382
14a5cf38
JH
383/*
384 * This section is just to make sure these functions get pulled in from
71200d45 385 * libsfio.a
14a5cf38 386 */
760ac839
LW
387
388#undef PerlIO_tmpfile
389PerlIO *
c78749f2 390PerlIO_tmpfile(void)
760ac839 391{
14a5cf38 392 return sftmp(0);
760ac839
LW
393}
394
395void
e8632036 396PerlIO_init(pTHX)
760ac839 397{
14a5cf38
JH
398 /*
399 * Force this file to be included in perl binary. Which allows this
400 * file to force inclusion of other functions that may be required by
71200d45 401 * loadable extensions e.g. for FileHandle::tmpfile
14a5cf38 402 */
760ac839 403
14a5cf38 404 /*
71200d45 405 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
14a5cf38 406 * results in a lot of lseek()s to regular files and lot of small
71200d45 407 * writes to pipes.
14a5cf38
JH
408 */
409 sfset(sfstdout, SF_SHARE, 0);
760ac839
LW
410}
411
b9d6bf13 412/* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
92bff44d 413PerlIO *
4b069b44 414PerlIO_importFILE(FILE *stdio, const char *mode)
92bff44d 415{
de009b76 416 const int fd = fileno(stdio);
4b069b44 417 if (!mode || !*mode) {
81428673 418 mode = "r+";
4b069b44
NIS
419 }
420 return PerlIO_fdopen(fd, mode);
92bff44d
NIS
421}
422
423FILE *
424PerlIO_findFILE(PerlIO *pio)
425{
de009b76
AL
426 const int fd = PerlIO_fileno(pio);
427 FILE * const f = fdopen(fd, "r+");
14a5cf38
JH
428 PerlIO_flush(pio);
429 if (!f && errno == EINVAL)
430 f = fdopen(fd, "w");
431 if (!f && errno == EINVAL)
432 f = fdopen(fd, "r");
433 return f;
92bff44d
NIS
434}
435
436
22569500 437#else /* USE_SFIO */
6f9d8c32 438/*======================================================================================*/
14a5cf38 439/*
71200d45 440 * Implement all the PerlIO interface ourselves.
9e353e3b 441 */
760ac839 442
76ced9ad
NIS
443#include "perliol.h"
444
14a5cf38
JH
445/*
446 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
71200d45 447 * files
14a5cf38 448 */
02f66e2f
NIS
449#ifdef I_UNISTD
450#include <unistd.h>
451#endif
06da4f11
NIS
452#ifdef HAS_MMAP
453#include <sys/mman.h>
454#endif
455
6f9d8c32 456void
14a5cf38
JH
457PerlIO_debug(const char *fmt, ...)
458{
14a5cf38
JH
459 va_list ap;
460 dSYS;
461 va_start(ap, fmt);
27da23d5 462 if (!PL_perlio_debug_fd && !PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
dcda55fc 463 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
14a5cf38 464 if (s && *s)
27da23d5 465 PL_perlio_debug_fd = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
14a5cf38 466 else
27da23d5 467 PL_perlio_debug_fd = -1;
14a5cf38 468 }
27da23d5 469 if (PL_perlio_debug_fd > 0) {
14a5cf38 470 dTHX;
70ace5da 471#ifdef USE_ITHREADS
dcda55fc 472 const char * const s = CopFILE(PL_curcop);
70ace5da
NIS
473 /* Use fixed buffer as sv_catpvf etc. needs SVs */
474 char buffer[1024];
dcda55fc 475 const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
42d9b98d
NC
476 const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
477 PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
70ace5da 478#else
dcda55fc
AL
479 const char *s = CopFILE(PL_curcop);
480 STRLEN len;
396482e1 481 SV * const sv = newSVpvs("");
dcda55fc 482 Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s ? s : "(none)",
14a5cf38
JH
483 (IV) CopLINE(PL_curcop));
484 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
485
b83604b4 486 s = SvPV_const(sv, len);
27da23d5 487 PerlLIO_write(PL_perlio_debug_fd, s, len);
14a5cf38 488 SvREFCNT_dec(sv);
70ace5da 489#endif
14a5cf38
JH
490 }
491 va_end(ap);
6f9d8c32
NIS
492}
493
9e353e3b
NIS
494/*--------------------------------------------------------------------------------------*/
495
14a5cf38 496/*
71200d45 497 * Inner level routines
14a5cf38 498 */
9e353e3b 499
14a5cf38 500/*
71200d45 501 * Table of pointers to the PerlIO structs (malloc'ed)
14a5cf38 502 */
05d1247b 503#define PERLIO_TABLE_SIZE 64
6f9d8c32 504
760ac839 505PerlIO *
5f1a76d0 506PerlIO_allocate(pTHX)
6f9d8c32 507{
97aff369 508 dVAR;
14a5cf38 509 /*
71200d45 510 * Find a free slot in the table, allocating new table as necessary
14a5cf38
JH
511 */
512 PerlIO **last;
513 PerlIO *f;
a1ea730d 514 last = &PL_perlio;
14a5cf38
JH
515 while ((f = *last)) {
516 int i;
517 last = (PerlIO **) (f);
518 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
519 if (!*++f) {
520 return f;
521 }
522 }
523 }
a02a5408 524 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
14a5cf38
JH
525 if (!f) {
526 return NULL;
527 }
528 *last = f;
529 return f + 1;
05d1247b
NIS
530}
531
a1ea730d
NIS
532#undef PerlIO_fdupopen
533PerlIO *
ecdeb87c 534PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
a1ea730d 535{
04892f78 536 if (PerlIOValid(f)) {
de009b76 537 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
fe5a182c 538 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
210e727c
JH
539 if (tab && tab->Dup)
540 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
37725cdc
NIS
541 else {
542 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
543 }
a1ea730d 544 }
210e727c
JH
545 else
546 SETERRNO(EBADF, SS_IVCHAN);
547
548 return NULL;
a1ea730d
NIS
549}
550
551void
5f1a76d0 552PerlIO_cleantable(pTHX_ PerlIO **tablep)
05d1247b 553{
dcda55fc 554 PerlIO * const table = *tablep;
14a5cf38
JH
555 if (table) {
556 int i;
557 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
558 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
dcda55fc 559 PerlIO * const f = table + i;
14a5cf38
JH
560 if (*f) {
561 PerlIO_close(f);
562 }
563 }
3a1ee7e8 564 Safefree(table);
14a5cf38 565 *tablep = NULL;
05d1247b 566 }
05d1247b
NIS
567}
568
fcf2db38
NIS
569
570PerlIO_list_t *
3a1ee7e8 571PerlIO_list_alloc(pTHX)
fcf2db38 572{
14a5cf38 573 PerlIO_list_t *list;
a02a5408 574 Newxz(list, 1, PerlIO_list_t);
14a5cf38
JH
575 list->refcnt = 1;
576 return list;
fcf2db38
NIS
577}
578
579void
3a1ee7e8 580PerlIO_list_free(pTHX_ PerlIO_list_t *list)
fcf2db38 581{
14a5cf38
JH
582 if (list) {
583 if (--list->refcnt == 0) {
584 if (list->array) {
14a5cf38
JH
585 IV i;
586 for (i = 0; i < list->cur; i++) {
587 if (list->array[i].arg)
588 SvREFCNT_dec(list->array[i].arg);
589 }
590 Safefree(list->array);
591 }
592 Safefree(list);
593 }
594 }
fcf2db38
NIS
595}
596
597void
3a1ee7e8 598PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
14a5cf38 599{
97aff369 600 dVAR;
14a5cf38
JH
601 PerlIO_pair_t *p;
602 if (list->cur >= list->len) {
603 list->len += 8;
604 if (list->array)
605 Renew(list->array, list->len, PerlIO_pair_t);
606 else
a02a5408 607 Newx(list->array, list->len, PerlIO_pair_t);
14a5cf38
JH
608 }
609 p = &(list->array[list->cur++]);
610 p->funcs = funcs;
611 if ((p->arg = arg)) {
7fc63493 612 (void)SvREFCNT_inc(arg);
14a5cf38 613 }
fcf2db38
NIS
614}
615
3a1ee7e8
NIS
616PerlIO_list_t *
617PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
618{
694c95cf
JH
619 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
620 if (proto) {
621 int i;
622 list = PerlIO_list_alloc(aTHX);
623 for (i=0; i < proto->cur; i++) {
a0714e2c 624 SV *arg = NULL;
694c95cf
JH
625 if (proto->array[i].arg)
626 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
627 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
628 }
3a1ee7e8
NIS
629 }
630 return list;
631}
4a4a6116 632
05d1247b 633void
3a1ee7e8 634PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
9a6404c5 635{
3aaf42a7 636#ifdef USE_ITHREADS
3a1ee7e8
NIS
637 PerlIO **table = &proto->Iperlio;
638 PerlIO *f;
639 PL_perlio = NULL;
640 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
641 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
642 PerlIO_allocate(aTHX); /* root slot is never used */
694c95cf 643 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
3a1ee7e8
NIS
644 while ((f = *table)) {
645 int i;
646 table = (PerlIO **) (f++);
647 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
648 if (*f) {
93a8090d 649 (void) fp_dup(f, 0, param);
3a1ee7e8
NIS
650 }
651 f++;
652 }
653 }
1b6737cc
AL
654#else
655 PERL_UNUSED_ARG(proto);
656 PERL_UNUSED_ARG(param);
3aaf42a7 657#endif
9a6404c5
DM
658}
659
660void
13621cfb
NIS
661PerlIO_destruct(pTHX)
662{
97aff369 663 dVAR;
a1ea730d 664 PerlIO **table = &PL_perlio;
14a5cf38 665 PerlIO *f;
694c95cf
JH
666#ifdef USE_ITHREADS
667 PerlIO_debug("Destruct %p\n",aTHX);
668#endif
14a5cf38
JH
669 while ((f = *table)) {
670 int i;
671 table = (PerlIO **) (f++);
672 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
673 PerlIO *x = f;
dcda55fc 674 const PerlIOl *l;
14a5cf38
JH
675 while ((l = *x)) {
676 if (l->tab->kind & PERLIO_K_DESTRUCT) {
677 PerlIO_debug("Destruct popping %s\n", l->tab->name);
678 PerlIO_flush(x);
679 PerlIO_pop(aTHX_ x);
680 }
681 else {
682 x = PerlIONext(x);
683 }
684 }
685 f++;
686 }
687 }
13621cfb
NIS
688}
689
690void
a999f61b 691PerlIO_pop(pTHX_ PerlIO *f)
760ac839 692{
dcda55fc 693 const PerlIOl *l = *f;
14a5cf38 694 if (l) {
fe5a182c 695 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
14a5cf38
JH
696 if (l->tab->Popped) {
697 /*
698 * If popped returns non-zero do not free its layer structure
699 * it has either done so itself, or it is shared and still in
71200d45 700 * use
14a5cf38 701 */
f62ce20a 702 if ((*l->tab->Popped) (aTHX_ f) != 0)
14a5cf38
JH
703 return;
704 }
b47cad08 705 *f = l->next;
3a1ee7e8 706 Safefree(l);
a8c08ecd 707 }
6f9d8c32
NIS
708}
709
39f7a870
JH
710/* Return as an array the stack of layers on a filehandle. Note that
711 * the stack is returned top-first in the array, and there are three
712 * times as many array elements as there are layers in the stack: the
713 * first element of a layer triplet is the name, the second one is the
714 * arguments, and the third one is the flags. */
715
716AV *
717PerlIO_get_layers(pTHX_ PerlIO *f)
718{
97aff369 719 dVAR;
dcda55fc 720 AV * const av = newAV();
39f7a870 721
dcda55fc
AL
722 if (PerlIOValid(f)) {
723 PerlIOl *l = PerlIOBase(f);
724
725 while (l) {
726 SV * const name = l->tab && l->tab->name ?
727 newSVpv(l->tab->name, 0) : &PL_sv_undef;
728 SV * const arg = l->tab && l->tab->Getarg ?
729 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
730 av_push(av, name);
731 av_push(av, arg);
732 av_push(av, newSViv((IV)l->flags));
733 l = l->next;
734 }
735 }
39f7a870 736
dcda55fc 737 return av;
39f7a870
JH
738}
739
9e353e3b 740/*--------------------------------------------------------------------------------------*/
14a5cf38 741/*
71200d45 742 * XS Interface for perl code
14a5cf38 743 */
9e353e3b 744
fcf2db38 745PerlIO_funcs *
2edd7e44 746PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
f3862f8b 747{
27da23d5 748 dVAR;
14a5cf38
JH
749 IV i;
750 if ((SSize_t) len <= 0)
751 len = strlen(name);
3a1ee7e8 752 for (i = 0; i < PL_known_layers->cur; i++) {
46c461b5 753 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
a9f76400 754 if (memEQ(f->name, name, len) && f->name[len] == 0) {
fe5a182c 755 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
14a5cf38
JH
756 return f;
757 }
758 }
3a1ee7e8
NIS
759 if (load && PL_subname && PL_def_layerlist
760 && PL_def_layerlist->cur >= 2) {
d7a09b41
SR
761 if (PL_in_load_module) {
762 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
763 return NULL;
764 } else {
396482e1 765 SV * const pkgsv = newSVpvs("PerlIO");
46c461b5
AL
766 SV * const layer = newSVpvn(name, len);
767 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
768 ENTER;
d7a09b41 769 SAVEINT(PL_in_load_module);
c9bca74a 770 if (cv) {
9cfa90c0 771 SAVEGENERICSV(PL_warnhook);
504618e9 772 (void)SvREFCNT_inc(cv);
c9bca74a
NIS
773 PL_warnhook = (SV *) cv;
774 }
d7a09b41
SR
775 PL_in_load_module++;
776 /*
777 * The two SVs are magically freed by load_module
778 */
a0714e2c 779 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
d7a09b41
SR
780 PL_in_load_module--;
781 LEAVE;
782 return PerlIO_find_layer(aTHX_ name, len, 0);
783 }
14a5cf38
JH
784 }
785 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
786 return NULL;
f3862f8b
NIS
787}
788
2a1bc955 789#ifdef USE_ATTRIBUTES_FOR_PERLIO
b13b2135
NIS
790
791static int
792perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
793{
14a5cf38 794 if (SvROK(sv)) {
dcda55fc
AL
795 IO * const io = GvIOn((GV *) SvRV(sv));
796 PerlIO * const ifp = IoIFP(io);
797 PerlIO * const ofp = IoOFP(io);
14a5cf38
JH
798 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
799 }
800 return 0;
b13b2135
NIS
801}
802
803static int
804perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
805{
14a5cf38 806 if (SvROK(sv)) {
dcda55fc
AL
807 IO * const io = GvIOn((GV *) SvRV(sv));
808 PerlIO * const ifp = IoIFP(io);
809 PerlIO * const ofp = IoOFP(io);
14a5cf38
JH
810 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
811 }
812 return 0;
b13b2135
NIS
813}
814
815static int
816perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
817{
14a5cf38
JH
818 Perl_warn(aTHX_ "clear %" SVf, sv);
819 return 0;
b13b2135
NIS
820}
821
822static int
823perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
824{
14a5cf38
JH
825 Perl_warn(aTHX_ "free %" SVf, sv);
826 return 0;
b13b2135
NIS
827}
828
829MGVTBL perlio_vtab = {
14a5cf38
JH
830 perlio_mg_get,
831 perlio_mg_set,
22569500 832 NULL, /* len */
14a5cf38
JH
833 perlio_mg_clear,
834 perlio_mg_free
b13b2135
NIS
835};
836
837XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
838{
14a5cf38 839 dXSARGS;
dcda55fc
AL
840 SV * const sv = SvRV(ST(1));
841 AV * const av = newAV();
14a5cf38
JH
842 MAGIC *mg;
843 int count = 0;
844 int i;
845 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
846 SvRMAGICAL_off(sv);
847 mg = mg_find(sv, PERL_MAGIC_ext);
848 mg->mg_virtual = &perlio_vtab;
849 mg_magical(sv);
850 Perl_warn(aTHX_ "attrib %" SVf, sv);
851 for (i = 2; i < items; i++) {
852 STRLEN len;
dcda55fc
AL
853 const char * const name = SvPV_const(ST(i), len);
854 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
14a5cf38
JH
855 if (layer) {
856 av_push(av, SvREFCNT_inc(layer));
857 }
858 else {
859 ST(count) = ST(i);
860 count++;
861 }
862 }
863 SvREFCNT_dec(av);
864 XSRETURN(count);
865}
866
22569500 867#endif /* USE_ATTIBUTES_FOR_PERLIO */
2a1bc955 868
e3f3bf95
NIS
869SV *
870PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
f3862f8b 871{
89529cee 872 HV * const stash = gv_stashpvs("PerlIO::Layer", TRUE);
46c461b5 873 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
14a5cf38 874 return sv;
e3f3bf95
NIS
875}
876
5ca1d77f 877XS(XS_PerlIO__Layer__NoWarnings)
c9bca74a 878{
37725cdc 879 /* This is used as a %SIG{__WARN__} handler to supress warnings
c9bca74a
NIS
880 during loading of layers.
881 */
97aff369 882 dVAR;
c9bca74a
NIS
883 dXSARGS;
884 if (items)
e62f0680 885 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
c9bca74a
NIS
886 XSRETURN(0);
887}
888
5ca1d77f 889XS(XS_PerlIO__Layer__find)
0c4f7ff0 890{
97aff369 891 dVAR;
14a5cf38
JH
892 dXSARGS;
893 if (items < 2)
894 Perl_croak(aTHX_ "Usage class->find(name[,load])");
895 else {
de009b76 896 STRLEN len;
46c461b5 897 const char * const name = SvPV_const(ST(1), len);
de009b76 898 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
46c461b5 899 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
14a5cf38
JH
900 ST(0) =
901 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
902 &PL_sv_undef;
903 XSRETURN(1);
904 }
0c4f7ff0
NIS
905}
906
e3f3bf95
NIS
907void
908PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
909{
97aff369 910 dVAR;
3a1ee7e8
NIS
911 if (!PL_known_layers)
912 PL_known_layers = PerlIO_list_alloc(aTHX);
a0714e2c 913 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
fe5a182c 914 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
f3862f8b
NIS
915}
916
1141d9f8 917int
fcf2db38 918PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
1141d9f8 919{
97aff369 920 dVAR;
14a5cf38
JH
921 if (names) {
922 const char *s = names;
923 while (*s) {
924 while (isSPACE(*s) || *s == ':')
925 s++;
926 if (*s) {
927 STRLEN llen = 0;
928 const char *e = s;
bd61b366 929 const char *as = NULL;
14a5cf38
JH
930 STRLEN alen = 0;
931 if (!isIDFIRST(*s)) {
932 /*
933 * Message is consistent with how attribute lists are
934 * passed. Even though this means "foo : : bar" is
71200d45 935 * seen as an invalid separator character.
14a5cf38 936 */
de009b76 937 const char q = ((*s == '\'') ? '"' : '\'');
99ef548b 938 if (ckWARN(WARN_LAYER))
22569500 939 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 940 "Invalid separator character %c%c%c in PerlIO layer specification %s",
1e616cf5 941 q, *s, q, s);
93189314 942 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
943 return -1;
944 }
945 do {
946 e++;
947 } while (isALNUM(*e));
948 llen = e - s;
949 if (*e == '(') {
950 int nesting = 1;
951 as = ++e;
952 while (nesting) {
953 switch (*e++) {
954 case ')':
955 if (--nesting == 0)
956 alen = (e - 1) - as;
957 break;
958 case '(':
959 ++nesting;
960 break;
961 case '\\':
962 /*
963 * It's a nul terminated string, not allowed
964 * to \ the terminating null. Anything other
71200d45 965 * character is passed over.
14a5cf38
JH
966 */
967 if (*e++) {
968 break;
969 }
970 /*
71200d45 971 * Drop through
14a5cf38
JH
972 */
973 case '\0':
974 e--;
22569500
NIS
975 if (ckWARN(WARN_LAYER))
976 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 977 "Argument list not closed for PerlIO layer \"%.*s\"",
14a5cf38
JH
978 (int) (e - s), s);
979 return -1;
980 default:
981 /*
71200d45 982 * boring.
14a5cf38
JH
983 */
984 break;
985 }
986 }
987 }
988 if (e > s) {
46c461b5 989 PerlIO_funcs * const layer =
14a5cf38
JH
990 PerlIO_find_layer(aTHX_ s, llen, 1);
991 if (layer) {
3a1ee7e8 992 PerlIO_list_push(aTHX_ av, layer,
14a5cf38
JH
993 (as) ? newSVpvn(as,
994 alen) :
995 &PL_sv_undef);
996 }
997 else {
041457d9 998 if (ckWARN(WARN_LAYER))
b4581f09 999 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
14a5cf38
JH
1000 (int) llen, s);
1001 return -1;
1002 }
1003 }
1004 s = e;
1005 }
1006 }
1007 }
1008 return 0;
1141d9f8
NIS
1009}
1010
dfebf958 1011void
fcf2db38 1012PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
dfebf958 1013{
97aff369 1014 dVAR;
27da23d5 1015 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
35990314 1016#ifdef PERLIO_USING_CRLF
6ce75a77 1017 tab = &PerlIO_crlf;
846be114 1018#else
6ce75a77 1019 if (PerlIO_stdio.Set_ptrcnt)
22569500 1020 tab = &PerlIO_stdio;
846be114 1021#endif
14a5cf38 1022 PerlIO_debug("Pushing %s\n", tab->name);
3a1ee7e8 1023 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
14a5cf38 1024 &PL_sv_undef);
dfebf958
NIS
1025}
1026
e3f3bf95 1027SV *
14a5cf38 1028PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
e3f3bf95 1029{
14a5cf38 1030 return av->array[n].arg;
e3f3bf95
NIS
1031}
1032
f3862f8b 1033PerlIO_funcs *
14a5cf38 1034PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
f3862f8b 1035{
14a5cf38
JH
1036 if (n >= 0 && n < av->cur) {
1037 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1038 av->array[n].funcs->name);
1039 return av->array[n].funcs;
1040 }
1041 if (!def)
1042 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1043 return def;
e3f3bf95
NIS
1044}
1045
4ec2216f
NIS
1046IV
1047PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1048{
8772537c
AL
1049 PERL_UNUSED_ARG(mode);
1050 PERL_UNUSED_ARG(arg);
1051 PERL_UNUSED_ARG(tab);
4ec2216f
NIS
1052 if (PerlIOValid(f)) {
1053 PerlIO_flush(f);
1054 PerlIO_pop(aTHX_ f);
1055 return 0;
1056 }
1057 return -1;
1058}
1059
27da23d5 1060PERLIO_FUNCS_DECL(PerlIO_remove) = {
4ec2216f
NIS
1061 sizeof(PerlIO_funcs),
1062 "pop",
1063 0,
1064 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1065 PerlIOPop_pushed,
1066 NULL,
1067 NULL,
1068 NULL,
1069 NULL,
1070 NULL,
1071 NULL,
1072 NULL,
1073 NULL,
1074 NULL,
1075 NULL,
de009b76
AL
1076 NULL,
1077 NULL,
4ec2216f
NIS
1078 NULL, /* flush */
1079 NULL, /* fill */
1080 NULL,
1081 NULL,
1082 NULL,
1083 NULL,
1084 NULL, /* get_base */
1085 NULL, /* get_bufsiz */
1086 NULL, /* get_ptr */
1087 NULL, /* get_cnt */
1088 NULL, /* set_ptrcnt */
1089};
1090
fcf2db38 1091PerlIO_list_t *
e3f3bf95
NIS
1092PerlIO_default_layers(pTHX)
1093{
97aff369 1094 dVAR;
3a1ee7e8 1095 if (!PL_def_layerlist) {
bd61b366 1096 const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
27da23d5 1097 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
3a1ee7e8 1098 PL_def_layerlist = PerlIO_list_alloc(aTHX);
27da23d5 1099 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
979e2c82 1100#if defined(WIN32)
27da23d5 1101 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
2f8118af 1102#if 0
14a5cf38 1103 osLayer = &PerlIO_win32;
0c4128ad 1104#endif
2f8118af 1105#endif
27da23d5
JH
1106 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1107 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1108 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1109 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
06da4f11 1110#ifdef HAS_MMAP
27da23d5 1111 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
06da4f11 1112#endif
27da23d5
JH
1113 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1114 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1115 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
3a1ee7e8 1116 PerlIO_list_push(aTHX_ PL_def_layerlist,
14a5cf38
JH
1117 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1118 &PL_sv_undef);
1119 if (s) {
3a1ee7e8 1120 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
14a5cf38
JH
1121 }
1122 else {
3a1ee7e8 1123 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
14a5cf38 1124 }
1141d9f8 1125 }
3a1ee7e8
NIS
1126 if (PL_def_layerlist->cur < 2) {
1127 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
f3862f8b 1128 }
3a1ee7e8 1129 return PL_def_layerlist;
e3f3bf95
NIS
1130}
1131
0c4f7ff0
NIS
1132void
1133Perl_boot_core_PerlIO(pTHX)
1134{
1135#ifdef USE_ATTRIBUTES_FOR_PERLIO
14a5cf38
JH
1136 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1137 __FILE__);
0c4f7ff0 1138#endif
14a5cf38 1139 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
c9bca74a 1140 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
0c4f7ff0 1141}
e3f3bf95
NIS
1142
1143PerlIO_funcs *
1144PerlIO_default_layer(pTHX_ I32 n)
1145{
97aff369 1146 dVAR;
46c461b5 1147 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
14a5cf38
JH
1148 if (n < 0)
1149 n += av->cur;
27da23d5 1150 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
f3862f8b
NIS
1151}
1152
a999f61b
NIS
1153#define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1154#define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
60382766
NIS
1155
1156void
1141d9f8 1157PerlIO_stdstreams(pTHX)
60382766 1158{
97aff369 1159 dVAR;
a1ea730d 1160 if (!PL_perlio) {
14a5cf38
JH
1161 PerlIO_allocate(aTHX);
1162 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1163 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1164 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1165 }
60382766
NIS
1166}
1167
1168PerlIO *
27da23d5 1169PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
14a5cf38 1170{
2dc2558e
NIS
1171 if (tab->fsize != sizeof(PerlIO_funcs)) {
1172 mismatch:
1173 Perl_croak(aTHX_ "Layer does not match this perl");
1174 }
1175 if (tab->size) {
b464bac0 1176 PerlIOl *l;
2dc2558e
NIS
1177 if (tab->size < sizeof(PerlIOl)) {
1178 goto mismatch;
1179 }
1180 /* Real layer with a data area */
a02a5408 1181 Newxc(l,tab->size,char,PerlIOl);
2dc2558e
NIS
1182 if (l && f) {
1183 Zero(l, tab->size, char);
1184 l->next = *f;
27da23d5 1185 l->tab = (PerlIO_funcs*) tab;
2dc2558e
NIS
1186 *f = l;
1187 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1188 (mode) ? mode : "(Null)", (void*)arg);
210e727c 1189 if (*l->tab->Pushed &&
27da23d5 1190 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
2dc2558e
NIS
1191 PerlIO_pop(aTHX_ f);
1192 return NULL;
1193 }
1194 }
1195 }
1196 else if (f) {
1197 /* Pseudo-layer where push does its own stack adjust */
00f51856
NIS
1198 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1199 (mode) ? mode : "(Null)", (void*)arg);
210e727c 1200 if (tab->Pushed &&
27da23d5 1201 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
210e727c 1202 return NULL;
14a5cf38
JH
1203 }
1204 }
1205 return f;
60382766
NIS
1206}
1207
dfebf958 1208IV
86e05cf2
NIS
1209PerlIOBase_binmode(pTHX_ PerlIO *f)
1210{
1211 if (PerlIOValid(f)) {
1212 /* Is layer suitable for raw stream ? */
1213 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1214 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1215 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1216 }
1217 else {
1218 /* Not suitable - pop it */
1219 PerlIO_pop(aTHX_ f);
1220 }
1221 return 0;
1222 }
1223 return -1;
1224}
1225
1226IV
2dc2558e 1227PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
dfebf958 1228{
8772537c
AL
1229 PERL_UNUSED_ARG(mode);
1230 PERL_UNUSED_ARG(arg);
1231 PERL_UNUSED_ARG(tab);
86e05cf2 1232
04892f78 1233 if (PerlIOValid(f)) {
86e05cf2 1234 PerlIO *t;
de009b76 1235 const PerlIOl *l;
14a5cf38 1236 PerlIO_flush(f);
86e05cf2
NIS
1237 /*
1238 * Strip all layers that are not suitable for a raw stream
1239 */
1240 t = f;
1241 while (t && (l = *t)) {
1242 if (l->tab->Binmode) {
1243 /* Has a handler - normal case */
1244 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1245 if (*t == l) {
1246 /* Layer still there - move down a layer */
1247 t = PerlIONext(t);
1248 }
1249 }
1250 else {
1251 return -1;
1252 }
14a5cf38
JH
1253 }
1254 else {
86e05cf2
NIS
1255 /* No handler - pop it */
1256 PerlIO_pop(aTHX_ t);
14a5cf38
JH
1257 }
1258 }
86e05cf2
NIS
1259 if (PerlIOValid(f)) {
1260 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1261 return 0;
1262 }
14a5cf38
JH
1263 }
1264 return -1;
dfebf958
NIS
1265}
1266
ac27b0f5 1267int
14a5cf38 1268PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
d9dac8cd 1269 PerlIO_list_t *layers, IV n, IV max)
14a5cf38 1270{
14a5cf38
JH
1271 int code = 0;
1272 while (n < max) {
8772537c 1273 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
14a5cf38
JH
1274 if (tab) {
1275 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1276 code = -1;
1277 break;
1278 }
1279 }
1280 n++;
1281 }
1282 return code;
e3f3bf95
NIS
1283}
1284
1285int
ac27b0f5
NIS
1286PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1287{
14a5cf38 1288 int code = 0;
53f1b6d2 1289 if (f && names) {
8772537c 1290 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
14a5cf38
JH
1291 code = PerlIO_parse_layers(aTHX_ layers, names);
1292 if (code == 0) {
d9dac8cd 1293 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
14a5cf38 1294 }
3a1ee7e8 1295 PerlIO_list_free(aTHX_ layers);
ac27b0f5 1296 }
14a5cf38 1297 return code;
ac27b0f5
NIS
1298}
1299
f3862f8b 1300
60382766 1301/*--------------------------------------------------------------------------------------*/
14a5cf38 1302/*
71200d45 1303 * Given the abstraction above the public API functions
14a5cf38 1304 */
60382766
NIS
1305
1306int
f5b9d040 1307PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
76ced9ad 1308{
68b5363f
PD
1309 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1310 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1311 iotype, mode, (names) ? names : "(Null)");
1312
03c0554d
NIS
1313 if (names) {
1314 /* Do not flush etc. if (e.g.) switching encodings.
1315 if a pushed layer knows it needs to flush lower layers
1316 (for example :unix which is never going to call them)
1317 it can do the flush when it is pushed.
1318 */
1319 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1320 }
1321 else {
86e05cf2 1322 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
35990314 1323#ifdef PERLIO_USING_CRLF
03c0554d
NIS
1324 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1325 O_BINARY so we can look for it in mode.
1326 */
1327 if (!(mode & O_BINARY)) {
1328 /* Text mode */
86e05cf2
NIS
1329 /* FIXME?: Looking down the layer stack seems wrong,
1330 but is a way of reaching past (say) an encoding layer
1331 to flip CRLF-ness of the layer(s) below
1332 */
03c0554d
NIS
1333 while (*f) {
1334 /* Perhaps we should turn on bottom-most aware layer
1335 e.g. Ilya's idea that UNIX TTY could serve
1336 */
1337 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1338 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1339 /* Not in text mode - flush any pending stuff and flip it */
1340 PerlIO_flush(f);
1341 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1342 }
1343 /* Only need to turn it on in one layer so we are done */
1344 return TRUE;
ed53a2bb 1345 }
03c0554d 1346 f = PerlIONext(f);
14a5cf38 1347 }
03c0554d
NIS
1348 /* Not finding a CRLF aware layer presumably means we are binary
1349 which is not what was requested - so we failed
1350 We _could_ push :crlf layer but so could caller
1351 */
1352 return FALSE;
14a5cf38 1353 }
6ce75a77 1354#endif
86e05cf2
NIS
1355 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1356 So code that used to be here is now in PerlIORaw_pushed().
03c0554d 1357 */
a0714e2c 1358 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
14a5cf38 1359 }
f5b9d040
NIS
1360}
1361
f5b9d040 1362int
e87a358a 1363PerlIO__close(pTHX_ PerlIO *f)
f5b9d040 1364{
37725cdc 1365 if (PerlIOValid(f)) {
46c461b5 1366 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
37725cdc
NIS
1367 if (tab && tab->Close)
1368 return (*tab->Close)(aTHX_ f);
1369 else
1370 return PerlIOBase_close(aTHX_ f);
1371 }
14a5cf38 1372 else {
93189314 1373 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1374 return -1;
1375 }
76ced9ad
NIS
1376}
1377
b931b1d9 1378int
e87a358a 1379Perl_PerlIO_close(pTHX_ PerlIO *f)
b931b1d9 1380{
de009b76 1381 const int code = PerlIO__close(aTHX_ f);
37725cdc
NIS
1382 while (PerlIOValid(f)) {
1383 PerlIO_pop(aTHX_ f);
f6c77cf1 1384 }
14a5cf38 1385 return code;
b931b1d9
NIS
1386}
1387
b931b1d9 1388int
e87a358a 1389Perl_PerlIO_fileno(pTHX_ PerlIO *f)
b931b1d9 1390{
97aff369 1391 dVAR;
b32dd47e 1392 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
b931b1d9
NIS
1393}
1394
1141d9f8
NIS
1395static const char *
1396PerlIO_context_layers(pTHX_ const char *mode)
1397{
97aff369 1398 dVAR;
14a5cf38
JH
1399 const char *type = NULL;
1400 /*
71200d45 1401 * Need to supply default layer info from open.pm
14a5cf38
JH
1402 */
1403 if (PL_curcop) {
dcda55fc 1404 SV * const layers = PL_curcop->cop_io;
14a5cf38
JH
1405 if (layers) {
1406 STRLEN len;
b83604b4 1407 type = SvPV_const(layers, len);
14a5cf38
JH
1408 if (type && mode[0] != 'r') {
1409 /*
71200d45 1410 * Skip to write part
14a5cf38 1411 */
dcda55fc 1412 const char * const s = strchr(type, 0);
eb160463 1413 if (s && (STRLEN)(s - type) < len) {
14a5cf38
JH
1414 type = s + 1;
1415 }
1416 }
1417 }
1418 }
1419 return type;
1141d9f8
NIS
1420}
1421
fcf2db38 1422static PerlIO_funcs *
2edd7e44
NIS
1423PerlIO_layer_from_ref(pTHX_ SV *sv)
1424{
97aff369 1425 dVAR;
14a5cf38 1426 /*
71200d45 1427 * For any scalar type load the handler which is bundled with perl
14a5cf38
JH
1428 */
1429 if (SvTYPE(sv) < SVt_PVAV)
6a245ed1 1430 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
14a5cf38
JH
1431
1432 /*
71200d45 1433 * For other types allow if layer is known but don't try and load it
14a5cf38
JH
1434 */
1435 switch (SvTYPE(sv)) {
1436 case SVt_PVAV:
6a245ed1 1437 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
14a5cf38 1438 case SVt_PVHV:
6a245ed1 1439 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
14a5cf38 1440 case SVt_PVCV:
6a245ed1 1441 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
14a5cf38 1442 case SVt_PVGV:
6a245ed1 1443 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
14a5cf38
JH
1444 }
1445 return NULL;
2edd7e44
NIS
1446}
1447
fcf2db38 1448PerlIO_list_t *
14a5cf38
JH
1449PerlIO_resolve_layers(pTHX_ const char *layers,
1450 const char *mode, int narg, SV **args)
1451{
97aff369 1452 dVAR;
14a5cf38
JH
1453 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1454 int incdef = 1;
a1ea730d 1455 if (!PL_perlio)
14a5cf38
JH
1456 PerlIO_stdstreams(aTHX);
1457 if (narg) {
dcda55fc 1458 SV * const arg = *args;
14a5cf38 1459 /*
71200d45
NIS
1460 * If it is a reference but not an object see if we have a handler
1461 * for it
14a5cf38
JH
1462 */
1463 if (SvROK(arg) && !sv_isobject(arg)) {
46c461b5 1464 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
14a5cf38 1465 if (handler) {
3a1ee7e8
NIS
1466 def = PerlIO_list_alloc(aTHX);
1467 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
14a5cf38
JH
1468 incdef = 0;
1469 }
1470 /*
e934609f 1471 * Don't fail if handler cannot be found :via(...) etc. may do
14a5cf38 1472 * something sensible else we will just stringfy and open
71200d45 1473 * resulting string.
14a5cf38
JH
1474 */
1475 }
1476 }
9fe371da 1477 if (!layers || !*layers)
14a5cf38
JH
1478 layers = PerlIO_context_layers(aTHX_ mode);
1479 if (layers && *layers) {
1480 PerlIO_list_t *av;
1481 if (incdef) {
de009b76 1482 IV i;
3a1ee7e8 1483 av = PerlIO_list_alloc(aTHX);
14a5cf38 1484 for (i = 0; i < def->cur; i++) {
3a1ee7e8 1485 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
14a5cf38
JH
1486 def->array[i].arg);
1487 }
1488 }
1489 else {
1490 av = def;
1491 }
0cff2cf3
NIS
1492 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1493 return av;
1494 }
1495 else {
1496 PerlIO_list_free(aTHX_ av);
1497 return (PerlIO_list_t *) NULL;
1498 }
14a5cf38
JH
1499 }
1500 else {
1501 if (incdef)
1502 def->refcnt++;
1503 return def;
1504 }
ee518936
NIS
1505}
1506
1507PerlIO *
14a5cf38
JH
1508PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1509 int imode, int perm, PerlIO *f, int narg, SV **args)
1510{
97aff369 1511 dVAR;
14a5cf38
JH
1512 if (!f && narg == 1 && *args == &PL_sv_undef) {
1513 if ((f = PerlIO_tmpfile())) {
9fe371da 1514 if (!layers || !*layers)
14a5cf38
JH
1515 layers = PerlIO_context_layers(aTHX_ mode);
1516 if (layers && *layers)
1517 PerlIO_apply_layers(aTHX_ f, mode, layers);
1518 }
1519 }
1520 else {
de009b76 1521 PerlIO_list_t *layera;
14a5cf38
JH
1522 IV n;
1523 PerlIO_funcs *tab = NULL;
04892f78 1524 if (PerlIOValid(f)) {
14a5cf38 1525 /*
71200d45
NIS
1526 * This is "reopen" - it is not tested as perl does not use it
1527 * yet
14a5cf38
JH
1528 */
1529 PerlIOl *l = *f;
3a1ee7e8 1530 layera = PerlIO_list_alloc(aTHX);
14a5cf38 1531 while (l) {
46c461b5 1532 SV * const arg = (l->tab->Getarg)
04892f78
NIS
1533 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1534 : &PL_sv_undef;
3a1ee7e8 1535 PerlIO_list_push(aTHX_ layera, l->tab, arg);
14a5cf38
JH
1536 l = *PerlIONext(&l);
1537 }
1538 }
1539 else {
1540 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
0cff2cf3
NIS
1541 if (!layera) {
1542 return NULL;
1543 }
14a5cf38
JH
1544 }
1545 /*
71200d45 1546 * Start at "top" of layer stack
14a5cf38
JH
1547 */
1548 n = layera->cur - 1;
1549 while (n >= 0) {
46c461b5 1550 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
14a5cf38
JH
1551 if (t && t->Open) {
1552 tab = t;
1553 break;
1554 }
1555 n--;
1556 }
1557 if (tab) {
1558 /*
71200d45 1559 * Found that layer 'n' can do opens - call it
14a5cf38 1560 */
7cf31beb 1561 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
3b8752bb 1562 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
7cf31beb 1563 }
14a5cf38 1564 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
355d3743
PD
1565 tab->name, layers ? layers : "(Null)", mode, fd,
1566 imode, perm, (void*)f, narg, (void*)args);
210e727c
JH
1567 if (tab->Open)
1568 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1569 f, narg, args);
1570 else {
1571 SETERRNO(EINVAL, LIB_INVARG);
1572 f = NULL;
1573 }
14a5cf38
JH
1574 if (f) {
1575 if (n + 1 < layera->cur) {
1576 /*
1577 * More layers above the one that we used to open -
71200d45 1578 * apply them now
14a5cf38 1579 */
d9dac8cd
NIS
1580 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1581 /* If pushing layers fails close the file */
1582 PerlIO_close(f);
14a5cf38
JH
1583 f = NULL;
1584 }
1585 }
1586 }
1587 }
3a1ee7e8 1588 PerlIO_list_free(aTHX_ layera);
14a5cf38
JH
1589 }
1590 return f;
ee518936 1591}
b931b1d9
NIS
1592
1593
9e353e3b 1594SSize_t
e87a358a 1595Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 1596{
b32dd47e 1597 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1598}
1599
313ca112 1600SSize_t
e87a358a 1601Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1602{
b32dd47e 1603 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1604}
1605
9e353e3b 1606SSize_t
e87a358a 1607Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1608{
b32dd47e 1609 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1610}
1611
6f9d8c32 1612int
e87a358a 1613Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
760ac839 1614{
b32dd47e 1615 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
760ac839
LW
1616}
1617
9e353e3b 1618Off_t
e87a358a 1619Perl_PerlIO_tell(pTHX_ PerlIO *f)
760ac839 1620{
b32dd47e 1621 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
760ac839
LW
1622}
1623
6f9d8c32 1624int
e87a358a 1625Perl_PerlIO_flush(pTHX_ PerlIO *f)
760ac839 1626{
97aff369 1627 dVAR;
14a5cf38
JH
1628 if (f) {
1629 if (*f) {
de009b76 1630 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1b7a0411
JH
1631
1632 if (tab && tab->Flush)
f62ce20a 1633 return (*tab->Flush) (aTHX_ f);
1b7a0411
JH
1634 else
1635 return 0; /* If no Flush defined, silently succeed. */
14a5cf38
JH
1636 }
1637 else {
fe5a182c 1638 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
93189314 1639 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1640 return -1;
1641 }
1642 }
1643 else {
1644 /*
1645 * Is it good API design to do flush-all on NULL, a potentially
1646 * errorneous input? Maybe some magical value (PerlIO*
1647 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1648 * things on fflush(NULL), but should we be bound by their design
71200d45 1649 * decisions? --jhi
14a5cf38 1650 */
a1ea730d 1651 PerlIO **table = &PL_perlio;
14a5cf38
JH
1652 int code = 0;
1653 while ((f = *table)) {
1654 int i;
1655 table = (PerlIO **) (f++);
1656 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1657 if (*f && PerlIO_flush(f) != 0)
1658 code = -1;
1659 f++;
1660 }
1661 }
1662 return code;
1663 }
760ac839
LW
1664}
1665
a9c883f6 1666void
f62ce20a 1667PerlIOBase_flush_linebuf(pTHX)
a9c883f6 1668{
97aff369 1669 dVAR;
a1ea730d 1670 PerlIO **table = &PL_perlio;
14a5cf38
JH
1671 PerlIO *f;
1672 while ((f = *table)) {
1673 int i;
1674 table = (PerlIO **) (f++);
1675 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1676 if (*f
1677 && (PerlIOBase(f)->
1678 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1679 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1680 PerlIO_flush(f);
1681 f++;
1682 }
a9c883f6 1683 }
a9c883f6
NIS
1684}
1685
06da4f11 1686int
e87a358a 1687Perl_PerlIO_fill(pTHX_ PerlIO *f)
06da4f11 1688{
b32dd47e 1689 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
06da4f11
NIS
1690}
1691
f3862f8b
NIS
1692int
1693PerlIO_isutf8(PerlIO *f)
1694{
1b7a0411
JH
1695 if (PerlIOValid(f))
1696 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1697 else
1698 SETERRNO(EBADF, SS_IVCHAN);
37725cdc 1699
1b7a0411 1700 return -1;
f3862f8b
NIS
1701}
1702
6f9d8c32 1703int
e87a358a 1704Perl_PerlIO_eof(pTHX_ PerlIO *f)
760ac839 1705{
b32dd47e 1706 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
9e353e3b
NIS
1707}
1708
9e353e3b 1709int
e87a358a 1710Perl_PerlIO_error(pTHX_ PerlIO *f)
9e353e3b 1711{
b32dd47e 1712 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
9e353e3b
NIS
1713}
1714
9e353e3b 1715void
e87a358a 1716Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
9e353e3b 1717{
b32dd47e 1718 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
9e353e3b
NIS
1719}
1720
9e353e3b 1721void
e87a358a 1722Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1723{
b32dd47e 1724 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
9e353e3b
NIS
1725}
1726
9e353e3b
NIS
1727int
1728PerlIO_has_base(PerlIO *f)
1729{
1b7a0411 1730 if (PerlIOValid(f)) {
46c461b5 1731 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1732
1733 if (tab)
1734 return (tab->Get_base != NULL);
1735 SETERRNO(EINVAL, LIB_INVARG);
1736 }
1737 else
1738 SETERRNO(EBADF, SS_IVCHAN);
1739
1740 return 0;
760ac839
LW
1741}
1742
9e353e3b
NIS
1743int
1744PerlIO_fast_gets(PerlIO *f)
760ac839 1745{
04892f78 1746 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
46c461b5 1747 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1748
1749 if (tab)
1750 return (tab->Set_ptrcnt != NULL);
1751 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1752 }
1b7a0411
JH
1753 else
1754 SETERRNO(EBADF, SS_IVCHAN);
1755
14a5cf38 1756 return 0;
9e353e3b
NIS
1757}
1758
9e353e3b
NIS
1759int
1760PerlIO_has_cntptr(PerlIO *f)
1761{
04892f78 1762 if (PerlIOValid(f)) {
46c461b5 1763 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1764
1765 if (tab)
1766 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1767 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1768 }
1b7a0411
JH
1769 else
1770 SETERRNO(EBADF, SS_IVCHAN);
1771
14a5cf38 1772 return 0;
9e353e3b
NIS
1773}
1774
9e353e3b
NIS
1775int
1776PerlIO_canset_cnt(PerlIO *f)
1777{
04892f78 1778 if (PerlIOValid(f)) {
46c461b5 1779 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1780
1781 if (tab)
1782 return (tab->Set_ptrcnt != NULL);
1783 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1784 }
1b7a0411
JH
1785 else
1786 SETERRNO(EBADF, SS_IVCHAN);
1787
14a5cf38 1788 return 0;
760ac839
LW
1789}
1790
888911fc 1791STDCHAR *
e87a358a 1792Perl_PerlIO_get_base(pTHX_ PerlIO *f)
760ac839 1793{
b32dd47e 1794 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
9e353e3b
NIS
1795}
1796
9e353e3b 1797int
e87a358a 1798Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 1799{
b32dd47e 1800 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
9e353e3b
NIS
1801}
1802
9e353e3b 1803STDCHAR *
e87a358a 1804Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
9e353e3b 1805{
b32dd47e 1806 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
9e353e3b
NIS
1807}
1808
05d1247b 1809int
e87a358a 1810Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1811{
b32dd47e 1812 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
9e353e3b
NIS
1813}
1814
9e353e3b 1815void
e87a358a 1816Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1817{
b32dd47e 1818 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
9e353e3b
NIS
1819}
1820
9e353e3b 1821void
e87a358a 1822Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1823{
b32dd47e 1824 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
9e353e3b
NIS
1825}
1826
4ec2216f 1827
9e353e3b 1828/*--------------------------------------------------------------------------------------*/
14a5cf38 1829/*
71200d45 1830 * utf8 and raw dummy layers
14a5cf38 1831 */
dfebf958 1832
26fb694e 1833IV
2dc2558e 1834PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
26fb694e 1835{
8772537c
AL
1836 PERL_UNUSED_ARG(mode);
1837 PERL_UNUSED_ARG(arg);
00f51856 1838 if (PerlIOValid(f)) {
14a5cf38
JH
1839 if (tab->kind & PERLIO_K_UTF8)
1840 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1841 else
1842 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1843 return 0;
1844 }
1845 return -1;
26fb694e
NIS
1846}
1847
27da23d5 1848PERLIO_FUNCS_DECL(PerlIO_utf8) = {
2dc2558e 1849 sizeof(PerlIO_funcs),
14a5cf38 1850 "utf8",
2dc2558e 1851 0,
6874a2de 1852 PERLIO_K_DUMMY | PERLIO_K_UTF8,
14a5cf38
JH
1853 PerlIOUtf8_pushed,
1854 NULL,
1855 NULL,
1856 NULL,
1857 NULL,
1858 NULL,
1859 NULL,
1860 NULL,
1861 NULL,
1862 NULL,
1863 NULL,
de009b76
AL
1864 NULL,
1865 NULL,
22569500
NIS
1866 NULL, /* flush */
1867 NULL, /* fill */
14a5cf38
JH
1868 NULL,
1869 NULL,
1870 NULL,
1871 NULL,
22569500
NIS
1872 NULL, /* get_base */
1873 NULL, /* get_bufsiz */
1874 NULL, /* get_ptr */
1875 NULL, /* get_cnt */
1876 NULL, /* set_ptrcnt */
26fb694e
NIS
1877};
1878
27da23d5 1879PERLIO_FUNCS_DECL(PerlIO_byte) = {
2dc2558e 1880 sizeof(PerlIO_funcs),
14a5cf38 1881 "bytes",
2dc2558e 1882 0,
14a5cf38
JH
1883 PERLIO_K_DUMMY,
1884 PerlIOUtf8_pushed,
1885 NULL,
1886 NULL,
1887 NULL,
1888 NULL,
1889 NULL,
1890 NULL,
1891 NULL,
1892 NULL,
1893 NULL,
1894 NULL,
de009b76
AL
1895 NULL,
1896 NULL,
22569500
NIS
1897 NULL, /* flush */
1898 NULL, /* fill */
14a5cf38
JH
1899 NULL,
1900 NULL,
1901 NULL,
1902 NULL,
22569500
NIS
1903 NULL, /* get_base */
1904 NULL, /* get_bufsiz */
1905 NULL, /* get_ptr */
1906 NULL, /* get_cnt */
1907 NULL, /* set_ptrcnt */
dfebf958
NIS
1908};
1909
1910PerlIO *
14a5cf38
JH
1911PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1912 IV n, const char *mode, int fd, int imode, int perm,
1913 PerlIO *old, int narg, SV **args)
dfebf958 1914{
8772537c
AL
1915 PerlIO_funcs * const tab = PerlIO_default_btm();
1916 PERL_UNUSED_ARG(self);
210e727c
JH
1917 if (tab && tab->Open)
1918 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1919 old, narg, args);
1920 SETERRNO(EINVAL, LIB_INVARG);
1921 return NULL;
dfebf958
NIS
1922}
1923
27da23d5 1924PERLIO_FUNCS_DECL(PerlIO_raw) = {
2dc2558e 1925 sizeof(PerlIO_funcs),
14a5cf38 1926 "raw",
2dc2558e 1927 0,
14a5cf38
JH
1928 PERLIO_K_DUMMY,
1929 PerlIORaw_pushed,
1930 PerlIOBase_popped,
1931 PerlIORaw_open,
1932 NULL,
1933 NULL,
1934 NULL,
1935 NULL,
1936 NULL,
1937 NULL,
1938 NULL,
1939 NULL,
de009b76
AL
1940 NULL,
1941 NULL,
22569500
NIS
1942 NULL, /* flush */
1943 NULL, /* fill */
14a5cf38
JH
1944 NULL,
1945 NULL,
1946 NULL,
1947 NULL,
22569500
NIS
1948 NULL, /* get_base */
1949 NULL, /* get_bufsiz */
1950 NULL, /* get_ptr */
1951 NULL, /* get_cnt */
1952 NULL, /* set_ptrcnt */
dfebf958
NIS
1953};
1954/*--------------------------------------------------------------------------------------*/
1955/*--------------------------------------------------------------------------------------*/
14a5cf38 1956/*
71200d45 1957 * "Methods" of the "base class"
14a5cf38 1958 */
9e353e3b
NIS
1959
1960IV
f62ce20a 1961PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1962{
04892f78 1963 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
9e353e3b
NIS
1964}
1965
f5b9d040 1966char *
81428673 1967PerlIO_modestr(PerlIO * f, char *buf)
14a5cf38
JH
1968{
1969 char *s = buf;
81428673 1970 if (PerlIOValid(f)) {
de009b76 1971 const IV flags = PerlIOBase(f)->flags;
81428673
NIS
1972 if (flags & PERLIO_F_APPEND) {
1973 *s++ = 'a';
1974 if (flags & PERLIO_F_CANREAD) {
1975 *s++ = '+';
1976 }
14a5cf38 1977 }
81428673
NIS
1978 else if (flags & PERLIO_F_CANREAD) {
1979 *s++ = 'r';
1980 if (flags & PERLIO_F_CANWRITE)
1981 *s++ = '+';
1982 }
1983 else if (flags & PERLIO_F_CANWRITE) {
1984 *s++ = 'w';
1985 if (flags & PERLIO_F_CANREAD) {
1986 *s++ = '+';
1987 }
14a5cf38 1988 }
35990314 1989#ifdef PERLIO_USING_CRLF
81428673
NIS
1990 if (!(flags & PERLIO_F_CRLF))
1991 *s++ = 'b';
5f1a76d0 1992#endif
81428673 1993 }
14a5cf38
JH
1994 *s = '\0';
1995 return buf;
f5b9d040
NIS
1996}
1997
81428673 1998
76ced9ad 1999IV
2dc2558e 2000PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
9e353e3b 2001{
de009b76 2002 PerlIOl * const l = PerlIOBase(f);
8772537c 2003 PERL_UNUSED_ARG(arg);
de009b76 2004
14a5cf38
JH
2005 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2006 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2007 if (tab->Set_ptrcnt != NULL)
2008 l->flags |= PERLIO_F_FASTGETS;
2009 if (mode) {
3b6c1aba 2010 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2011 mode++;
2012 switch (*mode++) {
2013 case 'r':
2014 l->flags |= PERLIO_F_CANREAD;
2015 break;
2016 case 'a':
2017 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2018 break;
2019 case 'w':
2020 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2021 break;
2022 default:
93189314 2023 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2024 return -1;
2025 }
2026 while (*mode) {
2027 switch (*mode++) {
2028 case '+':
2029 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2030 break;
2031 case 'b':
2032 l->flags &= ~PERLIO_F_CRLF;
2033 break;
2034 case 't':
2035 l->flags |= PERLIO_F_CRLF;
2036 break;
2037 default:
93189314 2038 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2039 return -1;
2040 }
2041 }
2042 }
2043 else {
2044 if (l->next) {
2045 l->flags |= l->next->flags &
2046 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2047 PERLIO_F_APPEND);
2048 }
2049 }
5e2ab84b 2050#if 0
14a5cf38
JH
2051 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2052 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2053 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 2054#endif
14a5cf38 2055 return 0;
76ced9ad
NIS
2056}
2057
2058IV
f62ce20a 2059PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 2060{
8772537c 2061 PERL_UNUSED_ARG(f);
14a5cf38 2062 return 0;
760ac839
LW
2063}
2064
9e353e3b 2065SSize_t
f62ce20a 2066PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2067{
14a5cf38 2068 /*
71200d45 2069 * Save the position as current head considers it
14a5cf38 2070 */
de009b76 2071 const Off_t old = PerlIO_tell(f);
a0714e2c 2072 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
14a5cf38 2073 PerlIOSelf(f, PerlIOBuf)->posn = old;
de009b76 2074 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
9e353e3b
NIS
2075}
2076
f6c77cf1 2077SSize_t
f62ce20a 2078PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 2079{
14a5cf38
JH
2080 STDCHAR *buf = (STDCHAR *) vbuf;
2081 if (f) {
263df5f1
JH
2082 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2083 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2084 SETERRNO(EBADF, SS_IVCHAN);
2085 return 0;
2086 }
14a5cf38 2087 while (count > 0) {
93c2c2ec
IZ
2088 get_cnt:
2089 {
14a5cf38
JH
2090 SSize_t avail = PerlIO_get_cnt(f);
2091 SSize_t take = 0;
2092 if (avail > 0)
eb160463 2093 take = ((SSize_t)count < avail) ? count : avail;
14a5cf38
JH
2094 if (take > 0) {
2095 STDCHAR *ptr = PerlIO_get_ptr(f);
2096 Copy(ptr, buf, take, STDCHAR);
2097 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2098 count -= take;
2099 buf += take;
93c2c2ec
IZ
2100 if (avail == 0) /* set_ptrcnt could have reset avail */
2101 goto get_cnt;
14a5cf38
JH
2102 }
2103 if (count > 0 && avail <= 0) {
2104 if (PerlIO_fill(f) != 0)
2105 break;
2106 }
93c2c2ec 2107 }
14a5cf38
JH
2108 }
2109 return (buf - (STDCHAR *) vbuf);
2110 }
f6c77cf1 2111 return 0;
f6c77cf1
NIS
2112}
2113
9e353e3b 2114IV
f62ce20a 2115PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 2116{
8772537c 2117 PERL_UNUSED_ARG(f);
14a5cf38 2118 return 0;
9e353e3b
NIS
2119}
2120
2121IV
f62ce20a 2122PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 2123{
8772537c 2124 PERL_UNUSED_ARG(f);
14a5cf38 2125 return -1;
06da4f11
NIS
2126}
2127
2128IV
f62ce20a 2129PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 2130{
37725cdc
NIS
2131 IV code = -1;
2132 if (PerlIOValid(f)) {
2133 PerlIO *n = PerlIONext(f);
2134 code = PerlIO_flush(f);
2135 PerlIOBase(f)->flags &=
2136 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2137 while (PerlIOValid(n)) {
de009b76 2138 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
37725cdc
NIS
2139 if (tab && tab->Close) {
2140 if ((*tab->Close)(aTHX_ n) != 0)
2141 code = -1;
2142 break;
2143 }
2144 else {
2145 PerlIOBase(n)->flags &=
2146 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2147 }
2148 n = PerlIONext(n);
2149 }
2150 }
2151 else {
2152 SETERRNO(EBADF, SS_IVCHAN);
2153 }
14a5cf38 2154 return code;
9e353e3b
NIS
2155}
2156
2157IV
f62ce20a 2158PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 2159{
04892f78 2160 if (PerlIOValid(f)) {
14a5cf38
JH
2161 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2162 }
2163 return 1;
9e353e3b
NIS
2164}
2165
2166IV
f62ce20a 2167PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 2168{
04892f78 2169 if (PerlIOValid(f)) {
14a5cf38
JH
2170 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2171 }
2172 return 1;
9e353e3b
NIS
2173}
2174
2175void
f62ce20a 2176PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 2177{
04892f78 2178 if (PerlIOValid(f)) {
dcda55fc 2179 PerlIO * const n = PerlIONext(f);
14a5cf38 2180 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
04892f78 2181 if (PerlIOValid(n))
14a5cf38
JH
2182 PerlIO_clearerr(n);
2183 }
9e353e3b
NIS
2184}
2185
2186void
f62ce20a 2187PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 2188{
04892f78 2189 if (PerlIOValid(f)) {
14a5cf38
JH
2190 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2191 }
9e353e3b
NIS
2192}
2193
93a8090d
NIS
2194SV *
2195PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2196{
2197 if (!arg)
a0714e2c 2198 return NULL;
93a8090d
NIS
2199#ifdef sv_dup
2200 if (param) {
2201 return sv_dup(arg, param);
2202 }
2203 else {
2204 return newSVsv(arg);
2205 }
2206#else
1b6737cc 2207 PERL_UNUSED_ARG(param);
93a8090d
NIS
2208 return newSVsv(arg);
2209#endif
2210}
2211
2212PerlIO *
ecdeb87c 2213PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d 2214{
1b6737cc 2215 PerlIO * const nexto = PerlIONext(o);
04892f78 2216 if (PerlIOValid(nexto)) {
de009b76 2217 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
37725cdc
NIS
2218 if (tab && tab->Dup)
2219 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2220 else
2221 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
93a8090d
NIS
2222 }
2223 if (f) {
dcda55fc 2224 PerlIO_funcs * const self = PerlIOBase(o)->tab;
210e727c 2225 SV *arg;
93a8090d 2226 char buf[8];
fe5a182c
JH
2227 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2228 self->name, (void*)f, (void*)o, (void*)param);
210e727c
JH
2229 if (self->Getarg)
2230 arg = (*self->Getarg)(aTHX_ o, param, flags);
2231 else {
a0714e2c 2232 arg = NULL;
93a8090d
NIS
2233 }
2234 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
ecdeb87c 2235 if (arg) {
93a8090d
NIS
2236 SvREFCNT_dec(arg);
2237 }
2238 }
2239 return f;
2240}
2241
168d5872 2242#ifdef USE_THREADS
93a8090d 2243perl_mutex PerlIO_mutex;
93a8090d 2244#endif
27da23d5
JH
2245
2246/* PL_perlio_fd_refcnt[] is in intrpvar.h */
93a8090d 2247
22c96fc1
NC
2248/* Must be called with PerlIO_mutex locked. */
2249static void
2250S_more_refcounted_fds(pTHX_ const int new_fd) {
2251 const int old_max = PL_perlio_fd_refcnt_size;
2252 const int new_max = 16 + (new_fd & 15);
2253 int *new_array;
2254
2255 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2256 old_max, new_fd, new_max);
2257
2258 if (new_fd < old_max) {
2259 return;
2260 }
2261
2262 new_array
2263 = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2264
2265 if (!new_array) {
2266#ifdef USE_THREADS
2267 MUTEX_UNLOCK(&PerlIO_mutex);
2268#endif
2269 /* Can't use PerlIO to write as it allocates memory */
2270 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2271 PL_no_mem, strlen(PL_no_mem));
2272 my_exit(1);
2273 }
2274
2275 PL_perlio_fd_refcnt_size = new_max;
2276 PL_perlio_fd_refcnt = new_array;
2277
2278 PerlIO_debug("Zeroing %p, %d\n", new_array + old_max, new_max - old_max);
2279
2280 Zero(new_array + old_max, new_max - old_max, int);
2281}
2282
2283
93a8090d
NIS
2284void
2285PerlIO_init(pTHX)
2286{
2287 /* Place holder for stdstreams call ??? */
168d5872 2288#ifdef USE_THREADS
27da23d5 2289 MUTEX_INIT(&PerlIO_mutex);
93a8090d
NIS
2290#endif
2291}
2292
168d5872
NIS
2293void
2294PerlIOUnix_refcnt_inc(int fd)
2295{
27da23d5 2296 dTHX;
22c96fc1 2297 if (fd >= 0) {
97aff369 2298 dVAR;
22c96fc1 2299
168d5872
NIS
2300#ifdef USE_THREADS
2301 MUTEX_LOCK(&PerlIO_mutex);
2302#endif
22c96fc1
NC
2303 if (fd >= PL_perlio_fd_refcnt_size)
2304 S_more_refcounted_fds(aTHX_ fd);
2305
27da23d5
JH
2306 PL_perlio_fd_refcnt[fd]++;
2307 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
22c96fc1 2308
168d5872
NIS
2309#ifdef USE_THREADS
2310 MUTEX_UNLOCK(&PerlIO_mutex);
2311#endif
2312 }
2313}
2314
168d5872
NIS
2315int
2316PerlIOUnix_refcnt_dec(int fd)
2317{
27da23d5 2318 dTHX;
168d5872 2319 int cnt = 0;
22c96fc1 2320 if (fd >= 0) {
97aff369 2321 dVAR;
168d5872
NIS
2322#ifdef USE_THREADS
2323 MUTEX_LOCK(&PerlIO_mutex);
2324#endif
22c96fc1
NC
2325 /* XXX should this be a panic? */
2326 if (fd >= PL_perlio_fd_refcnt_size)
2327 S_more_refcounted_fds(aTHX_ fd);
2328
2329 /* XXX should this be a panic if it drops below 0? */
27da23d5 2330 cnt = --PL_perlio_fd_refcnt[fd];
168d5872
NIS
2331 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2332#ifdef USE_THREADS
2333 MUTEX_UNLOCK(&PerlIO_mutex);
2334#endif
2335 }
2336 return cnt;
2337}
2338
694c95cf
JH
2339void
2340PerlIO_cleanup(pTHX)
2341{
97aff369 2342 dVAR;
694c95cf
JH
2343 int i;
2344#ifdef USE_ITHREADS
9f4bd222
NIS
2345 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2346#else
2347 PerlIO_debug("Cleanup layers\n");
694c95cf
JH
2348#endif
2349 /* Raise STDIN..STDERR refcount so we don't close them */
2350 for (i=0; i < 3; i++)
2351 PerlIOUnix_refcnt_inc(i);
2352 PerlIO_cleantable(aTHX_ &PL_perlio);
2353 /* Restore STDIN..STDERR refcount */
2354 for (i=0; i < 3; i++)
2355 PerlIOUnix_refcnt_dec(i);
9f4bd222
NIS
2356
2357 if (PL_known_layers) {
2358 PerlIO_list_free(aTHX_ PL_known_layers);
2359 PL_known_layers = NULL;
2360 }
27da23d5 2361 if (PL_def_layerlist) {
9f4bd222
NIS
2362 PerlIO_list_free(aTHX_ PL_def_layerlist);
2363 PL_def_layerlist = NULL;
2364 }
694c95cf
JH
2365}
2366
2367
2368
9e353e3b 2369/*--------------------------------------------------------------------------------------*/
14a5cf38 2370/*
71200d45 2371 * Bottom-most level for UNIX-like case
14a5cf38 2372 */
9e353e3b 2373
14a5cf38 2374typedef struct {
22569500
NIS
2375 struct _PerlIO base; /* The generic part */
2376 int fd; /* UNIX like file descriptor */
2377 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2378} PerlIOUnix;
2379
6f9d8c32 2380int
9e353e3b 2381PerlIOUnix_oflags(const char *mode)
760ac839 2382{
14a5cf38 2383 int oflags = -1;
3b6c1aba 2384 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
14a5cf38
JH
2385 mode++;
2386 switch (*mode) {
2387 case 'r':
2388 oflags = O_RDONLY;
2389 if (*++mode == '+') {
2390 oflags = O_RDWR;
2391 mode++;
2392 }
2393 break;
2394
2395 case 'w':
2396 oflags = O_CREAT | O_TRUNC;
2397 if (*++mode == '+') {
2398 oflags |= O_RDWR;
2399 mode++;
2400 }
2401 else
2402 oflags |= O_WRONLY;
2403 break;
2404
2405 case 'a':
2406 oflags = O_CREAT | O_APPEND;
2407 if (*++mode == '+') {
2408 oflags |= O_RDWR;
2409 mode++;
2410 }
2411 else
2412 oflags |= O_WRONLY;
2413 break;
2414 }
2415 if (*mode == 'b') {
2416 oflags |= O_BINARY;
2417 oflags &= ~O_TEXT;
2418 mode++;
2419 }
2420 else if (*mode == 't') {
2421 oflags |= O_TEXT;
2422 oflags &= ~O_BINARY;
2423 mode++;
2424 }
2425 /*
71200d45 2426 * Always open in binary mode
14a5cf38
JH
2427 */
2428 oflags |= O_BINARY;
2429 if (*mode || oflags == -1) {
93189314 2430 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2431 oflags = -1;
2432 }
2433 return oflags;
9e353e3b
NIS
2434}
2435
2436IV
f62ce20a 2437PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2438{
14a5cf38 2439 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2440}
2441
aa063c35
NIS
2442static void
2443PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
4b803d04 2444{
de009b76 2445 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
6caa5a9c 2446#if defined(WIN32)
aa063c35
NIS
2447 Stat_t st;
2448 if (PerlLIO_fstat(fd, &st) == 0) {
6caa5a9c 2449 if (!S_ISREG(st.st_mode)) {
aa063c35 2450 PerlIO_debug("%d is not regular file\n",fd);
6caa5a9c
NIS
2451 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2452 }
aa063c35
NIS
2453 else {
2454 PerlIO_debug("%d _is_ a regular file\n",fd);
2455 }
6caa5a9c
NIS
2456 }
2457#endif
aa063c35
NIS
2458 s->fd = fd;
2459 s->oflags = imode;
2460 PerlIOUnix_refcnt_inc(fd);
2461}
2462
2463IV
2464PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2465{
2466 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
14a5cf38 2467 if (*PerlIONext(f)) {
4b069b44 2468 /* We never call down so do any pending stuff now */
03c0554d 2469 PerlIO_flush(PerlIONext(f));
14a5cf38 2470 /*
71200d45 2471 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2472 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2473 * Should the value on NULL mode be 0 or -1?
14a5cf38 2474 */
acbd16bf 2475 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
aa063c35 2476 mode ? PerlIOUnix_oflags(mode) : -1);
14a5cf38
JH
2477 }
2478 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
6caa5a9c 2479
14a5cf38 2480 return code;
4b803d04
NIS
2481}
2482
c2fcde81
JH
2483IV
2484PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2485{
de009b76 2486 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
0723351e 2487 Off_t new_loc;
c2fcde81
JH
2488 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2489#ifdef ESPIPE
2490 SETERRNO(ESPIPE, LIB_INVARG);
2491#else
2492 SETERRNO(EINVAL, LIB_INVARG);
2493#endif
2494 return -1;
2495 }
0723351e
NC
2496 new_loc = PerlLIO_lseek(fd, offset, whence);
2497 if (new_loc == (Off_t) - 1)
dcda55fc 2498 return -1;
c2fcde81
JH
2499 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2500 return 0;
2501}
2502
9e353e3b 2503PerlIO *
14a5cf38
JH
2504PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2505 IV n, const char *mode, int fd, int imode,
2506 int perm, PerlIO *f, int narg, SV **args)
2507{
d9dac8cd 2508 if (PerlIOValid(f)) {
14a5cf38 2509 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2510 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2511 }
2512 if (narg > 0) {
3b6c1aba 2513 if (*mode == IoTYPE_NUMERIC)
14a5cf38
JH
2514 mode++;
2515 else {
2516 imode = PerlIOUnix_oflags(mode);
2517 perm = 0666;
2518 }
2519 if (imode != -1) {
e62f0680 2520 const char *path = SvPV_nolen_const(*args);
14a5cf38
JH
2521 fd = PerlLIO_open3(path, imode, perm);
2522 }
2523 }
2524 if (fd >= 0) {
3b6c1aba 2525 if (*mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2526 mode++;
2527 if (!f) {
2528 f = PerlIO_allocate(aTHX);
d9dac8cd
NIS
2529 }
2530 if (!PerlIOValid(f)) {
a33cf58c
NIS
2531 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2532 return NULL;
2533 }
d9dac8cd 2534 }
aa063c35 2535 PerlIOUnix_setfd(aTHX_ f, fd, imode);
14a5cf38 2536 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
c2fcde81
JH
2537 if (*mode == IoTYPE_APPEND)
2538 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
14a5cf38
JH
2539 return f;
2540 }
2541 else {
2542 if (f) {
2543 /*
71200d45 2544 * FIXME: pop layers ???
14a5cf38
JH
2545 */
2546 }
2547 return NULL;
2548 }
9e353e3b
NIS
2549}
2550
71200d45 2551PerlIO *
ecdeb87c 2552PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 2553{
dcda55fc 2554 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2555 int fd = os->fd;
ecdeb87c
NIS
2556 if (flags & PERLIO_DUP_FD) {
2557 fd = PerlLIO_dup(fd);
2558 }
22c96fc1 2559 if (fd >= 0) {
ecdeb87c 2560 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2561 if (f) {
2562 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
aa063c35 2563 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
71200d45
NIS
2564 return f;
2565 }
71200d45
NIS
2566 }
2567 return NULL;
2568}
2569
2570
9e353e3b 2571SSize_t
f62ce20a 2572PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2573{
97aff369 2574 dVAR;
de009b76 2575 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2576#ifdef PERLIO_STD_SPECIAL
2577 if (fd == 0)
2578 return PERLIO_STD_IN(fd, vbuf, count);
2579#endif
81428673 2580 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
1fd8f4ce 2581 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
263df5f1 2582 return 0;
1fd8f4ce 2583 }
14a5cf38 2584 while (1) {
b464bac0 2585 const SSize_t len = PerlLIO_read(fd, vbuf, count);
14a5cf38 2586 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2587 if (len < 0) {
2588 if (errno != EAGAIN) {
2589 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2590 }
2591 }
2592 else if (len == 0 && count != 0) {
14a5cf38 2593 PerlIOBase(f)->flags |= PERLIO_F_EOF;
ba85f2ea
NIS
2594 SETERRNO(0,0);
2595 }
14a5cf38
JH
2596 return len;
2597 }
2598 PERL_ASYNC_CHECK();
2599 }
b464bac0 2600 /*NOTREACHED*/
9e353e3b
NIS
2601}
2602
2603SSize_t
f62ce20a 2604PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2605{
97aff369 2606 dVAR;
de009b76 2607 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2608#ifdef PERLIO_STD_SPECIAL
2609 if (fd == 1 || fd == 2)
2610 return PERLIO_STD_OUT(fd, vbuf, count);
2611#endif
14a5cf38 2612 while (1) {
de009b76 2613 const SSize_t len = PerlLIO_write(fd, vbuf, count);
14a5cf38 2614 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2615 if (len < 0) {
2616 if (errno != EAGAIN) {
2617 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2618 }
2619 }
14a5cf38
JH
2620 return len;
2621 }
2622 PERL_ASYNC_CHECK();
06da4f11 2623 }
1b6737cc 2624 /*NOTREACHED*/
9e353e3b
NIS
2625}
2626
9e353e3b 2627Off_t
f62ce20a 2628PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2629{
14a5cf38 2630 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2631}
2632
71200d45 2633
9e353e3b 2634IV
f62ce20a 2635PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2636{
97aff369 2637 dVAR;
de009b76 2638 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
14a5cf38 2639 int code = 0;
168d5872
NIS
2640 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2641 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2642 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2643 return 0;
22569500 2644 }
93a8090d
NIS
2645 }
2646 else {
93189314 2647 SETERRNO(EBADF,SS_IVCHAN);
93a8090d
NIS
2648 return -1;
2649 }
14a5cf38
JH
2650 while (PerlLIO_close(fd) != 0) {
2651 if (errno != EINTR) {
2652 code = -1;
2653 break;
2654 }
2655 PERL_ASYNC_CHECK();
2656 }
2657 if (code == 0) {
2658 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2659 }
2660 return code;
9e353e3b
NIS
2661}
2662
27da23d5 2663PERLIO_FUNCS_DECL(PerlIO_unix) = {
2dc2558e 2664 sizeof(PerlIO_funcs),
14a5cf38
JH
2665 "unix",
2666 sizeof(PerlIOUnix),
2667 PERLIO_K_RAW,
2668 PerlIOUnix_pushed,
44798d05 2669 PerlIOBase_popped,
14a5cf38 2670 PerlIOUnix_open,
86e05cf2 2671 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2672 NULL,
2673 PerlIOUnix_fileno,
71200d45 2674 PerlIOUnix_dup,
14a5cf38
JH
2675 PerlIOUnix_read,
2676 PerlIOBase_unread,
2677 PerlIOUnix_write,
2678 PerlIOUnix_seek,
2679 PerlIOUnix_tell,
2680 PerlIOUnix_close,
22569500
NIS
2681 PerlIOBase_noop_ok, /* flush */
2682 PerlIOBase_noop_fail, /* fill */
14a5cf38
JH
2683 PerlIOBase_eof,
2684 PerlIOBase_error,
2685 PerlIOBase_clearerr,
2686 PerlIOBase_setlinebuf,
22569500
NIS
2687 NULL, /* get_base */
2688 NULL, /* get_bufsiz */
2689 NULL, /* get_ptr */
2690 NULL, /* get_cnt */
2691 NULL, /* set_ptrcnt */
9e353e3b
NIS
2692};
2693
2694/*--------------------------------------------------------------------------------------*/
14a5cf38 2695/*
71200d45 2696 * stdio as a layer
14a5cf38 2697 */
9e353e3b 2698
313e59c8
NIS
2699#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2700/* perl5.8 - This ensures the last minute VMS ungetc fix is not
2701 broken by the last second glibc 2.3 fix
2702 */
2703#define STDIO_BUFFER_WRITABLE
2704#endif
2705
2706
14a5cf38
JH
2707typedef struct {
2708 struct _PerlIO base;
22569500 2709 FILE *stdio; /* The stream */
9e353e3b
NIS
2710} PerlIOStdio;
2711
2712IV
f62ce20a 2713PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2714{
c4420975
AL
2715 if (PerlIOValid(f)) {
2716 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2717 if (s)
2718 return PerlSIO_fileno(s);
439ba545
NIS
2719 }
2720 errno = EBADF;
2721 return -1;
9e353e3b
NIS
2722}
2723
766a733e 2724char *
14a5cf38
JH
2725PerlIOStdio_mode(const char *mode, char *tmode)
2726{
de009b76 2727 char * const ret = tmode;
a0625d38
SR
2728 if (mode) {
2729 while (*mode) {
2730 *tmode++ = *mode++;
2731 }
14a5cf38 2732 }
95005ad8 2733#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
6ce75a77
JH
2734 *tmode++ = 'b';
2735#endif
14a5cf38
JH
2736 *tmode = '\0';
2737 return ret;
2738}
2739
4b803d04 2740IV
2dc2558e 2741PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2742{
1fd8f4ce
NIS
2743 PerlIO *n;
2744 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
46c461b5 2745 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
1fd8f4ce
NIS
2746 if (toptab == tab) {
2747 /* Top is already stdio - pop self (duplicate) and use original */
2748 PerlIO_pop(aTHX_ f);
2749 return 0;
2750 } else {
de009b76 2751 const int fd = PerlIO_fileno(n);
1fd8f4ce
NIS
2752 char tmode[8];
2753 FILE *stdio;
81428673 2754 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
1fd8f4ce
NIS
2755 mode = PerlIOStdio_mode(mode, tmode)))) {
2756 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2757 /* We never call down so do any pending stuff now */
2758 PerlIO_flush(PerlIONext(f));
81428673 2759 }
1fd8f4ce
NIS
2760 else {
2761 return -1;
2762 }
2763 }
14a5cf38 2764 }
2dc2558e 2765 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4b803d04
NIS
2766}
2767
22569500 2768
9e353e3b 2769PerlIO *
4b069b44 2770PerlIO_importFILE(FILE *stdio, const char *mode)
9e353e3b 2771{
14a5cf38
JH
2772 dTHX;
2773 PerlIO *f = NULL;
2774 if (stdio) {
22569500 2775 PerlIOStdio *s;
4b069b44
NIS
2776 if (!mode || !*mode) {
2777 /* We need to probe to see how we can open the stream
2778 so start with read/write and then try write and read
2779 we dup() so that we can fclose without loosing the fd.
2780
2781 Note that the errno value set by a failing fdopen
2782 varies between stdio implementations.
2783 */
de009b76 2784 const int fd = PerlLIO_dup(fileno(stdio));
a33cf58c 2785 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
4b069b44 2786 if (!f2) {
a33cf58c 2787 f2 = PerlSIO_fdopen(fd, (mode = "w"));
4b069b44
NIS
2788 }
2789 if (!f2) {
a33cf58c 2790 f2 = PerlSIO_fdopen(fd, (mode = "r"));
4b069b44
NIS
2791 }
2792 if (!f2) {
2793 /* Don't seem to be able to open */
2794 PerlLIO_close(fd);
2795 return f;
2796 }
2797 fclose(f2);
22569500 2798 }
a0714e2c 2799 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
a33cf58c
NIS
2800 s = PerlIOSelf(f, PerlIOStdio);
2801 s->stdio = stdio;
2802 }
14a5cf38
JH
2803 }
2804 return f;
9e353e3b
NIS
2805}
2806
2807PerlIO *
14a5cf38
JH
2808PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2809 IV n, const char *mode, int fd, int imode,
2810 int perm, PerlIO *f, int narg, SV **args)
2811{
2812 char tmode[8];
d9dac8cd 2813 if (PerlIOValid(f)) {
dcda55fc
AL
2814 const char * const path = SvPV_nolen_const(*args);
2815 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2816 FILE *stdio;
2817 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2818 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2819 s->stdio);
2820 if (!s->stdio)
2821 return NULL;
2822 s->stdio = stdio;
1751d015 2823 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2824 return f;
2825 }
2826 else {
2827 if (narg > 0) {
dcda55fc 2828 const char * const path = SvPV_nolen_const(*args);
3b6c1aba 2829 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
2830 mode++;
2831 fd = PerlLIO_open3(path, imode, perm);
2832 }
2833 else {
95005ad8
GH
2834 FILE *stdio;
2835 bool appended = FALSE;
2836#ifdef __CYGWIN__
2837 /* Cygwin wants its 'b' early. */
2838 appended = TRUE;
2839 mode = PerlIOStdio_mode(mode, tmode);
2840#endif
2841 stdio = PerlSIO_fopen(path, mode);
6f0313ac
JH
2842 if (stdio) {
2843 PerlIOStdio *s;
2844 if (!f) {
2845 f = PerlIO_allocate(aTHX);
2846 }
95005ad8
GH
2847 if (!appended)
2848 mode = PerlIOStdio_mode(mode, tmode);
2849 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2850 if (f) {
6f0313ac
JH
2851 s = PerlIOSelf(f, PerlIOStdio);
2852 s->stdio = stdio;
2853 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2854 }
2855 return f;
2856 }
2857 else {
2858 return NULL;
2859 }
14a5cf38
JH
2860 }
2861 }
2862 if (fd >= 0) {
2863 FILE *stdio = NULL;
2864 int init = 0;
3b6c1aba 2865 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
2866 init = 1;
2867 mode++;
2868 }
2869 if (init) {
2870 switch (fd) {
2871 case 0:
2872 stdio = PerlSIO_stdin;
2873 break;
2874 case 1:
2875 stdio = PerlSIO_stdout;
2876 break;
2877 case 2:
2878 stdio = PerlSIO_stderr;
2879 break;
2880 }
2881 }
2882 else {
2883 stdio = PerlSIO_fdopen(fd, mode =
2884 PerlIOStdio_mode(mode, tmode));
2885 }
2886 if (stdio) {
d9dac8cd
NIS
2887 if (!f) {
2888 f = PerlIO_allocate(aTHX);
2889 }
a33cf58c 2890 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
dcda55fc 2891 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
a33cf58c
NIS
2892 s->stdio = stdio;
2893 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2894 }
14a5cf38
JH
2895 return f;
2896 }
2897 }
2898 }
ee518936 2899 return NULL;
9e353e3b
NIS
2900}
2901
1751d015 2902PerlIO *
ecdeb87c 2903PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2904{
2905 /* This assumes no layers underneath - which is what
2906 happens, but is not how I remember it. NI-S 2001/10/16
2907 */
ecdeb87c 2908 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2909 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
de009b76 2910 const int fd = fileno(stdio);
9217ff3f 2911 char mode[8];
ecdeb87c 2912 if (flags & PERLIO_DUP_FD) {
de009b76 2913 const int dfd = PerlLIO_dup(fileno(stdio));
9217ff3f
NIS
2914 if (dfd >= 0) {
2915 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2916 goto set_this;
ecdeb87c
NIS
2917 }
2918 else {
2919 /* FIXME: To avoid messy error recovery if dup fails
2920 re-use the existing stdio as though flag was not set
2921 */
2922 }
2923 }
9217ff3f
NIS
2924 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2925 set_this:
694c95cf
JH
2926 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2927 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2928 }
2929 return f;
2930}
2931
0d7a5398
NIS
2932static int
2933PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2934{
2935 /* XXX this could use PerlIO_canset_fileno() and
37725cdc 2936 * PerlIO_set_fileno() support from Configure
0d7a5398 2937 */
ef8eacb8
AT
2938# if defined(__UCLIBC__)
2939 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2940 f->__filedes = -1;
2941 return 1;
2942# elif defined(__GLIBC__)
0d7a5398 2943 /* There may be a better way for GLIBC:
37725cdc 2944 - libio.h defines a flag to not close() on cleanup
0d7a5398
NIS
2945 */
2946 f->_fileno = -1;
2947 return 1;
2948# elif defined(__sun__)
2949# if defined(_LP64)
2950 /* On solaris, if _LP64 is defined, the FILE structure is this:
2951 *
2952 * struct FILE {
2953 * long __pad[16];
2954 * };
2955 *
37725cdc 2956 * It turns out that the fd is stored in the top 32 bits of
0d7a5398
NIS
2957 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2958 * to contain a pointer or offset into another structure. All the
2959 * remaining fields are zero.
2960 *
2961 * We set the top bits to -1 (0xFFFFFFFF).
2962 */
2963 f->__pad[4] |= 0xffffffff00000000L;
2964 assert(fileno(f) == 0xffffffff);
2965# else /* !defined(_LP64) */
37725cdc
NIS
2966 /* _file is just a unsigned char :-(
2967 Not clear why we dup() rather than using -1
2968 even if that would be treated as 0xFF - so will
0d7a5398
NIS
2969 a dup fail ...
2970 */
673c27c1 2971 f->_file = PerlLIO_dup(fileno(f));
0d7a5398
NIS
2972# endif /* defined(_LP64) */
2973 return 1;
2974# elif defined(__hpux)
2975 f->__fileH = 0xff;
2976 f->__fileL = 0xff;
2977 return 1;
9837d373 2978 /* Next one ->_file seems to be a reasonable fallback, i.e. if
37725cdc 2979 your platform does not have special entry try this one.
9837d373
NIS
2980 [For OSF only have confirmation for Tru64 (alpha)
2981 but assume other OSFs will be similar.]
37725cdc 2982 */
9837d373 2983# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
0d7a5398
NIS
2984 f->_file = -1;
2985 return 1;
2986# elif defined(__FreeBSD__)
2987 /* There may be a better way on FreeBSD:
37725cdc
NIS
2988 - we could insert a dummy func in the _close function entry
2989 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2990 */
2991 f->_file = -1;
0c49ea6a
SU
2992 return 1;
2993# elif defined(__OpenBSD__)
2994 /* There may be a better way on OpenBSD:
2995 - we could insert a dummy func in the _close function entry
2996 f->_close = (int (*)(void *)) dummy_close;
2997 */
2998 f->_file = -1;
0d7a5398 2999 return 1;
59ad941d
IZ
3000# elif defined(__EMX__)
3001 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3002 f->_handle = -1;
3003 return 1;
0d7a5398
NIS
3004# elif defined(__CYGWIN__)
3005 /* There may be a better way on CYGWIN:
37725cdc
NIS
3006 - we could insert a dummy func in the _close function entry
3007 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
3008 */
3009 f->_file = -1;
3010 return 1;
3011# elif defined(WIN32)
3012# if defined(__BORLANDC__)
3013 f->fd = PerlLIO_dup(fileno(f));
b475b3e6
JH
3014# elif defined(UNDER_CE)
3015 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3016 structure at all
3017 */
0d7a5398
NIS
3018# else
3019 f->_file = -1;
3020# endif
3021 return 1;
3022# else
3023#if 0
37725cdc 3024 /* Sarathy's code did this - we fall back to a dup/dup2 hack
0d7a5398 3025 (which isn't thread safe) instead
37725cdc 3026 */
0d7a5398
NIS
3027# error "Don't know how to set FILE.fileno on your platform"
3028#endif
8772537c 3029 PERL_UNUSED_ARG(f);
0d7a5398
NIS
3030 return 0;
3031# endif
3032}
3033
1751d015 3034IV
f62ce20a 3035PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 3036{
c4420975 3037 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
3038 if (!stdio) {
3039 errno = EBADF;
3040 return -1;
3041 }
9217ff3f 3042 else {
de009b76 3043 const int fd = fileno(stdio);
0d7a5398
NIS
3044 int socksfd = 0;
3045 int invalidate = 0;
bbfd922f 3046 IV result = 0;
0d7a5398
NIS
3047 int saveerr = 0;
3048 int dupfd = 0;
3049#ifdef SOCKS5_VERSION_NAME
37725cdc
NIS
3050 /* Socks lib overrides close() but stdio isn't linked to
3051 that library (though we are) - so we must call close()
3052 on sockets on stdio's behalf.
3053 */
0d7a5398
NIS
3054 int optval;
3055 Sock_size_t optlen = sizeof(int);
3056 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
3057 socksfd = 1;
37725cdc 3058 invalidate = 1;
0d7a5398
NIS
3059 }
3060#endif
9217ff3f
NIS
3061 if (PerlIOUnix_refcnt_dec(fd) > 0) {
3062 /* File descriptor still in use */
0d7a5398
NIS
3063 invalidate = 1;
3064 socksfd = 0;
37725cdc 3065 }
0d7a5398 3066 if (invalidate) {
37725cdc
NIS
3067 /* For STD* handles don't close the stdio at all
3068 this is because we have shared the FILE * too
0d7a5398
NIS
3069 */
3070 if (stdio == stdin) {
3071 /* Some stdios are buggy fflush-ing inputs */
3072 return 0;
3073 }
3074 else if (stdio == stdout || stdio == stderr) {
9217ff3f
NIS
3075 return PerlIO_flush(f);
3076 }
37725cdc
NIS
3077 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3078 Use Sarathy's trick from maint-5.6 to invalidate the
3079 fileno slot of the FILE *
3080 */
bbfd922f 3081 result = PerlIO_flush(f);
0d7a5398
NIS
3082 saveerr = errno;
3083 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
9217ff3f
NIS
3084 dupfd = PerlLIO_dup(fd);
3085 }
37725cdc 3086 }
0d7a5398 3087 result = PerlSIO_fclose(stdio);
37725cdc
NIS
3088 /* We treat error from stdio as success if we invalidated
3089 errno may NOT be expected EBADF
e8529473
NIS
3090 */
3091 if (invalidate && result != 0) {
0d7a5398
NIS
3092 errno = saveerr;
3093 result = 0;
37725cdc 3094 }
0d7a5398
NIS
3095 if (socksfd) {
3096 /* in SOCKS case let close() determine return value */
3097 result = close(fd);
3098 }
3099 if (dupfd) {
3100 PerlLIO_dup2(dupfd,fd);
8a521f28 3101 PerlLIO_close(dupfd);
9217ff3f
NIS
3102 }
3103 return result;
37725cdc 3104 }
1751d015
NIS
3105}
3106
9e353e3b 3107SSize_t
f62ce20a 3108PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 3109{
97aff369 3110 dVAR;
c4420975 3111 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3112 SSize_t got = 0;
4d948241
NIS
3113 for (;;) {
3114 if (count == 1) {
3115 STDCHAR *buf = (STDCHAR *) vbuf;
3116 /*
3117 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3118 * stdio does not do that for fread()
3119 */
de009b76 3120 const int ch = PerlSIO_fgetc(s);
4d948241
NIS
3121 if (ch != EOF) {
3122 *buf = ch;
3123 got = 1;
3124 }
14a5cf38 3125 }
4d948241
NIS
3126 else
3127 got = PerlSIO_fread(vbuf, 1, count, s);
b1d8b47a
CS
3128 if (got == 0 && PerlSIO_ferror(s))
3129 got = -1;
42a7a32f 3130 if (got >= 0 || errno != EINTR)
4d948241
NIS
3131 break;
3132 PERL_ASYNC_CHECK();
42a7a32f 3133 SETERRNO(0,0); /* just in case */
14a5cf38 3134 }
14a5cf38 3135 return got;
9e353e3b
NIS
3136}
3137
3138SSize_t
f62ce20a 3139PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3140{
14a5cf38 3141 SSize_t unread = 0;
c4420975 3142 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3143
313e59c8 3144#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3145 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3146 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3147 STDCHAR *base = PerlIO_get_base(f);
3148 SSize_t cnt = PerlIO_get_cnt(f);
3149 STDCHAR *ptr = PerlIO_get_ptr(f);
3150 SSize_t avail = ptr - base;
3151 if (avail > 0) {
3152 if (avail > count) {
3153 avail = count;
3154 }
3155 ptr -= avail;
3156 Move(buf-avail,ptr,avail,STDCHAR);
3157 count -= avail;
3158 unread += avail;
3159 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
3160 if (PerlSIO_feof(s) && unread >= 0)
3161 PerlSIO_clearerr(s);
3162 }
3163 }
313e59c8
NIS
3164 else
3165#endif
3166 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3167 /* We can get pointer to buffer but not its base
3168 Do ungetc() but check chars are ending up in the
3169 buffer
3170 */
3171 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3172 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3173 while (count > 0) {
de009b76 3174 const int ch = *--buf & 0xFF;
9f7cd136
NIS
3175 if (ungetc(ch,s) != ch) {
3176 /* ungetc did not work */
3177 break;
3178 }
3179 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3180 /* Did not change pointer as expected */
3181 fgetc(s); /* get char back again */
3182 break;
3183 }
3184 /* It worked ! */
3185 count--;
3186 unread++;
93679785
NIS
3187 }
3188 }
3189
3190 if (count > 0) {
3191 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3192 }
3193 return unread;
9e353e3b
NIS
3194}
3195
3196SSize_t
f62ce20a 3197PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3198{
97aff369 3199 dVAR;
4d948241
NIS
3200 SSize_t got;
3201 for (;;) {
3202 got = PerlSIO_fwrite(vbuf, 1, count,
3203 PerlIOSelf(f, PerlIOStdio)->stdio);
42a7a32f 3204 if (got >= 0 || errno != EINTR)
4d948241
NIS
3205 break;
3206 PERL_ASYNC_CHECK();
42a7a32f 3207 SETERRNO(0,0); /* just in case */
4d948241
NIS
3208 }
3209 return got;
9e353e3b
NIS
3210}
3211
94a175e1 3212IV
f62ce20a 3213PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3214{
c4420975 3215 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3216 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
3217}
3218
3219Off_t
f62ce20a 3220PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 3221{
c4420975 3222 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3223 return PerlSIO_ftell(stdio);
9e353e3b
NIS
3224}
3225
3226IV
f62ce20a 3227PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 3228{
c4420975 3229 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38
JH
3230 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3231 return PerlSIO_fflush(stdio);
3232 }
3233 else {
88b61e10 3234#if 0
14a5cf38
JH
3235 /*
3236 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 3237 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 3238 * design is to do _this_ but not have layer above flush this
71200d45 3239 * layer read-to-read
14a5cf38
JH
3240 */
3241 /*
71200d45 3242 * Not writeable - sync by attempting a seek
14a5cf38 3243 */
79852593 3244 const int err = errno;
14a5cf38
JH
3245 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3246 errno = err;
88b61e10 3247#endif
14a5cf38
JH
3248 }
3249 return 0;
9e353e3b
NIS
3250}
3251
3252IV
f62ce20a 3253PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 3254{
14a5cf38 3255 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3256}
3257
3258IV
f62ce20a 3259PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 3260{
263df5f1 3261 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3262}
3263
3264void
f62ce20a 3265PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 3266{
14a5cf38 3267 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3268}
3269
3270void
f62ce20a 3271PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b
NIS
3272{
3273#ifdef HAS_SETLINEBUF
14a5cf38 3274 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 3275#else
bd61b366 3276 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
9e353e3b
NIS
3277#endif
3278}
3279
3280#ifdef FILE_base
3281STDCHAR *
f62ce20a 3282PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 3283{
c4420975 3284 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3285 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
3286}
3287
3288Size_t
f62ce20a 3289PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3290{
c4420975 3291 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3292 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
3293}
3294#endif
3295
3296#ifdef USE_STDIO_PTR
3297STDCHAR *
f62ce20a 3298PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3299{
c4420975 3300 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3301 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
3302}
3303
3304SSize_t
f62ce20a 3305PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3306{
c4420975 3307 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3308 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
3309}
3310
3311void
f62ce20a 3312PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3313{
c4420975 3314 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3315 if (ptr != NULL) {
9e353e3b 3316#ifdef STDIO_PTR_LVALUE
22569500 3317 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 3318#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 3319 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
3320 assert(PerlSIO_get_cnt(stdio) == (cnt));
3321 }
9e353e3b
NIS
3322#endif
3323#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 3324 /*
71200d45 3325 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
3326 */
3327 return;
9e353e3b 3328#endif
22569500 3329#else /* STDIO_PTR_LVALUE */
14a5cf38 3330 PerlProc_abort();
22569500 3331#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
3332 }
3333 /*
71200d45 3334 * Now (or only) set cnt
14a5cf38 3335 */
9e353e3b 3336#ifdef STDIO_CNT_LVALUE
14a5cf38 3337 PerlSIO_set_cnt(stdio, cnt);
22569500 3338#else /* STDIO_CNT_LVALUE */
9e353e3b 3339#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
3340 PerlSIO_set_ptr(stdio,
3341 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3342 cnt));
22569500 3343#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 3344 PerlProc_abort();
22569500
NIS
3345#endif /* STDIO_PTR_LVAL_SETS_CNT */
3346#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
3347}
3348
93679785 3349
9e353e3b
NIS
3350#endif
3351
93679785
NIS
3352IV
3353PerlIOStdio_fill(pTHX_ PerlIO *f)
3354{
c4420975 3355 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785
NIS
3356 int c;
3357 /*
3358 * fflush()ing read-only streams can cause trouble on some stdio-s
3359 */
3360 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3361 if (PerlSIO_fflush(stdio) != 0)
3362 return EOF;
3363 }
3364 c = PerlSIO_fgetc(stdio);
3365 if (c == EOF)
3366 return EOF;
3367
3368#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
3369
3370#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3371 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3372 /* Fake ungetc() to the real buffer in case system's ungetc
3373 goes elsewhere
3374 */
3375 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3376 SSize_t cnt = PerlSIO_get_cnt(stdio);
3377 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3378 if (ptr == base+1) {
3379 *--ptr = (STDCHAR) c;
3380 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3381 if (PerlSIO_feof(stdio))
3382 PerlSIO_clearerr(stdio);
3383 return 0;
3384 }
3385 }
313e59c8
NIS
3386 else
3387#endif
3388 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3389 STDCHAR ch = c;
3390 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3391 return 0;
3392 }
3393 }
93679785
NIS
3394#endif
3395
3396#if defined(VMS)
3397 /* An ungetc()d char is handled separately from the regular
3398 * buffer, so we stuff it in the buffer ourselves.
3399 * Should never get called as should hit code above
3400 */
bad9695d
NIS
3401 *(--((*stdio)->_ptr)) = (unsigned char) c;
3402 (*stdio)->_cnt++;
93679785
NIS
3403#else
3404 /* If buffer snoop scheme above fails fall back to
9f7cd136 3405 using ungetc().
93679785
NIS
3406 */
3407 if (PerlSIO_ungetc(c, stdio) != c)
3408 return EOF;
3409#endif
3410 return 0;
3411}
3412
3413
3414
27da23d5 3415PERLIO_FUNCS_DECL(PerlIO_stdio) = {
2dc2558e 3416 sizeof(PerlIO_funcs),
14a5cf38
JH
3417 "stdio",
3418 sizeof(PerlIOStdio),
86e05cf2 3419 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 3420 PerlIOStdio_pushed,
44798d05 3421 PerlIOBase_popped,
14a5cf38 3422 PerlIOStdio_open,
86e05cf2 3423 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3424 NULL,
3425 PerlIOStdio_fileno,
71200d45 3426 PerlIOStdio_dup,
14a5cf38
JH
3427 PerlIOStdio_read,
3428 PerlIOStdio_unread,
3429 PerlIOStdio_write,
3430 PerlIOStdio_seek,
3431 PerlIOStdio_tell,
3432 PerlIOStdio_close,
3433 PerlIOStdio_flush,
3434 PerlIOStdio_fill,
3435 PerlIOStdio_eof,
3436 PerlIOStdio_error,
3437 PerlIOStdio_clearerr,
3438 PerlIOStdio_setlinebuf,
9e353e3b 3439#ifdef FILE_base
14a5cf38
JH
3440 PerlIOStdio_get_base,
3441 PerlIOStdio_get_bufsiz,
9e353e3b 3442#else
14a5cf38
JH
3443 NULL,
3444 NULL,
9e353e3b
NIS
3445#endif
3446#ifdef USE_STDIO_PTR
14a5cf38
JH
3447 PerlIOStdio_get_ptr,
3448 PerlIOStdio_get_cnt,
15b61c98 3449# if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
79ed0f43 3450 PerlIOStdio_set_ptrcnt,
15b61c98
JH
3451# else
3452 NULL,
3453# endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3454#else
3455 NULL,
14a5cf38
JH
3456 NULL,
3457 NULL,
15b61c98 3458#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3459};
3460
b9d6bf13
JH
3461/* Note that calls to PerlIO_exportFILE() are reversed using
3462 * PerlIO_releaseFILE(), not importFILE. */
9e353e3b 3463FILE *
81428673 3464PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3465{
e87a358a 3466 dTHX;
81428673
NIS
3467 FILE *stdio = NULL;
3468 if (PerlIOValid(f)) {
3469 char buf[8];
3470 PerlIO_flush(f);
3471 if (!mode || !*mode) {
3472 mode = PerlIO_modestr(f, buf);
3473 }
3474 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3475 if (stdio) {
3476 PerlIOl *l = *f;
9f75cc58 3477 PerlIO *f2;
81428673
NIS
3478 /* De-link any lower layers so new :stdio sticks */
3479 *f = NULL;
a0714e2c 3480 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
9f75cc58 3481 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
81428673
NIS
3482 s->stdio = stdio;
3483 /* Link previous lower layers under new one */
3484 *PerlIONext(f) = l;
3485 }
3486 else {
3487 /* restore layers list */
3488 *f = l;
3489 }
a33cf58c 3490 }
14a5cf38
JH
3491 }
3492 return stdio;
9e353e3b
NIS
3493}
3494
81428673 3495
9e353e3b
NIS
3496FILE *
3497PerlIO_findFILE(PerlIO *f)
3498{
14a5cf38
JH
3499 PerlIOl *l = *f;
3500 while (l) {
3501 if (l->tab == &PerlIO_stdio) {
3502 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3503 return s->stdio;
3504 }
3505 l = *PerlIONext(&l);
f7e7eb72 3506 }
4b069b44 3507 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
bd61b366 3508 return PerlIO_exportFILE(f, NULL);
9e353e3b
NIS
3509}
3510
b9d6bf13 3511/* Use this to reverse PerlIO_exportFILE calls. */
9e353e3b
NIS
3512void
3513PerlIO_releaseFILE(PerlIO *p, FILE *f)
3514{
27da23d5 3515 dVAR;
22569500
NIS
3516 PerlIOl *l;
3517 while ((l = *p)) {
3518 if (l->tab == &PerlIO_stdio) {
3519 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3520 if (s->stdio == f) {
3521 dTHX;
3522 PerlIO_pop(aTHX_ p);
3523 return;
3524 }
3525 }
3526 p = PerlIONext(p);
3527 }
3528 return;
9e353e3b
NIS
3529}
3530
3531/*--------------------------------------------------------------------------------------*/
14a5cf38 3532/*
71200d45 3533 * perlio buffer layer
14a5cf38 3534 */
9e353e3b 3535
5e2ab84b 3536IV
2dc2558e 3537PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3538{
14a5cf38 3539 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
de009b76 3540 const int fd = PerlIO_fileno(f);
14a5cf38
JH
3541 if (fd >= 0 && PerlLIO_isatty(fd)) {
3542 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3543 }
4b069b44 3544 if (*PerlIONext(f)) {
de009b76 3545 const Off_t posn = PerlIO_tell(PerlIONext(f));
4b069b44
NIS
3546 if (posn != (Off_t) - 1) {
3547 b->posn = posn;
3548 }
14a5cf38 3549 }
2dc2558e 3550 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3551}
3552
9e353e3b 3553PerlIO *
14a5cf38
JH
3554PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3555 IV n, const char *mode, int fd, int imode, int perm,
3556 PerlIO *f, int narg, SV **args)
3557{
04892f78 3558 if (PerlIOValid(f)) {
14a5cf38 3559 PerlIO *next = PerlIONext(f);
67363c0d
JH
3560 PerlIO_funcs *tab =
3561 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3562 if (tab && tab->Open)
3563 next =
3564 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3565 next, narg, args);
2dc2558e 3566 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3567 return NULL;
3568 }
3569 }
3570 else {
04892f78 3571 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38 3572 int init = 0;
3b6c1aba 3573 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
3574 init = 1;
3575 /*
71200d45 3576 * mode++;
14a5cf38
JH
3577 */
3578 }
67363c0d
JH
3579 if (tab && tab->Open)
3580 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3581 f, narg, args);
3582 else
3583 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 3584 if (f) {
22569500 3585 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
b26b1ab5
NC
3586 /*
3587 * if push fails during open, open fails. close will pop us.
3588 */
3589 PerlIO_close (f);
3590 return NULL;
3591 } else {
3592 fd = PerlIO_fileno(f);
b26b1ab5
NC
3593 if (init && fd == 2) {
3594 /*
3595 * Initial stderr is unbuffered
3596 */
3597 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3598 }