This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Archive-Tar-1.30. Since change #27571 is not included,
[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 1411
fcf2db38 1412static PerlIO_funcs *
2edd7e44
NIS
1413PerlIO_layer_from_ref(pTHX_ SV *sv)
1414{
97aff369 1415 dVAR;
14a5cf38 1416 /*
71200d45 1417 * For any scalar type load the handler which is bundled with perl
14a5cf38
JH
1418 */
1419 if (SvTYPE(sv) < SVt_PVAV)
6a245ed1 1420 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
14a5cf38
JH
1421
1422 /*
71200d45 1423 * For other types allow if layer is known but don't try and load it
14a5cf38
JH
1424 */
1425 switch (SvTYPE(sv)) {
1426 case SVt_PVAV:
6a245ed1 1427 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
14a5cf38 1428 case SVt_PVHV:
6a245ed1 1429 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
14a5cf38 1430 case SVt_PVCV:
6a245ed1 1431 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
14a5cf38 1432 case SVt_PVGV:
6a245ed1 1433 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
42d0e0b7
AL
1434 default:
1435 return NULL;
14a5cf38 1436 }
2edd7e44
NIS
1437}
1438
fcf2db38 1439PerlIO_list_t *
14a5cf38
JH
1440PerlIO_resolve_layers(pTHX_ const char *layers,
1441 const char *mode, int narg, SV **args)
1442{
97aff369 1443 dVAR;
14a5cf38
JH
1444 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1445 int incdef = 1;
a1ea730d 1446 if (!PL_perlio)
14a5cf38
JH
1447 PerlIO_stdstreams(aTHX);
1448 if (narg) {
dcda55fc 1449 SV * const arg = *args;
14a5cf38 1450 /*
71200d45
NIS
1451 * If it is a reference but not an object see if we have a handler
1452 * for it
14a5cf38
JH
1453 */
1454 if (SvROK(arg) && !sv_isobject(arg)) {
46c461b5 1455 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
14a5cf38 1456 if (handler) {
3a1ee7e8
NIS
1457 def = PerlIO_list_alloc(aTHX);
1458 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
14a5cf38
JH
1459 incdef = 0;
1460 }
1461 /*
e934609f 1462 * Don't fail if handler cannot be found :via(...) etc. may do
14a5cf38 1463 * something sensible else we will just stringfy and open
71200d45 1464 * resulting string.
14a5cf38
JH
1465 */
1466 }
1467 }
9fe371da 1468 if (!layers || !*layers)
11bcd5da 1469 layers = Perl_PerlIO_context_layers(aTHX_ mode);
14a5cf38
JH
1470 if (layers && *layers) {
1471 PerlIO_list_t *av;
1472 if (incdef) {
de009b76 1473 IV i;
3a1ee7e8 1474 av = PerlIO_list_alloc(aTHX);
14a5cf38 1475 for (i = 0; i < def->cur; i++) {
3a1ee7e8 1476 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
14a5cf38
JH
1477 def->array[i].arg);
1478 }
1479 }
1480 else {
1481 av = def;
1482 }
0cff2cf3
NIS
1483 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1484 return av;
1485 }
1486 else {
1487 PerlIO_list_free(aTHX_ av);
b37c2d43 1488 return NULL;
0cff2cf3 1489 }
14a5cf38
JH
1490 }
1491 else {
1492 if (incdef)
1493 def->refcnt++;
1494 return def;
1495 }
ee518936
NIS
1496}
1497
1498PerlIO *
14a5cf38
JH
1499PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1500 int imode, int perm, PerlIO *f, int narg, SV **args)
1501{
97aff369 1502 dVAR;
14a5cf38
JH
1503 if (!f && narg == 1 && *args == &PL_sv_undef) {
1504 if ((f = PerlIO_tmpfile())) {
9fe371da 1505 if (!layers || !*layers)
11bcd5da 1506 layers = Perl_PerlIO_context_layers(aTHX_ mode);
14a5cf38
JH
1507 if (layers && *layers)
1508 PerlIO_apply_layers(aTHX_ f, mode, layers);
1509 }
1510 }
1511 else {
de009b76 1512 PerlIO_list_t *layera;
14a5cf38
JH
1513 IV n;
1514 PerlIO_funcs *tab = NULL;
04892f78 1515 if (PerlIOValid(f)) {
14a5cf38 1516 /*
71200d45
NIS
1517 * This is "reopen" - it is not tested as perl does not use it
1518 * yet
14a5cf38
JH
1519 */
1520 PerlIOl *l = *f;
3a1ee7e8 1521 layera = PerlIO_list_alloc(aTHX);
14a5cf38 1522 while (l) {
46c461b5 1523 SV * const arg = (l->tab->Getarg)
04892f78
NIS
1524 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1525 : &PL_sv_undef;
3a1ee7e8 1526 PerlIO_list_push(aTHX_ layera, l->tab, arg);
14a5cf38
JH
1527 l = *PerlIONext(&l);
1528 }
1529 }
1530 else {
1531 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
0cff2cf3
NIS
1532 if (!layera) {
1533 return NULL;
1534 }
14a5cf38
JH
1535 }
1536 /*
71200d45 1537 * Start at "top" of layer stack
14a5cf38
JH
1538 */
1539 n = layera->cur - 1;
1540 while (n >= 0) {
46c461b5 1541 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
14a5cf38
JH
1542 if (t && t->Open) {
1543 tab = t;
1544 break;
1545 }
1546 n--;
1547 }
1548 if (tab) {
1549 /*
71200d45 1550 * Found that layer 'n' can do opens - call it
14a5cf38 1551 */
7cf31beb 1552 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
3b8752bb 1553 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
7cf31beb 1554 }
14a5cf38 1555 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
355d3743
PD
1556 tab->name, layers ? layers : "(Null)", mode, fd,
1557 imode, perm, (void*)f, narg, (void*)args);
210e727c
JH
1558 if (tab->Open)
1559 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1560 f, narg, args);
1561 else {
1562 SETERRNO(EINVAL, LIB_INVARG);
1563 f = NULL;
1564 }
14a5cf38
JH
1565 if (f) {
1566 if (n + 1 < layera->cur) {
1567 /*
1568 * More layers above the one that we used to open -
71200d45 1569 * apply them now
14a5cf38 1570 */
d9dac8cd
NIS
1571 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1572 /* If pushing layers fails close the file */
1573 PerlIO_close(f);
14a5cf38
JH
1574 f = NULL;
1575 }
1576 }
1577 }
1578 }
3a1ee7e8 1579 PerlIO_list_free(aTHX_ layera);
14a5cf38
JH
1580 }
1581 return f;
ee518936 1582}
b931b1d9
NIS
1583
1584
9e353e3b 1585SSize_t
e87a358a 1586Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 1587{
b32dd47e 1588 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1589}
1590
313ca112 1591SSize_t
e87a358a 1592Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1593{
b32dd47e 1594 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1595}
1596
9e353e3b 1597SSize_t
e87a358a 1598Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1599{
b32dd47e 1600 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1601}
1602
6f9d8c32 1603int
e87a358a 1604Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
760ac839 1605{
b32dd47e 1606 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
760ac839
LW
1607}
1608
9e353e3b 1609Off_t
e87a358a 1610Perl_PerlIO_tell(pTHX_ PerlIO *f)
760ac839 1611{
b32dd47e 1612 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
760ac839
LW
1613}
1614
6f9d8c32 1615int
e87a358a 1616Perl_PerlIO_flush(pTHX_ PerlIO *f)
760ac839 1617{
97aff369 1618 dVAR;
14a5cf38
JH
1619 if (f) {
1620 if (*f) {
de009b76 1621 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1b7a0411
JH
1622
1623 if (tab && tab->Flush)
f62ce20a 1624 return (*tab->Flush) (aTHX_ f);
1b7a0411
JH
1625 else
1626 return 0; /* If no Flush defined, silently succeed. */
14a5cf38
JH
1627 }
1628 else {
fe5a182c 1629 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
93189314 1630 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1631 return -1;
1632 }
1633 }
1634 else {
1635 /*
1636 * Is it good API design to do flush-all on NULL, a potentially
1637 * errorneous input? Maybe some magical value (PerlIO*
1638 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1639 * things on fflush(NULL), but should we be bound by their design
71200d45 1640 * decisions? --jhi
14a5cf38 1641 */
a1ea730d 1642 PerlIO **table = &PL_perlio;
14a5cf38
JH
1643 int code = 0;
1644 while ((f = *table)) {
1645 int i;
1646 table = (PerlIO **) (f++);
1647 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1648 if (*f && PerlIO_flush(f) != 0)
1649 code = -1;
1650 f++;
1651 }
1652 }
1653 return code;
1654 }
760ac839
LW
1655}
1656
a9c883f6 1657void
f62ce20a 1658PerlIOBase_flush_linebuf(pTHX)
a9c883f6 1659{
97aff369 1660 dVAR;
a1ea730d 1661 PerlIO **table = &PL_perlio;
14a5cf38
JH
1662 PerlIO *f;
1663 while ((f = *table)) {
1664 int i;
1665 table = (PerlIO **) (f++);
1666 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1667 if (*f
1668 && (PerlIOBase(f)->
1669 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1670 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1671 PerlIO_flush(f);
1672 f++;
1673 }
a9c883f6 1674 }
a9c883f6
NIS
1675}
1676
06da4f11 1677int
e87a358a 1678Perl_PerlIO_fill(pTHX_ PerlIO *f)
06da4f11 1679{
b32dd47e 1680 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
06da4f11
NIS
1681}
1682
f3862f8b
NIS
1683int
1684PerlIO_isutf8(PerlIO *f)
1685{
1b7a0411
JH
1686 if (PerlIOValid(f))
1687 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1688 else
1689 SETERRNO(EBADF, SS_IVCHAN);
37725cdc 1690
1b7a0411 1691 return -1;
f3862f8b
NIS
1692}
1693
6f9d8c32 1694int
e87a358a 1695Perl_PerlIO_eof(pTHX_ PerlIO *f)
760ac839 1696{
b32dd47e 1697 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
9e353e3b
NIS
1698}
1699
9e353e3b 1700int
e87a358a 1701Perl_PerlIO_error(pTHX_ PerlIO *f)
9e353e3b 1702{
b32dd47e 1703 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
9e353e3b
NIS
1704}
1705
9e353e3b 1706void
e87a358a 1707Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
9e353e3b 1708{
b32dd47e 1709 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
9e353e3b
NIS
1710}
1711
9e353e3b 1712void
e87a358a 1713Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1714{
b32dd47e 1715 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
9e353e3b
NIS
1716}
1717
9e353e3b
NIS
1718int
1719PerlIO_has_base(PerlIO *f)
1720{
1b7a0411 1721 if (PerlIOValid(f)) {
46c461b5 1722 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1723
1724 if (tab)
1725 return (tab->Get_base != NULL);
1726 SETERRNO(EINVAL, LIB_INVARG);
1727 }
1728 else
1729 SETERRNO(EBADF, SS_IVCHAN);
1730
1731 return 0;
760ac839
LW
1732}
1733
9e353e3b
NIS
1734int
1735PerlIO_fast_gets(PerlIO *f)
760ac839 1736{
04892f78 1737 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
46c461b5 1738 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1739
1740 if (tab)
1741 return (tab->Set_ptrcnt != NULL);
1742 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1743 }
1b7a0411
JH
1744 else
1745 SETERRNO(EBADF, SS_IVCHAN);
1746
14a5cf38 1747 return 0;
9e353e3b
NIS
1748}
1749
9e353e3b
NIS
1750int
1751PerlIO_has_cntptr(PerlIO *f)
1752{
04892f78 1753 if (PerlIOValid(f)) {
46c461b5 1754 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1755
1756 if (tab)
1757 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1758 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1759 }
1b7a0411
JH
1760 else
1761 SETERRNO(EBADF, SS_IVCHAN);
1762
14a5cf38 1763 return 0;
9e353e3b
NIS
1764}
1765
9e353e3b
NIS
1766int
1767PerlIO_canset_cnt(PerlIO *f)
1768{
04892f78 1769 if (PerlIOValid(f)) {
46c461b5 1770 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1771
1772 if (tab)
1773 return (tab->Set_ptrcnt != NULL);
1774 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1775 }
1b7a0411
JH
1776 else
1777 SETERRNO(EBADF, SS_IVCHAN);
1778
14a5cf38 1779 return 0;
760ac839
LW
1780}
1781
888911fc 1782STDCHAR *
e87a358a 1783Perl_PerlIO_get_base(pTHX_ PerlIO *f)
760ac839 1784{
b32dd47e 1785 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
9e353e3b
NIS
1786}
1787
9e353e3b 1788int
e87a358a 1789Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 1790{
b32dd47e 1791 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
9e353e3b
NIS
1792}
1793
9e353e3b 1794STDCHAR *
e87a358a 1795Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
9e353e3b 1796{
b32dd47e 1797 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
9e353e3b
NIS
1798}
1799
05d1247b 1800int
e87a358a 1801Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1802{
b32dd47e 1803 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
9e353e3b
NIS
1804}
1805
9e353e3b 1806void
e87a358a 1807Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1808{
b32dd47e 1809 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
9e353e3b
NIS
1810}
1811
9e353e3b 1812void
e87a358a 1813Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1814{
b32dd47e 1815 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
9e353e3b
NIS
1816}
1817
4ec2216f 1818
9e353e3b 1819/*--------------------------------------------------------------------------------------*/
14a5cf38 1820/*
71200d45 1821 * utf8 and raw dummy layers
14a5cf38 1822 */
dfebf958 1823
26fb694e 1824IV
2dc2558e 1825PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
26fb694e 1826{
96a5add6 1827 PERL_UNUSED_CONTEXT;
8772537c
AL
1828 PERL_UNUSED_ARG(mode);
1829 PERL_UNUSED_ARG(arg);
00f51856 1830 if (PerlIOValid(f)) {
14a5cf38
JH
1831 if (tab->kind & PERLIO_K_UTF8)
1832 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1833 else
1834 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1835 return 0;
1836 }
1837 return -1;
26fb694e
NIS
1838}
1839
27da23d5 1840PERLIO_FUNCS_DECL(PerlIO_utf8) = {
2dc2558e 1841 sizeof(PerlIO_funcs),
14a5cf38 1842 "utf8",
2dc2558e 1843 0,
6874a2de 1844 PERLIO_K_DUMMY | PERLIO_K_UTF8,
14a5cf38
JH
1845 PerlIOUtf8_pushed,
1846 NULL,
1847 NULL,
1848 NULL,
1849 NULL,
1850 NULL,
1851 NULL,
1852 NULL,
1853 NULL,
1854 NULL,
1855 NULL,
de009b76
AL
1856 NULL,
1857 NULL,
22569500
NIS
1858 NULL, /* flush */
1859 NULL, /* fill */
14a5cf38
JH
1860 NULL,
1861 NULL,
1862 NULL,
1863 NULL,
22569500
NIS
1864 NULL, /* get_base */
1865 NULL, /* get_bufsiz */
1866 NULL, /* get_ptr */
1867 NULL, /* get_cnt */
1868 NULL, /* set_ptrcnt */
26fb694e
NIS
1869};
1870
27da23d5 1871PERLIO_FUNCS_DECL(PerlIO_byte) = {
2dc2558e 1872 sizeof(PerlIO_funcs),
14a5cf38 1873 "bytes",
2dc2558e 1874 0,
14a5cf38
JH
1875 PERLIO_K_DUMMY,
1876 PerlIOUtf8_pushed,
1877 NULL,
1878 NULL,
1879 NULL,
1880 NULL,
1881 NULL,
1882 NULL,
1883 NULL,
1884 NULL,
1885 NULL,
1886 NULL,
de009b76
AL
1887 NULL,
1888 NULL,
22569500
NIS
1889 NULL, /* flush */
1890 NULL, /* fill */
14a5cf38
JH
1891 NULL,
1892 NULL,
1893 NULL,
1894 NULL,
22569500
NIS
1895 NULL, /* get_base */
1896 NULL, /* get_bufsiz */
1897 NULL, /* get_ptr */
1898 NULL, /* get_cnt */
1899 NULL, /* set_ptrcnt */
dfebf958
NIS
1900};
1901
1902PerlIO *
14a5cf38
JH
1903PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1904 IV n, const char *mode, int fd, int imode, int perm,
1905 PerlIO *old, int narg, SV **args)
dfebf958 1906{
8772537c
AL
1907 PerlIO_funcs * const tab = PerlIO_default_btm();
1908 PERL_UNUSED_ARG(self);
210e727c
JH
1909 if (tab && tab->Open)
1910 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1911 old, narg, args);
1912 SETERRNO(EINVAL, LIB_INVARG);
1913 return NULL;
dfebf958
NIS
1914}
1915
27da23d5 1916PERLIO_FUNCS_DECL(PerlIO_raw) = {
2dc2558e 1917 sizeof(PerlIO_funcs),
14a5cf38 1918 "raw",
2dc2558e 1919 0,
14a5cf38
JH
1920 PERLIO_K_DUMMY,
1921 PerlIORaw_pushed,
1922 PerlIOBase_popped,
1923 PerlIORaw_open,
1924 NULL,
1925 NULL,
1926 NULL,
1927 NULL,
1928 NULL,
1929 NULL,
1930 NULL,
1931 NULL,
de009b76
AL
1932 NULL,
1933 NULL,
22569500
NIS
1934 NULL, /* flush */
1935 NULL, /* fill */
14a5cf38
JH
1936 NULL,
1937 NULL,
1938 NULL,
1939 NULL,
22569500
NIS
1940 NULL, /* get_base */
1941 NULL, /* get_bufsiz */
1942 NULL, /* get_ptr */
1943 NULL, /* get_cnt */
1944 NULL, /* set_ptrcnt */
dfebf958
NIS
1945};
1946/*--------------------------------------------------------------------------------------*/
1947/*--------------------------------------------------------------------------------------*/
14a5cf38 1948/*
71200d45 1949 * "Methods" of the "base class"
14a5cf38 1950 */
9e353e3b
NIS
1951
1952IV
f62ce20a 1953PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1954{
04892f78 1955 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
9e353e3b
NIS
1956}
1957
f5b9d040 1958char *
81428673 1959PerlIO_modestr(PerlIO * f, char *buf)
14a5cf38
JH
1960{
1961 char *s = buf;
81428673 1962 if (PerlIOValid(f)) {
de009b76 1963 const IV flags = PerlIOBase(f)->flags;
81428673
NIS
1964 if (flags & PERLIO_F_APPEND) {
1965 *s++ = 'a';
1966 if (flags & PERLIO_F_CANREAD) {
1967 *s++ = '+';
1968 }
14a5cf38 1969 }
81428673
NIS
1970 else if (flags & PERLIO_F_CANREAD) {
1971 *s++ = 'r';
1972 if (flags & PERLIO_F_CANWRITE)
1973 *s++ = '+';
1974 }
1975 else if (flags & PERLIO_F_CANWRITE) {
1976 *s++ = 'w';
1977 if (flags & PERLIO_F_CANREAD) {
1978 *s++ = '+';
1979 }
14a5cf38 1980 }
35990314 1981#ifdef PERLIO_USING_CRLF
81428673
NIS
1982 if (!(flags & PERLIO_F_CRLF))
1983 *s++ = 'b';
5f1a76d0 1984#endif
81428673 1985 }
14a5cf38
JH
1986 *s = '\0';
1987 return buf;
f5b9d040
NIS
1988}
1989
81428673 1990
76ced9ad 1991IV
2dc2558e 1992PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
9e353e3b 1993{
de009b76 1994 PerlIOl * const l = PerlIOBase(f);
96a5add6 1995 PERL_UNUSED_CONTEXT;
8772537c 1996 PERL_UNUSED_ARG(arg);
de009b76 1997
14a5cf38
JH
1998 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1999 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2000 if (tab->Set_ptrcnt != NULL)
2001 l->flags |= PERLIO_F_FASTGETS;
2002 if (mode) {
3b6c1aba 2003 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2004 mode++;
2005 switch (*mode++) {
2006 case 'r':
2007 l->flags |= PERLIO_F_CANREAD;
2008 break;
2009 case 'a':
2010 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2011 break;
2012 case 'w':
2013 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2014 break;
2015 default:
93189314 2016 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2017 return -1;
2018 }
2019 while (*mode) {
2020 switch (*mode++) {
2021 case '+':
2022 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2023 break;
2024 case 'b':
2025 l->flags &= ~PERLIO_F_CRLF;
2026 break;
2027 case 't':
2028 l->flags |= PERLIO_F_CRLF;
2029 break;
2030 default:
93189314 2031 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2032 return -1;
2033 }
2034 }
2035 }
2036 else {
2037 if (l->next) {
2038 l->flags |= l->next->flags &
2039 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2040 PERLIO_F_APPEND);
2041 }
2042 }
5e2ab84b 2043#if 0
14a5cf38
JH
2044 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2045 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2046 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 2047#endif
14a5cf38 2048 return 0;
76ced9ad
NIS
2049}
2050
2051IV
f62ce20a 2052PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 2053{
96a5add6 2054 PERL_UNUSED_CONTEXT;
8772537c 2055 PERL_UNUSED_ARG(f);
14a5cf38 2056 return 0;
760ac839
LW
2057}
2058
9e353e3b 2059SSize_t
f62ce20a 2060PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2061{
14a5cf38 2062 /*
71200d45 2063 * Save the position as current head considers it
14a5cf38 2064 */
de009b76 2065 const Off_t old = PerlIO_tell(f);
a0714e2c 2066 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
14a5cf38 2067 PerlIOSelf(f, PerlIOBuf)->posn = old;
de009b76 2068 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
9e353e3b
NIS
2069}
2070
f6c77cf1 2071SSize_t
f62ce20a 2072PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 2073{
14a5cf38
JH
2074 STDCHAR *buf = (STDCHAR *) vbuf;
2075 if (f) {
263df5f1
JH
2076 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2077 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2078 SETERRNO(EBADF, SS_IVCHAN);
2079 return 0;
2080 }
14a5cf38 2081 while (count > 0) {
93c2c2ec
IZ
2082 get_cnt:
2083 {
14a5cf38
JH
2084 SSize_t avail = PerlIO_get_cnt(f);
2085 SSize_t take = 0;
2086 if (avail > 0)
bb7a0f54 2087 take = ((SSize_t)count < avail) ? (SSize_t)count : avail;
14a5cf38
JH
2088 if (take > 0) {
2089 STDCHAR *ptr = PerlIO_get_ptr(f);
2090 Copy(ptr, buf, take, STDCHAR);
2091 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2092 count -= take;
2093 buf += take;
93c2c2ec
IZ
2094 if (avail == 0) /* set_ptrcnt could have reset avail */
2095 goto get_cnt;
14a5cf38
JH
2096 }
2097 if (count > 0 && avail <= 0) {
2098 if (PerlIO_fill(f) != 0)
2099 break;
2100 }
93c2c2ec 2101 }
14a5cf38
JH
2102 }
2103 return (buf - (STDCHAR *) vbuf);
2104 }
f6c77cf1 2105 return 0;
f6c77cf1
NIS
2106}
2107
9e353e3b 2108IV
f62ce20a 2109PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 2110{
96a5add6 2111 PERL_UNUSED_CONTEXT;
8772537c 2112 PERL_UNUSED_ARG(f);
14a5cf38 2113 return 0;
9e353e3b
NIS
2114}
2115
2116IV
f62ce20a 2117PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 2118{
96a5add6 2119 PERL_UNUSED_CONTEXT;
8772537c 2120 PERL_UNUSED_ARG(f);
14a5cf38 2121 return -1;
06da4f11
NIS
2122}
2123
2124IV
f62ce20a 2125PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 2126{
37725cdc
NIS
2127 IV code = -1;
2128 if (PerlIOValid(f)) {
2129 PerlIO *n = PerlIONext(f);
2130 code = PerlIO_flush(f);
2131 PerlIOBase(f)->flags &=
2132 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2133 while (PerlIOValid(n)) {
de009b76 2134 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
37725cdc
NIS
2135 if (tab && tab->Close) {
2136 if ((*tab->Close)(aTHX_ n) != 0)
2137 code = -1;
2138 break;
2139 }
2140 else {
2141 PerlIOBase(n)->flags &=
2142 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2143 }
2144 n = PerlIONext(n);
2145 }
2146 }
2147 else {
2148 SETERRNO(EBADF, SS_IVCHAN);
2149 }
14a5cf38 2150 return code;
9e353e3b
NIS
2151}
2152
2153IV
f62ce20a 2154PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 2155{
96a5add6 2156 PERL_UNUSED_CONTEXT;
04892f78 2157 if (PerlIOValid(f)) {
14a5cf38
JH
2158 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2159 }
2160 return 1;
9e353e3b
NIS
2161}
2162
2163IV
f62ce20a 2164PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 2165{
96a5add6 2166 PERL_UNUSED_CONTEXT;
04892f78 2167 if (PerlIOValid(f)) {
14a5cf38
JH
2168 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2169 }
2170 return 1;
9e353e3b
NIS
2171}
2172
2173void
f62ce20a 2174PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 2175{
04892f78 2176 if (PerlIOValid(f)) {
dcda55fc 2177 PerlIO * const n = PerlIONext(f);
14a5cf38 2178 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
04892f78 2179 if (PerlIOValid(n))
14a5cf38
JH
2180 PerlIO_clearerr(n);
2181 }
9e353e3b
NIS
2182}
2183
2184void
f62ce20a 2185PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 2186{
96a5add6 2187 PERL_UNUSED_CONTEXT;
04892f78 2188 if (PerlIOValid(f)) {
14a5cf38
JH
2189 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2190 }
9e353e3b
NIS
2191}
2192
93a8090d
NIS
2193SV *
2194PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2195{
2196 if (!arg)
a0714e2c 2197 return NULL;
93a8090d
NIS
2198#ifdef sv_dup
2199 if (param) {
2200 return sv_dup(arg, param);
2201 }
2202 else {
2203 return newSVsv(arg);
2204 }
2205#else
1b6737cc 2206 PERL_UNUSED_ARG(param);
93a8090d
NIS
2207 return newSVsv(arg);
2208#endif
2209}
2210
2211PerlIO *
ecdeb87c 2212PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d 2213{
1b6737cc 2214 PerlIO * const nexto = PerlIONext(o);
04892f78 2215 if (PerlIOValid(nexto)) {
de009b76 2216 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
37725cdc
NIS
2217 if (tab && tab->Dup)
2218 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2219 else
2220 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
93a8090d
NIS
2221 }
2222 if (f) {
dcda55fc 2223 PerlIO_funcs * const self = PerlIOBase(o)->tab;
210e727c 2224 SV *arg;
93a8090d 2225 char buf[8];
fe5a182c
JH
2226 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2227 self->name, (void*)f, (void*)o, (void*)param);
210e727c
JH
2228 if (self->Getarg)
2229 arg = (*self->Getarg)(aTHX_ o, param, flags);
2230 else {
a0714e2c 2231 arg = NULL;
93a8090d
NIS
2232 }
2233 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
ecdeb87c 2234 if (arg) {
93a8090d
NIS
2235 SvREFCNT_dec(arg);
2236 }
2237 }
2238 return f;
2239}
2240
168d5872 2241#ifdef USE_THREADS
93a8090d 2242perl_mutex PerlIO_mutex;
93a8090d 2243#endif
27da23d5
JH
2244
2245/* PL_perlio_fd_refcnt[] is in intrpvar.h */
93a8090d 2246
22c96fc1
NC
2247/* Must be called with PerlIO_mutex locked. */
2248static void
2249S_more_refcounted_fds(pTHX_ const int new_fd) {
7a89be66 2250 dVAR;
22c96fc1 2251 const int old_max = PL_perlio_fd_refcnt_size;
f4ae5be6 2252 const int new_max = 16 + (new_fd & ~15);
22c96fc1
NC
2253 int *new_array;
2254
2255 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2256 old_max, new_fd, new_max);
2257
2258 if (new_fd < old_max) {
2259 return;
2260 }
2261
f4ae5be6
NC
2262 assert (new_max > new_fd);
2263
22c96fc1
NC
2264 new_array
2265 = PerlMemShared_realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
2266
2267 if (!new_array) {
2268#ifdef USE_THREADS
2269 MUTEX_UNLOCK(&PerlIO_mutex);
2270#endif
2271 /* Can't use PerlIO to write as it allocates memory */
2272 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2273 PL_no_mem, strlen(PL_no_mem));
2274 my_exit(1);
2275 }
2276
2277 PL_perlio_fd_refcnt_size = new_max;
2278 PL_perlio_fd_refcnt = new_array;
2279
95b63a38
JH
2280 PerlIO_debug("Zeroing %p, %d\n",
2281 (void*)(new_array + old_max),
2282 new_max - old_max);
22c96fc1
NC
2283
2284 Zero(new_array + old_max, new_max - old_max, int);
2285}
2286
2287
93a8090d
NIS
2288void
2289PerlIO_init(pTHX)
2290{
2291 /* Place holder for stdstreams call ??? */
168d5872 2292#ifdef USE_THREADS
27da23d5 2293 MUTEX_INIT(&PerlIO_mutex);
96a5add6
AL
2294#else
2295 PERL_UNUSED_CONTEXT;
93a8090d
NIS
2296#endif
2297}
2298
168d5872
NIS
2299void
2300PerlIOUnix_refcnt_inc(int fd)
2301{
27da23d5 2302 dTHX;
22c96fc1 2303 if (fd >= 0) {
97aff369 2304 dVAR;
22c96fc1 2305
168d5872
NIS
2306#ifdef USE_THREADS
2307 MUTEX_LOCK(&PerlIO_mutex);
2308#endif
22c96fc1
NC
2309 if (fd >= PL_perlio_fd_refcnt_size)
2310 S_more_refcounted_fds(aTHX_ fd);
2311
27da23d5
JH
2312 PL_perlio_fd_refcnt[fd]++;
2313 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
22c96fc1 2314
168d5872
NIS
2315#ifdef USE_THREADS
2316 MUTEX_UNLOCK(&PerlIO_mutex);
2317#endif
2318 }
2319}
2320
168d5872
NIS
2321int
2322PerlIOUnix_refcnt_dec(int fd)
2323{
27da23d5 2324 dTHX;
168d5872 2325 int cnt = 0;
22c96fc1 2326 if (fd >= 0) {
97aff369 2327 dVAR;
168d5872
NIS
2328#ifdef USE_THREADS
2329 MUTEX_LOCK(&PerlIO_mutex);
2330#endif
22c96fc1
NC
2331 /* XXX should this be a panic? */
2332 if (fd >= PL_perlio_fd_refcnt_size)
2333 S_more_refcounted_fds(aTHX_ fd);
2334
2335 /* XXX should this be a panic if it drops below 0? */
27da23d5 2336 cnt = --PL_perlio_fd_refcnt[fd];
168d5872
NIS
2337 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2338#ifdef USE_THREADS
2339 MUTEX_UNLOCK(&PerlIO_mutex);
2340#endif
2341 }
2342 return cnt;
2343}
2344
694c95cf
JH
2345void
2346PerlIO_cleanup(pTHX)
2347{
97aff369 2348 dVAR;
694c95cf
JH
2349 int i;
2350#ifdef USE_ITHREADS
a25429c6 2351 PerlIO_debug("Cleanup layers for %p\n",(void*)aTHX);
9f4bd222
NIS
2352#else
2353 PerlIO_debug("Cleanup layers\n");
694c95cf
JH
2354#endif
2355 /* Raise STDIN..STDERR refcount so we don't close them */
2356 for (i=0; i < 3; i++)
2357 PerlIOUnix_refcnt_inc(i);
2358 PerlIO_cleantable(aTHX_ &PL_perlio);
2359 /* Restore STDIN..STDERR refcount */
2360 for (i=0; i < 3; i++)
2361 PerlIOUnix_refcnt_dec(i);
9f4bd222
NIS
2362
2363 if (PL_known_layers) {
2364 PerlIO_list_free(aTHX_ PL_known_layers);
2365 PL_known_layers = NULL;
2366 }
27da23d5 2367 if (PL_def_layerlist) {
9f4bd222
NIS
2368 PerlIO_list_free(aTHX_ PL_def_layerlist);
2369 PL_def_layerlist = NULL;
2370 }
694c95cf
JH
2371}
2372
2373
2374
9e353e3b 2375/*--------------------------------------------------------------------------------------*/
14a5cf38 2376/*
71200d45 2377 * Bottom-most level for UNIX-like case
14a5cf38 2378 */
9e353e3b 2379
14a5cf38 2380typedef struct {
22569500
NIS
2381 struct _PerlIO base; /* The generic part */
2382 int fd; /* UNIX like file descriptor */
2383 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2384} PerlIOUnix;
2385
6f9d8c32 2386int
9e353e3b 2387PerlIOUnix_oflags(const char *mode)
760ac839 2388{
14a5cf38 2389 int oflags = -1;
3b6c1aba 2390 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
14a5cf38
JH
2391 mode++;
2392 switch (*mode) {
2393 case 'r':
2394 oflags = O_RDONLY;
2395 if (*++mode == '+') {
2396 oflags = O_RDWR;
2397 mode++;
2398 }
2399 break;
2400
2401 case 'w':
2402 oflags = O_CREAT | O_TRUNC;
2403 if (*++mode == '+') {
2404 oflags |= O_RDWR;
2405 mode++;
2406 }
2407 else
2408 oflags |= O_WRONLY;
2409 break;
2410
2411 case 'a':
2412 oflags = O_CREAT | O_APPEND;
2413 if (*++mode == '+') {
2414 oflags |= O_RDWR;
2415 mode++;
2416 }
2417 else
2418 oflags |= O_WRONLY;
2419 break;
2420 }
2421 if (*mode == 'b') {
2422 oflags |= O_BINARY;
2423 oflags &= ~O_TEXT;
2424 mode++;
2425 }
2426 else if (*mode == 't') {
2427 oflags |= O_TEXT;
2428 oflags &= ~O_BINARY;
2429 mode++;
2430 }
2431 /*
71200d45 2432 * Always open in binary mode
14a5cf38
JH
2433 */
2434 oflags |= O_BINARY;
2435 if (*mode || oflags == -1) {
93189314 2436 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2437 oflags = -1;
2438 }
2439 return oflags;
9e353e3b
NIS
2440}
2441
2442IV
f62ce20a 2443PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2444{
96a5add6 2445 PERL_UNUSED_CONTEXT;
14a5cf38 2446 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2447}
2448
aa063c35
NIS
2449static void
2450PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
4b803d04 2451{
de009b76 2452 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
6caa5a9c 2453#if defined(WIN32)
aa063c35
NIS
2454 Stat_t st;
2455 if (PerlLIO_fstat(fd, &st) == 0) {
6caa5a9c 2456 if (!S_ISREG(st.st_mode)) {
aa063c35 2457 PerlIO_debug("%d is not regular file\n",fd);
6caa5a9c
NIS
2458 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2459 }
aa063c35
NIS
2460 else {
2461 PerlIO_debug("%d _is_ a regular file\n",fd);
2462 }
6caa5a9c
NIS
2463 }
2464#endif
aa063c35
NIS
2465 s->fd = fd;
2466 s->oflags = imode;
2467 PerlIOUnix_refcnt_inc(fd);
96a5add6 2468 PERL_UNUSED_CONTEXT;
aa063c35
NIS
2469}
2470
2471IV
2472PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2473{
2474 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
14a5cf38 2475 if (*PerlIONext(f)) {
4b069b44 2476 /* We never call down so do any pending stuff now */
03c0554d 2477 PerlIO_flush(PerlIONext(f));
14a5cf38 2478 /*
71200d45 2479 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2480 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2481 * Should the value on NULL mode be 0 or -1?
14a5cf38 2482 */
acbd16bf 2483 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
aa063c35 2484 mode ? PerlIOUnix_oflags(mode) : -1);
14a5cf38
JH
2485 }
2486 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
6caa5a9c 2487
14a5cf38 2488 return code;
4b803d04
NIS
2489}
2490
c2fcde81
JH
2491IV
2492PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2493{
de009b76 2494 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
0723351e 2495 Off_t new_loc;
96a5add6 2496 PERL_UNUSED_CONTEXT;
c2fcde81
JH
2497 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2498#ifdef ESPIPE
2499 SETERRNO(ESPIPE, LIB_INVARG);
2500#else
2501 SETERRNO(EINVAL, LIB_INVARG);
2502#endif
2503 return -1;
2504 }
0723351e
NC
2505 new_loc = PerlLIO_lseek(fd, offset, whence);
2506 if (new_loc == (Off_t) - 1)
dcda55fc 2507 return -1;
c2fcde81
JH
2508 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2509 return 0;
2510}
2511
9e353e3b 2512PerlIO *
14a5cf38
JH
2513PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2514 IV n, const char *mode, int fd, int imode,
2515 int perm, PerlIO *f, int narg, SV **args)
2516{
d9dac8cd 2517 if (PerlIOValid(f)) {
14a5cf38 2518 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2519 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2520 }
2521 if (narg > 0) {
3b6c1aba 2522 if (*mode == IoTYPE_NUMERIC)
14a5cf38
JH
2523 mode++;
2524 else {
2525 imode = PerlIOUnix_oflags(mode);
2526 perm = 0666;
2527 }
2528 if (imode != -1) {
e62f0680 2529 const char *path = SvPV_nolen_const(*args);
14a5cf38
JH
2530 fd = PerlLIO_open3(path, imode, perm);
2531 }
2532 }
2533 if (fd >= 0) {
3b6c1aba 2534 if (*mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2535 mode++;
2536 if (!f) {
2537 f = PerlIO_allocate(aTHX);
d9dac8cd
NIS
2538 }
2539 if (!PerlIOValid(f)) {
a33cf58c
NIS
2540 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2541 return NULL;
2542 }
d9dac8cd 2543 }
aa063c35 2544 PerlIOUnix_setfd(aTHX_ f, fd, imode);
14a5cf38 2545 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
c2fcde81
JH
2546 if (*mode == IoTYPE_APPEND)
2547 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
14a5cf38
JH
2548 return f;
2549 }
2550 else {
2551 if (f) {
6f207bd3 2552 NOOP;
14a5cf38 2553 /*
71200d45 2554 * FIXME: pop layers ???
14a5cf38
JH
2555 */
2556 }
2557 return NULL;
2558 }
9e353e3b
NIS
2559}
2560
71200d45 2561PerlIO *
ecdeb87c 2562PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 2563{
dcda55fc 2564 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2565 int fd = os->fd;
ecdeb87c
NIS
2566 if (flags & PERLIO_DUP_FD) {
2567 fd = PerlLIO_dup(fd);
2568 }
22c96fc1 2569 if (fd >= 0) {
ecdeb87c 2570 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2571 if (f) {
2572 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
aa063c35 2573 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
71200d45
NIS
2574 return f;
2575 }
71200d45
NIS
2576 }
2577 return NULL;
2578}
2579
2580
9e353e3b 2581SSize_t
f62ce20a 2582PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2583{
97aff369 2584 dVAR;
de009b76 2585 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2586#ifdef PERLIO_STD_SPECIAL
2587 if (fd == 0)
2588 return PERLIO_STD_IN(fd, vbuf, count);
2589#endif
81428673 2590 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
1fd8f4ce 2591 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
263df5f1 2592 return 0;
1fd8f4ce 2593 }
14a5cf38 2594 while (1) {
b464bac0 2595 const SSize_t len = PerlLIO_read(fd, vbuf, count);
14a5cf38 2596 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2597 if (len < 0) {
2598 if (errno != EAGAIN) {
2599 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2600 }
2601 }
2602 else if (len == 0 && count != 0) {
14a5cf38 2603 PerlIOBase(f)->flags |= PERLIO_F_EOF;
ba85f2ea
NIS
2604 SETERRNO(0,0);
2605 }
14a5cf38
JH
2606 return len;
2607 }
2608 PERL_ASYNC_CHECK();
2609 }
b464bac0 2610 /*NOTREACHED*/
9e353e3b
NIS
2611}
2612
2613SSize_t
f62ce20a 2614PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2615{
97aff369 2616 dVAR;
de009b76 2617 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2618#ifdef PERLIO_STD_SPECIAL
2619 if (fd == 1 || fd == 2)
2620 return PERLIO_STD_OUT(fd, vbuf, count);
2621#endif
14a5cf38 2622 while (1) {
de009b76 2623 const SSize_t len = PerlLIO_write(fd, vbuf, count);
14a5cf38 2624 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2625 if (len < 0) {
2626 if (errno != EAGAIN) {
2627 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2628 }
2629 }
14a5cf38
JH
2630 return len;
2631 }
2632 PERL_ASYNC_CHECK();
06da4f11 2633 }
1b6737cc 2634 /*NOTREACHED*/
9e353e3b
NIS
2635}
2636
9e353e3b 2637Off_t
f62ce20a 2638PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2639{
96a5add6
AL
2640 PERL_UNUSED_CONTEXT;
2641
14a5cf38 2642 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2643}
2644
71200d45 2645
9e353e3b 2646IV
f62ce20a 2647PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2648{
97aff369 2649 dVAR;
de009b76 2650 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
14a5cf38 2651 int code = 0;
168d5872
NIS
2652 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2653 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2654 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2655 return 0;
22569500 2656 }
93a8090d
NIS
2657 }
2658 else {
93189314 2659 SETERRNO(EBADF,SS_IVCHAN);
93a8090d
NIS
2660 return -1;
2661 }
14a5cf38
JH
2662 while (PerlLIO_close(fd) != 0) {
2663 if (errno != EINTR) {
2664 code = -1;
2665 break;
2666 }
2667 PERL_ASYNC_CHECK();
2668 }
2669 if (code == 0) {
2670 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2671 }
2672 return code;
9e353e3b
NIS
2673}
2674
27da23d5 2675PERLIO_FUNCS_DECL(PerlIO_unix) = {
2dc2558e 2676 sizeof(PerlIO_funcs),
14a5cf38
JH
2677 "unix",
2678 sizeof(PerlIOUnix),
2679 PERLIO_K_RAW,
2680 PerlIOUnix_pushed,
44798d05 2681 PerlIOBase_popped,
14a5cf38 2682 PerlIOUnix_open,
86e05cf2 2683 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2684 NULL,
2685 PerlIOUnix_fileno,
71200d45 2686 PerlIOUnix_dup,
14a5cf38
JH
2687 PerlIOUnix_read,
2688 PerlIOBase_unread,
2689 PerlIOUnix_write,
2690 PerlIOUnix_seek,
2691 PerlIOUnix_tell,
2692 PerlIOUnix_close,
22569500
NIS
2693 PerlIOBase_noop_ok, /* flush */
2694 PerlIOBase_noop_fail, /* fill */
14a5cf38
JH
2695 PerlIOBase_eof,
2696 PerlIOBase_error,
2697 PerlIOBase_clearerr,
2698 PerlIOBase_setlinebuf,
22569500
NIS
2699 NULL, /* get_base */
2700 NULL, /* get_bufsiz */
2701 NULL, /* get_ptr */
2702 NULL, /* get_cnt */
2703 NULL, /* set_ptrcnt */
9e353e3b
NIS
2704};
2705
2706/*--------------------------------------------------------------------------------------*/
14a5cf38 2707/*
71200d45 2708 * stdio as a layer
14a5cf38 2709 */
9e353e3b 2710
313e59c8
NIS
2711#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2712/* perl5.8 - This ensures the last minute VMS ungetc fix is not
2713 broken by the last second glibc 2.3 fix
2714 */
2715#define STDIO_BUFFER_WRITABLE
2716#endif
2717
2718
14a5cf38
JH
2719typedef struct {
2720 struct _PerlIO base;
22569500 2721 FILE *stdio; /* The stream */
9e353e3b
NIS
2722} PerlIOStdio;
2723
2724IV
f62ce20a 2725PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2726{
96a5add6
AL
2727 PERL_UNUSED_CONTEXT;
2728
c4420975
AL
2729 if (PerlIOValid(f)) {
2730 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2731 if (s)
2732 return PerlSIO_fileno(s);
439ba545
NIS
2733 }
2734 errno = EBADF;
2735 return -1;
9e353e3b
NIS
2736}
2737
766a733e 2738char *
14a5cf38
JH
2739PerlIOStdio_mode(const char *mode, char *tmode)
2740{
de009b76 2741 char * const ret = tmode;
a0625d38
SR
2742 if (mode) {
2743 while (*mode) {
2744 *tmode++ = *mode++;
2745 }
14a5cf38 2746 }
95005ad8 2747#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
6ce75a77
JH
2748 *tmode++ = 'b';
2749#endif
14a5cf38
JH
2750 *tmode = '\0';
2751 return ret;
2752}
2753
4b803d04 2754IV
2dc2558e 2755PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2756{
1fd8f4ce
NIS
2757 PerlIO *n;
2758 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
46c461b5 2759 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
1fd8f4ce
NIS
2760 if (toptab == tab) {
2761 /* Top is already stdio - pop self (duplicate) and use original */
2762 PerlIO_pop(aTHX_ f);
2763 return 0;
2764 } else {
de009b76 2765 const int fd = PerlIO_fileno(n);
1fd8f4ce
NIS
2766 char tmode[8];
2767 FILE *stdio;
81428673 2768 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
1fd8f4ce
NIS
2769 mode = PerlIOStdio_mode(mode, tmode)))) {
2770 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2771 /* We never call down so do any pending stuff now */
2772 PerlIO_flush(PerlIONext(f));
81428673 2773 }
1fd8f4ce
NIS
2774 else {
2775 return -1;
2776 }
2777 }
14a5cf38 2778 }
2dc2558e 2779 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4b803d04
NIS
2780}
2781
22569500 2782
9e353e3b 2783PerlIO *
4b069b44 2784PerlIO_importFILE(FILE *stdio, const char *mode)
9e353e3b 2785{
14a5cf38
JH
2786 dTHX;
2787 PerlIO *f = NULL;
2788 if (stdio) {
22569500 2789 PerlIOStdio *s;
4b069b44
NIS
2790 if (!mode || !*mode) {
2791 /* We need to probe to see how we can open the stream
2792 so start with read/write and then try write and read
2793 we dup() so that we can fclose without loosing the fd.
2794
2795 Note that the errno value set by a failing fdopen
2796 varies between stdio implementations.
2797 */
de009b76 2798 const int fd = PerlLIO_dup(fileno(stdio));
a33cf58c 2799 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
4b069b44 2800 if (!f2) {
a33cf58c 2801 f2 = PerlSIO_fdopen(fd, (mode = "w"));
4b069b44
NIS
2802 }
2803 if (!f2) {
a33cf58c 2804 f2 = PerlSIO_fdopen(fd, (mode = "r"));
4b069b44
NIS
2805 }
2806 if (!f2) {
2807 /* Don't seem to be able to open */
2808 PerlLIO_close(fd);
2809 return f;
2810 }
2811 fclose(f2);
22569500 2812 }
a0714e2c 2813 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
a33cf58c
NIS
2814 s = PerlIOSelf(f, PerlIOStdio);
2815 s->stdio = stdio;
2816 }
14a5cf38
JH
2817 }
2818 return f;
9e353e3b
NIS
2819}
2820
2821PerlIO *
14a5cf38
JH
2822PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2823 IV n, const char *mode, int fd, int imode,
2824 int perm, PerlIO *f, int narg, SV **args)
2825{
2826 char tmode[8];
d9dac8cd 2827 if (PerlIOValid(f)) {
dcda55fc
AL
2828 const char * const path = SvPV_nolen_const(*args);
2829 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2830 FILE *stdio;
2831 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2832 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2833 s->stdio);
2834 if (!s->stdio)
2835 return NULL;
2836 s->stdio = stdio;
1751d015 2837 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2838 return f;
2839 }
2840 else {
2841 if (narg > 0) {
dcda55fc 2842 const char * const path = SvPV_nolen_const(*args);
3b6c1aba 2843 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
2844 mode++;
2845 fd = PerlLIO_open3(path, imode, perm);
2846 }
2847 else {
95005ad8
GH
2848 FILE *stdio;
2849 bool appended = FALSE;
2850#ifdef __CYGWIN__
2851 /* Cygwin wants its 'b' early. */
2852 appended = TRUE;
2853 mode = PerlIOStdio_mode(mode, tmode);
2854#endif
2855 stdio = PerlSIO_fopen(path, mode);
6f0313ac 2856 if (stdio) {
6f0313ac
JH
2857 if (!f) {
2858 f = PerlIO_allocate(aTHX);
2859 }
95005ad8
GH
2860 if (!appended)
2861 mode = PerlIOStdio_mode(mode, tmode);
2862 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2863 if (f) {
0f0f9e2b
JH
2864 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2865 PerlIOUnix_refcnt_inc(fileno(stdio));
2866 } else {
2867 PerlSIO_fclose(stdio);
6f0313ac
JH
2868 }
2869 return f;
2870 }
2871 else {
2872 return NULL;
2873 }
14a5cf38
JH
2874 }
2875 }
2876 if (fd >= 0) {
2877 FILE *stdio = NULL;
2878 int init = 0;
3b6c1aba 2879 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
2880 init = 1;
2881 mode++;
2882 }
2883 if (init) {
2884 switch (fd) {
2885 case 0:
2886 stdio = PerlSIO_stdin;
2887 break;
2888 case 1:
2889 stdio = PerlSIO_stdout;
2890 break;
2891 case 2:
2892 stdio = PerlSIO_stderr;
2893 break;
2894 }
2895 }
2896 else {
2897 stdio = PerlSIO_fdopen(fd, mode =
2898 PerlIOStdio_mode(mode, tmode));
2899 }
2900 if (stdio) {
d9dac8cd
NIS
2901 if (!f) {
2902 f = PerlIO_allocate(aTHX);
2903 }
a33cf58c 2904 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
1a159fba
JH
2905 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2906 PerlIOUnix_refcnt_inc(fileno(stdio));
a33cf58c 2907 }
14a5cf38
JH
2908 return f;
2909 }
2910 }
2911 }
ee518936 2912 return NULL;
9e353e3b
NIS
2913}
2914
1751d015 2915PerlIO *
ecdeb87c 2916PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2917{
2918 /* This assumes no layers underneath - which is what
2919 happens, but is not how I remember it. NI-S 2001/10/16
2920 */
ecdeb87c 2921 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2922 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
de009b76 2923 const int fd = fileno(stdio);
9217ff3f 2924 char mode[8];
ecdeb87c 2925 if (flags & PERLIO_DUP_FD) {
de009b76 2926 const int dfd = PerlLIO_dup(fileno(stdio));
9217ff3f
NIS
2927 if (dfd >= 0) {
2928 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2929 goto set_this;
ecdeb87c
NIS
2930 }
2931 else {
6f207bd3 2932 NOOP;
ecdeb87c
NIS
2933 /* FIXME: To avoid messy error recovery if dup fails
2934 re-use the existing stdio as though flag was not set
2935 */
2936 }
2937 }
9217ff3f
NIS
2938 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2939 set_this:
694c95cf
JH
2940 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2941 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2942 }
2943 return f;
2944}
2945
0d7a5398
NIS
2946static int
2947PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2948{
96a5add6
AL
2949 PERL_UNUSED_CONTEXT;
2950
0d7a5398 2951 /* XXX this could use PerlIO_canset_fileno() and
37725cdc 2952 * PerlIO_set_fileno() support from Configure
0d7a5398 2953 */
ef8eacb8
AT
2954# if defined(__UCLIBC__)
2955 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2956 f->__filedes = -1;
2957 return 1;
2958# elif defined(__GLIBC__)
0d7a5398 2959 /* There may be a better way for GLIBC:
37725cdc 2960 - libio.h defines a flag to not close() on cleanup
0d7a5398
NIS
2961 */
2962 f->_fileno = -1;
2963 return 1;
2964# elif defined(__sun__)
cfedb851 2965 return 0;
0d7a5398
NIS
2966# elif defined(__hpux)
2967 f->__fileH = 0xff;
2968 f->__fileL = 0xff;
2969 return 1;
9837d373 2970 /* Next one ->_file seems to be a reasonable fallback, i.e. if
37725cdc 2971 your platform does not have special entry try this one.
9837d373
NIS
2972 [For OSF only have confirmation for Tru64 (alpha)
2973 but assume other OSFs will be similar.]
37725cdc 2974 */
9837d373 2975# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
0d7a5398
NIS
2976 f->_file = -1;
2977 return 1;
2978# elif defined(__FreeBSD__)
2979 /* There may be a better way on FreeBSD:
37725cdc
NIS
2980 - we could insert a dummy func in the _close function entry
2981 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2982 */
2983 f->_file = -1;
0c49ea6a
SU
2984 return 1;
2985# elif defined(__OpenBSD__)
2986 /* There may be a better way on OpenBSD:
2987 - we could insert a dummy func in the _close function entry
2988 f->_close = (int (*)(void *)) dummy_close;
2989 */
2990 f->_file = -1;
0d7a5398 2991 return 1;
59ad941d
IZ
2992# elif defined(__EMX__)
2993 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
2994 f->_handle = -1;
2995 return 1;
0d7a5398
NIS
2996# elif defined(__CYGWIN__)
2997 /* There may be a better way on CYGWIN:
37725cdc
NIS
2998 - we could insert a dummy func in the _close function entry
2999 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
3000 */
3001 f->_file = -1;
3002 return 1;
3003# elif defined(WIN32)
3004# if defined(__BORLANDC__)
3005 f->fd = PerlLIO_dup(fileno(f));
b475b3e6
JH
3006# elif defined(UNDER_CE)
3007 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3008 structure at all
3009 */
0d7a5398
NIS
3010# else
3011 f->_file = -1;
3012# endif
3013 return 1;
3014# else
3015#if 0
37725cdc 3016 /* Sarathy's code did this - we fall back to a dup/dup2 hack
0d7a5398 3017 (which isn't thread safe) instead
37725cdc 3018 */
0d7a5398
NIS
3019# error "Don't know how to set FILE.fileno on your platform"
3020#endif
8772537c 3021 PERL_UNUSED_ARG(f);
0d7a5398
NIS
3022 return 0;
3023# endif
3024}
3025
1751d015 3026IV
f62ce20a 3027PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 3028{
c4420975 3029 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
3030 if (!stdio) {
3031 errno = EBADF;
3032 return -1;
3033 }
9217ff3f 3034 else {
de009b76 3035 const int fd = fileno(stdio);
0d7a5398 3036 int invalidate = 0;
bbfd922f 3037 IV result = 0;
0d7a5398
NIS
3038 int saveerr = 0;
3039 int dupfd = 0;
3040#ifdef SOCKS5_VERSION_NAME
37725cdc
NIS
3041 /* Socks lib overrides close() but stdio isn't linked to
3042 that library (though we are) - so we must call close()
3043 on sockets on stdio's behalf.
3044 */
0d7a5398
NIS
3045 int optval;
3046 Sock_size_t optlen = sizeof(int);
6b4ce6c8 3047 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
37725cdc 3048 invalidate = 1;
0d7a5398 3049#endif
6b4ce6c8 3050 if (PerlIOUnix_refcnt_dec(fd) > 0) /* File descriptor still in use */
0d7a5398 3051 invalidate = 1;
0d7a5398 3052 if (invalidate) {
6b4ce6c8
AL
3053 /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
3054 if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
3055 return 0;
3056 if (stdio == stdout || stdio == stderr)
3057 return PerlIO_flush(f);
37725cdc
NIS
3058 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3059 Use Sarathy's trick from maint-5.6 to invalidate the
3060 fileno slot of the FILE *
3061 */
bbfd922f 3062 result = PerlIO_flush(f);
0d7a5398 3063 saveerr = errno;
6b4ce6c8
AL
3064 invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
3065 if (!invalidate)
3066 dupfd = PerlLIO_dup(fd);
37725cdc 3067 }
0d7a5398 3068 result = PerlSIO_fclose(stdio);
37725cdc
NIS
3069 /* We treat error from stdio as success if we invalidated
3070 errno may NOT be expected EBADF
e8529473
NIS
3071 */
3072 if (invalidate && result != 0) {
0d7a5398
NIS
3073 errno = saveerr;
3074 result = 0;
37725cdc 3075 }
6b4ce6c8
AL
3076#ifdef SOCKS5_VERSION_NAME
3077 /* in SOCKS' case, let close() determine return value */
3078 result = close(fd);
3079#endif
0d7a5398
NIS
3080 if (dupfd) {
3081 PerlLIO_dup2(dupfd,fd);
8a521f28 3082 PerlLIO_close(dupfd);
9217ff3f
NIS
3083 }
3084 return result;
37725cdc 3085 }
1751d015
NIS
3086}
3087
9e353e3b 3088SSize_t
f62ce20a 3089PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 3090{
97aff369 3091 dVAR;
c4420975 3092 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3093 SSize_t got = 0;
4d948241
NIS
3094 for (;;) {
3095 if (count == 1) {
3096 STDCHAR *buf = (STDCHAR *) vbuf;
3097 /*
3098 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3099 * stdio does not do that for fread()
3100 */
de009b76 3101 const int ch = PerlSIO_fgetc(s);
4d948241
NIS
3102 if (ch != EOF) {
3103 *buf = ch;
3104 got = 1;
3105 }
14a5cf38 3106 }
4d948241
NIS
3107 else
3108 got = PerlSIO_fread(vbuf, 1, count, s);
b1d8b47a
CS
3109 if (got == 0 && PerlSIO_ferror(s))
3110 got = -1;
42a7a32f 3111 if (got >= 0 || errno != EINTR)
4d948241
NIS
3112 break;
3113 PERL_ASYNC_CHECK();
42a7a32f 3114 SETERRNO(0,0); /* just in case */
14a5cf38 3115 }
14a5cf38 3116 return got;
9e353e3b
NIS
3117}
3118
3119SSize_t
f62ce20a 3120PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3121{
14a5cf38 3122 SSize_t unread = 0;
c4420975 3123 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3124
313e59c8 3125#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3126 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3127 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3128 STDCHAR *base = PerlIO_get_base(f);
3129 SSize_t cnt = PerlIO_get_cnt(f);
3130 STDCHAR *ptr = PerlIO_get_ptr(f);
3131 SSize_t avail = ptr - base;
3132 if (avail > 0) {
3133 if (avail > count) {
3134 avail = count;
3135 }
3136 ptr -= avail;
3137 Move(buf-avail,ptr,avail,STDCHAR);
3138 count -= avail;
3139 unread += avail;
3140 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
3141 if (PerlSIO_feof(s) && unread >= 0)
3142 PerlSIO_clearerr(s);
3143 }
3144 }
313e59c8
NIS
3145 else
3146#endif
3147 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3148 /* We can get pointer to buffer but not its base
3149 Do ungetc() but check chars are ending up in the
3150 buffer
3151 */
3152 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3153 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3154 while (count > 0) {
de009b76 3155 const int ch = *--buf & 0xFF;
9f7cd136
NIS
3156 if (ungetc(ch,s) != ch) {
3157 /* ungetc did not work */
3158 break;
3159 }
3160 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3161 /* Did not change pointer as expected */
3162 fgetc(s); /* get char back again */
3163 break;
3164 }
3165 /* It worked ! */
3166 count--;
3167 unread++;
93679785
NIS
3168 }
3169 }
3170
3171 if (count > 0) {
3172 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3173 }
3174 return unread;
9e353e3b
NIS
3175}
3176
3177SSize_t
f62ce20a 3178PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3179{
97aff369 3180 dVAR;
4d948241
NIS
3181 SSize_t got;
3182 for (;;) {
3183 got = PerlSIO_fwrite(vbuf, 1, count,
3184 PerlIOSelf(f, PerlIOStdio)->stdio);
42a7a32f 3185 if (got >= 0 || errno != EINTR)
4d948241
NIS
3186 break;
3187 PERL_ASYNC_CHECK();
42a7a32f 3188 SETERRNO(0,0); /* just in case */
4d948241
NIS
3189 }
3190 return got;
9e353e3b
NIS
3191}
3192
94a175e1 3193IV
f62ce20a 3194PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3195{
c4420975 3196 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3197 PERL_UNUSED_CONTEXT;
3198
94a175e1 3199 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
3200}
3201
3202Off_t
f62ce20a 3203PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 3204{
c4420975 3205 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3206 PERL_UNUSED_CONTEXT;
3207
94a175e1 3208 return PerlSIO_ftell(stdio);
9e353e3b
NIS
3209}
3210
3211IV
f62ce20a 3212PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 3213{
c4420975 3214 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3215 PERL_UNUSED_CONTEXT;
3216
14a5cf38
JH
3217 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3218 return PerlSIO_fflush(stdio);
3219 }
3220 else {
6f207bd3 3221 NOOP;
88b61e10 3222#if 0
14a5cf38
JH
3223 /*
3224 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 3225 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 3226 * design is to do _this_ but not have layer above flush this
71200d45 3227 * layer read-to-read
14a5cf38
JH
3228 */
3229 /*
71200d45 3230 * Not writeable - sync by attempting a seek
14a5cf38 3231 */
79852593 3232 const int err = errno;
14a5cf38
JH
3233 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3234 errno = err;
88b61e10 3235#endif
14a5cf38
JH
3236 }
3237 return 0;
9e353e3b
NIS
3238}
3239
3240IV
f62ce20a 3241PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 3242{
96a5add6
AL
3243 PERL_UNUSED_CONTEXT;
3244
14a5cf38 3245 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3246}
3247
3248IV
f62ce20a 3249PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 3250{
96a5add6
AL
3251 PERL_UNUSED_CONTEXT;
3252
263df5f1 3253 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3254}
3255
3256void
f62ce20a 3257PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 3258{
96a5add6
AL
3259 PERL_UNUSED_CONTEXT;
3260
14a5cf38 3261 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3262}
3263
3264void
f62ce20a 3265PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 3266{
96a5add6
AL
3267 PERL_UNUSED_CONTEXT;
3268
9e353e3b 3269#ifdef HAS_SETLINEBUF
14a5cf38 3270 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 3271#else
bd61b366 3272 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
9e353e3b
NIS
3273#endif
3274}
3275
3276#ifdef FILE_base
3277STDCHAR *
f62ce20a 3278PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 3279{
c4420975 3280 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3281 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
3282}
3283
3284Size_t
f62ce20a 3285PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3286{
c4420975 3287 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3288 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
3289}
3290#endif
3291
3292#ifdef USE_STDIO_PTR
3293STDCHAR *
f62ce20a 3294PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3295{
c4420975 3296 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3297 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
3298}
3299
3300SSize_t
f62ce20a 3301PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3302{
c4420975 3303 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3304 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
3305}
3306
3307void
f62ce20a 3308PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3309{
c4420975 3310 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3311 if (ptr != NULL) {
9e353e3b 3312#ifdef STDIO_PTR_LVALUE
22569500 3313 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 3314#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 3315 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
3316 assert(PerlSIO_get_cnt(stdio) == (cnt));
3317 }
9e353e3b
NIS
3318#endif
3319#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 3320 /*
71200d45 3321 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
3322 */
3323 return;
9e353e3b 3324#endif
22569500 3325#else /* STDIO_PTR_LVALUE */
14a5cf38 3326 PerlProc_abort();
22569500 3327#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
3328 }
3329 /*
71200d45 3330 * Now (or only) set cnt
14a5cf38 3331 */
9e353e3b 3332#ifdef STDIO_CNT_LVALUE
14a5cf38 3333 PerlSIO_set_cnt(stdio, cnt);
22569500 3334#else /* STDIO_CNT_LVALUE */
9e353e3b 3335#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
3336 PerlSIO_set_ptr(stdio,
3337 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3338 cnt));
22569500 3339#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 3340 PerlProc_abort();
22569500
NIS
3341#endif /* STDIO_PTR_LVAL_SETS_CNT */
3342#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
3343}
3344
93679785 3345
9e353e3b
NIS
3346#endif
3347
93679785
NIS
3348IV
3349PerlIOStdio_fill(pTHX_ PerlIO *f)
3350{
c4420975 3351 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3352 int c;
96a5add6
AL
3353 PERL_UNUSED_CONTEXT;
3354
93679785
NIS
3355 /*
3356 * fflush()ing read-only streams can cause trouble on some stdio-s
3357 */
3358 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3359 if (PerlSIO_fflush(stdio) != 0)
3360 return EOF;
3361 }
3362 c = PerlSIO_fgetc(stdio);
3363 if (c == EOF)
3364 return EOF;
3365
3366#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
3367
3368#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3369 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3370 /* Fake ungetc() to the real buffer in case system's ungetc
3371 goes elsewhere
3372 */
3373 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3374 SSize_t cnt = PerlSIO_get_cnt(stdio);
3375 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3376 if (ptr == base+1) {
3377 *--ptr = (STDCHAR) c;
3378 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3379 if (PerlSIO_feof(stdio))
3380 PerlSIO_clearerr(stdio);
3381 return 0;
3382 }
3383 }
313e59c8
NIS
3384 else
3385#endif
3386 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3387 STDCHAR ch = c;
3388 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3389 return 0;
3390 }
3391 }
93679785
NIS
3392#endif
3393
3394#if defined(VMS)
3395 /* An ungetc()d char is handled separately from the regular
3396 * buffer, so we stuff it in the buffer ourselves.
3397 * Should never get called as should hit code above
3398 */
bad9695d
NIS
3399 *(--((*stdio)->_ptr)) = (unsigned char) c;
3400 (*stdio)->_cnt++;
93679785
NIS
3401#else
3402 /* If buffer snoop scheme above fails fall back to
9f7cd136 3403 using ungetc().
93679785
NIS
3404 */
3405 if (PerlSIO_ungetc(c, stdio) != c)
3406 return EOF;
3407#endif
3408 return 0;
3409}
3410
3411
3412
27da23d5 3413PERLIO_FUNCS_DECL(PerlIO_stdio) = {
2dc2558e 3414 sizeof(PerlIO_funcs),
14a5cf38
JH
3415 "stdio",
3416 sizeof(PerlIOStdio),
86e05cf2 3417 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 3418 PerlIOStdio_pushed,
44798d05 3419 PerlIOBase_popped,
14a5cf38 3420 PerlIOStdio_open,
86e05cf2 3421 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3422 NULL,
3423 PerlIOStdio_fileno,
71200d45 3424 PerlIOStdio_dup,
14a5cf38
JH
3425 PerlIOStdio_read,
3426 PerlIOStdio_unread,
3427 PerlIOStdio_write,
3428 PerlIOStdio_seek,
3429 PerlIOStdio_tell,
3430 PerlIOStdio_close,
3431 PerlIOStdio_flush,
3432 PerlIOStdio_fill,
3433 PerlIOStdio_eof,
3434 PerlIOStdio_error,
3435 PerlIOStdio_clearerr,
3436 PerlIOStdio_setlinebuf,
9e353e3b 3437#ifdef FILE_base
14a5cf38
JH
3438 PerlIOStdio_get_base,
3439 PerlIOStdio_get_bufsiz,
9e353e3b 3440#else
14a5cf38
JH
3441 NULL,
3442 NULL,
9e353e3b
NIS
3443#endif
3444#ifdef USE_STDIO_PTR
14a5cf38
JH
3445 PerlIOStdio_get_ptr,
3446 PerlIOStdio_get_cnt,
15b61c98 3447# if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
79ed0f43 3448 PerlIOStdio_set_ptrcnt,
15b61c98
JH
3449# else
3450 NULL,
3451# endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3452#else
3453 NULL,
14a5cf38
JH
3454 NULL,
3455 NULL,
15b61c98 3456#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3457};
3458
b9d6bf13
JH
3459/* Note that calls to PerlIO_exportFILE() are reversed using
3460 * PerlIO_releaseFILE(), not importFILE. */
9e353e3b 3461FILE *
81428673 3462PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3463{
e87a358a 3464 dTHX;
81428673
NIS
3465 FILE *stdio = NULL;
3466 if (PerlIOValid(f)) {
3467 char buf[8];
3468 PerlIO_flush(f);
3469 if (!mode || !*mode) {
3470 mode = PerlIO_modestr(f, buf);
3471 }
3472 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3473 if (stdio) {
3474 PerlIOl *l = *f;
9f75cc58 3475 PerlIO *f2;
81428673
NIS
3476 /* De-link any lower layers so new :stdio sticks */
3477 *f = NULL;
a0714e2c 3478 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
9f75cc58 3479 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
81428673
NIS
3480 s->stdio = stdio;
3481 /* Link previous lower layers under new one */
3482 *PerlIONext(f) = l;
3483 }
3484 else {
3485 /* restore layers list */
3486 *f = l;
3487 }
a33cf58c 3488 }
14a5cf38
JH
3489 }
3490 return stdio;
9e353e3b
NIS
3491}
3492
81428673 3493
9e353e3b
NIS
3494FILE *
3495PerlIO_findFILE(PerlIO *f)
3496{
14a5cf38
JH
3497 PerlIOl *l = *f;
3498 while (l) {
3499 if (l->tab == &PerlIO_stdio) {
3500 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3501 return s->stdio;
3502 }
3503 l = *PerlIONext(&l);
f7e7eb72 3504 }
4b069b44 3505 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
bd61b366 3506 return PerlIO_exportFILE(f, NULL);
9e353e3b
NIS
3507}
3508
b9d6bf13 3509/* Use this to reverse PerlIO_exportFILE calls. */
9e353e3b
NIS
3510void
3511PerlIO_releaseFILE(PerlIO *p, FILE *f)
3512{
27da23d5 3513 dVAR;
22569500
NIS
3514 PerlIOl *l;
3515 while ((l = *p)) {
3516 if (l->tab == &PerlIO_stdio) {
3517 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3518 if (s->stdio == f) {
3519 dTHX;
3520 PerlIO_pop(aTHX_ p);
3521 return;
3522 }
3523 }
3524 p = PerlIONext(p);
3525 }
3526 return;
9e353e3b
NIS
3527}
3528
3529/*--------------------------------------------------------------------------------------*/
14a5cf38 3530/*
71200d45 3531 * perlio buffer layer
14a5cf38 3532 */
9e353e3b 3533
5e2ab84b 3534IV
2dc2558e 3535PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3536{
14a5cf38 3537 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
de009b76 3538 const int fd = PerlIO_fileno(f);
14a5cf38
JH
3539 if (fd >= 0 && PerlLIO_isatty(fd)) {
3540 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3541 }
4b069b44 3542 if (*PerlIONext(f)) {
de009b76 3543 const Off_t posn = PerlIO_tell(PerlIONext(f));
4b069b44
NIS
3544 if (posn != (Off_t) - 1) {
3545 b->posn = posn;
3546 }
14a5cf38 3547 }
2dc2558e 3548 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3549}
3550
9e353e3b 3551PerlIO *
14a5cf38
JH
3552PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3553 IV n, const char *mode, int fd, int imode, int perm,
3554 PerlIO *f, int narg, SV **args)
3555{
04892f78 3556 if (PerlIOValid(f)) {
14a5cf38 3557 PerlIO *next = PerlIONext(f);
67363c0d
JH
3558 PerlIO_funcs *tab =
3559 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3560 if (tab && tab->Open)
3561 next =
3562 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3563 next, narg, args);
2dc2558e 3564 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3565 return NULL;
3566 }
3567 }
3568 else {
04892f78 3569 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38 3570 int init = 0;
3b6c1aba 3571 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
3572 init = 1;
3573 /*
71200d45 3574 * mode++;
14a5cf38
JH
3575 */