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