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