This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correct the paramter to Perl_op_xmldump(). The one that got away from
[perl5.git] / perlio.c
CommitLineData
14a5cf38 1/*
5cb43542
RGS
2 * perlio.c
3 * Copyright (c) 1996-2006, Nick Ing-Simmons
c1d0198f 4 * Copyright (c) 2006, 2007, Larry Wall and others
5cb43542
RGS
5 *
6 * You may distribute under the terms of either the GNU General Public License
7 * or the Artistic License, as specified in the README file.
760ac839
LW
8 */
9
14a5cf38 10/*
d31a8517
AT
11 * Hour after hour for nearly three weary days he had jogged up and down,
12 * over passes, and through long dales, and across many streams.
13 */
14
166f8a29
DM
15/* This file contains the functions needed to implement PerlIO, which
16 * is Perl's private replacement for the C stdio library. This is used
17 * by default unless you compile with -Uuseperlio or run with
18 * PERLIO=:stdio (but don't do this unless you know what you're doing)
19 */
20
d31a8517 21/*
71200d45 22 * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get
14a5cf38 23 * at the dispatch tables, even when we do not need it for other reasons.
71200d45 24 * Invent a dSYS macro to abstract this out
14a5cf38 25 */
7bcba3d4
NIS
26#ifdef PERL_IMPLICIT_SYS
27#define dSYS dTHX
28#else
29#define dSYS dNOOP
30#endif
31
760ac839 32#define VOIDUSED 1
12ae5dfc
JH
33#ifdef PERL_MICRO
34# include "uconfig.h"
35#else
b0f06652
VK
36# ifndef USE_CROSS_COMPILE
37# include "config.h"
38# else
39# include "xconfig.h"
40# endif
12ae5dfc 41#endif
760ac839 42
6f9d8c32 43#define PERLIO_NOT_STDIO 0
760ac839 44#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
14a5cf38 45/*
71200d45 46 * #define PerlIO FILE
14a5cf38 47 */
760ac839
LW
48#endif
49/*
6f9d8c32 50 * This file provides those parts of PerlIO abstraction
88b61e10 51 * which are not #defined in perlio.h.
6f9d8c32 52 * Which these are depends on various Configure #ifdef's
760ac839
LW
53 */
54
55#include "EXTERN.h"
864dbfa3 56#define PERL_IN_PERLIO_C
760ac839
LW
57#include "perl.h"
58
32af7c23
CL
59#ifdef PERL_IMPLICIT_CONTEXT
60#undef dSYS
61#define dSYS dTHX
62#endif
63
0c4f7ff0
NIS
64#include "XSUB.h"
65
9cffb111
OS
66#ifdef __Lynx__
67/* Missing proto on LynxOS */
68int mkstemp(char*);
69#endif
70
1b7a0411 71/* Call the callback or PerlIOBase, and return failure. */
b32dd47e 72#define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
1b7a0411 73 if (PerlIOValid(f)) { \
46c461b5 74 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
75 if (tab && tab->callback) \
76 return (*tab->callback) args; \
77 else \
78 return PerlIOBase_ ## base args; \
79 } \
80 else \
81 SETERRNO(EBADF, SS_IVCHAN); \
82 return failure
83
84/* Call the callback or fail, and return failure. */
b32dd47e 85#define Perl_PerlIO_or_fail(f, callback, failure, args) \
1b7a0411 86 if (PerlIOValid(f)) { \
46c461b5 87 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
88 if (tab && tab->callback) \
89 return (*tab->callback) args; \
90 SETERRNO(EINVAL, LIB_INVARG); \
91 } \
92 else \
93 SETERRNO(EBADF, SS_IVCHAN); \
94 return failure
95
96/* Call the callback or PerlIOBase, and be void. */
b32dd47e 97#define Perl_PerlIO_or_Base_void(f, callback, base, args) \
1b7a0411 98 if (PerlIOValid(f)) { \
46c461b5 99 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
100 if (tab && tab->callback) \
101 (*tab->callback) args; \
102 else \
103 PerlIOBase_ ## base args; \
1b7a0411
JH
104 } \
105 else \
106 SETERRNO(EBADF, SS_IVCHAN)
107
108/* Call the callback or fail, and be void. */
b32dd47e 109#define Perl_PerlIO_or_fail_void(f, callback, args) \
1b7a0411 110 if (PerlIOValid(f)) { \
46c461b5 111 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
112 if (tab && tab->callback) \
113 (*tab->callback) args; \
37725cdc
NIS
114 else \
115 SETERRNO(EINVAL, LIB_INVARG); \
1b7a0411
JH
116 } \
117 else \
118 SETERRNO(EBADF, SS_IVCHAN)
119
89a3a251
JH
120#if defined(__osf__) && _XOPEN_SOURCE < 500
121extern int fseeko(FILE *, off_t, int);
122extern off_t ftello(FILE *);
123#endif
124
71ab4674 125#ifndef USE_SFIO
a0c21aa1
JH
126
127EXTERN_C int perlsio_binmode(FILE *fp, int iotype, int mode);
128
71ab4674
SP
129int
130perlsio_binmode(FILE *fp, int iotype, int mode)
131{
132 /*
133 * This used to be contents of do_binmode in doio.c
134 */
135#ifdef DOSISH
136# if defined(atarist) || defined(__MINT__)
58c0efa5 137 PERL_UNUSED_ARG(iotype);
71ab4674
SP
138 if (!fflush(fp)) {
139 if (mode & O_BINARY)
140 ((FILE *) fp)->_flag |= _IOBIN;
141 else
142 ((FILE *) fp)->_flag &= ~_IOBIN;
143 return 1;
144 }
145 return 0;
146# else
147 dTHX;
58c0efa5 148 PERL_UNUSED_ARG(iotype);
71ab4674
SP
149#ifdef NETWARE
150 if (PerlLIO_setmode(fp, mode) != -1) {
151#else
152 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
153#endif
154# if defined(WIN32) && defined(__BORLANDC__)
155 /*
156 * The translation mode of the stream is maintained independent
157of
158 * the translation mode of the fd in the Borland RTL (heavy
159 * digging through their runtime sources reveal). User has to
160set
161 * the mode explicitly for the stream (though they don't
162document
163 * this anywhere). GSAR 97-5-24
164 */
165 fseek(fp, 0L, 0);
166 if (mode & O_BINARY)
167 fp->flags |= _F_BIN;
168 else
169 fp->flags &= ~_F_BIN;
170# endif
171 return 1;
172 }
173 else
174 return 0;
175# endif
176#else
177# if defined(USEMYBINMODE)
178 dTHX;
58c0efa5
RGS
179# if defined(__CYGWIN__)
180 PERL_UNUSED_ARG(iotype);
181# endif
71ab4674
SP
182 if (my_binmode(fp, iotype, mode) != FALSE)
183 return 1;
184 else
185 return 0;
186# else
187 PERL_UNUSED_ARG(fp);
188 PERL_UNUSED_ARG(iotype);
189 PERL_UNUSED_ARG(mode);
190 return 1;
191# endif
192#endif
193}
194#endif /* sfio */
195
06c7082d 196#ifndef O_ACCMODE
22569500 197#define O_ACCMODE 3 /* Assume traditional implementation */
06c7082d
NIS
198#endif
199
200int
201PerlIO_intmode2str(int rawmode, char *mode, int *writing)
202{
de009b76 203 const int result = rawmode & O_ACCMODE;
06c7082d
NIS
204 int ix = 0;
205 int ptype;
206 switch (result) {
207 case O_RDONLY:
208 ptype = IoTYPE_RDONLY;
209 break;
210 case O_WRONLY:
211 ptype = IoTYPE_WRONLY;
212 break;
213 case O_RDWR:
214 default:
215 ptype = IoTYPE_RDWR;
216 break;
217 }
218 if (writing)
219 *writing = (result != O_RDONLY);
220
221 if (result == O_RDONLY) {
222 mode[ix++] = 'r';
223 }
224#ifdef O_APPEND
225 else if (rawmode & O_APPEND) {
226 mode[ix++] = 'a';
227 if (result != O_WRONLY)
228 mode[ix++] = '+';
229 }
230#endif
231 else {
232 if (result == O_WRONLY)
233 mode[ix++] = 'w';
234 else {
235 mode[ix++] = 'r';
236 mode[ix++] = '+';
237 }
238 }
239 if (rawmode & O_BINARY)
240 mode[ix++] = 'b';
241 mode[ix] = '\0';
242 return ptype;
243}
244
eb73beca
NIS
245#ifndef PERLIO_LAYERS
246int
247PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
248{
6874a2de
NIS
249 if (!names || !*names
250 || strEQ(names, ":crlf")
251 || strEQ(names, ":raw")
252 || strEQ(names, ":bytes")
253 ) {
14a5cf38
JH
254 return 0;
255 }
256 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
257 /*
71200d45 258 * NOTREACHED
14a5cf38
JH
259 */
260 return -1;
eb73beca
NIS
261}
262
13621cfb
NIS
263void
264PerlIO_destruct(pTHX)
265{
266}
267
f5b9d040
NIS
268int
269PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
270{
92bff44d 271#ifdef USE_SFIO
8772537c
AL
272 PERL_UNUSED_ARG(iotype);
273 PERL_UNUSED_ARG(mode);
274 PERL_UNUSED_ARG(names);
14a5cf38 275 return 1;
92bff44d 276#else
14a5cf38 277 return perlsio_binmode(fp, iotype, mode);
92bff44d 278#endif
f5b9d040 279}
60382766 280
e0fa5af2 281PerlIO *
ecdeb87c 282PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
e0fa5af2 283{
a0fd4948 284#if defined(PERL_MICRO) || defined(__SYMBIAN32__)
0553478e
NIS
285 return NULL;
286#else
287#ifdef PERL_IMPLICIT_SYS
22569500 288 return PerlSIO_fdupopen(f);
0553478e 289#else
30753f56
NIS
290#ifdef WIN32
291 return win32_fdupopen(f);
292#else
e0fa5af2 293 if (f) {
504618e9 294 const int fd = PerlLIO_dup(PerlIO_fileno(f));
e0fa5af2 295 if (fd >= 0) {
06c7082d 296 char mode[8];
a5936e02 297#ifdef DJGPP
dcda55fc
AL
298 const int omode = djgpp_get_stream_mode(f);
299#else
300 const int omode = fcntl(fd, F_GETFL);
a5936e02 301#endif
06c7082d 302 PerlIO_intmode2str(omode,mode,NULL);
e0fa5af2 303 /* the r+ is a hack */
06c7082d 304 return PerlIO_fdopen(fd, mode);
e0fa5af2
NIS
305 }
306 return NULL;
307 }
308 else {
93189314 309 SETERRNO(EBADF, SS_IVCHAN);
e0fa5af2 310 }
7114a2d2 311#endif
e0fa5af2 312 return NULL;
0553478e 313#endif
30753f56 314#endif
e0fa5af2
NIS
315}
316
317
14a5cf38 318/*
71200d45 319 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
14a5cf38 320 */
ee518936
NIS
321
322PerlIO *
14a5cf38
JH
323PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
324 int imode, int perm, PerlIO *old, int narg, SV **args)
325{
7cf31beb
NIS
326 if (narg) {
327 if (narg > 1) {
3b8752bb 328 Perl_croak(aTHX_ "More than one argument to open");
7cf31beb 329 }
14a5cf38
JH
330 if (*args == &PL_sv_undef)
331 return PerlIO_tmpfile();
332 else {
e62f0680 333 const char *name = SvPV_nolen_const(*args);
3b6c1aba 334 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
335 fd = PerlLIO_open3(name, imode, perm);
336 if (fd >= 0)
de009b76 337 return PerlIO_fdopen(fd, mode + 1);
14a5cf38
JH
338 }
339 else if (old) {
340 return PerlIO_reopen(name, mode, old);
341 }
342 else {
343 return PerlIO_open(name, mode);
344 }
345 }
346 }
347 else {
348 return PerlIO_fdopen(fd, (char *) mode);
349 }
350 return NULL;
ee518936
NIS
351}
352
0c4f7ff0
NIS
353XS(XS_PerlIO__Layer__find)
354{
14a5cf38
JH
355 dXSARGS;
356 if (items < 2)
357 Perl_croak(aTHX_ "Usage class->find(name[,load])");
358 else {
dcda55fc 359 const char * const name = SvPV_nolen_const(ST(1));
14a5cf38
JH
360 ST(0) = (strEQ(name, "crlf")
361 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
362 XSRETURN(1);
363 }
0c4f7ff0
NIS
364}
365
366
367void
368Perl_boot_core_PerlIO(pTHX)
369{
14a5cf38 370 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
0c4f7ff0
NIS
371}
372
ac27b0f5
NIS
373#endif
374
32e30700 375
6f9d8c32 376#ifdef PERLIO_IS_STDIO
760ac839
LW
377
378void
e8632036 379PerlIO_init(pTHX)
760ac839 380{
96a5add6 381 PERL_UNUSED_CONTEXT;
14a5cf38
JH
382 /*
383 * Does nothing (yet) except force this file to be included in perl
71200d45 384 * binary. That allows this file to force inclusion of other functions
14a5cf38 385 * that may be required by loadable extensions e.g. for
71200d45 386 * FileHandle::tmpfile
14a5cf38 387 */
760ac839
LW
388}
389
33dcbb9a 390#undef PerlIO_tmpfile
391PerlIO *
8ac85365 392PerlIO_tmpfile(void)
33dcbb9a 393{
14a5cf38 394 return tmpfile();
33dcbb9a 395}
396
22569500 397#else /* PERLIO_IS_STDIO */
760ac839
LW
398
399#ifdef USE_SFIO
400
401#undef HAS_FSETPOS
402#undef HAS_FGETPOS
403
14a5cf38
JH
404/*
405 * This section is just to make sure these functions get pulled in from
71200d45 406 * libsfio.a
14a5cf38 407 */
760ac839
LW
408
409#undef PerlIO_tmpfile
410PerlIO *
c78749f2 411PerlIO_tmpfile(void)
760ac839 412{
14a5cf38 413 return sftmp(0);
760ac839
LW
414}
415
416void
e8632036 417PerlIO_init(pTHX)
760ac839 418{
96a5add6 419 PERL_UNUSED_CONTEXT;
14a5cf38
JH
420 /*
421 * Force this file to be included in perl binary. Which allows this
422 * file to force inclusion of other functions that may be required by
71200d45 423 * loadable extensions e.g. for FileHandle::tmpfile
14a5cf38 424 */
760ac839 425
14a5cf38 426 /*
71200d45 427 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
14a5cf38 428 * results in a lot of lseek()s to regular files and lot of small
71200d45 429 * writes to pipes.
14a5cf38
JH
430 */
431 sfset(sfstdout, SF_SHARE, 0);
760ac839
LW
432}
433
b9d6bf13 434/* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
92bff44d 435PerlIO *
4b069b44 436PerlIO_importFILE(FILE *stdio, const char *mode)
92bff44d 437{
de009b76 438 const int fd = fileno(stdio);
4b069b44 439 if (!mode || !*mode) {
81428673 440 mode = "r+";
4b069b44
NIS
441 }
442 return PerlIO_fdopen(fd, mode);
92bff44d
NIS
443}
444
445FILE *
446PerlIO_findFILE(PerlIO *pio)
447{
de009b76
AL
448 const int fd = PerlIO_fileno(pio);
449 FILE * const f = fdopen(fd, "r+");
14a5cf38
JH
450 PerlIO_flush(pio);
451 if (!f && errno == EINVAL)
452 f = fdopen(fd, "w");
453 if (!f && errno == EINVAL)
454 f = fdopen(fd, "r");
455 return f;
92bff44d
NIS
456}
457
458
22569500 459#else /* USE_SFIO */
6f9d8c32 460/*======================================================================================*/
14a5cf38 461/*
71200d45 462 * Implement all the PerlIO interface ourselves.
9e353e3b 463 */
760ac839 464
76ced9ad
NIS
465#include "perliol.h"
466
14a5cf38
JH
467/*
468 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
71200d45 469 * files
14a5cf38 470 */
02f66e2f
NIS
471#ifdef I_UNISTD
472#include <unistd.h>
473#endif
06da4f11
NIS
474#ifdef HAS_MMAP
475#include <sys/mman.h>
476#endif
477
6f9d8c32 478void
14a5cf38
JH
479PerlIO_debug(const char *fmt, ...)
480{
14a5cf38
JH
481 va_list ap;
482 dSYS;
483 va_start(ap, fmt);
582588d2
NC
484 if (!PL_perlio_debug_fd) {
485 if (!PL_tainting && PL_uid == PL_euid && PL_gid == PL_egid) {
486 const char * const s = PerlEnv_getenv("PERLIO_DEBUG");
487 if (s && *s)
488 PL_perlio_debug_fd
489 = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666);
490 else
491 PL_perlio_debug_fd = -1;
492 } else {
493 /* tainting or set*id, so ignore the environment, and ensure we
494 skip these tests next time through. */
27da23d5 495 PL_perlio_debug_fd = -1;
582588d2 496 }
14a5cf38 497 }
27da23d5 498 if (PL_perlio_debug_fd > 0) {
14a5cf38 499 dTHX;
70ace5da 500#ifdef USE_ITHREADS
dcda55fc 501 const char * const s = CopFILE(PL_curcop);
70ace5da
NIS
502 /* Use fixed buffer as sv_catpvf etc. needs SVs */
503 char buffer[1024];
1208b3dd
JH
504 const STRLEN len1 = my_snprintf(buffer, sizeof(buffer), "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop));
505 const STRLEN len2 = my_vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap);
506 PerlLIO_write(PL_perlio_debug_fd, buffer, len1 + len2);
70ace5da 507#else
dcda55fc
AL
508 const char *s = CopFILE(PL_curcop);
509 STRLEN len;
550e2ce0
NC
510 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%" IVdf " ", s ? s : "(none)",
511 (IV) CopLINE(PL_curcop));
14a5cf38
JH
512 Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap);
513
b83604b4 514 s = SvPV_const(sv, len);
27da23d5 515 PerlLIO_write(PL_perlio_debug_fd, s, len);
14a5cf38 516 SvREFCNT_dec(sv);
70ace5da 517#endif
14a5cf38
JH
518 }
519 va_end(ap);
6f9d8c32
NIS
520}
521
9e353e3b
NIS
522/*--------------------------------------------------------------------------------------*/
523
14a5cf38 524/*
71200d45 525 * Inner level routines
14a5cf38 526 */
9e353e3b 527
14a5cf38 528/*
71200d45 529 * Table of pointers to the PerlIO structs (malloc'ed)
14a5cf38 530 */
05d1247b 531#define PERLIO_TABLE_SIZE 64
6f9d8c32 532
760ac839 533PerlIO *
5f1a76d0 534PerlIO_allocate(pTHX)
6f9d8c32 535{
97aff369 536 dVAR;
14a5cf38 537 /*
71200d45 538 * Find a free slot in the table, allocating new table as necessary
14a5cf38
JH
539 */
540 PerlIO **last;
541 PerlIO *f;
a1ea730d 542 last = &PL_perlio;
14a5cf38
JH
543 while ((f = *last)) {
544 int i;
545 last = (PerlIO **) (f);
546 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
547 if (!*++f) {
548 return f;
549 }
550 }
551 }
a02a5408 552 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
14a5cf38
JH
553 if (!f) {
554 return NULL;
555 }
556 *last = f;
557 return f + 1;
05d1247b
NIS
558}
559
a1ea730d
NIS
560#undef PerlIO_fdupopen
561PerlIO *
ecdeb87c 562PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
a1ea730d 563{
04892f78 564 if (PerlIOValid(f)) {
de009b76 565 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
fe5a182c 566 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
210e727c
JH
567 if (tab && tab->Dup)
568 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
37725cdc
NIS
569 else {
570 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
571 }
a1ea730d 572 }
210e727c
JH
573 else
574 SETERRNO(EBADF, SS_IVCHAN);
575
576 return NULL;
a1ea730d
NIS
577}
578
579void
5f1a76d0 580PerlIO_cleantable(pTHX_ PerlIO **tablep)
05d1247b 581{
dcda55fc 582 PerlIO * const table = *tablep;
14a5cf38
JH
583 if (table) {
584 int i;
585 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
586 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
dcda55fc 587 PerlIO * const f = table + i;
14a5cf38
JH
588 if (*f) {
589 PerlIO_close(f);
590 }
591 }
3a1ee7e8 592 Safefree(table);
14a5cf38 593 *tablep = NULL;
05d1247b 594 }
05d1247b
NIS
595}
596
fcf2db38
NIS
597
598PerlIO_list_t *
3a1ee7e8 599PerlIO_list_alloc(pTHX)
fcf2db38 600{
14a5cf38 601 PerlIO_list_t *list;
96a5add6 602 PERL_UNUSED_CONTEXT;
a02a5408 603 Newxz(list, 1, PerlIO_list_t);
14a5cf38
JH
604 list->refcnt = 1;
605 return list;
fcf2db38
NIS
606}
607
608void
3a1ee7e8 609PerlIO_list_free(pTHX_ PerlIO_list_t *list)
fcf2db38 610{
14a5cf38
JH
611 if (list) {
612 if (--list->refcnt == 0) {
613 if (list->array) {
14a5cf38
JH
614 IV i;
615 for (i = 0; i < list->cur; i++) {
616 if (list->array[i].arg)
617 SvREFCNT_dec(list->array[i].arg);
618 }
619 Safefree(list->array);
620 }
621 Safefree(list);
622 }
623 }
fcf2db38
NIS
624}
625
626void
3a1ee7e8 627PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
14a5cf38 628{
97aff369 629 dVAR;
334e202e 630 PerlIO_pair_t *p;
b37c2d43
AL
631 PERL_UNUSED_CONTEXT;
632
14a5cf38
JH
633 if (list->cur >= list->len) {
634 list->len += 8;
635 if (list->array)
636 Renew(list->array, list->len, PerlIO_pair_t);
637 else
a02a5408 638 Newx(list->array, list->len, PerlIO_pair_t);
14a5cf38
JH
639 }
640 p = &(list->array[list->cur++]);
641 p->funcs = funcs;
642 if ((p->arg = arg)) {
f84c484e 643 SvREFCNT_inc_simple_void_NN(arg);
14a5cf38 644 }
fcf2db38
NIS
645}
646
3a1ee7e8
NIS
647PerlIO_list_t *
648PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
649{
b37c2d43 650 PerlIO_list_t *list = NULL;
694c95cf
JH
651 if (proto) {
652 int i;
653 list = PerlIO_list_alloc(aTHX);
654 for (i=0; i < proto->cur; i++) {
a951d81d
BL
655 SV *arg = proto->array[i].arg;
656#ifdef sv_dup
657 if (arg && param)
658 arg = sv_dup(arg, param);
659#else
660 PERL_UNUSED_ARG(param);
661#endif
694c95cf
JH
662 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
663 }
3a1ee7e8
NIS
664 }
665 return list;
666}
4a4a6116 667
05d1247b 668void
3a1ee7e8 669PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
9a6404c5 670{
3aaf42a7 671#ifdef USE_ITHREADS
3a1ee7e8
NIS
672 PerlIO **table = &proto->Iperlio;
673 PerlIO *f;
674 PL_perlio = NULL;
675 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
676 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
677 PerlIO_allocate(aTHX); /* root slot is never used */
a25429c6 678 PerlIO_debug("Clone %p from %p\n",(void*)aTHX,(void*)proto);
3a1ee7e8
NIS
679 while ((f = *table)) {
680 int i;
681 table = (PerlIO **) (f++);
682 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
683 if (*f) {
93a8090d 684 (void) fp_dup(f, 0, param);
3a1ee7e8
NIS
685 }
686 f++;
687 }
688 }
1b6737cc 689#else
a25429c6 690 PERL_UNUSED_CONTEXT;
1b6737cc
AL
691 PERL_UNUSED_ARG(proto);
692 PERL_UNUSED_ARG(param);
3aaf42a7 693#endif
9a6404c5
DM
694}
695
696void
13621cfb
NIS
697PerlIO_destruct(pTHX)
698{
97aff369 699 dVAR;
a1ea730d 700 PerlIO **table = &PL_perlio;
14a5cf38 701 PerlIO *f;
694c95cf 702#ifdef USE_ITHREADS
a25429c6 703 PerlIO_debug("Destruct %p\n",(void*)aTHX);
694c95cf 704#endif
14a5cf38
JH
705 while ((f = *table)) {
706 int i;
707 table = (PerlIO **) (f++);
708 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
709 PerlIO *x = f;
dcda55fc 710 const PerlIOl *l;
14a5cf38
JH
711 while ((l = *x)) {
712 if (l->tab->kind & PERLIO_K_DESTRUCT) {
713 PerlIO_debug("Destruct popping %s\n", l->tab->name);
714 PerlIO_flush(x);
715 PerlIO_pop(aTHX_ x);
716 }
717 else {
718 x = PerlIONext(x);
719 }
720 }
721 f++;
722 }
723 }
13621cfb
NIS
724}
725
726void
a999f61b 727PerlIO_pop(pTHX_ PerlIO *f)
760ac839 728{
dcda55fc 729 const PerlIOl *l = *f;
14a5cf38 730 if (l) {
fe5a182c 731 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
14a5cf38
JH
732 if (l->tab->Popped) {
733 /*
734 * If popped returns non-zero do not free its layer structure
735 * it has either done so itself, or it is shared and still in
71200d45 736 * use
14a5cf38 737 */
f62ce20a 738 if ((*l->tab->Popped) (aTHX_ f) != 0)
14a5cf38
JH
739 return;
740 }
b47cad08 741 *f = l->next;
3a1ee7e8 742 Safefree(l);
a8c08ecd 743 }
6f9d8c32
NIS
744}
745
39f7a870
JH
746/* Return as an array the stack of layers on a filehandle. Note that
747 * the stack is returned top-first in the array, and there are three
748 * times as many array elements as there are layers in the stack: the
749 * first element of a layer triplet is the name, the second one is the
750 * arguments, and the third one is the flags. */
751
752AV *
753PerlIO_get_layers(pTHX_ PerlIO *f)
754{
97aff369 755 dVAR;
dcda55fc 756 AV * const av = newAV();
39f7a870 757
dcda55fc
AL
758 if (PerlIOValid(f)) {
759 PerlIOl *l = PerlIOBase(f);
760
761 while (l) {
92e45a3e
NC
762 /* There is some collusion in the implementation of
763 XS_PerlIO_get_layers - it knows that name and flags are
764 generated as fresh SVs here, and takes advantage of that to
765 "copy" them by taking a reference. If it changes here, it needs
766 to change there too. */
dcda55fc
AL
767 SV * const name = l->tab && l->tab->name ?
768 newSVpv(l->tab->name, 0) : &PL_sv_undef;
769 SV * const arg = l->tab && l->tab->Getarg ?
770 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
771 av_push(av, name);
772 av_push(av, arg);
773 av_push(av, newSViv((IV)l->flags));
774 l = l->next;
775 }
776 }
39f7a870 777
dcda55fc 778 return av;
39f7a870
JH
779}
780
9e353e3b 781/*--------------------------------------------------------------------------------------*/
14a5cf38 782/*
71200d45 783 * XS Interface for perl code
14a5cf38 784 */
9e353e3b 785
fcf2db38 786PerlIO_funcs *
2edd7e44 787PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
f3862f8b 788{
27da23d5 789 dVAR;
14a5cf38
JH
790 IV i;
791 if ((SSize_t) len <= 0)
792 len = strlen(name);
3a1ee7e8 793 for (i = 0; i < PL_known_layers->cur; i++) {
46c461b5 794 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
a9f76400 795 if (memEQ(f->name, name, len) && f->name[len] == 0) {
fe5a182c 796 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
14a5cf38
JH
797 return f;
798 }
799 }
3a1ee7e8
NIS
800 if (load && PL_subname && PL_def_layerlist
801 && PL_def_layerlist->cur >= 2) {
d7a09b41
SR
802 if (PL_in_load_module) {
803 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
804 return NULL;
805 } else {
396482e1 806 SV * const pkgsv = newSVpvs("PerlIO");
46c461b5 807 SV * const layer = newSVpvn(name, len);
780a5241 808 CV * const cv = Perl_get_cvn_flags(aTHX_ STR_WITH_LEN("PerlIO::Layer::NoWarnings"), 0);
46c461b5 809 ENTER;
d7a09b41 810 SAVEINT(PL_in_load_module);
c9bca74a 811 if (cv) {
9cfa90c0 812 SAVEGENERICSV(PL_warnhook);
b37c2d43 813 PL_warnhook = (SV *) (SvREFCNT_inc_simple_NN(cv));
c9bca74a 814 }
d7a09b41
SR
815 PL_in_load_module++;
816 /*
817 * The two SVs are magically freed by load_module
818 */
a0714e2c 819 Perl_load_module(aTHX_ 0, pkgsv, NULL, layer, NULL);
d7a09b41
SR
820 PL_in_load_module--;
821 LEAVE;
822 return PerlIO_find_layer(aTHX_ name, len, 0);
823 }
14a5cf38
JH
824 }
825 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
826 return NULL;
f3862f8b
NIS
827}
828
2a1bc955 829#ifdef USE_ATTRIBUTES_FOR_PERLIO
b13b2135
NIS
830
831static int
832perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
833{
14a5cf38 834 if (SvROK(sv)) {
dcda55fc
AL
835 IO * const io = GvIOn((GV *) SvRV(sv));
836 PerlIO * const ifp = IoIFP(io);
837 PerlIO * const ofp = IoOFP(io);
be2597df
MHM
838 Perl_warn(aTHX_ "set %" SVf " %p %p %p",
839 SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
14a5cf38
JH
840 }
841 return 0;
b13b2135
NIS
842}
843
844static int
845perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
846{
14a5cf38 847 if (SvROK(sv)) {
dcda55fc
AL
848 IO * const io = GvIOn((GV *) SvRV(sv));
849 PerlIO * const ifp = IoIFP(io);
850 PerlIO * const ofp = IoOFP(io);
be2597df
MHM
851 Perl_warn(aTHX_ "get %" SVf " %p %p %p",
852 SVfARG(sv), (void*)io, (void*)ifp, (void*)ofp);
14a5cf38
JH
853 }
854 return 0;
b13b2135
NIS
855}
856
857static int
858perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
859{
be2597df 860 Perl_warn(aTHX_ "clear %" SVf, SVfARG(sv));
14a5cf38 861 return 0;
b13b2135
NIS
862}
863
864static int
865perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
866{
be2597df 867 Perl_warn(aTHX_ "free %" SVf, SVfARG(sv));
14a5cf38 868 return 0;
b13b2135
NIS
869}
870
871MGVTBL perlio_vtab = {
14a5cf38
JH
872 perlio_mg_get,
873 perlio_mg_set,
22569500 874 NULL, /* len */
14a5cf38
JH
875 perlio_mg_clear,
876 perlio_mg_free
b13b2135
NIS
877};
878
879XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
880{
14a5cf38 881 dXSARGS;
dcda55fc
AL
882 SV * const sv = SvRV(ST(1));
883 AV * const av = newAV();
14a5cf38
JH
884 MAGIC *mg;
885 int count = 0;
886 int i;
887 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
888 SvRMAGICAL_off(sv);
889 mg = mg_find(sv, PERL_MAGIC_ext);
890 mg->mg_virtual = &perlio_vtab;
891 mg_magical(sv);
be2597df 892 Perl_warn(aTHX_ "attrib %" SVf, SVfARG(sv));
14a5cf38
JH
893 for (i = 2; i < items; i++) {
894 STRLEN len;
dcda55fc
AL
895 const char * const name = SvPV_const(ST(i), len);
896 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
14a5cf38 897 if (layer) {
b37c2d43 898 av_push(av, SvREFCNT_inc_simple_NN(layer));
14a5cf38
JH
899 }
900 else {
901 ST(count) = ST(i);
902 count++;
903 }
904 }
905 SvREFCNT_dec(av);
906 XSRETURN(count);
907}
908
22569500 909#endif /* USE_ATTIBUTES_FOR_PERLIO */
2a1bc955 910
e3f3bf95
NIS
911SV *
912PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
f3862f8b 913{
da51bb9b 914 HV * const stash = gv_stashpvs("PerlIO::Layer", GV_ADD);
46c461b5 915 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
14a5cf38 916 return sv;
e3f3bf95
NIS
917}
918
5ca1d77f 919XS(XS_PerlIO__Layer__NoWarnings)
c9bca74a 920{
37725cdc 921 /* This is used as a %SIG{__WARN__} handler to supress warnings
c9bca74a
NIS
922 during loading of layers.
923 */
97aff369 924 dVAR;
c9bca74a 925 dXSARGS;
58c0efa5 926 PERL_UNUSED_ARG(cv);
c9bca74a 927 if (items)
e62f0680 928 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
c9bca74a
NIS
929 XSRETURN(0);
930}
931
5ca1d77f 932XS(XS_PerlIO__Layer__find)
0c4f7ff0 933{
97aff369 934 dVAR;
14a5cf38 935 dXSARGS;
58c0efa5 936 PERL_UNUSED_ARG(cv);
14a5cf38
JH
937 if (items < 2)
938 Perl_croak(aTHX_ "Usage class->find(name[,load])");
939 else {
de009b76 940 STRLEN len;
46c461b5 941 const char * const name = SvPV_const(ST(1), len);
de009b76 942 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
46c461b5 943 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
14a5cf38
JH
944 ST(0) =
945 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
946 &PL_sv_undef;
947 XSRETURN(1);
948 }
0c4f7ff0
NIS
949}
950
e3f3bf95
NIS
951void
952PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
953{
97aff369 954 dVAR;
3a1ee7e8
NIS
955 if (!PL_known_layers)
956 PL_known_layers = PerlIO_list_alloc(aTHX);
a0714e2c 957 PerlIO_list_push(aTHX_ PL_known_layers, tab, NULL);
fe5a182c 958 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
f3862f8b
NIS
959}
960
1141d9f8 961int
fcf2db38 962PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
1141d9f8 963{
97aff369 964 dVAR;
14a5cf38
JH
965 if (names) {
966 const char *s = names;
967 while (*s) {
968 while (isSPACE(*s) || *s == ':')
969 s++;
970 if (*s) {
971 STRLEN llen = 0;
972 const char *e = s;
bd61b366 973 const char *as = NULL;
14a5cf38
JH
974 STRLEN alen = 0;
975 if (!isIDFIRST(*s)) {
976 /*
977 * Message is consistent with how attribute lists are
978 * passed. Even though this means "foo : : bar" is
71200d45 979 * seen as an invalid separator character.
14a5cf38 980 */
de009b76 981 const char q = ((*s == '\'') ? '"' : '\'');
99ef548b 982 if (ckWARN(WARN_LAYER))
22569500 983 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 984 "Invalid separator character %c%c%c in PerlIO layer specification %s",
1e616cf5 985 q, *s, q, s);
93189314 986 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
987 return -1;
988 }
989 do {
990 e++;
991 } while (isALNUM(*e));
992 llen = e - s;
993 if (*e == '(') {
994 int nesting = 1;
995 as = ++e;
996 while (nesting) {
997 switch (*e++) {
998 case ')':
999 if (--nesting == 0)
1000 alen = (e - 1) - as;
1001 break;
1002 case '(':
1003 ++nesting;
1004 break;
1005 case '\\':
1006 /*
1007 * It's a nul terminated string, not allowed
1008 * to \ the terminating null. Anything other
71200d45 1009 * character is passed over.
14a5cf38
JH
1010 */
1011 if (*e++) {
1012 break;
1013 }
1014 /*
71200d45 1015 * Drop through
14a5cf38
JH
1016 */
1017 case '\0':
1018 e--;
22569500
NIS
1019 if (ckWARN(WARN_LAYER))
1020 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 1021 "Argument list not closed for PerlIO layer \"%.*s\"",
14a5cf38
JH
1022 (int) (e - s), s);
1023 return -1;
1024 default:
1025 /*
71200d45 1026 * boring.
14a5cf38
JH
1027 */
1028 break;
1029 }
1030 }
1031 }
1032 if (e > s) {
46c461b5 1033 PerlIO_funcs * const layer =
14a5cf38
JH
1034 PerlIO_find_layer(aTHX_ s, llen, 1);
1035 if (layer) {
a951d81d
BL
1036 SV *arg = NULL;
1037 if (as)
1038 arg = newSVpvn(as, alen);
3a1ee7e8 1039 PerlIO_list_push(aTHX_ av, layer,
a951d81d
BL
1040 (arg) ? arg : &PL_sv_undef);
1041 if (arg)
1042 SvREFCNT_dec(arg);
14a5cf38
JH
1043 }
1044 else {
041457d9 1045 if (ckWARN(WARN_LAYER))
b4581f09 1046 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
14a5cf38
JH
1047 (int) llen, s);
1048 return -1;
1049 }
1050 }
1051 s = e;
1052 }
1053 }
1054 }
1055 return 0;
1141d9f8
NIS
1056}
1057
dfebf958 1058void
fcf2db38 1059PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
dfebf958 1060{
97aff369 1061 dVAR;
27da23d5 1062 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
35990314 1063#ifdef PERLIO_USING_CRLF
6ce75a77 1064 tab = &PerlIO_crlf;
846be114 1065#else
6ce75a77 1066 if (PerlIO_stdio.Set_ptrcnt)
22569500 1067 tab = &PerlIO_stdio;
846be114 1068#endif
14a5cf38 1069 PerlIO_debug("Pushing %s\n", tab->name);
3a1ee7e8 1070 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
14a5cf38 1071 &PL_sv_undef);
dfebf958
NIS
1072}
1073
e3f3bf95 1074SV *
14a5cf38 1075PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
e3f3bf95 1076{
14a5cf38 1077 return av->array[n].arg;
e3f3bf95
NIS
1078}
1079
f3862f8b 1080PerlIO_funcs *
14a5cf38 1081PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
f3862f8b 1082{
14a5cf38
JH
1083 if (n >= 0 && n < av->cur) {
1084 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1085 av->array[n].funcs->name);
1086 return av->array[n].funcs;
1087 }
1088 if (!def)
1089 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1090 return def;
e3f3bf95
NIS
1091}
1092
4ec2216f
NIS
1093IV
1094PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1095{
8772537c
AL
1096 PERL_UNUSED_ARG(mode);
1097 PERL_UNUSED_ARG(arg);
1098 PERL_UNUSED_ARG(tab);
4ec2216f
NIS
1099 if (PerlIOValid(f)) {
1100 PerlIO_flush(f);
1101 PerlIO_pop(aTHX_ f);
1102 return 0;
1103 }
1104 return -1;
1105}
1106
27da23d5 1107PERLIO_FUNCS_DECL(PerlIO_remove) = {
4ec2216f
NIS
1108 sizeof(PerlIO_funcs),
1109 "pop",
1110 0,
1111 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1112 PerlIOPop_pushed,
1113 NULL,
1114 NULL,
1115 NULL,
1116 NULL,
1117 NULL,
1118 NULL,
1119 NULL,
1120 NULL,
1121 NULL,
1122 NULL,
de009b76
AL
1123 NULL,
1124 NULL,
4ec2216f
NIS
1125 NULL, /* flush */
1126 NULL, /* fill */
1127 NULL,
1128 NULL,
1129 NULL,
1130 NULL,
1131 NULL, /* get_base */
1132 NULL, /* get_bufsiz */
1133 NULL, /* get_ptr */
1134 NULL, /* get_cnt */
1135 NULL, /* set_ptrcnt */
1136};
1137
fcf2db38 1138PerlIO_list_t *
e3f3bf95
NIS
1139PerlIO_default_layers(pTHX)
1140{
97aff369 1141 dVAR;
3a1ee7e8 1142 if (!PL_def_layerlist) {
bd61b366 1143 const char * const s = (PL_tainting) ? NULL : PerlEnv_getenv("PERLIO");
27da23d5 1144 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
3a1ee7e8 1145 PL_def_layerlist = PerlIO_list_alloc(aTHX);
27da23d5 1146 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
979e2c82 1147#if defined(WIN32)
27da23d5 1148 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
2f8118af 1149#if 0
14a5cf38 1150 osLayer = &PerlIO_win32;
0c4128ad 1151#endif
2f8118af 1152#endif
27da23d5
JH
1153 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1154 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1155 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1156 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
06da4f11 1157#ifdef HAS_MMAP
27da23d5 1158 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
06da4f11 1159#endif
27da23d5
JH
1160 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1161 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1162 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
3a1ee7e8 1163 PerlIO_list_push(aTHX_ PL_def_layerlist,
14a5cf38
JH
1164 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1165 &PL_sv_undef);
1166 if (s) {
3a1ee7e8 1167 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
14a5cf38
JH
1168 }
1169 else {
3a1ee7e8 1170 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
14a5cf38 1171 }
1141d9f8 1172 }
3a1ee7e8
NIS
1173 if (PL_def_layerlist->cur < 2) {
1174 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
f3862f8b 1175 }
3a1ee7e8 1176 return PL_def_layerlist;
e3f3bf95
NIS
1177}
1178
0c4f7ff0
NIS
1179void
1180Perl_boot_core_PerlIO(pTHX)
1181{
1182#ifdef USE_ATTRIBUTES_FOR_PERLIO
14a5cf38
JH
1183 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1184 __FILE__);
0c4f7ff0 1185#endif
14a5cf38 1186 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
c9bca74a 1187 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
0c4f7ff0 1188}
e3f3bf95
NIS
1189
1190PerlIO_funcs *
1191PerlIO_default_layer(pTHX_ I32 n)
1192{
97aff369 1193 dVAR;
46c461b5 1194 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
14a5cf38
JH
1195 if (n < 0)
1196 n += av->cur;
27da23d5 1197 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
f3862f8b
NIS
1198}
1199
a999f61b
NIS
1200#define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1201#define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
60382766
NIS
1202
1203void
1141d9f8 1204PerlIO_stdstreams(pTHX)
60382766 1205{
97aff369 1206 dVAR;
a1ea730d 1207 if (!PL_perlio) {
14a5cf38
JH
1208 PerlIO_allocate(aTHX);
1209 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1210 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1211 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1212 }
60382766
NIS
1213}
1214
1215PerlIO *
27da23d5 1216PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
14a5cf38 1217{
2dc2558e
NIS
1218 if (tab->fsize != sizeof(PerlIO_funcs)) {
1219 mismatch:
1220 Perl_croak(aTHX_ "Layer does not match this perl");
1221 }
1222 if (tab->size) {
b464bac0 1223 PerlIOl *l;
2dc2558e
NIS
1224 if (tab->size < sizeof(PerlIOl)) {
1225 goto mismatch;
1226 }
1227 /* Real layer with a data area */
002e75cf
JH
1228 if (f) {
1229 char *temp;
1230 Newxz(temp, tab->size, char);
1231 l = (PerlIOl*)temp;
1232 if (l) {
1233 l->next = *f;
1234 l->tab = (PerlIO_funcs*) tab;
1235 *f = l;
1236 PerlIO_debug("PerlIO_push f=%p %s %s %p\n",
1237 (void*)f, tab->name,
1238 (mode) ? mode : "(Null)", (void*)arg);
1239 if (*l->tab->Pushed &&
1240 (*l->tab->Pushed)
1241 (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
1242 PerlIO_pop(aTHX_ f);
1243 return NULL;
1244 }
2dc2558e 1245 }
002e75cf
JH
1246 else
1247 return NULL;
2dc2558e
NIS
1248 }
1249 }
1250 else if (f) {
1251 /* Pseudo-layer where push does its own stack adjust */
00f51856
NIS
1252 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1253 (mode) ? mode : "(Null)", (void*)arg);
210e727c 1254 if (tab->Pushed &&
27da23d5 1255 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
210e727c 1256 return NULL;
14a5cf38
JH
1257 }
1258 }
1259 return f;
60382766
NIS
1260}
1261
dfebf958 1262IV
86e05cf2
NIS
1263PerlIOBase_binmode(pTHX_ PerlIO *f)
1264{
1265 if (PerlIOValid(f)) {
1266 /* Is layer suitable for raw stream ? */
1267 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1268 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1269 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1270 }
1271 else {
1272 /* Not suitable - pop it */
1273 PerlIO_pop(aTHX_ f);
1274 }
1275 return 0;
1276 }
1277 return -1;
1278}
1279
1280IV
2dc2558e 1281PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
dfebf958 1282{
8772537c
AL
1283 PERL_UNUSED_ARG(mode);
1284 PERL_UNUSED_ARG(arg);
1285 PERL_UNUSED_ARG(tab);
86e05cf2 1286
04892f78 1287 if (PerlIOValid(f)) {
86e05cf2 1288 PerlIO *t;
de009b76 1289 const PerlIOl *l;
14a5cf38 1290 PerlIO_flush(f);
86e05cf2
NIS
1291 /*
1292 * Strip all layers that are not suitable for a raw stream
1293 */
1294 t = f;
1295 while (t && (l = *t)) {
1296 if (l->tab->Binmode) {
1297 /* Has a handler - normal case */
1298 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1299 if (*t == l) {
1300 /* Layer still there - move down a layer */
1301 t = PerlIONext(t);
1302 }
1303 }
1304 else {
1305 return -1;
1306 }
14a5cf38
JH
1307 }
1308 else {
86e05cf2
NIS
1309 /* No handler - pop it */
1310 PerlIO_pop(aTHX_ t);
14a5cf38
JH
1311 }
1312 }
86e05cf2
NIS
1313 if (PerlIOValid(f)) {
1314 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1315 return 0;
1316 }
14a5cf38
JH
1317 }
1318 return -1;
dfebf958
NIS
1319}
1320
ac27b0f5 1321int
14a5cf38 1322PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
d9dac8cd 1323 PerlIO_list_t *layers, IV n, IV max)
14a5cf38 1324{
14a5cf38
JH
1325 int code = 0;
1326 while (n < max) {
8772537c 1327 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
14a5cf38
JH
1328 if (tab) {
1329 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1330 code = -1;
1331 break;
1332 }
1333 }
1334 n++;
1335 }
1336 return code;
e3f3bf95
NIS
1337}
1338
1339int
ac27b0f5
NIS
1340PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1341{
14a5cf38 1342 int code = 0;
53f1b6d2 1343 if (f && names) {
8772537c 1344 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
14a5cf38
JH
1345 code = PerlIO_parse_layers(aTHX_ layers, names);
1346 if (code == 0) {
d9dac8cd 1347 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
14a5cf38 1348 }
3a1ee7e8 1349 PerlIO_list_free(aTHX_ layers);
ac27b0f5 1350 }
14a5cf38 1351 return code;
ac27b0f5
NIS
1352}
1353
f3862f8b 1354
60382766 1355/*--------------------------------------------------------------------------------------*/
14a5cf38 1356/*
71200d45 1357 * Given the abstraction above the public API functions
14a5cf38 1358 */
60382766
NIS
1359
1360int
f5b9d040 1361PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
76ced9ad 1362{
68b5363f
PD
1363 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1364 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1365 iotype, mode, (names) ? names : "(Null)");
1366
03c0554d
NIS
1367 if (names) {
1368 /* Do not flush etc. if (e.g.) switching encodings.
1369 if a pushed layer knows it needs to flush lower layers
1370 (for example :unix which is never going to call them)
1371 it can do the flush when it is pushed.
1372 */
1373 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1374 }
1375 else {
86e05cf2 1376 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
35990314 1377#ifdef PERLIO_USING_CRLF
03c0554d
NIS
1378 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1379 O_BINARY so we can look for it in mode.
1380 */
1381 if (!(mode & O_BINARY)) {
1382 /* Text mode */
86e05cf2
NIS
1383 /* FIXME?: Looking down the layer stack seems wrong,
1384 but is a way of reaching past (say) an encoding layer
1385 to flip CRLF-ness of the layer(s) below
1386 */
03c0554d
NIS
1387 while (*f) {
1388 /* Perhaps we should turn on bottom-most aware layer
1389 e.g. Ilya's idea that UNIX TTY could serve
1390 */
1391 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1392 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1393 /* Not in text mode - flush any pending stuff and flip it */
1394 PerlIO_flush(f);
1395 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1396 }
1397 /* Only need to turn it on in one layer so we are done */
1398 return TRUE;
ed53a2bb 1399 }
03c0554d 1400 f = PerlIONext(f);
14a5cf38 1401 }
03c0554d
NIS
1402 /* Not finding a CRLF aware layer presumably means we are binary
1403 which is not what was requested - so we failed
1404 We _could_ push :crlf layer but so could caller
1405 */
1406 return FALSE;
14a5cf38 1407 }
6ce75a77 1408#endif
86e05cf2
NIS
1409 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1410 So code that used to be here is now in PerlIORaw_pushed().
03c0554d 1411 */
a0714e2c 1412 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), NULL, NULL) ? TRUE : FALSE;
14a5cf38 1413 }
f5b9d040
NIS
1414}
1415
f5b9d040 1416int
e87a358a 1417PerlIO__close(pTHX_ PerlIO *f)
f5b9d040 1418{
37725cdc 1419 if (PerlIOValid(f)) {
46c461b5 1420 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
37725cdc
NIS
1421 if (tab && tab->Close)
1422 return (*tab->Close)(aTHX_ f);
1423 else
1424 return PerlIOBase_close(aTHX_ f);
1425 }
14a5cf38 1426 else {
93189314 1427 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1428 return -1;
1429 }
76ced9ad
NIS
1430}
1431
b931b1d9 1432int
e87a358a 1433Perl_PerlIO_close(pTHX_ PerlIO *f)
b931b1d9 1434{
de009b76 1435 const int code = PerlIO__close(aTHX_ f);
37725cdc
NIS
1436 while (PerlIOValid(f)) {
1437 PerlIO_pop(aTHX_ f);
f6c77cf1 1438 }
14a5cf38 1439 return code;
b931b1d9
NIS
1440}
1441
b931b1d9 1442int
e87a358a 1443Perl_PerlIO_fileno(pTHX_ PerlIO *f)
b931b1d9 1444{
97aff369 1445 dVAR;
b32dd47e 1446 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
b931b1d9
NIS
1447}
1448
1141d9f8 1449
fcf2db38 1450static PerlIO_funcs *
2edd7e44
NIS
1451PerlIO_layer_from_ref(pTHX_ SV *sv)
1452{
97aff369 1453 dVAR;
14a5cf38 1454 /*
71200d45 1455 * For any scalar type load the handler which is bundled with perl
14a5cf38 1456 */
75208dda
RGS
1457 if (SvTYPE(sv) < SVt_PVAV) {
1458 PerlIO_funcs *f = PerlIO_find_layer(aTHX_ STR_WITH_LEN("scalar"), 1);
1459 /* This isn't supposed to happen, since PerlIO::scalar is core,
1460 * but could happen anyway in smaller installs or with PAR */
1461 if (!f && ckWARN(WARN_LAYER))
1462 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"scalar\"");
1463 return f;
1464 }
14a5cf38
JH
1465
1466 /*
71200d45 1467 * For other types allow if layer is known but don't try and load it
14a5cf38
JH
1468 */
1469 switch (SvTYPE(sv)) {
1470 case SVt_PVAV:
6a245ed1 1471 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Array"), 0);
14a5cf38 1472 case SVt_PVHV:
6a245ed1 1473 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Hash"), 0);
14a5cf38 1474 case SVt_PVCV:
6a245ed1 1475 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0);
14a5cf38 1476 case SVt_PVGV:
6a245ed1 1477 return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0);
42d0e0b7
AL
1478 default:
1479 return NULL;
14a5cf38 1480 }
2edd7e44
NIS
1481}
1482
fcf2db38 1483PerlIO_list_t *
14a5cf38
JH
1484PerlIO_resolve_layers(pTHX_ const char *layers,
1485 const char *mode, int narg, SV **args)
1486{
97aff369 1487 dVAR;
14a5cf38
JH
1488 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1489 int incdef = 1;
a1ea730d 1490 if (!PL_perlio)
14a5cf38
JH
1491 PerlIO_stdstreams(aTHX);
1492 if (narg) {
dcda55fc 1493 SV * const arg = *args;
14a5cf38 1494 /*
71200d45
NIS
1495 * If it is a reference but not an object see if we have a handler
1496 * for it
14a5cf38
JH
1497 */
1498 if (SvROK(arg) && !sv_isobject(arg)) {
46c461b5 1499 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
14a5cf38 1500 if (handler) {
3a1ee7e8
NIS
1501 def = PerlIO_list_alloc(aTHX);
1502 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
14a5cf38
JH
1503 incdef = 0;
1504 }
1505 /*
e934609f 1506 * Don't fail if handler cannot be found :via(...) etc. may do
14a5cf38 1507 * something sensible else we will just stringfy and open
71200d45 1508 * resulting string.
14a5cf38
JH
1509 */
1510 }
1511 }
9fe371da 1512 if (!layers || !*layers)
11bcd5da 1513 layers = Perl_PerlIO_context_layers(aTHX_ mode);
14a5cf38
JH
1514 if (layers && *layers) {
1515 PerlIO_list_t *av;
1516 if (incdef) {
a951d81d 1517 av = PerlIO_clone_list(aTHX_ def, NULL);
14a5cf38
JH
1518 }
1519 else {
1520 av = def;
1521 }
0cff2cf3
NIS
1522 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1523 return av;
1524 }
1525 else {
1526 PerlIO_list_free(aTHX_ av);
b37c2d43 1527 return NULL;
0cff2cf3 1528 }
14a5cf38
JH
1529 }
1530 else {
1531 if (incdef)
1532 def->refcnt++;
1533 return def;
1534 }
ee518936
NIS
1535}
1536
1537PerlIO *
14a5cf38
JH
1538PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1539 int imode, int perm, PerlIO *f, int narg, SV **args)
1540{
97aff369 1541 dVAR;
14a5cf38
JH
1542 if (!f && narg == 1 && *args == &PL_sv_undef) {
1543 if ((f = PerlIO_tmpfile())) {
9fe371da 1544 if (!layers || !*layers)
11bcd5da 1545 layers = Perl_PerlIO_context_layers(aTHX_ mode);
14a5cf38
JH
1546 if (layers && *layers)
1547 PerlIO_apply_layers(aTHX_ f, mode, layers);
1548 }
1549 }
1550 else {
de009b76 1551 PerlIO_list_t *layera;
14a5cf38
JH
1552 IV n;
1553 PerlIO_funcs *tab = NULL;
04892f78 1554 if (PerlIOValid(f)) {
14a5cf38 1555 /*
71200d45
NIS
1556 * This is "reopen" - it is not tested as perl does not use it
1557 * yet
14a5cf38
JH
1558 */
1559 PerlIOl *l = *f;
3a1ee7e8 1560 layera = PerlIO_list_alloc(aTHX);
14a5cf38 1561 while (l) {
a951d81d
BL
1562 SV *arg = NULL;
1563 if (l->tab->Getarg)
1564 arg = (*l->tab->Getarg) (aTHX_ &l, NULL, 0);
1565 PerlIO_list_push(aTHX_ layera, l->tab,
1566 (arg) ? arg : &PL_sv_undef);
1567 if (arg)
1568 SvREFCNT_dec(arg);
14a5cf38
JH
1569 l = *PerlIONext(&l);
1570 }
1571 }
1572 else {
1573 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
0cff2cf3
NIS
1574 if (!layera) {
1575 return NULL;
1576 }
14a5cf38
JH
1577 }
1578 /*
71200d45 1579 * Start at "top" of layer stack
14a5cf38
JH
1580 */
1581 n = layera->cur - 1;
1582 while (n >= 0) {
46c461b5 1583 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
14a5cf38
JH
1584 if (t && t->Open) {
1585 tab = t;
1586 break;
1587 }
1588 n--;
1589 }
1590 if (tab) {
1591 /*
71200d45 1592 * Found that layer 'n' can do opens - call it
14a5cf38 1593 */
7cf31beb 1594 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
3b8752bb 1595 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
7cf31beb 1596 }
14a5cf38 1597 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
355d3743
PD
1598 tab->name, layers ? layers : "(Null)", mode, fd,
1599 imode, perm, (void*)f, narg, (void*)args);
210e727c
JH
1600 if (tab->Open)
1601 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1602 f, narg, args);
1603 else {
1604 SETERRNO(EINVAL, LIB_INVARG);
1605 f = NULL;
1606 }
14a5cf38
JH
1607 if (f) {
1608 if (n + 1 < layera->cur) {
1609 /*
1610 * More layers above the one that we used to open -
71200d45 1611 * apply them now
14a5cf38 1612 */
d9dac8cd
NIS
1613 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1614 /* If pushing layers fails close the file */
1615 PerlIO_close(f);
14a5cf38
JH
1616 f = NULL;
1617 }
1618 }
1619 }
1620 }
3a1ee7e8 1621 PerlIO_list_free(aTHX_ layera);
14a5cf38
JH
1622 }
1623 return f;
ee518936 1624}
b931b1d9
NIS
1625
1626
9e353e3b 1627SSize_t
e87a358a 1628Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 1629{
b32dd47e 1630 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1631}
1632
313ca112 1633SSize_t
e87a358a 1634Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1635{
b32dd47e 1636 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1637}
1638
9e353e3b 1639SSize_t
e87a358a 1640Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1641{
b32dd47e 1642 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1643}
1644
6f9d8c32 1645int
e87a358a 1646Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
760ac839 1647{
b32dd47e 1648 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
760ac839
LW
1649}
1650
9e353e3b 1651Off_t
e87a358a 1652Perl_PerlIO_tell(pTHX_ PerlIO *f)
760ac839 1653{
b32dd47e 1654 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
760ac839
LW
1655}
1656
6f9d8c32 1657int
e87a358a 1658Perl_PerlIO_flush(pTHX_ PerlIO *f)
760ac839 1659{
97aff369 1660 dVAR;
14a5cf38
JH
1661 if (f) {
1662 if (*f) {
de009b76 1663 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1b7a0411
JH
1664
1665 if (tab && tab->Flush)
f62ce20a 1666 return (*tab->Flush) (aTHX_ f);
1b7a0411
JH
1667 else
1668 return 0; /* If no Flush defined, silently succeed. */
14a5cf38
JH
1669 }
1670 else {
fe5a182c 1671 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
93189314 1672 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1673 return -1;
1674 }
1675 }
1676 else {
1677 /*
1678 * Is it good API design to do flush-all on NULL, a potentially
1679 * errorneous input? Maybe some magical value (PerlIO*
1680 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1681 * things on fflush(NULL), but should we be bound by their design
71200d45 1682 * decisions? --jhi
14a5cf38 1683 */
a1ea730d 1684 PerlIO **table = &PL_perlio;
14a5cf38
JH
1685 int code = 0;
1686 while ((f = *table)) {
1687 int i;
1688 table = (PerlIO **) (f++);
1689 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1690 if (*f && PerlIO_flush(f) != 0)
1691 code = -1;
1692 f++;
1693 }
1694 }
1695 return code;
1696 }
760ac839
LW
1697}
1698
a9c883f6 1699void
f62ce20a 1700PerlIOBase_flush_linebuf(pTHX)
a9c883f6 1701{
97aff369 1702 dVAR;
a1ea730d 1703 PerlIO **table = &PL_perlio;
14a5cf38
JH
1704 PerlIO *f;
1705 while ((f = *table)) {
1706 int i;
1707 table = (PerlIO **) (f++);
1708 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1709 if (*f
1710 && (PerlIOBase(f)->
1711 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1712 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1713 PerlIO_flush(f);
1714 f++;
1715 }
a9c883f6 1716 }
a9c883f6
NIS
1717}
1718
06da4f11 1719int
e87a358a 1720Perl_PerlIO_fill(pTHX_ PerlIO *f)
06da4f11 1721{
b32dd47e 1722 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
06da4f11
NIS
1723}
1724
f3862f8b
NIS
1725int
1726PerlIO_isutf8(PerlIO *f)
1727{
1b7a0411
JH
1728 if (PerlIOValid(f))
1729 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1730 else
1731 SETERRNO(EBADF, SS_IVCHAN);
37725cdc 1732
1b7a0411 1733 return -1;
f3862f8b
NIS
1734}
1735
6f9d8c32 1736int
e87a358a 1737Perl_PerlIO_eof(pTHX_ PerlIO *f)
760ac839 1738{
b32dd47e 1739 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
9e353e3b
NIS
1740}
1741
9e353e3b 1742int
e87a358a 1743Perl_PerlIO_error(pTHX_ PerlIO *f)
9e353e3b 1744{
b32dd47e 1745 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
9e353e3b
NIS
1746}
1747
9e353e3b 1748void
e87a358a 1749Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
9e353e3b 1750{
b32dd47e 1751 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
9e353e3b
NIS
1752}
1753
9e353e3b 1754void
e87a358a 1755Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1756{
b32dd47e 1757 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
9e353e3b
NIS
1758}
1759
9e353e3b
NIS
1760int
1761PerlIO_has_base(PerlIO *f)
1762{
1b7a0411 1763 if (PerlIOValid(f)) {
46c461b5 1764 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1765
1766 if (tab)
1767 return (tab->Get_base != NULL);
1768 SETERRNO(EINVAL, LIB_INVARG);
1769 }
1770 else
1771 SETERRNO(EBADF, SS_IVCHAN);
1772
1773 return 0;
760ac839
LW
1774}
1775
9e353e3b
NIS
1776int
1777PerlIO_fast_gets(PerlIO *f)
760ac839 1778{
04892f78 1779 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
46c461b5 1780 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1781
1782 if (tab)
1783 return (tab->Set_ptrcnt != NULL);
1784 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1785 }
1b7a0411
JH
1786 else
1787 SETERRNO(EBADF, SS_IVCHAN);
1788
14a5cf38 1789 return 0;
9e353e3b
NIS
1790}
1791
9e353e3b
NIS
1792int
1793PerlIO_has_cntptr(PerlIO *f)
1794{
04892f78 1795 if (PerlIOValid(f)) {
46c461b5 1796 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1797
1798 if (tab)
1799 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1800 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1801 }
1b7a0411
JH
1802 else
1803 SETERRNO(EBADF, SS_IVCHAN);
1804
14a5cf38 1805 return 0;
9e353e3b
NIS
1806}
1807
9e353e3b
NIS
1808int
1809PerlIO_canset_cnt(PerlIO *f)
1810{
04892f78 1811 if (PerlIOValid(f)) {
46c461b5 1812 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1813
1814 if (tab)
1815 return (tab->Set_ptrcnt != NULL);
1816 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1817 }
1b7a0411
JH
1818 else
1819 SETERRNO(EBADF, SS_IVCHAN);
1820
14a5cf38 1821 return 0;
760ac839
LW
1822}
1823
888911fc 1824STDCHAR *
e87a358a 1825Perl_PerlIO_get_base(pTHX_ PerlIO *f)
760ac839 1826{
b32dd47e 1827 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
9e353e3b
NIS
1828}
1829
9e353e3b 1830int
e87a358a 1831Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 1832{
b32dd47e 1833 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
9e353e3b
NIS
1834}
1835
9e353e3b 1836STDCHAR *
e87a358a 1837Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
9e353e3b 1838{
b32dd47e 1839 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
9e353e3b
NIS
1840}
1841
05d1247b 1842int
e87a358a 1843Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1844{
b32dd47e 1845 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
9e353e3b
NIS
1846}
1847
9e353e3b 1848void
e87a358a 1849Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1850{
b32dd47e 1851 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
9e353e3b
NIS
1852}
1853
9e353e3b 1854void
e87a358a 1855Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1856{
b32dd47e 1857 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
9e353e3b
NIS
1858}
1859
4ec2216f 1860
9e353e3b 1861/*--------------------------------------------------------------------------------------*/
14a5cf38 1862/*
71200d45 1863 * utf8 and raw dummy layers
14a5cf38 1864 */
dfebf958 1865
26fb694e 1866IV
2dc2558e 1867PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
26fb694e 1868{
96a5add6 1869 PERL_UNUSED_CONTEXT;
8772537c
AL
1870 PERL_UNUSED_ARG(mode);
1871 PERL_UNUSED_ARG(arg);
00f51856 1872 if (PerlIOValid(f)) {
14a5cf38
JH
1873 if (tab->kind & PERLIO_K_UTF8)
1874 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1875 else
1876 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1877 return 0;
1878 }
1879 return -1;
26fb694e
NIS
1880}
1881
27da23d5 1882PERLIO_FUNCS_DECL(PerlIO_utf8) = {
2dc2558e 1883 sizeof(PerlIO_funcs),
14a5cf38 1884 "utf8",
2dc2558e 1885 0,
6874a2de 1886 PERLIO_K_DUMMY | PERLIO_K_UTF8,
14a5cf38
JH
1887 PerlIOUtf8_pushed,
1888 NULL,
1889 NULL,
1890 NULL,
1891 NULL,
1892 NULL,
1893 NULL,
1894 NULL,
1895 NULL,
1896 NULL,
1897 NULL,
de009b76
AL
1898 NULL,
1899 NULL,
22569500
NIS
1900 NULL, /* flush */
1901 NULL, /* fill */
14a5cf38
JH
1902 NULL,
1903 NULL,
1904 NULL,
1905 NULL,
22569500
NIS
1906 NULL, /* get_base */
1907 NULL, /* get_bufsiz */
1908 NULL, /* get_ptr */
1909 NULL, /* get_cnt */
1910 NULL, /* set_ptrcnt */
26fb694e
NIS
1911};
1912
27da23d5 1913PERLIO_FUNCS_DECL(PerlIO_byte) = {
2dc2558e 1914 sizeof(PerlIO_funcs),
14a5cf38 1915 "bytes",
2dc2558e 1916 0,
14a5cf38
JH
1917 PERLIO_K_DUMMY,
1918 PerlIOUtf8_pushed,
1919 NULL,
1920 NULL,
1921 NULL,
1922 NULL,
1923 NULL,
1924 NULL,
1925 NULL,
1926 NULL,
1927 NULL,
1928 NULL,
de009b76
AL
1929 NULL,
1930 NULL,
22569500
NIS
1931 NULL, /* flush */
1932 NULL, /* fill */
14a5cf38
JH
1933 NULL,
1934 NULL,
1935 NULL,
1936 NULL,
22569500
NIS
1937 NULL, /* get_base */
1938 NULL, /* get_bufsiz */
1939 NULL, /* get_ptr */
1940 NULL, /* get_cnt */
1941 NULL, /* set_ptrcnt */
dfebf958
NIS
1942};
1943
1944PerlIO *
14a5cf38
JH
1945PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1946 IV n, const char *mode, int fd, int imode, int perm,
1947 PerlIO *old, int narg, SV **args)
dfebf958 1948{
8772537c
AL
1949 PerlIO_funcs * const tab = PerlIO_default_btm();
1950 PERL_UNUSED_ARG(self);
210e727c
JH
1951 if (tab && tab->Open)
1952 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1953 old, narg, args);
1954 SETERRNO(EINVAL, LIB_INVARG);
1955 return NULL;
dfebf958
NIS
1956}
1957
27da23d5 1958PERLIO_FUNCS_DECL(PerlIO_raw) = {
2dc2558e 1959 sizeof(PerlIO_funcs),
14a5cf38 1960 "raw",
2dc2558e 1961 0,
14a5cf38
JH
1962 PERLIO_K_DUMMY,
1963 PerlIORaw_pushed,
1964 PerlIOBase_popped,
1965 PerlIORaw_open,
1966 NULL,
1967 NULL,
1968 NULL,
1969 NULL,
1970 NULL,
1971 NULL,
1972 NULL,
1973 NULL,
de009b76
AL
1974 NULL,
1975 NULL,
22569500
NIS
1976 NULL, /* flush */
1977 NULL, /* fill */
14a5cf38
JH
1978 NULL,
1979 NULL,
1980 NULL,
1981 NULL,
22569500
NIS
1982 NULL, /* get_base */
1983 NULL, /* get_bufsiz */
1984 NULL, /* get_ptr */
1985 NULL, /* get_cnt */
1986 NULL, /* set_ptrcnt */
dfebf958
NIS
1987};
1988/*--------------------------------------------------------------------------------------*/
1989/*--------------------------------------------------------------------------------------*/
14a5cf38 1990/*
71200d45 1991 * "Methods" of the "base class"
14a5cf38 1992 */
9e353e3b
NIS
1993
1994IV
f62ce20a 1995PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1996{
04892f78 1997 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
9e353e3b
NIS
1998}
1999
f5b9d040 2000char *
81428673 2001PerlIO_modestr(PerlIO * f, char *buf)
14a5cf38
JH
2002{
2003 char *s = buf;
81428673 2004 if (PerlIOValid(f)) {
de009b76 2005 const IV flags = PerlIOBase(f)->flags;
81428673
NIS
2006 if (flags & PERLIO_F_APPEND) {
2007 *s++ = 'a';
2008 if (flags & PERLIO_F_CANREAD) {
2009 *s++ = '+';
2010 }
14a5cf38 2011 }
81428673
NIS
2012 else if (flags & PERLIO_F_CANREAD) {
2013 *s++ = 'r';
2014 if (flags & PERLIO_F_CANWRITE)
2015 *s++ = '+';
2016 }
2017 else if (flags & PERLIO_F_CANWRITE) {
2018 *s++ = 'w';
2019 if (flags & PERLIO_F_CANREAD) {
2020 *s++ = '+';
2021 }
14a5cf38 2022 }
35990314 2023#ifdef PERLIO_USING_CRLF
81428673
NIS
2024 if (!(flags & PERLIO_F_CRLF))
2025 *s++ = 'b';
5f1a76d0 2026#endif
81428673 2027 }
14a5cf38
JH
2028 *s = '\0';
2029 return buf;
f5b9d040
NIS
2030}
2031
81428673 2032
76ced9ad 2033IV
2dc2558e 2034PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
9e353e3b 2035{
de009b76 2036 PerlIOl * const l = PerlIOBase(f);
96a5add6 2037 PERL_UNUSED_CONTEXT;
8772537c 2038 PERL_UNUSED_ARG(arg);
de009b76 2039
14a5cf38
JH
2040 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
2041 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
2042 if (tab->Set_ptrcnt != NULL)
2043 l->flags |= PERLIO_F_FASTGETS;
2044 if (mode) {
3b6c1aba 2045 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2046 mode++;
2047 switch (*mode++) {
2048 case 'r':
2049 l->flags |= PERLIO_F_CANREAD;
2050 break;
2051 case 'a':
2052 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2053 break;
2054 case 'w':
2055 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2056 break;
2057 default:
93189314 2058 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2059 return -1;
2060 }
2061 while (*mode) {
2062 switch (*mode++) {
2063 case '+':
2064 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2065 break;
2066 case 'b':
2067 l->flags &= ~PERLIO_F_CRLF;
2068 break;
2069 case 't':
2070 l->flags |= PERLIO_F_CRLF;
2071 break;
2072 default:
93189314 2073 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2074 return -1;
2075 }
2076 }
2077 }
2078 else {
2079 if (l->next) {
2080 l->flags |= l->next->flags &
2081 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2082 PERLIO_F_APPEND);
2083 }
2084 }
5e2ab84b 2085#if 0
14a5cf38 2086 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
6c9570dc 2087 (void*)f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
14a5cf38 2088 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 2089#endif
14a5cf38 2090 return 0;
76ced9ad
NIS
2091}
2092
2093IV
f62ce20a 2094PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 2095{
96a5add6 2096 PERL_UNUSED_CONTEXT;
8772537c 2097 PERL_UNUSED_ARG(f);
14a5cf38 2098 return 0;
760ac839
LW
2099}
2100
9e353e3b 2101SSize_t
f62ce20a 2102PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2103{
14a5cf38 2104 /*
71200d45 2105 * Save the position as current head considers it
14a5cf38 2106 */
de009b76 2107 const Off_t old = PerlIO_tell(f);
a0714e2c 2108 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", NULL);
14a5cf38 2109 PerlIOSelf(f, PerlIOBuf)->posn = old;
de009b76 2110 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
9e353e3b
NIS
2111}
2112
f6c77cf1 2113SSize_t
f62ce20a 2114PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 2115{
14a5cf38
JH
2116 STDCHAR *buf = (STDCHAR *) vbuf;
2117 if (f) {
263df5f1
JH
2118 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2119 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2120 SETERRNO(EBADF, SS_IVCHAN);
2121 return 0;
2122 }
14a5cf38 2123 while (count > 0) {
93c2c2ec
IZ
2124 get_cnt:
2125 {
14a5cf38
JH
2126 SSize_t avail = PerlIO_get_cnt(f);
2127 SSize_t take = 0;
2128 if (avail > 0)
bb7a0f54 2129 take = ((SSize_t)count < avail) ? (SSize_t)count : avail;
14a5cf38
JH
2130 if (take > 0) {
2131 STDCHAR *ptr = PerlIO_get_ptr(f);
2132 Copy(ptr, buf, take, STDCHAR);
2133 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2134 count -= take;
2135 buf += take;
93c2c2ec
IZ
2136 if (avail == 0) /* set_ptrcnt could have reset avail */
2137 goto get_cnt;
14a5cf38
JH
2138 }
2139 if (count > 0 && avail <= 0) {
2140 if (PerlIO_fill(f) != 0)
2141 break;
2142 }
93c2c2ec 2143 }
14a5cf38
JH
2144 }
2145 return (buf - (STDCHAR *) vbuf);
2146 }
f6c77cf1 2147 return 0;
f6c77cf1
NIS
2148}
2149
9e353e3b 2150IV
f62ce20a 2151PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 2152{
96a5add6 2153 PERL_UNUSED_CONTEXT;
8772537c 2154 PERL_UNUSED_ARG(f);
14a5cf38 2155 return 0;
9e353e3b
NIS
2156}
2157
2158IV
f62ce20a 2159PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 2160{
96a5add6 2161 PERL_UNUSED_CONTEXT;
8772537c 2162 PERL_UNUSED_ARG(f);
14a5cf38 2163 return -1;
06da4f11
NIS
2164}
2165
2166IV
f62ce20a 2167PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 2168{
37725cdc
NIS
2169 IV code = -1;
2170 if (PerlIOValid(f)) {
2171 PerlIO *n = PerlIONext(f);
2172 code = PerlIO_flush(f);
2173 PerlIOBase(f)->flags &=
2174 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2175 while (PerlIOValid(n)) {
de009b76 2176 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
37725cdc
NIS
2177 if (tab && tab->Close) {
2178 if ((*tab->Close)(aTHX_ n) != 0)
2179 code = -1;
2180 break;
2181 }
2182 else {
2183 PerlIOBase(n)->flags &=
2184 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2185 }
2186 n = PerlIONext(n);
2187 }
2188 }
2189 else {
2190 SETERRNO(EBADF, SS_IVCHAN);
2191 }
14a5cf38 2192 return code;
9e353e3b
NIS
2193}
2194
2195IV
f62ce20a 2196PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 2197{
96a5add6 2198 PERL_UNUSED_CONTEXT;
04892f78 2199 if (PerlIOValid(f)) {
14a5cf38
JH
2200 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2201 }
2202 return 1;
9e353e3b
NIS
2203}
2204
2205IV
f62ce20a 2206PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 2207{
96a5add6 2208 PERL_UNUSED_CONTEXT;
04892f78 2209 if (PerlIOValid(f)) {
14a5cf38
JH
2210 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2211 }
2212 return 1;
9e353e3b
NIS
2213}
2214
2215void
f62ce20a 2216PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 2217{
04892f78 2218 if (PerlIOValid(f)) {
dcda55fc 2219 PerlIO * const n = PerlIONext(f);
14a5cf38 2220 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
04892f78 2221 if (PerlIOValid(n))
14a5cf38
JH
2222 PerlIO_clearerr(n);
2223 }
9e353e3b
NIS
2224}
2225
2226void
f62ce20a 2227PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 2228{
96a5add6 2229 PERL_UNUSED_CONTEXT;
04892f78 2230 if (PerlIOValid(f)) {
14a5cf38
JH
2231 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2232 }
9e353e3b
NIS
2233}
2234
93a8090d
NIS
2235SV *
2236PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2237{
2238 if (!arg)
a0714e2c 2239 return NULL;
93a8090d
NIS
2240#ifdef sv_dup
2241 if (param) {
a951d81d
BL
2242 arg = sv_dup(arg, param);
2243 SvREFCNT_inc_simple_void_NN(arg);
2244 return arg;
93a8090d
NIS
2245 }
2246 else {
2247 return newSVsv(arg);
2248 }
2249#else
1b6737cc 2250 PERL_UNUSED_ARG(param);
93a8090d
NIS
2251 return newSVsv(arg);
2252#endif
2253}
2254
2255PerlIO *
ecdeb87c 2256PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d 2257{
1b6737cc 2258 PerlIO * const nexto = PerlIONext(o);
04892f78 2259 if (PerlIOValid(nexto)) {
de009b76 2260 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
37725cdc
NIS
2261 if (tab && tab->Dup)
2262 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2263 else
2264 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
93a8090d
NIS
2265 }
2266 if (f) {
dcda55fc 2267 PerlIO_funcs * const self = PerlIOBase(o)->tab;
a951d81d 2268 SV *arg = NULL;
93a8090d 2269 char buf[8];
fe5a182c
JH
2270 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2271 self->name, (void*)f, (void*)o, (void*)param);
210e727c
JH
2272 if (self->Getarg)
2273 arg = (*self->Getarg)(aTHX_ o, param, flags);
93a8090d 2274 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
f0720f70
RGS
2275 if (PerlIOBase(o)->flags & PERLIO_F_UTF8)
2276 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
a951d81d 2277 if (arg)
93a8090d 2278 SvREFCNT_dec(arg);
93a8090d
NIS
2279 }
2280 return f;
2281}
2282
27da23d5 2283/* PL_perlio_fd_refcnt[] is in intrpvar.h */
93a8090d 2284
8b84d7dd 2285/* Must be called with PL_perlio_mutex locked. */
22c96fc1
NC
2286static void
2287S_more_refcounted_fds(pTHX_ const int new_fd) {
7a89be66 2288 dVAR;
22c96fc1 2289 const int old_max = PL_perlio_fd_refcnt_size;
f4ae5be6 2290 const int new_max = 16 + (new_fd & ~15);
22c96fc1
NC
2291 int *new_array;
2292
2293 PerlIO_debug("More fds - old=%d, need %d, new=%d\n",
2294 old_max, new_fd, new_max);
2295
2296 if (new_fd < old_max) {
2297 return;
2298 }
2299
f4ae5be6
NC
2300 assert (new_max > new_fd);
2301
eae082a0
JH
2302 /* Use plain realloc() since we need this memory to be really
2303 * global and visible to all the interpreters and/or threads. */
2304 new_array = (int*) realloc(PL_perlio_fd_refcnt, new_max * sizeof(int));
22c96fc1
NC
2305
2306 if (!new_array) {
8b84d7dd 2307#ifdef USE_ITHREADS
6cb8cb21 2308 MUTEX_UNLOCK(&PL_perlio_mutex);
22c96fc1
NC
2309#endif
2310 /* Can't use PerlIO to write as it allocates memory */
2311 PerlLIO_write(PerlIO_fileno(Perl_error_log),
2312 PL_no_mem, strlen(PL_no_mem));
2313 my_exit(1);
2314 }
2315
2316 PL_perlio_fd_refcnt_size = new_max;
2317 PL_perlio_fd_refcnt = new_array;
2318
95b63a38
JH
2319 PerlIO_debug("Zeroing %p, %d\n",
2320 (void*)(new_array + old_max),
2321 new_max - old_max);
22c96fc1
NC
2322
2323 Zero(new_array + old_max, new_max - old_max, int);
2324}
2325
2326
93a8090d
NIS
2327void
2328PerlIO_init(pTHX)
2329{
8b84d7dd 2330 /* MUTEX_INIT(&PL_perlio_mutex) is done in PERL_SYS_INIT3(). */
96a5add6 2331 PERL_UNUSED_CONTEXT;
93a8090d
NIS
2332}
2333
168d5872
NIS
2334void
2335PerlIOUnix_refcnt_inc(int fd)
2336{
27da23d5 2337 dTHX;
22c96fc1 2338 if (fd >= 0) {
97aff369 2339 dVAR;
22c96fc1 2340
8b84d7dd 2341#ifdef USE_ITHREADS
6cb8cb21 2342 MUTEX_LOCK(&PL_perlio_mutex);
168d5872 2343#endif
22c96fc1
NC
2344 if (fd >= PL_perlio_fd_refcnt_size)
2345 S_more_refcounted_fds(aTHX_ fd);
2346
27da23d5 2347 PL_perlio_fd_refcnt[fd]++;
8b84d7dd
RGS
2348 if (PL_perlio_fd_refcnt[fd] <= 0) {
2349 Perl_croak(aTHX_ "refcnt_inc: fd %d: %d <= 0\n",
2350 fd, PL_perlio_fd_refcnt[fd]);
2351 }
2352 PerlIO_debug("refcnt_inc: fd %d refcnt=%d\n",
2353 fd, PL_perlio_fd_refcnt[fd]);
22c96fc1 2354
8b84d7dd 2355#ifdef USE_ITHREADS
6cb8cb21 2356 MUTEX_UNLOCK(&PL_perlio_mutex);
168d5872 2357#endif
8b84d7dd
RGS
2358 } else {
2359 Perl_croak(aTHX_ "refcnt_inc: fd %d < 0\n", fd);
168d5872
NIS
2360 }
2361}
2362
168d5872
NIS
2363int
2364PerlIOUnix_refcnt_dec(int fd)
2365{
27da23d5 2366 dTHX;
168d5872 2367 int cnt = 0;
22c96fc1 2368 if (fd >= 0) {
97aff369 2369 dVAR;
8b84d7dd 2370#ifdef USE_ITHREADS
6cb8cb21 2371 MUTEX_LOCK(&PL_perlio_mutex);
168d5872 2372#endif
8b84d7dd
RGS
2373 if (fd >= PL_perlio_fd_refcnt_size) {
2374 Perl_croak(aTHX_ "refcnt_dec: fd %d >= refcnt_size %d\n",
2375 fd, PL_perlio_fd_refcnt_size);
2376 }
2377 if (PL_perlio_fd_refcnt[fd] <= 0) {
2378 Perl_croak(aTHX_ "refcnt_dec: fd %d: %d <= 0\n",
2379 fd, PL_perlio_fd_refcnt[fd]);
2380 }
27da23d5 2381 cnt = --PL_perlio_fd_refcnt[fd];
8b84d7dd
RGS
2382 PerlIO_debug("refcnt_dec: fd %d refcnt=%d\n", fd, cnt);
2383#ifdef USE_ITHREADS
6cb8cb21 2384 MUTEX_UNLOCK(&PL_perlio_mutex);
168d5872 2385#endif
8b84d7dd
RGS
2386 } else {
2387 Perl_croak(aTHX_ "refcnt_dec: fd %d < 0\n", fd);
168d5872
NIS
2388 }
2389 return cnt;
2390}
2391
694c95cf
JH
2392void
2393PerlIO_cleanup(pTHX)
2394{
97aff369 2395 dVAR;
694c95cf
JH
2396 int i;
2397#ifdef USE_ITHREADS
a25429c6 2398 PerlIO_debug("Cleanup layers for %p\n",(void*)aTHX);
9f4bd222
NIS
2399#else
2400 PerlIO_debug("Cleanup layers\n");
694c95cf 2401#endif
e47547a8 2402
694c95cf
JH
2403 /* Raise STDIN..STDERR refcount so we don't close them */
2404 for (i=0; i < 3; i++)
2405 PerlIOUnix_refcnt_inc(i);
2406 PerlIO_cleantable(aTHX_ &PL_perlio);
2407 /* Restore STDIN..STDERR refcount */
2408 for (i=0; i < 3; i++)
2409 PerlIOUnix_refcnt_dec(i);
9f4bd222
NIS
2410
2411 if (PL_known_layers) {
2412 PerlIO_list_free(aTHX_ PL_known_layers);
2413 PL_known_layers = NULL;
2414 }
27da23d5 2415 if (PL_def_layerlist) {
9f4bd222
NIS
2416 PerlIO_list_free(aTHX_ PL_def_layerlist);
2417 PL_def_layerlist = NULL;
2418 }
6cb8cb21
RGS
2419}
2420
77db880c 2421void PerlIO_teardown() /* Call only from PERL_SYS_TERM(). */
6cb8cb21 2422{
53d44271 2423 dVAR;
4f3da17a
DM
2424#if 0
2425/* XXX we can't rely on an interpreter being present at this late stage,
2426 XXX so we can't use a function like PerlLIO_write that relies on one
2427 being present (at least in win32) :-(.
2428 Disable for now.
2429*/
6cb8cb21
RGS
2430#ifdef DEBUGGING
2431 {
2432 /* By now all filehandles should have been closed, so any
2433 * stray (non-STD-)filehandles indicate *possible* (PerlIO)
2434 * errors. */
77db880c
JH
2435#define PERLIO_TEARDOWN_MESSAGE_BUF_SIZE 64
2436#define PERLIO_TEARDOWN_MESSAGE_FD 2
2437 char buf[PERLIO_TEARDOWN_MESSAGE_BUF_SIZE];
6cb8cb21
RGS
2438 int i;
2439 for (i = 3; i < PL_perlio_fd_refcnt_size; i++) {
77db880c
JH
2440 if (PL_perlio_fd_refcnt[i]) {
2441 const STRLEN len =
2442 my_snprintf(buf, sizeof(buf),
2443 "PerlIO_teardown: fd %d refcnt=%d\n",
2444 i, PL_perlio_fd_refcnt[i]);
2445 PerlLIO_write(PERLIO_TEARDOWN_MESSAGE_FD, buf, len);
2446 }
6cb8cb21
RGS
2447 }
2448 }
2449#endif
4f3da17a 2450#endif
eae082a0
JH
2451 /* Not bothering with PL_perlio_mutex since by now
2452 * all the interpreters are gone. */
1cd82952
RGS
2453 if (PL_perlio_fd_refcnt_size /* Assuming initial size of zero. */
2454 && PL_perlio_fd_refcnt) {
eae082a0 2455 free(PL_perlio_fd_refcnt); /* To match realloc() in S_more_refcounted_fds(). */
432ce874
YO
2456 PL_perlio_fd_refcnt = NULL;
2457 PL_perlio_fd_refcnt_size = 0;
1cd82952 2458 }
694c95cf
JH
2459}
2460
9e353e3b 2461/*--------------------------------------------------------------------------------------*/
14a5cf38 2462/*
71200d45 2463 * Bottom-most level for UNIX-like case
14a5cf38 2464 */
9e353e3b 2465
14a5cf38 2466typedef struct {
22569500
NIS
2467 struct _PerlIO base; /* The generic part */
2468 int fd; /* UNIX like file descriptor */
2469 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2470} PerlIOUnix;
2471
6f9d8c32 2472int
9e353e3b 2473PerlIOUnix_oflags(const char *mode)
760ac839 2474{
14a5cf38 2475 int oflags = -1;
3b6c1aba 2476 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
14a5cf38
JH
2477 mode++;
2478 switch (*mode) {
2479 case 'r':
2480 oflags = O_RDONLY;
2481 if (*++mode == '+') {
2482 oflags = O_RDWR;
2483 mode++;
2484 }
2485 break;
2486
2487 case 'w':
2488 oflags = O_CREAT | O_TRUNC;
2489 if (*++mode == '+') {
2490 oflags |= O_RDWR;
2491 mode++;
2492 }
2493 else
2494 oflags |= O_WRONLY;
2495 break;
2496
2497 case 'a':
2498 oflags = O_CREAT | O_APPEND;
2499 if (*++mode == '+') {
2500 oflags |= O_RDWR;
2501 mode++;
2502 }
2503 else
2504 oflags |= O_WRONLY;
2505 break;
2506 }
2507 if (*mode == 'b') {
2508 oflags |= O_BINARY;
2509 oflags &= ~O_TEXT;
2510 mode++;
2511 }
2512 else if (*mode == 't') {
2513 oflags |= O_TEXT;
2514 oflags &= ~O_BINARY;
2515 mode++;
2516 }
2517 /*
71200d45 2518 * Always open in binary mode
14a5cf38
JH
2519 */
2520 oflags |= O_BINARY;
2521 if (*mode || oflags == -1) {
93189314 2522 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2523 oflags = -1;
2524 }
2525 return oflags;
9e353e3b
NIS
2526}
2527
2528IV
f62ce20a 2529PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2530{
96a5add6 2531 PERL_UNUSED_CONTEXT;
14a5cf38 2532 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2533}
2534
aa063c35
NIS
2535static void
2536PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
4b803d04 2537{
de009b76 2538 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
6caa5a9c 2539#if defined(WIN32)
aa063c35
NIS
2540 Stat_t st;
2541 if (PerlLIO_fstat(fd, &st) == 0) {
6caa5a9c 2542 if (!S_ISREG(st.st_mode)) {
aa063c35 2543 PerlIO_debug("%d is not regular file\n",fd);
6caa5a9c
NIS
2544 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2545 }
aa063c35
NIS
2546 else {
2547 PerlIO_debug("%d _is_ a regular file\n",fd);
2548 }
6caa5a9c
NIS
2549 }
2550#endif
aa063c35
NIS
2551 s->fd = fd;
2552 s->oflags = imode;
2553 PerlIOUnix_refcnt_inc(fd);
96a5add6 2554 PERL_UNUSED_CONTEXT;
aa063c35
NIS
2555}
2556
2557IV
2558PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2559{
2560 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
14a5cf38 2561 if (*PerlIONext(f)) {
4b069b44 2562 /* We never call down so do any pending stuff now */
03c0554d 2563 PerlIO_flush(PerlIONext(f));
14a5cf38 2564 /*
71200d45 2565 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2566 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2567 * Should the value on NULL mode be 0 or -1?
14a5cf38 2568 */
acbd16bf 2569 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
aa063c35 2570 mode ? PerlIOUnix_oflags(mode) : -1);
14a5cf38
JH
2571 }
2572 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
6caa5a9c 2573
14a5cf38 2574 return code;
4b803d04
NIS
2575}
2576
c2fcde81
JH
2577IV
2578PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2579{
de009b76 2580 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
0723351e 2581 Off_t new_loc;
96a5add6 2582 PERL_UNUSED_CONTEXT;
c2fcde81
JH
2583 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2584#ifdef ESPIPE
2585 SETERRNO(ESPIPE, LIB_INVARG);
2586#else
2587 SETERRNO(EINVAL, LIB_INVARG);
2588#endif
2589 return -1;
2590 }
0723351e
NC
2591 new_loc = PerlLIO_lseek(fd, offset, whence);
2592 if (new_loc == (Off_t) - 1)
dcda55fc 2593 return -1;
c2fcde81
JH
2594 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2595 return 0;
2596}
2597
9e353e3b 2598PerlIO *
14a5cf38
JH
2599PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2600 IV n, const char *mode, int fd, int imode,
2601 int perm, PerlIO *f, int narg, SV **args)
2602{
d9dac8cd 2603 if (PerlIOValid(f)) {
14a5cf38 2604 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2605 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2606 }
2607 if (narg > 0) {
3b6c1aba 2608 if (*mode == IoTYPE_NUMERIC)
14a5cf38
JH
2609 mode++;
2610 else {
2611 imode = PerlIOUnix_oflags(mode);
2612 perm = 0666;
2613 }
2614 if (imode != -1) {
e62f0680 2615 const char *path = SvPV_nolen_const(*args);
14a5cf38
JH
2616 fd = PerlLIO_open3(path, imode, perm);
2617 }
2618 }
2619 if (fd >= 0) {
3b6c1aba 2620 if (*mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2621 mode++;
2622 if (!f) {
2623 f = PerlIO_allocate(aTHX);
d9dac8cd
NIS
2624 }
2625 if (!PerlIOValid(f)) {
a33cf58c
NIS
2626 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2627 return NULL;
2628 }
d9dac8cd 2629 }
aa063c35 2630 PerlIOUnix_setfd(aTHX_ f, fd, imode);
14a5cf38 2631 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
c2fcde81
JH
2632 if (*mode == IoTYPE_APPEND)
2633 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
14a5cf38
JH
2634 return f;
2635 }
2636 else {
2637 if (f) {
6f207bd3 2638 NOOP;
14a5cf38 2639 /*
71200d45 2640 * FIXME: pop layers ???
14a5cf38
JH
2641 */
2642 }
2643 return NULL;
2644 }
9e353e3b
NIS
2645}
2646
71200d45 2647PerlIO *
ecdeb87c 2648PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 2649{
dcda55fc 2650 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2651 int fd = os->fd;
ecdeb87c
NIS
2652 if (flags & PERLIO_DUP_FD) {
2653 fd = PerlLIO_dup(fd);
2654 }
22c96fc1 2655 if (fd >= 0) {
ecdeb87c 2656 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2657 if (f) {
2658 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
aa063c35 2659 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
71200d45
NIS
2660 return f;
2661 }
71200d45
NIS
2662 }
2663 return NULL;
2664}
2665
2666
9e353e3b 2667SSize_t
f62ce20a 2668PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2669{
97aff369 2670 dVAR;
de009b76 2671 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2672#ifdef PERLIO_STD_SPECIAL
2673 if (fd == 0)
2674 return PERLIO_STD_IN(fd, vbuf, count);
2675#endif
81428673 2676 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
1fd8f4ce 2677 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
263df5f1 2678 return 0;
1fd8f4ce 2679 }
14a5cf38 2680 while (1) {
b464bac0 2681 const SSize_t len = PerlLIO_read(fd, vbuf, count);
14a5cf38 2682 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2683 if (len < 0) {
2684 if (errno != EAGAIN) {
2685 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2686 }
2687 }
2688 else if (len == 0 && count != 0) {
14a5cf38 2689 PerlIOBase(f)->flags |= PERLIO_F_EOF;
ba85f2ea
NIS
2690 SETERRNO(0,0);
2691 }
14a5cf38
JH
2692 return len;
2693 }
2694 PERL_ASYNC_CHECK();
2695 }
b464bac0 2696 /*NOTREACHED*/
9e353e3b
NIS
2697}
2698
2699SSize_t
f62ce20a 2700PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2701{
97aff369 2702 dVAR;
de009b76 2703 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2704#ifdef PERLIO_STD_SPECIAL
2705 if (fd == 1 || fd == 2)
2706 return PERLIO_STD_OUT(fd, vbuf, count);
2707#endif
14a5cf38 2708 while (1) {
de009b76 2709 const SSize_t len = PerlLIO_write(fd, vbuf, count);
14a5cf38 2710 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2711 if (len < 0) {
2712 if (errno != EAGAIN) {
2713 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2714 }
2715 }
14a5cf38
JH
2716 return len;
2717 }
2718 PERL_ASYNC_CHECK();
06da4f11 2719 }
1b6737cc 2720 /*NOTREACHED*/
9e353e3b
NIS
2721}
2722
9e353e3b 2723Off_t
f62ce20a 2724PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2725{
96a5add6
AL
2726 PERL_UNUSED_CONTEXT;
2727
14a5cf38 2728 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2729}
2730
71200d45 2731
9e353e3b 2732IV
f62ce20a 2733PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2734{
97aff369 2735 dVAR;
de009b76 2736 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
14a5cf38 2737 int code = 0;
168d5872
NIS
2738 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2739 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2740 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2741 return 0;
22569500 2742 }
93a8090d
NIS
2743 }
2744 else {
93189314 2745 SETERRNO(EBADF,SS_IVCHAN);
93a8090d
NIS
2746 return -1;
2747 }
14a5cf38
JH
2748 while (PerlLIO_close(fd) != 0) {
2749 if (errno != EINTR) {
2750 code = -1;
2751 break;
2752 }
2753 PERL_ASYNC_CHECK();
2754 }
2755 if (code == 0) {
2756 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2757 }
2758 return code;
9e353e3b
NIS
2759}
2760
27da23d5 2761PERLIO_FUNCS_DECL(PerlIO_unix) = {
2dc2558e 2762 sizeof(PerlIO_funcs),
14a5cf38
JH
2763 "unix",
2764 sizeof(PerlIOUnix),
2765 PERLIO_K_RAW,
2766 PerlIOUnix_pushed,
44798d05 2767 PerlIOBase_popped,
14a5cf38 2768 PerlIOUnix_open,
86e05cf2 2769 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2770 NULL,
2771 PerlIOUnix_fileno,
71200d45 2772 PerlIOUnix_dup,
14a5cf38
JH
2773 PerlIOUnix_read,
2774 PerlIOBase_unread,
2775 PerlIOUnix_write,
2776 PerlIOUnix_seek,
2777 PerlIOUnix_tell,
2778 PerlIOUnix_close,
22569500
NIS
2779 PerlIOBase_noop_ok, /* flush */
2780 PerlIOBase_noop_fail, /* fill */
14a5cf38
JH
2781 PerlIOBase_eof,
2782 PerlIOBase_error,
2783 PerlIOBase_clearerr,
2784 PerlIOBase_setlinebuf,
22569500
NIS
2785 NULL, /* get_base */
2786 NULL, /* get_bufsiz */
2787 NULL, /* get_ptr */
2788 NULL, /* get_cnt */
2789 NULL, /* set_ptrcnt */
9e353e3b
NIS
2790};
2791
2792/*--------------------------------------------------------------------------------------*/
14a5cf38 2793/*
71200d45 2794 * stdio as a layer
14a5cf38 2795 */
9e353e3b 2796
313e59c8
NIS
2797#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2798/* perl5.8 - This ensures the last minute VMS ungetc fix is not
2799 broken by the last second glibc 2.3 fix
2800 */
2801#define STDIO_BUFFER_WRITABLE
2802#endif
2803
2804
14a5cf38
JH
2805typedef struct {
2806 struct _PerlIO base;
22569500 2807 FILE *stdio; /* The stream */
9e353e3b
NIS
2808} PerlIOStdio;
2809
2810IV
f62ce20a 2811PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2812{
96a5add6
AL
2813 PERL_UNUSED_CONTEXT;
2814
c4420975
AL
2815 if (PerlIOValid(f)) {
2816 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2817 if (s)
2818 return PerlSIO_fileno(s);
439ba545
NIS
2819 }
2820 errno = EBADF;
2821 return -1;
9e353e3b
NIS
2822}
2823
766a733e 2824char *
14a5cf38
JH
2825PerlIOStdio_mode(const char *mode, char *tmode)
2826{
de009b76 2827 char * const ret = tmode;
a0625d38
SR
2828 if (mode) {
2829 while (*mode) {
2830 *tmode++ = *mode++;
2831 }
14a5cf38 2832 }
95005ad8 2833#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
6ce75a77
JH
2834 *tmode++ = 'b';
2835#endif
14a5cf38
JH
2836 *tmode = '\0';
2837 return ret;
2838}
2839
4b803d04 2840IV
2dc2558e 2841PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2842{
1fd8f4ce
NIS
2843 PerlIO *n;
2844 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
46c461b5 2845 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
1fd8f4ce
NIS
2846 if (toptab == tab) {
2847 /* Top is already stdio - pop self (duplicate) and use original */
2848 PerlIO_pop(aTHX_ f);
2849 return 0;
2850 } else {
de009b76 2851 const int fd = PerlIO_fileno(n);
1fd8f4ce
NIS
2852 char tmode[8];
2853 FILE *stdio;
81428673 2854 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
1fd8f4ce
NIS
2855 mode = PerlIOStdio_mode(mode, tmode)))) {
2856 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2857 /* We never call down so do any pending stuff now */
2858 PerlIO_flush(PerlIONext(f));
81428673 2859 }
1fd8f4ce
NIS
2860 else {
2861 return -1;
2862 }
2863 }
14a5cf38 2864 }
2dc2558e 2865 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4b803d04
NIS
2866}
2867
22569500 2868
9e353e3b 2869PerlIO *
4b069b44 2870PerlIO_importFILE(FILE *stdio, const char *mode)
9e353e3b 2871{
14a5cf38
JH
2872 dTHX;
2873 PerlIO *f = NULL;
2874 if (stdio) {
22569500 2875 PerlIOStdio *s;
4b069b44
NIS
2876 if (!mode || !*mode) {
2877 /* We need to probe to see how we can open the stream
2878 so start with read/write and then try write and read
2879 we dup() so that we can fclose without loosing the fd.
2880
2881 Note that the errno value set by a failing fdopen
2882 varies between stdio implementations.
2883 */
de009b76 2884 const int fd = PerlLIO_dup(fileno(stdio));
a33cf58c 2885 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
4b069b44 2886 if (!f2) {
a33cf58c 2887 f2 = PerlSIO_fdopen(fd, (mode = "w"));
4b069b44
NIS
2888 }
2889 if (!f2) {
a33cf58c 2890 f2 = PerlSIO_fdopen(fd, (mode = "r"));
4b069b44
NIS
2891 }
2892 if (!f2) {
2893 /* Don't seem to be able to open */
2894 PerlLIO_close(fd);
2895 return f;
2896 }
2897 fclose(f2);
22569500 2898 }
a0714e2c 2899 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, NULL))) {
a33cf58c
NIS
2900 s = PerlIOSelf(f, PerlIOStdio);
2901 s->stdio = stdio;
c586124f 2902 PerlIOUnix_refcnt_inc(fileno(stdio));
a33cf58c 2903 }
14a5cf38
JH
2904 }
2905 return f;
9e353e3b
NIS
2906}
2907
2908PerlIO *
14a5cf38
JH
2909PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2910 IV n, const char *mode, int fd, int imode,
2911 int perm, PerlIO *f, int narg, SV **args)
2912{
2913 char tmode[8];
d9dac8cd 2914 if (PerlIOValid(f)) {
dcda55fc
AL
2915 const char * const path = SvPV_nolen_const(*args);
2916 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2917 FILE *stdio;
2918 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2919 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2920 s->stdio);
2921 if (!s->stdio)
2922 return NULL;
2923 s->stdio = stdio;
1751d015 2924 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2925 return f;
2926 }
2927 else {
2928 if (narg > 0) {
dcda55fc 2929 const char * const path = SvPV_nolen_const(*args);
3b6c1aba 2930 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
2931 mode++;
2932 fd = PerlLIO_open3(path, imode, perm);
2933 }
2934 else {
95005ad8
GH
2935 FILE *stdio;
2936 bool appended = FALSE;
2937#ifdef __CYGWIN__
2938 /* Cygwin wants its 'b' early. */
2939 appended = TRUE;
2940 mode = PerlIOStdio_mode(mode, tmode);
2941#endif
2942 stdio = PerlSIO_fopen(path, mode);
6f0313ac 2943 if (stdio) {
6f0313ac
JH
2944 if (!f) {
2945 f = PerlIO_allocate(aTHX);
2946 }
95005ad8
GH
2947 if (!appended)
2948 mode = PerlIOStdio_mode(mode, tmode);
2949 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2950 if (f) {
0f0f9e2b
JH
2951 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2952 PerlIOUnix_refcnt_inc(fileno(stdio));
2953 } else {
2954 PerlSIO_fclose(stdio);
6f0313ac
JH
2955 }
2956 return f;
2957 }
2958 else {
2959 return NULL;
2960 }
14a5cf38
JH
2961 }
2962 }
2963 if (fd >= 0) {
2964 FILE *stdio = NULL;
2965 int init = 0;
3b6c1aba 2966 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
2967 init = 1;
2968 mode++;
2969 }
2970 if (init) {
2971 switch (fd) {
2972 case 0:
2973 stdio = PerlSIO_stdin;
2974 break;
2975 case 1:
2976 stdio = PerlSIO_stdout;
2977 break;
2978 case 2:
2979 stdio = PerlSIO_stderr;
2980 break;
2981 }
2982 }
2983 else {
2984 stdio = PerlSIO_fdopen(fd, mode =
2985 PerlIOStdio_mode(mode, tmode));
2986 }
2987 if (stdio) {
d9dac8cd
NIS
2988 if (!f) {
2989 f = PerlIO_allocate(aTHX);
2990 }
a33cf58c 2991 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
1a159fba
JH
2992 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2993 PerlIOUnix_refcnt_inc(fileno(stdio));
a33cf58c 2994 }
14a5cf38
JH
2995 return f;
2996 }
2997 }
2998 }
ee518936 2999 return NULL;
9e353e3b
NIS
3000}
3001
1751d015 3002PerlIO *
ecdeb87c 3003PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
3004{
3005 /* This assumes no layers underneath - which is what
3006 happens, but is not how I remember it. NI-S 2001/10/16
3007 */
ecdeb87c 3008 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 3009 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
de009b76 3010 const int fd = fileno(stdio);
9217ff3f 3011 char mode[8];
ecdeb87c 3012 if (flags & PERLIO_DUP_FD) {
de009b76 3013 const int dfd = PerlLIO_dup(fileno(stdio));
9217ff3f
NIS
3014 if (dfd >= 0) {
3015 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
3016 goto set_this;
ecdeb87c
NIS
3017 }
3018 else {
6f207bd3 3019 NOOP;
ecdeb87c
NIS
3020 /* FIXME: To avoid messy error recovery if dup fails
3021 re-use the existing stdio as though flag was not set
3022 */
3023 }
3024 }
9217ff3f
NIS
3025 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
3026 set_this:
694c95cf
JH
3027 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
3028 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
3029 }
3030 return f;
3031}
3032
0d7a5398
NIS
3033static int
3034PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
3035{
96a5add6
AL
3036 PERL_UNUSED_CONTEXT;
3037
0d7a5398 3038 /* XXX this could use PerlIO_canset_fileno() and
37725cdc 3039 * PerlIO_set_fileno() support from Configure
0d7a5398 3040 */
ef8eacb8
AT
3041# if defined(__UCLIBC__)
3042 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
3043 f->__filedes = -1;
3044 return 1;
3045# elif defined(__GLIBC__)
0d7a5398 3046 /* There may be a better way for GLIBC:
37725cdc 3047 - libio.h defines a flag to not close() on cleanup
0d7a5398
NIS
3048 */
3049 f->_fileno = -1;
3050 return 1;
3051# elif defined(__sun__)
f5992bc4 3052 PERL_UNUSED_ARG(f);
cfedb851 3053 return 0;
0d7a5398
NIS
3054# elif defined(__hpux)
3055 f->__fileH = 0xff;
3056 f->__fileL = 0xff;
3057 return 1;
9837d373 3058 /* Next one ->_file seems to be a reasonable fallback, i.e. if
37725cdc 3059 your platform does not have special entry try this one.
9837d373
NIS
3060 [For OSF only have confirmation for Tru64 (alpha)
3061 but assume other OSFs will be similar.]
37725cdc 3062 */
9837d373 3063# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
0d7a5398
NIS
3064 f->_file = -1;
3065 return 1;
3066# elif defined(__FreeBSD__)
3067 /* There may be a better way on FreeBSD:
37725cdc
NIS
3068 - we could insert a dummy func in the _close function entry
3069 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
3070 */
3071 f->_file = -1;
0c49ea6a
SU
3072 return 1;
3073# elif defined(__OpenBSD__)
3074 /* There may be a better way on OpenBSD:
3075 - we could insert a dummy func in the _close function entry
3076 f->_close = (int (*)(void *)) dummy_close;
3077 */
3078 f->_file = -1;
0d7a5398 3079 return 1;
59ad941d
IZ
3080# elif defined(__EMX__)
3081 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
3082 f->_handle = -1;
3083 return 1;
0d7a5398
NIS
3084# elif defined(__CYGWIN__)
3085 /* There may be a better way on CYGWIN:
37725cdc
NIS
3086 - we could insert a dummy func in the _close function entry
3087 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
3088 */
3089 f->_file = -1;
3090 return 1;
3091# elif defined(WIN32)
3092# if defined(__BORLANDC__)
3093 f->fd = PerlLIO_dup(fileno(f));
b475b3e6
JH
3094# elif defined(UNDER_CE)
3095 /* WIN_CE does not have access to FILE internals, it hardly has FILE
3096 structure at all
3097 */
0d7a5398
NIS
3098# else
3099 f->_file = -1;
3100# endif
3101 return 1;
3102# else
3103#if 0
37725cdc 3104 /* Sarathy's code did this - we fall back to a dup/dup2 hack
0d7a5398 3105 (which isn't thread safe) instead
37725cdc 3106 */
0d7a5398
NIS
3107# error "Don't know how to set FILE.fileno on your platform"
3108#endif
8772537c 3109 PERL_UNUSED_ARG(f);
0d7a5398
NIS
3110 return 0;
3111# endif
3112}
3113
1751d015 3114IV
f62ce20a 3115PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 3116{
c4420975 3117 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
3118 if (!stdio) {
3119 errno = EBADF;
3120 return -1;
3121 }
9217ff3f 3122 else {
de009b76 3123 const int fd = fileno(stdio);
0d7a5398 3124 int invalidate = 0;
bbfd922f 3125 IV result = 0;
0d7a5398
NIS
3126 int saveerr = 0;
3127 int dupfd = 0;
3128#ifdef SOCKS5_VERSION_NAME
37725cdc
NIS
3129 /* Socks lib overrides close() but stdio isn't linked to
3130 that library (though we are) - so we must call close()
3131 on sockets on stdio's behalf.
3132 */
0d7a5398
NIS
3133 int optval;
3134 Sock_size_t optlen = sizeof(int);
6b4ce6c8 3135 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0)
37725cdc 3136 invalidate = 1;
0d7a5398 3137#endif
6b4ce6c8 3138 if (PerlIOUnix_refcnt_dec(fd) > 0) /* File descriptor still in use */
0d7a5398 3139 invalidate = 1;
0d7a5398 3140 if (invalidate) {
6b4ce6c8
AL
3141 /* For STD* handles, don't close stdio, since we shared the FILE *, too. */
3142 if (stdio == stdin) /* Some stdios are buggy fflush-ing inputs */
3143 return 0;
3144 if (stdio == stdout || stdio == stderr)
3145 return PerlIO_flush(f);
37725cdc
NIS
3146 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3147 Use Sarathy's trick from maint-5.6 to invalidate the
3148 fileno slot of the FILE *
3149 */
bbfd922f 3150 result = PerlIO_flush(f);
0d7a5398 3151 saveerr = errno;
6b4ce6c8
AL
3152 invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio);
3153 if (!invalidate)
3154 dupfd = PerlLIO_dup(fd);
37725cdc 3155 }
0d7a5398 3156 result = PerlSIO_fclose(stdio);
37725cdc
NIS
3157 /* We treat error from stdio as success if we invalidated
3158 errno may NOT be expected EBADF
e8529473
NIS
3159 */
3160 if (invalidate && result != 0) {
0d7a5398
NIS
3161 errno = saveerr;
3162 result = 0;
37725cdc 3163 }
6b4ce6c8
AL
3164#ifdef SOCKS5_VERSION_NAME
3165 /* in SOCKS' case, let close() determine return value */
3166 result = close(fd);
3167#endif
0d7a5398
NIS
3168 if (dupfd) {
3169 PerlLIO_dup2(dupfd,fd);
8a521f28 3170 PerlLIO_close(dupfd);
9217ff3f
NIS
3171 }
3172 return result;
37725cdc 3173 }
1751d015
NIS
3174}
3175
9e353e3b 3176SSize_t
f62ce20a 3177PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 3178{
97aff369 3179 dVAR;
c4420975 3180 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3181 SSize_t got = 0;
4d948241
NIS
3182 for (;;) {
3183 if (count == 1) {
3184 STDCHAR *buf = (STDCHAR *) vbuf;
3185 /*
3186 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3187 * stdio does not do that for fread()
3188 */
de009b76 3189 const int ch = PerlSIO_fgetc(s);
4d948241
NIS
3190 if (ch != EOF) {
3191 *buf = ch;
3192 got = 1;
3193 }
14a5cf38 3194 }
4d948241
NIS
3195 else
3196 got = PerlSIO_fread(vbuf, 1, count, s);
b1d8b47a
CS
3197 if (got == 0 && PerlSIO_ferror(s))
3198 got = -1;
42a7a32f 3199 if (got >= 0 || errno != EINTR)
4d948241
NIS
3200 break;
3201 PERL_ASYNC_CHECK();
42a7a32f 3202 SETERRNO(0,0); /* just in case */
14a5cf38 3203 }
14a5cf38 3204 return got;
9e353e3b
NIS
3205}
3206
3207SSize_t
f62ce20a 3208PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3209{
14a5cf38 3210 SSize_t unread = 0;
c4420975 3211 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3212
313e59c8 3213#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3214 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3215 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3216 STDCHAR *base = PerlIO_get_base(f);
3217 SSize_t cnt = PerlIO_get_cnt(f);
3218 STDCHAR *ptr = PerlIO_get_ptr(f);
3219 SSize_t avail = ptr - base;
3220 if (avail > 0) {
3221 if (avail > count) {
3222 avail = count;
3223 }
3224 ptr -= avail;
3225 Move(buf-avail,ptr,avail,STDCHAR);
3226 count -= avail;
3227 unread += avail;
3228 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
3229 if (PerlSIO_feof(s) && unread >= 0)
3230 PerlSIO_clearerr(s);
3231 }
3232 }
313e59c8
NIS
3233 else
3234#endif
3235 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3236 /* We can get pointer to buffer but not its base
3237 Do ungetc() but check chars are ending up in the
3238 buffer
3239 */
3240 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3241 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3242 while (count > 0) {
de009b76 3243 const int ch = *--buf & 0xFF;
9f7cd136
NIS
3244 if (ungetc(ch,s) != ch) {
3245 /* ungetc did not work */
3246 break;
3247 }
3248 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3249 /* Did not change pointer as expected */
3250 fgetc(s); /* get char back again */
3251 break;
3252 }
3253 /* It worked ! */
3254 count--;
3255 unread++;
93679785
NIS
3256 }
3257 }
3258
3259 if (count > 0) {
3260 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3261 }
3262 return unread;
9e353e3b
NIS
3263}
3264
3265SSize_t
f62ce20a 3266PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3267{
97aff369 3268 dVAR;
4d948241
NIS
3269 SSize_t got;
3270 for (;;) {
3271 got = PerlSIO_fwrite(vbuf, 1, count,
3272 PerlIOSelf(f, PerlIOStdio)->stdio);
42a7a32f 3273 if (got >= 0 || errno != EINTR)
4d948241
NIS
3274 break;
3275 PERL_ASYNC_CHECK();
42a7a32f 3276 SETERRNO(0,0); /* just in case */
4d948241
NIS
3277 }
3278 return got;
9e353e3b
NIS
3279}
3280
94a175e1 3281IV
f62ce20a 3282PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3283{
c4420975 3284 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3285 PERL_UNUSED_CONTEXT;
3286
94a175e1 3287 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
3288}
3289
3290Off_t
f62ce20a 3291PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 3292{
c4420975 3293 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3294 PERL_UNUSED_CONTEXT;
3295
94a175e1 3296 return PerlSIO_ftell(stdio);
9e353e3b
NIS
3297}
3298
3299IV
f62ce20a 3300PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 3301{
c4420975 3302 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
96a5add6
AL
3303 PERL_UNUSED_CONTEXT;
3304
14a5cf38
JH
3305 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3306 return PerlSIO_fflush(stdio);
3307 }
3308 else {
6f207bd3 3309 NOOP;
88b61e10 3310#if 0
14a5cf38
JH
3311 /*
3312 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 3313 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 3314 * design is to do _this_ but not have layer above flush this
71200d45 3315 * layer read-to-read
14a5cf38
JH
3316 */
3317 /*
71200d45 3318 * Not writeable - sync by attempting a seek
14a5cf38 3319 */
79852593 3320 const int err = errno;
14a5cf38
JH
3321 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3322 errno = err;
88b61e10 3323#endif
14a5cf38
JH
3324 }
3325 return 0;
9e353e3b
NIS
3326}
3327
3328IV
f62ce20a 3329PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 3330{
96a5add6
AL
3331 PERL_UNUSED_CONTEXT;
3332
14a5cf38 3333 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3334}
3335
3336IV
f62ce20a 3337PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 3338{
96a5add6
AL
3339 PERL_UNUSED_CONTEXT;
3340
263df5f1 3341 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3342}
3343
3344void
f62ce20a 3345PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 3346{
96a5add6
AL
3347 PERL_UNUSED_CONTEXT;
3348
14a5cf38 3349 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3350}
3351
3352void
f62ce20a 3353PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 3354{
96a5add6
AL
3355 PERL_UNUSED_CONTEXT;
3356
9e353e3b 3357#ifdef HAS_SETLINEBUF
14a5cf38 3358 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 3359#else
bd61b366 3360 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, NULL, _IOLBF, 0);
9e353e3b
NIS
3361#endif
3362}
3363
3364#ifdef FILE_base
3365STDCHAR *
f62ce20a 3366PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 3367{
c4420975 3368 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3369 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
3370}
3371
3372Size_t
f62ce20a 3373PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3374{
c4420975 3375 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3376 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
3377}
3378#endif
3379
3380#ifdef USE_STDIO_PTR
3381STDCHAR *
f62ce20a 3382PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3383{
c4420975 3384 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3385 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
3386}
3387
3388SSize_t
f62ce20a 3389PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3390{
c4420975 3391 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3392 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
3393}
3394
3395void
f62ce20a 3396PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3397{
c4420975 3398 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3399 if (ptr != NULL) {
9e353e3b 3400#ifdef STDIO_PTR_LVALUE
d06fc7d4 3401 PerlSIO_set_ptr(stdio, ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 3402#ifdef STDIO_PTR_LVAL_SETS_CNT
f8a4dbc5 3403 assert(PerlSIO_get_cnt(stdio) == (cnt));
9e353e3b
NIS
3404#endif
3405#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 3406 /*
71200d45 3407 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
3408 */
3409 return;
9e353e3b 3410#endif
22569500 3411#else /* STDIO_PTR_LVALUE */
14a5cf38 3412 PerlProc_abort();
22569500 3413#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
3414 }
3415 /*
71200d45 3416 * Now (or only) set cnt
14a5cf38 3417 */
9e353e3b 3418#ifdef STDIO_CNT_LVALUE
14a5cf38 3419 PerlSIO_set_cnt(stdio, cnt);
22569500 3420#else /* STDIO_CNT_LVALUE */
9e353e3b 3421#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
3422 PerlSIO_set_ptr(stdio,
3423 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3424 cnt));
22569500 3425#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 3426 PerlProc_abort();
22569500
NIS
3427#endif /* STDIO_PTR_LVAL_SETS_CNT */
3428#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
3429}
3430
93679785 3431
9e353e3b
NIS
3432#endif
3433
93679785
NIS
3434IV
3435PerlIOStdio_fill(pTHX_ PerlIO *f)
3436{
c4420975 3437 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3438 int c;
96a5add6
AL
3439 PERL_UNUSED_CONTEXT;
3440
93679785
NIS
3441 /*
3442 * fflush()ing read-only streams can cause trouble on some stdio-s
3443 */
3444 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3445 if (PerlSIO_fflush(stdio) != 0)
3446 return EOF;
3447 }
f3be3723
BL
3448 for (;;) {
3449 c = PerlSIO_fgetc(stdio);
3450 if (c != EOF)
3451 break;
3452 if (! PerlSIO_ferror(stdio) || errno != EINTR)
3453 return EOF;
3454 PERL_ASYNC_CHECK();
3455 SETERRNO(0,0);
3456 }
93679785
NIS
3457
3458#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
3459
3460#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3461 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3462 /* Fake ungetc() to the real buffer in case system's ungetc
3463 goes elsewhere
3464 */
3465 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3466 SSize_t cnt = PerlSIO_get_cnt(stdio);
3467 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3468 if (ptr == base+1) {
3469 *--ptr = (STDCHAR) c;
3470 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3471 if (PerlSIO_feof(stdio))
3472 PerlSIO_clearerr(stdio);
3473 return 0;
3474 }
3475 }
313e59c8
NIS
3476 else
3477#endif
3478 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3479 STDCHAR ch = c;
3480 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3481 return 0;
3482 }
3483 }
93679785
NIS
3484#endif
3485
3486#if defined(VMS)
3487 /* An ungetc()d char is handled separately from the regular
3488 * buffer, so we stuff it in the buffer ourselves.
3489 * Should never get called as should hit code above
3490 */
bad9695d
NIS
3491 *(--((*stdio)->_ptr)) = (unsigned char) c;
3492 (*stdio)->_cnt++;
93679785
NIS
3493#else
3494 /* If buffer snoop scheme above fails fall back to
9f7cd136 3495 using ungetc().
93679785
NIS
3496 */
3497 if (PerlSIO_ungetc(c, stdio) != c)
3498 return EOF;
3499#endif
3500 return 0;
3501}
3502
3503
3504
27da23d5 3505PERLIO_FUNCS_DECL(PerlIO_stdio) = {
2dc2558e 3506 sizeof(PerlIO_funcs),
14a5cf38
JH
3507 "stdio",
3508 sizeof(PerlIOStdio),
86e05cf2 3509 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 3510 PerlIOStdio_pushed,
44798d05 3511 PerlIOBase_popped,
14a5cf38 3512 PerlIOStdio_open,
86e05cf2 3513 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3514 NULL,
3515 PerlIOStdio_fileno,
71200d45 3516 PerlIOStdio_dup,
14a5cf38
JH
3517 PerlIOStdio_read,
3518 PerlIOStdio_unread,
3519 PerlIOStdio_write,
3520 PerlIOStdio_seek,
3521 PerlIOStdio_tell,
3522 PerlIOStdio_close,
3523 PerlIOStdio_flush,
3524 PerlIOStdio_fill,
3525 PerlIOStdio_eof,
3526 PerlIOStdio_error,
3527 PerlIOStdio_clearerr,
3528 PerlIOStdio_setlinebuf,
9e353e3b 3529#ifdef FILE_base
14a5cf38
JH
3530 PerlIOStdio_get_base,
3531 PerlIOStdio_get_bufsiz,
9e353e3b 3532#else
14a5cf38
JH
3533 NULL,
3534 NULL,
9e353e3b
NIS
3535#endif
3536#ifdef USE_STDIO_PTR
14a5cf38
JH
3537 PerlIOStdio_get_ptr,
3538 PerlIOStdio_get_cnt,
15b61c98 3539# if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
79ed0f43 3540 PerlIOStdio_set_ptrcnt,
15b61c98
JH
3541# else
3542 NULL,
3543# endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3544#else
3545 NULL,
14a5cf38
JH
3546 NULL,
3547 NULL,
15b61c98 3548#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3549};
3550
b9d6bf13
JH
3551/* Note that calls to PerlIO_exportFILE() are reversed using
3552 * PerlIO_releaseFILE(), not importFILE. */
9e353e3b 3553FILE *
81428673 3554PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3555{
e87a358a 3556 dTHX;
81428673
NIS
3557 FILE *stdio = NULL;
3558 if (PerlIOValid(f)) {
3559 char buf[8];
3560 PerlIO_flush(f);
3561 if (!mode || !*mode) {
3562 mode = PerlIO_modestr(f, buf);
3563 }
3564 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3565 if (stdio) {
3566 PerlIOl *l = *f;
9f75cc58 3567 PerlIO *f2;
81428673
NIS
3568 /* De-link any lower layers so new :stdio sticks */
3569 *f = NULL;
a0714e2c 3570 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) {
9f75cc58 3571 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
81428673 3572 s->stdio = stdio;
6b54a403 3573 PerlIOUnix_refcnt_inc(fileno(stdio));
81428673
NIS
3574 /* Link previous lower layers under new one */
3575 *PerlIONext(f) = l;
3576 }
3577 else {
3578 /* restore layers list */
3579 *f = l;
3580 }
a33cf58c 3581 }
14a5cf38
JH
3582 }
3583 return stdio;
9e353e3b
NIS
3584}
3585
81428673 3586
9e353e3b
NIS
3587FILE *
3588PerlIO_findFILE(PerlIO *f)
3589{
14a5cf38 3590 PerlIOl *l = *f;
bbbc33d0 3591 FILE *stdio;
14a5cf38
JH
3592 while (l) {
3593 if (l->tab == &PerlIO_stdio) {
3594 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3595 return s->stdio;
3596 }
3597 l = *PerlIONext(&l);
f7e7eb72 3598 }
4b069b44 3599 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
bbbc33d0
NC
3600 /* However, we're not really exporting a FILE * to someone else (who
3601 becomes responsible for closing it, or calling PerlIO_releaseFILE())
3602 So we need to undo its refernce count increase on the underlying file
3603 descriptor. We have to do this, because if the loop above returns you
3604 the FILE *, then *it* didn't increase any reference count. So there's
3605 only one way to be consistent. */
3606 stdio = PerlIO_exportFILE(f, NULL);
3607 if (stdio) {
3608 const int fd = fileno(stdio);
3609 if (fd >= 0)
3610 PerlIOUnix_refcnt_dec(fd);
3611 }
3612 return stdio;
9e353e3b
NIS
3613}
3614
b9d6bf13 3615/* Use this to reverse PerlIO_exportFILE calls. */
9e353e3b
NIS
3616void
3617PerlIO_releaseFILE(PerlIO *p, FILE *f)
3618{
27da23d5 3619 dVAR;
22569500
NIS
3620 PerlIOl *l;
3621 while ((l = *p)) {
3622 if (l->tab == &PerlIO_stdio) {
3623 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3624 if (s->stdio == f) {
3625 dTHX;
6b54a403
NC
3626 const int fd = fileno(f);
3627 if (fd >= 0)
3628 PerlIOUnix_refcnt_dec(fd);
22569500
NIS
3629 PerlIO_pop(aTHX_ p);
3630 return;
3631 }
3632 }
3633 p = PerlIONext(p);
3634 }
3635 return;
9e353e3b
NIS
3636}
3637
3638/*--------------------------------------------------------------------------------------*/
14a5cf38 3639/*
71200d45 3640 * perlio buffer layer
14a5cf38 3641 */
9e353e3b 3642
5e2ab84b 3643IV
2dc2558e 3644PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3645{
14a5cf38 3646 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
de009b76 3647 const int fd = PerlIO_fileno(f);
14a5cf38
JH
3648 if (fd >= 0 && PerlLIO_isatty(fd)) {
3649 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3650 }
4b069b44 3651 if (*PerlIONext(f)) {
de009b76 3652 const Off_t posn = PerlIO_tell(PerlIONext(f));
4b069b44
NIS
3653 if (posn != (Off_t) - 1) {
3654 b->posn = posn;
3655 }
14a5cf38 3656 }
2dc2558e 3657 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3658}
3659
9e353e3b 3660PerlIO *
14a5cf38
JH
3661PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3662 IV n, const char *mode, int fd, int imode, int perm,
3663 PerlIO *f, int narg, SV **args)
3664{
04892f78 3665 if (PerlIOValid(f)) {
14a5cf38 3666 PerlIO *next = PerlIONext(f);
67363c0d
JH
3667 PerlIO_funcs *tab =
3668 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3669 if (tab && tab->Open)
3670 next =
3671 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3672 next, narg, args);
2dc2558e 3673 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3674 return NULL;
3675 }
3676 }
3677 else {
04892f78 3678 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38 3679 int init = 0;
3b6c1aba 3680 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
3681 init = 1;
3682 /*
71200d45 3683 * mode++;
14a5cf38
JH
3684 */
3685 }
67363c0d
JH
3686 if (tab && tab->Open)
3687 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3688 f, narg, args);
3689 else
3690 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 3691 if (f) {
22569500 3692 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
b26b1ab5
NC
3693 /*
3694 * if push fails during open, open fails. close will pop us.
3695 */
3696 PerlIO_close (f);
3697 return NULL;
3698 } else {
3699 fd = PerlIO_fileno(f);
b26b1ab5
NC
3700 if (init && fd == 2) {
3701 /*
3702 * Initial stderr is unbuffered
3703 */
3704 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3705 }
23b84778
IZ
3706#ifdef PERLIO_USING_CRLF
3707# ifdef PERLIO_IS_BINMODE_FD
3708 if (PERLIO_IS_BINMODE_FD(fd))
bd61b366 3709 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, NULL);
23b84778
IZ
3710 else
3711# endif
3712 /*
3713 * do something about failing setmode()? --jhi
3714 */
3715 PerlLIO_setmode(fd, O_BINARY);
3716#endif
14a5cf38
JH
3717 }
3718 }
ee518936 3719 }
14a5cf38 3720 return f;
9e353e3b
NIS
3721}
3722
14a5cf38
JH
3723/*
3724 * This "flush" is akin to sfio's sync in that it handles files in either
93c2c2ec
IZ
3725 * read or write state. For write state, we put the postponed data through
3726 * the next layers. For read state, we seek() the next layers to the
3727 * offset given by current position in the buffer, and discard the buffer
3728 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3729 * in any case?). Then the pass the stick further in chain.
14a5cf38 3730 */
9e353e3b 3731IV
f62ce20a 3732PerlIOBuf_flush(pTHX_ PerlIO *f)
6f9d8c32 3733{
dcda55fc 3734 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 3735 int code = 0;
04892f78 3736 PerlIO *n = PerlIONext(f);
14a5cf38
JH
3737 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3738 /*
71200d45 3739 * write() the buffer
14a5cf38 3740 */
de009b76
AL
3741 const STDCHAR *buf = b->buf;
3742 const STDCHAR *p = buf;
14a5cf38
JH
3743 while (p < b->ptr) {
3744 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3745 if (count > 0) {
3746 p += count;
3747 }
3748 else if (count < 0 || PerlIO_error(n)) {
3749 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3750 code = -1;
3751 break;
3752 }
3753 }
3754 b->posn += (p - buf);
3755 }
3756 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3757 STDCHAR *buf = PerlIO_get_base(f);
3758 /*
71200d45 3759 * Note position change
14a5cf38
JH
3760 */
3761 b->posn += (b->ptr - buf);
3762 if (b->ptr < b->end) {
4b069b44
NIS
3763 /* We did not consume all of it - try and seek downstream to
3764 our logical position
14a5cf38 3765 */
4b069b44 3766 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
04892f78
NIS
3767 /* Reload n as some layers may pop themselves on seek */
3768 b->posn = PerlIO_tell(n = PerlIONext(f));
14a5cf38 3769 }
ba5c3fe9 3770 else {
4b069b44
NIS
3771 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3772 data is lost for good - so return saying "ok" having undone
3773 the position adjust
3774 */
3775 b->posn -= (b->ptr - buf);
ba5c3fe9
NIS
3776 return code;
3777 }
14a5cf38
JH
3778 }
3779 }
3780 b->ptr = b->end = b->buf;
3781 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
04892f78 3782 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
04892f78 3783 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
14a5cf38
JH
3784 code = -1;
3785 return code;
6f9d8c32
NIS
3786}
3787
93c2c2ec
IZ
3788/* This discards the content of the buffer after b->ptr, and rereads
3789 * the buffer from the position off in the layer downstream; here off
3790 * is at offset corresponding to b->ptr - b->buf.
3791 */
06da4f11 3792IV
f62ce20a 3793PerlIOBuf_fill(pTHX_ PerlIO *f)
06da4f11 3794{
dcda55fc 3795 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
3796 PerlIO *n = PerlIONext(f);
3797 SSize_t avail;
3798 /*
4b069b44
NIS
3799 * Down-stream flush is defined not to loose read data so is harmless.
3800 * we would not normally be fill'ing if there was data left in anycase.
14a5cf38 3801 */
93c2c2ec 3802 if (PerlIO_flush(f) != 0) /* XXXX Check that its seek() succeeded?! */
14a5cf38
JH
3803 return -1;
3804 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
f62ce20a 3805 PerlIOBase_flush_linebuf(aTHX);
14a5cf38
JH
3806
3807 if (!b->buf)
22569500 3808 PerlIO_get_base(f); /* allocate via vtable */
14a5cf38 3809
0f0eef27 3810 assert(b->buf); /* The b->buf does get allocated via the vtable system. */
ec6fa4f0 3811
14a5cf38 3812 b->ptr = b->end = b->buf;
4b069b44
NIS
3813
3814 if (!PerlIOValid(n)) {
3815 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3816 return -1;
3817 }
3818
14a5cf38
JH
3819 if (PerlIO_fast_gets(n)) {
3820 /*
04892f78 3821 * Layer below is also buffered. We do _NOT_ want to call its
14a5cf38
JH
3822 * ->Read() because that will loop till it gets what we asked for
3823 * which may hang on a pipe etc. Instead take anything it has to
71200d45 3824 * hand, or ask it to fill _once_.
14a5cf38
JH
3825 */
3826 avail = PerlIO_get_cnt(n);
3827 if (avail <= 0) {
3828 avail = PerlIO_fill(n);
3829 if (avail == 0)
3830 avail = PerlIO_get_cnt(n);
3831 else {
3832 if (!PerlIO_error(n) && PerlIO_eof(n))
3833 avail = 0;
3834 }
3835 }
3836 if (avail > 0) {
3837 STDCHAR *ptr = PerlIO_get_ptr(n);
dcda55fc 3838 const SSize_t cnt = avail;
eb160463 3839 if (avail > (SSize_t)b->bufsiz)
14a5cf38
JH
3840 avail = b->bufsiz;
3841 Copy(ptr, b->buf, avail, STDCHAR);
3842 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3843 }
3844 }
3845 else {
3846 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3847 }
3848 if (avail <= 0) {
3849 if (avail == 0)
3850 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3851 else
3852 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3853 return -1;
3854 }
3855 b->end = b->buf + avail;
3856 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3857 return 0;
06da4f11
NIS
3858}
3859
6f9d8c32 3860SSize_t
f62ce20a 3861PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 3862{
04892f78 3863 if (PerlIOValid(f)) {
dcda55fc 3864 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
3865 if (!b->ptr)
3866 PerlIO_get_base(f);
f62ce20a 3867 return PerlIOBase_read(aTHX_ f, vbuf, count);
14a5cf38
JH
3868 }
3869 return 0;
6f9d8c32
NIS
3870}
3871
9e353e3b 3872SSize_t
f62ce20a 3873PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3874{
14a5cf38 3875 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
dcda55fc 3876 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
3877 SSize_t unread = 0;
3878 SSize_t avail;
3879 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3880 PerlIO_flush(f);
3881 if (!b->buf)
3882 PerlIO_get_base(f);
3883 if (b->buf) {
3884 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3885 /*
3886 * Buffer is already a read buffer, we can overwrite any chars
71200d45 3887 * which have been read back to buffer start
14a5cf38
JH
3888 */
3889 avail = (b->ptr - b->buf);
3890 }
3891 else {
3892 /*
3893 * Buffer is idle, set it up so whole buffer is available for
71200d45 3894 * unread
14a5cf38
JH
3895 */
3896 avail = b->bufsiz;
3897 b->end = b->buf + avail;
3898 b->ptr = b->end;
3899 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3900 /*
71200d45 3901 * Buffer extends _back_ from where we are now
14a5cf38
JH
3902 */
3903 b->posn -= b->bufsiz;
3904 }
3905 if (avail > (SSize_t) count) {
3906 /*
71200d45 3907 * If we have space for more than count, just move count
14a5cf38
JH
3908 */
3909 avail = count;
3910 }
3911 if (avail > 0) {
3912 b->ptr -= avail;
3913 buf -= avail;
3914 /*
3915 * In simple stdio-like ungetc() case chars will be already
71200d45 3916 * there
14a5cf38
JH
3917 */
3918 if (buf != b->ptr) {
3919 Copy(buf, b->ptr, avail, STDCHAR);
3920 }
3921 count -= avail;
3922 unread += avail;
3923 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3924 }
3925 }
93679785
NIS
3926 if (count > 0) {
3927 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3928 }
14a5cf38 3929 return unread;
760ac839
LW
3930}
3931
9e353e3b 3932SSize_t
f62ce20a 3933PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3934{
de009b76 3935 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 3936 const STDCHAR *buf = (const STDCHAR *) vbuf;
ee56a6b9 3937 const STDCHAR *flushptr = buf;
14a5cf38
JH
3938 Size_t written = 0;
3939 if (!b->buf)
3940 PerlIO_get_base(f);
3941 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3942 return 0;
0678cb22
NIS
3943 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3944 if (PerlIO_flush(f) != 0) {
3945 return 0;
3946 }
3947 }
ee56a6b9
CS
3948 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3949 flushptr = buf + count;
3950 while (flushptr > buf && *(flushptr - 1) != '\n')
3951 --flushptr;
3952 }
14a5cf38
JH
3953 while (count > 0) {
3954 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3955 if ((SSize_t) count < avail)
3956 avail = count;
ee56a6b9
CS
3957 if (flushptr > buf && flushptr <= buf + avail)
3958 avail = flushptr - buf;
14a5cf38 3959 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
ee56a6b9
CS
3960 if (avail) {
3961 Copy(buf, b->ptr, avail, STDCHAR);
3962 count -= avail;
3963 buf += avail;
3964 written += avail;
3965 b->ptr += avail;
3966 if (buf == flushptr)
3967 PerlIO_flush(f);
14a5cf38
JH
3968 }
3969 if (b->ptr >= (b->buf + b->bufsiz))
3970 PerlIO_flush(f);
3971 }
3972 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3973 PerlIO_flush(f);
3974 return written;
9e353e3b
NIS
3975}
3976
94a175e1 3977IV
f62ce20a 3978PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3979{
14a5cf38
JH
3980 IV code;
3981 if ((code = PerlIO_flush(f)) == 0) {
14a5cf38
JH
3982 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3983 code = PerlIO_seek(PerlIONext(f), offset, whence);
3984 if (code == 0) {
de009b76 3985 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
3986 b->posn = PerlIO_tell(PerlIONext(f));
3987 }
9e353e3b 3988 }
14a5cf38 3989 return code;
9e353e3b
NIS
3990}
3991
3992Off_t
f62ce20a 3993PerlIOBuf_tell(pTHX_ PerlIO *f)
9e353e3b 3994{
dcda55fc 3995 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 3996 /*
71200d45 3997 * b->posn is file position where b->buf was read, or will be written
14a5cf38
JH
3998 */
3999 Off_t posn = b->posn;
37725cdc 4000 if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) &&
0678cb22
NIS
4001 (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4002#if 1
4003 /* As O_APPEND files are normally shared in some sense it is better
4004 to flush :
4005 */
4006 PerlIO_flush(f);
4007#else
37725cdc 4008 /* when file is NOT shared then this is sufficient */
0678cb22
NIS
4009 PerlIO_seek(PerlIONext(f),0, SEEK_END);
4010#endif
4011 posn = b->posn = PerlIO_tell(PerlIONext(f));
4012 }
14a5cf38
JH
4013 if (b->buf) {
4014 /*
71200d45 4015 * If buffer is valid adjust position by amount in buffer
14a5cf38
JH
4016 */
4017 posn += (b->ptr - b->buf);
4018 }
4019 return posn;
9e353e3b
NIS
4020}
4021
4022IV
44798d05
NIS
4023PerlIOBuf_popped(pTHX_ PerlIO *f)
4024{
de009b76
AL
4025 const IV code = PerlIOBase_popped(aTHX_ f);
4026 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
44798d05
NIS
4027 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
4028 Safefree(b->buf);
4029 }
dcda55fc 4030 b->ptr = b->end = b->buf = NULL;
44798d05
NIS
4031 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4032 return code;
4033}
4034
4035IV
f62ce20a 4036PerlIOBuf_close(pTHX_ PerlIO *f)
9e353e3b 4037{
de009b76
AL
4038 const IV code = PerlIOBase_close(aTHX_ f);
4039 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 4040 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 4041 Safefree(b->buf);
14a5cf38 4042 }
dcda55fc 4043 b->ptr = b->end = b->buf = NULL;
14a5cf38
JH
4044 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4045 return code;
760ac839
LW
4046}
4047
9e353e3b 4048STDCHAR *
f62ce20a 4049PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
9e353e3b 4050{
dcda55fc 4051 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4052 if (!b->buf)
4053 PerlIO_get_base(f);
4054 return b->ptr;
9e353e3b
NIS
4055}
4056
05d1247b 4057SSize_t
f62ce20a 4058PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
9e353e3b 4059{
dcda55fc 4060 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4061 if (!b->buf)
4062 PerlIO_get_base(f);
4063 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
4064 return (b->end - b->ptr);
4065 return 0;
9e353e3b
NIS
4066}
4067
4068STDCHAR *
f62ce20a 4069PerlIOBuf_get_base(pTHX_ PerlIO *f)
9e353e3b 4070{
dcda55fc 4071 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
96a5add6
AL
4072 PERL_UNUSED_CONTEXT;
4073
14a5cf38
JH
4074 if (!b->buf) {
4075 if (!b->bufsiz)
4076 b->bufsiz = 4096;
a02a5408 4077 b->buf = Newxz(b->buf,b->bufsiz, STDCHAR);
14a5cf38
JH
4078 if (!b->buf) {
4079 b->buf = (STDCHAR *) & b->oneword;
4080 b->bufsiz = sizeof(b->oneword);
4081 }
dcda55fc 4082 b->end = b->ptr = b->buf;
06da4f11 4083 }
14a5cf38 4084 return b->buf;
9e353e3b
NIS
4085}
4086
4087Size_t
f62ce20a 4088PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
9e353e3b 4089{
dcda55fc 4090 const PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4091 if (!b->buf)
4092 PerlIO_get_base(f);
4093 return (b->end - b->buf);
9e353e3b
NIS
4094}
4095
4096void
f62ce20a 4097PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 4098{
dcda55fc 4099 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4100 if (!b->buf)
4101 PerlIO_get_base(f);
4102 b->ptr = ptr;
b727803b
RGS
4103 assert(PerlIO_get_cnt(f) == cnt);
4104 assert(b->ptr >= b->buf);
14a5cf38 4105 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
760ac839
LW
4106}
4107
71200d45 4108PerlIO *
ecdeb87c 4109PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 4110{
ecdeb87c 4111 return PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
4112}
4113
4114
4115
27da23d5 4116PERLIO_FUNCS_DECL(PerlIO_perlio) = {
2dc2558e 4117 sizeof(PerlIO_funcs),
14a5cf38
JH
4118 "perlio",
4119 sizeof(PerlIOBuf),
86e05cf2 4120 PERLIO_K_BUFFERED|PERLIO_K_RAW,
14a5cf38 4121 PerlIOBuf_pushed,
44798d05 4122 PerlIOBuf_popped,
14a5cf38 4123 PerlIOBuf_open,
86e05cf2 4124 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
4125 NULL,
4126 PerlIOBase_fileno,
71200d45 4127 PerlIOBuf_dup,
14a5cf38
JH
4128 PerlIOBuf_read,
4129 PerlIOBuf_unread,
4130 PerlIOBuf_write,
4131 PerlIOBuf_seek,
4132 PerlIOBuf_tell,
4133 PerlIOBuf_close,
4134 PerlIOBuf_flush,
4135 PerlIOBuf_fill,
4136 PerlIOBase_eof,
4137 PerlIOBase_error,
4138 PerlIOBase_clearerr,
4139 PerlIOBase_setlinebuf,
4140 PerlIOBuf_get_base,
4141 PerlIOBuf_bufsiz,
4142 PerlIOBuf_get_ptr,
4143 PerlIOBuf_get_cnt,
4144 PerlIOBuf_set_ptrcnt,
9e353e3b
NIS
4145};
4146
66ecd56b 4147/*--------------------------------------------------------------------------------------*/
14a5cf38 4148/*
71200d45 4149 * Temp layer to hold unread chars when cannot do it any other way
14a5cf38 4150 */
5e2ab84b
NIS
4151
4152IV
f62ce20a 4153PerlIOPending_fill(pTHX_ PerlIO *f)
5e2ab84b 4154{
14a5cf38 4155 /*
71200d45 4156 * Should never happen
14a5cf38
JH
4157 */
4158 PerlIO_flush(f);
4159 return 0;
5e2ab84b
NIS
4160}
4161
4162IV
f62ce20a 4163PerlIOPending_close(pTHX_ PerlIO *f)
5e2ab84b 4164{
14a5cf38 4165 /*
71200d45 4166 * A tad tricky - flush pops us, then we close new top
14a5cf38
JH
4167 */
4168 PerlIO_flush(f);
4169 return PerlIO_close(f);
5e2ab84b
NIS
4170}
4171
94a175e1 4172IV
f62ce20a 4173PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
5e2ab84b 4174{
14a5cf38 4175 /*
71200d45 4176 * A tad tricky - flush pops us, then we seek new top
14a5cf38
JH
4177 */
4178 PerlIO_flush(f);
4179 return PerlIO_seek(f, offset, whence);
5e2ab84b
NIS
4180}
4181
4182
4183IV
f62ce20a 4184PerlIOPending_flush(pTHX_ PerlIO *f)
5e2ab84b 4185{
dcda55fc 4186 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 4187 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 4188 Safefree(b->buf);
14a5cf38
JH
4189 b->buf = NULL;
4190 }
4191 PerlIO_pop(aTHX_ f);
4192 return 0;
5e2ab84b
NIS
4193}
4194
4195void
f62ce20a 4196PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
5e2ab84b 4197{
14a5cf38
JH
4198 if (cnt <= 0) {
4199 PerlIO_flush(f);
4200 }
4201 else {
f62ce20a 4202 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
14a5cf38 4203 }
5e2ab84b
NIS
4204}
4205
4206IV
2dc2558e 4207PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 4208{
de009b76 4209 const IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
dcda55fc 4210 PerlIOl * const l = PerlIOBase(f);
14a5cf38 4211 /*
71200d45
NIS
4212 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
4213 * etc. get muddled when it changes mid-string when we auto-pop.
14a5cf38
JH
4214 */
4215 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
4216 (PerlIOBase(PerlIONext(f))->
4217 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
4218 return code;
5e2ab84b
NIS
4219}
4220
4221SSize_t
f62ce20a 4222PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
5e2ab84b 4223{
14a5cf38
JH
4224 SSize_t avail = PerlIO_get_cnt(f);
4225 SSize_t got = 0;
eb160463 4226 if ((SSize_t)count < avail)
14a5cf38
JH
4227 avail = count;
4228 if (avail > 0)
f62ce20a 4229 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
eb160463 4230 if (got >= 0 && got < (SSize_t)count) {
de009b76 4231 const SSize_t more =
14a5cf38
JH
4232 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
4233 if (more >= 0 || got == 0)
4234 got += more;
4235 }
4236 return got;
5e2ab84b
NIS
4237}
4238
27da23d5 4239PERLIO_FUNCS_DECL(PerlIO_pending) = {
2dc2558e 4240 sizeof(PerlIO_funcs),
14a5cf38
JH
4241 "pending",
4242 sizeof(PerlIOBuf),
86e05cf2 4243 PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */
14a5cf38 4244 PerlIOPending_pushed,
44798d05 4245 PerlIOBuf_popped,
14a5cf38 4246 NULL,
86e05cf2 4247 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
4248 NULL,
4249 PerlIOBase_fileno,
71200d45 4250 PerlIOBuf_dup,
14a5cf38
JH
4251 PerlIOPending_read,
4252 PerlIOBuf_unread,
4253 PerlIOBuf_write,
4254 PerlIOPending_seek,
4255 PerlIOBuf_tell,
4256 PerlIOPending_close,
4257 PerlIOPending_flush,
4258 PerlIOPending_fill,
4259 PerlIOBase_eof,
4260 PerlIOBase_error,
4261 PerlIOBase_clearerr,
4262 PerlIOBase_setlinebuf,
4263 PerlIOBuf_get_base,
4264 PerlIOBuf_bufsiz,
4265 PerlIOBuf_get_ptr,
4266 PerlIOBuf_get_cnt,
4267 PerlIOPending_set_ptrcnt,
5e2ab84b
NIS
4268};
4269
4270
4271
4272/*--------------------------------------------------------------------------------------*/
14a5cf38
JH
4273/*
4274 * crlf - translation On read translate CR,LF to "\n" we do this by
4275 * overriding ptr/cnt entries to hand back a line at a time and keeping a
71200d45 4276 * record of which nl we "lied" about. On write translate "\n" to CR,LF
93c2c2ec
IZ
4277 *
4278 * c->nl points on the first byte of CR LF pair when it is temporarily
4279 * replaced by LF, or to the last CR of the buffer. In the former case
4280 * the caller thinks that the buffer ends at c->nl + 1, in the latter
4281 * that it ends at c->nl; these two cases can be distinguished by
4282 * *c->nl. c->nl is set during _getcnt() call, and unset during
4283 * _unread() and _flush() calls.
4284 * It only matters for read operations.
66ecd56b
NIS
4285 */
4286
14a5cf38 4287typedef struct {
22569500
NIS
4288 PerlIOBuf base; /* PerlIOBuf stuff */
4289 STDCHAR *nl; /* Position of crlf we "lied" about in the
14a5cf38 4290 * buffer */
99efab12
NIS
4291} PerlIOCrlf;
4292
ff1e3883
JD
4293/* Inherit the PERLIO_F_UTF8 flag from previous layer.
4294 * Otherwise the :crlf layer would always revert back to
4295 * raw mode.
4296 */
4297static void
4298S_inherit_utf8_flag(PerlIO *f)
4299{
4300 PerlIO *g = PerlIONext(f);
4301 if (PerlIOValid(g)) {
4302 if (PerlIOBase(g)->flags & PERLIO_F_UTF8) {
4303 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
4304 }
4305 }
4306}
4307
f5b9d040 4308IV
2dc2558e 4309PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
f5b9d040 4310{
14a5cf38
JH
4311 IV code;
4312 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
2dc2558e 4313 code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b 4314#if 0
14a5cf38 4315 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
6c9570dc 4316 (void*)f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
14a5cf38 4317 PerlIOBase(f)->flags);
5e2ab84b 4318#endif
8229d19f
JH
4319 {
4320 /* Enable the first CRLF capable layer you can find, but if none
4321 * found, the one we just pushed is fine. This results in at
4322 * any given moment at most one CRLF-capable layer being enabled
4323 * in the whole layer stack. */
4324 PerlIO *g = PerlIONext(f);
ff1e3883 4325 while (PerlIOValid(g)) {
8229d19f
JH
4326 PerlIOl *b = PerlIOBase(g);
4327 if (b && b->tab == &PerlIO_crlf) {
4328 if (!(b->flags & PERLIO_F_CRLF))
4329 b->flags |= PERLIO_F_CRLF;
ff1e3883 4330 S_inherit_utf8_flag(g);
8229d19f
JH
4331 PerlIO_pop(aTHX_ f);
4332 return code;
4333 }
4334 g = PerlIONext(g);
4335 }
4336 }
ff1e3883 4337 S_inherit_utf8_flag(f);
14a5cf38 4338 return code;
f5b9d040
NIS
4339}
4340
4341
99efab12 4342SSize_t
f62ce20a 4343PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
99efab12 4344{
dcda55fc 4345 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
93c2c2ec 4346 if (c->nl) { /* XXXX Shouldn't it be done only if b->ptr > c->nl? */
14a5cf38
JH
4347 *(c->nl) = 0xd;
4348 c->nl = NULL;
4349 }
4350 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
f62ce20a 4351 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
4352 else {
4353 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
4354 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
4355 SSize_t unread = 0;
4356 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4357 PerlIO_flush(f);
4358 if (!b->buf)
4359 PerlIO_get_base(f);
4360 if (b->buf) {
4361 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4362 b->end = b->ptr = b->buf + b->bufsiz;
4363 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
4364 b->posn -= b->bufsiz;
4365 }
4366 while (count > 0 && b->ptr > b->buf) {
dcda55fc 4367 const int ch = *--buf;
14a5cf38
JH
4368 if (ch == '\n') {
4369 if (b->ptr - 2 >= b->buf) {
4370 *--(b->ptr) = 0xa;
4371 *--(b->ptr) = 0xd;
4372 unread++;
4373 count--;
4374 }
4375 else {
93c2c2ec
IZ
4376 /* If b->ptr - 1 == b->buf, we are undoing reading 0xa */
4377 *--(b->ptr) = 0xa; /* Works even if 0xa == '\r' */
4378 unread++;
4379 count--;
14a5cf38
JH
4380 }
4381 }
4382 else {
4383 *--(b->ptr) = ch;
4384 unread++;
4385 count--;
4386 }
4387 }
4388 }
4389 return unread;
4390 }
99efab12
NIS
4391}
4392
93c2c2ec 4393/* XXXX This code assumes that buffer size >=2, but does not check it... */
99efab12 4394SSize_t
f62ce20a 4395PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
99efab12 4396{
dcda55fc 4397 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4398 if (!b->buf)
4399 PerlIO_get_base(f);
4400 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
dcda55fc 4401 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
23b3c6af
NIS
4402 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) {
4403 STDCHAR *nl = (c->nl) ? c->nl : b->ptr;
14a5cf38
JH
4404 scan:
4405 while (nl < b->end && *nl != 0xd)
4406 nl++;
4407 if (nl < b->end && *nl == 0xd) {
4408 test:
4409 if (nl + 1 < b->end) {
4410 if (nl[1] == 0xa) {
4411 *nl = '\n';
4412 c->nl = nl;
4413 }
4414 else {
4415 /*
71200d45 4416 * Not CR,LF but just CR
14a5cf38
JH
4417 */
4418 nl++;
4419 goto scan;
4420 }
4421 }
4422 else {
4423 /*
71200d45 4424 * Blast - found CR as last char in buffer
14a5cf38 4425 */
e87a358a 4426
14a5cf38
JH
4427 if (b->ptr < nl) {
4428 /*
4429 * They may not care, defer work as long as
71200d45 4430 * possible
14a5cf38 4431 */
a0d1d361 4432 c->nl = nl;
14a5cf38
JH
4433 return (nl - b->ptr);
4434 }
4435 else {
4436 int code;
22569500 4437 b->ptr++; /* say we have read it as far as
14a5cf38 4438 * flush() is concerned */
22569500 4439 b->buf++; /* Leave space in front of buffer */
e949e37c
NIS
4440 /* Note as we have moved buf up flush's
4441 posn += ptr-buf
4442 will naturally make posn point at CR
4443 */
22569500
NIS
4444 b->bufsiz--; /* Buffer is thus smaller */
4445 code = PerlIO_fill(f); /* Fetch some more */
4446 b->bufsiz++; /* Restore size for next time */
4447 b->buf--; /* Point at space */
4448 b->ptr = nl = b->buf; /* Which is what we hand
14a5cf38 4449 * off */
22569500 4450 *nl = 0xd; /* Fill in the CR */
14a5cf38 4451 if (code == 0)
22569500 4452 goto test; /* fill() call worked */
14a5cf38 4453 /*
71200d45 4454 * CR at EOF - just fall through
14a5cf38 4455 */
a0d1d361 4456 /* Should we clear EOF though ??? */
14a5cf38
JH
4457 }
4458 }
4459 }
4460 }
4461 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
4462 }
4463 return 0;
99efab12
NIS
4464}
4465
4466void
f62ce20a 4467PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
14a5cf38 4468{
dcda55fc
AL
4469 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
4470 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
14a5cf38
JH
4471 if (!b->buf)
4472 PerlIO_get_base(f);
4473 if (!ptr) {
a0d1d361 4474 if (c->nl) {
14a5cf38 4475 ptr = c->nl + 1;
22569500 4476 if (ptr == b->end && *c->nl == 0xd) {
a0d1d361 4477 /* Defered CR at end of buffer case - we lied about count */
22569500
NIS
4478 ptr--;
4479 }
4480 }
14a5cf38
JH
4481 else {
4482 ptr = b->end;
14a5cf38
JH
4483 }
4484 ptr -= cnt;
4485 }
4486 else {
6f207bd3 4487 NOOP;
3b4bd3fd 4488#if 0
14a5cf38 4489 /*
71200d45 4490 * Test code - delete when it works ...
14a5cf38 4491 */
3b4bd3fd 4492 IV flags = PerlIOBase(f)->flags;
ba7abf9d 4493 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
22569500 4494 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
a0d1d361
NIS
4495 /* Defered CR at end of buffer case - we lied about count */
4496 chk--;
22569500 4497 }
14a5cf38
JH
4498 chk -= cnt;
4499
a0d1d361 4500 if (ptr != chk ) {
99ef548b 4501 Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
6c9570dc
MHM
4502 " nl=%p e=%p for %d", (void*)ptr, (void*)chk,
4503 flags, c->nl, b->end, cnt);
14a5cf38 4504 }
99ef548b 4505#endif
14a5cf38
JH
4506 }
4507 if (c->nl) {
4508 if (ptr > c->nl) {
4509 /*
71200d45 4510 * They have taken what we lied about
14a5cf38
JH
4511 */
4512 *(c->nl) = 0xd;
4513 c->nl = NULL;
4514 ptr++;
4515 }
4516 }
4517 b->ptr = ptr;
4518 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
99efab12
NIS
4519}
4520
4521SSize_t
f62ce20a 4522PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
99efab12 4523{
14a5cf38 4524 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
f62ce20a 4525 return PerlIOBuf_write(aTHX_ f, vbuf, count);
14a5cf38 4526 else {
dcda55fc 4527 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 4528 const STDCHAR *buf = (const STDCHAR *) vbuf;
dcda55fc 4529 const STDCHAR * const ebuf = buf + count;
14a5cf38
JH
4530 if (!b->buf)
4531 PerlIO_get_base(f);
4532 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
4533 return 0;
4534 while (buf < ebuf) {
dcda55fc 4535 const STDCHAR * const eptr = b->buf + b->bufsiz;
14a5cf38
JH
4536 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
4537 while (buf < ebuf && b->ptr < eptr) {
4538 if (*buf == '\n') {
4539 if ((b->ptr + 2) > eptr) {
4540 /*
71200d45 4541 * Not room for both
14a5cf38
JH
4542 */
4543 PerlIO_flush(f);
4544 break;
4545 }
4546 else {
22569500
NIS
4547 *(b->ptr)++ = 0xd; /* CR */
4548 *(b->ptr)++ = 0xa; /* LF */
14a5cf38
JH
4549 buf++;
4550 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
4551 PerlIO_flush(f);
4552 break;
4553 }
4554 }
4555 }
4556 else {
dcda55fc 4557 *(b->ptr)++ = *buf++;
14a5cf38
JH
4558 }
4559 if (b->ptr >= eptr) {
4560 PerlIO_flush(f);
4561 break;
4562 }
4563 }
4564 }
4565 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
4566 PerlIO_flush(f);
4567 return (buf - (STDCHAR *) vbuf);
4568 }
99efab12
NIS
4569}
4570
4571IV
f62ce20a 4572PerlIOCrlf_flush(pTHX_ PerlIO *f)
99efab12 4573{
dcda55fc 4574 PerlIOCrlf * const c = PerlIOSelf(f, PerlIOCrlf);
14a5cf38
JH
4575 if (c->nl) {
4576 *(c->nl) = 0xd;
4577 c->nl = NULL;
4578 }
f62ce20a 4579 return PerlIOBuf_flush(aTHX_ f);
99efab12
NIS
4580}
4581
86e05cf2
NIS
4582IV
4583PerlIOCrlf_binmode(pTHX_ PerlIO *f)
4584{
4585 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
4586 /* In text mode - flush any pending stuff and flip it */
4587 PerlIOBase(f)->flags &= ~PERLIO_F_CRLF;
4588#ifndef PERLIO_USING_CRLF
4589 /* CRLF is unusual case - if this is just the :crlf layer pop it */
4590 if (PerlIOBase(f)->tab == &PerlIO_crlf) {
4591 PerlIO_pop(aTHX_ f);
4592 }
4593#endif
4594 }
4595 return 0;
4596}
4597
27da23d5 4598PERLIO_FUNCS_DECL(PerlIO_crlf) = {
2dc2558e 4599 sizeof(PerlIO_funcs),
14a5cf38
JH
4600 "crlf",
4601 sizeof(PerlIOCrlf),
86e05cf2 4602 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW,
14a5cf38 4603 PerlIOCrlf_pushed,
44798d05 4604 PerlIOBuf_popped, /* popped */
14a5cf38 4605 PerlIOBuf_open,
86e05cf2 4606 PerlIOCrlf_binmode, /* binmode */
14a5cf38
JH
4607 NULL,
4608 PerlIOBase_fileno,
71200d45 4609 PerlIOBuf_dup,
de009b76 4610 PerlIOBuf_read, /* generic read works with ptr/cnt lies */
22569500
NIS
4611 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
4612 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
14a5cf38
JH
4613 PerlIOBuf_seek,
4614 PerlIOBuf_tell,
4615 PerlIOBuf_close,
4616 PerlIOCrlf_flush,
4617 PerlIOBuf_fill,
4618 PerlIOBase_eof,
4619 PerlIOBase_error,
4620 PerlIOBase_clearerr,
4621 PerlIOBase_setlinebuf,
4622 PerlIOBuf_get_base,
4623 PerlIOBuf_bufsiz,
4624 PerlIOBuf_get_ptr,
4625 PerlIOCrlf_get_cnt,
4626 PerlIOCrlf_set_ptrcnt,
66ecd56b
NIS
4627};
4628
06da4f11
NIS
4629#ifdef HAS_MMAP
4630/*--------------------------------------------------------------------------------------*/
14a5cf38 4631/*
71200d45 4632 * mmap as "buffer" layer
14a5cf38 4633 */
06da4f11 4634
14a5cf38 4635typedef struct {
22569500
NIS
4636 PerlIOBuf base; /* PerlIOBuf stuff */
4637 Mmap_t mptr; /* Mapped address */
4638 Size_t len; /* mapped length */
4639 STDCHAR *bbuf; /* malloced buffer if map fails */
06da4f11
NIS
4640} PerlIOMmap;
4641
4642IV
f62ce20a 4643PerlIOMmap_map(pTHX_ PerlIO *f)
06da4f11 4644{
27da23d5 4645 dVAR;
de009b76
AL
4646 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4647 const IV flags = PerlIOBase(f)->flags;
14a5cf38
JH
4648 IV code = 0;
4649 if (m->len)
4650 abort();
4651 if (flags & PERLIO_F_CANREAD) {
dcda55fc 4652 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
de009b76 4653 const int fd = PerlIO_fileno(f);
10eefe7f
CB
4654 Stat_t st;
4655 code = Fstat(fd, &st);
14a5cf38
JH
4656 if (code == 0 && S_ISREG(st.st_mode)) {
4657 SSize_t len = st.st_size - b->posn;
4658 if (len > 0) {
4659 Off_t posn;
27da23d5
JH
4660 if (PL_mmap_page_size <= 0)
4661 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
4662 PL_mmap_page_size);
14a5cf38
JH
4663 if (b->posn < 0) {
4664 /*
4665 * This is a hack - should never happen - open should
71200d45 4666 * have set it !
14a5cf38
JH
4667 */
4668 b->posn = PerlIO_tell(PerlIONext(f));
4669 }
27da23d5 4670 posn = (b->posn / PL_mmap_page_size) * PL_mmap_page_size;
14a5cf38 4671 len = st.st_size - posn;
b91fbb93 4672 m->mptr = (Mmap_t)mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
14a5cf38 4673 if (m->mptr && m->mptr != (Mmap_t) - 1) {
a5262162 4674#if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
14a5cf38 4675 madvise(m->mptr, len, MADV_SEQUENTIAL);
06da4f11 4676#endif
a5262162 4677#if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
14a5cf38 4678 madvise(m->mptr, len, MADV_WILLNEED);
a5262162 4679#endif
14a5cf38
JH
4680 PerlIOBase(f)->flags =
4681 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
4682 b->end = ((STDCHAR *) m->mptr) + len;
4683 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
4684 b->ptr = b->buf;
4685 m->len = len;
4686 }
4687 else {
4688 b->buf = NULL;
4689 }
4690 }
4691 else {
4692 PerlIOBase(f)->flags =
4693 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
4694 b->buf = NULL;
4695 b->ptr = b->end = b->ptr;
4696 code = -1;
4697 }
4698 }
4699 }
4700 return code;
06da4f11
NIS
4701}
4702
4703IV
e87a358a 4704PerlIOMmap_unmap(pTHX_ PerlIO *f)
06da4f11 4705{
dcda55fc 4706 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
14a5cf38
JH
4707 IV code = 0;
4708 if (m->len) {
dcda55fc 4709 PerlIOBuf * const b = &m->base;
14a5cf38 4710 if (b->buf) {
1ccb7c8d
JH
4711 /* The munmap address argument is tricky: depending on the
4712 * standard it is either "void *" or "caddr_t" (which is
4713 * usually "char *" (signed or unsigned). If we cast it
4714 * to "void *", those that have it caddr_t and an uptight
4715 * C++ compiler, will freak out. But casting it as char*
4716 * should work. Maybe. (Using Mmap_t figured out by
4717 * Configure doesn't always work, apparently.) */
4718 code = munmap((char*)m->mptr, m->len);
14a5cf38
JH
4719 b->buf = NULL;
4720 m->len = 0;
4721 m->mptr = NULL;
4722 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
4723 code = -1;
4724 }
4725 b->ptr = b->end = b->buf;
4726 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
4727 }
4728 return code;
06da4f11
NIS
4729}
4730
4731STDCHAR *
f62ce20a 4732PerlIOMmap_get_base(pTHX_ PerlIO *f)
06da4f11 4733{
dcda55fc
AL
4734 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4735 PerlIOBuf * const b = &m->base;
14a5cf38
JH
4736 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
4737 /*
71200d45 4738 * Already have a readbuffer in progress
14a5cf38
JH
4739 */
4740 return b->buf;
4741 }
4742 if (b->buf) {
4743 /*
71200d45 4744 * We have a write buffer or flushed PerlIOBuf read buffer
14a5cf38 4745 */
22569500
NIS
4746 m->bbuf = b->buf; /* save it in case we need it again */
4747 b->buf = NULL; /* Clear to trigger below */
14a5cf38
JH
4748 }
4749 if (!b->buf) {
22569500 4750 PerlIOMmap_map(aTHX_ f); /* Try and map it */
14a5cf38
JH
4751 if (!b->buf) {
4752 /*
71200d45 4753 * Map did not work - recover PerlIOBuf buffer if we have one
14a5cf38
JH
4754 */
4755 b->buf = m->bbuf;
4756 }
4757 }
4758 b->ptr = b->end = b->buf;
4759 if (b->buf)
4760 return b->buf;
f62ce20a 4761 return PerlIOBuf_get_base(aTHX_ f);
06da4f11
NIS
4762}
4763
4764SSize_t
f62ce20a 4765PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
06da4f11 4766{
dcda55fc
AL
4767 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4768 PerlIOBuf * const b = &m->base;
14a5cf38
JH
4769 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
4770 PerlIO_flush(f);
4771 if (b->ptr && (b->ptr - count) >= b->buf
4772 && memEQ(b->ptr - count, vbuf, count)) {
4773 b->ptr -= count;
4774 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
4775 return count;
4776 }
4777 if (m->len) {
4778 /*
71200d45 4779 * Loose the unwritable mapped buffer
14a5cf38
JH
4780 */
4781 PerlIO_flush(f);
4782 /*
71200d45 4783 * If flush took the "buffer" see if we have one from before
14a5cf38
JH
4784 */
4785 if (!b->buf && m->bbuf)
4786 b->buf = m->bbuf;
4787 if (!b->buf) {
f62ce20a 4788 PerlIOBuf_get_base(aTHX_ f);
14a5cf38
JH
4789 m->bbuf = b->buf;
4790 }
4791 }
f62ce20a 4792 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
06da4f11
NIS
4793}
4794
4795SSize_t
f62ce20a 4796PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
06da4f11 4797{
de009b76
AL
4798 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4799 PerlIOBuf * const b = &m->base;
4800
14a5cf38
JH
4801 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
4802 /*
71200d45 4803 * No, or wrong sort of, buffer
14a5cf38
JH
4804 */
4805 if (m->len) {
e87a358a 4806 if (PerlIOMmap_unmap(aTHX_ f) != 0)
14a5cf38
JH
4807 return 0;
4808 }
4809 /*
71200d45 4810 * If unmap took the "buffer" see if we have one from before
14a5cf38
JH
4811 */
4812 if (!b->buf && m->bbuf)
4813 b->buf = m->bbuf;
4814 if (!b->buf) {
f62ce20a 4815 PerlIOBuf_get_base(aTHX_ f);
14a5cf38
JH
4816 m->bbuf = b->buf;
4817 }
06da4f11 4818 }
f62ce20a 4819 return PerlIOBuf_write(aTHX_ f, vbuf, count);
06da4f11
NIS
4820}
4821
4822IV
f62ce20a 4823PerlIOMmap_flush(pTHX_ PerlIO *f)
06da4f11 4824{
dcda55fc
AL
4825 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4826 PerlIOBuf * const b = &m->base;
f62ce20a 4827 IV code = PerlIOBuf_flush(aTHX_ f);
14a5cf38 4828 /*
71200d45 4829 * Now we are "synced" at PerlIOBuf level
14a5cf38
JH
4830 */
4831 if (b->buf) {
4832 if (m->len) {
4833 /*
71200d45 4834 * Unmap the buffer
14a5cf38 4835 */
e87a358a 4836 if (PerlIOMmap_unmap(aTHX_ f) != 0)
14a5cf38
JH
4837 code = -1;
4838 }
4839 else {
4840 /*
4841 * We seem to have a PerlIOBuf buffer which was not mapped
71200d45 4842 * remember it in case we need one later
14a5cf38
JH
4843 */
4844 m->bbuf = b->buf;
4845 }
4846 }
4847 return code;
06da4f11
NIS
4848}
4849
4850IV
f62ce20a 4851PerlIOMmap_fill(pTHX_ PerlIO *f)
06da4f11 4852{
dcda55fc 4853 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38
JH
4854 IV code = PerlIO_flush(f);
4855 if (code == 0 && !b->buf) {
f62ce20a 4856 code = PerlIOMmap_map(aTHX_ f);
14a5cf38
JH
4857 }
4858 if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
f62ce20a 4859 code = PerlIOBuf_fill(aTHX_ f);
14a5cf38
JH
4860 }
4861 return code;
06da4f11
NIS
4862}
4863
4864IV
f62ce20a 4865PerlIOMmap_close(pTHX_ PerlIO *f)
06da4f11 4866{
dcda55fc
AL
4867 PerlIOMmap * const m = PerlIOSelf(f, PerlIOMmap);
4868 PerlIOBuf * const b = &m->base;
14a5cf38
JH
4869 IV code = PerlIO_flush(f);
4870 if (m->bbuf) {
4871 b->buf = m->bbuf;
4872 m->bbuf = NULL;
4873 b->ptr = b->end = b->buf;
4874 }
f62ce20a 4875 if (PerlIOBuf_close(aTHX_ f) != 0)
14a5cf38
JH
4876 code = -1;
4877 return code;
06da4f11
NIS
4878}
4879
71200d45 4880PerlIO *
ecdeb87c 4881PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 4882{
ecdeb87c 4883 return PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
4884}
4885
06da4f11 4886
27da23d5 4887PERLIO_FUNCS_DECL(PerlIO_mmap) = {
2dc2558e 4888 sizeof(PerlIO_funcs),
14a5cf38
JH
4889 "mmap",
4890 sizeof(PerlIOMmap),
86e05cf2 4891 PERLIO_K_BUFFERED|PERLIO_K_RAW,
14a5cf38 4892 PerlIOBuf_pushed,
44798d05 4893 PerlIOBuf_popped,
14a5cf38 4894 PerlIOBuf_open,
86e05cf2 4895 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
4896 NULL,
4897 PerlIOBase_fileno,
71200d45 4898 PerlIOMmap_dup,
14a5cf38
JH
4899 PerlIOBuf_read,
4900 PerlIOMmap_unread,
4901 PerlIOMmap_write,
4902 PerlIOBuf_seek,
4903 PerlIOBuf_tell,
4904 PerlIOBuf_close,
4905 PerlIOMmap_flush,
4906 PerlIOMmap_fill,
4907 PerlIOBase_eof,
4908 PerlIOBase_error,
4909 PerlIOBase_clearerr,
4910 PerlIOBase_setlinebuf,
4911 PerlIOMmap_get_base,
4912 PerlIOBuf_bufsiz,
4913 PerlIOBuf_get_ptr,
4914 PerlIOBuf_get_cnt,
4915 PerlIOBuf_set_ptrcnt,
06da4f11
NIS
4916};
4917
22569500 4918#endif /* HAS_MMAP */
06da4f11 4919
9e353e3b 4920PerlIO *
e87a358a 4921Perl_PerlIO_stdin(pTHX)
9e353e3b 4922{
97aff369 4923 dVAR;
a1ea730d 4924 if (!PL_perlio) {
14a5cf38
JH
4925 PerlIO_stdstreams(aTHX);
4926 }
a1ea730d 4927 return &PL_perlio[1];
9e353e3b
NIS
4928}
4929
9e353e3b 4930PerlIO *
e87a358a 4931Perl_PerlIO_stdout(pTHX)
9e353e3b 4932{
97aff369 4933 dVAR;
a1ea730d 4934 if (!PL_perlio) {
14a5cf38
JH
4935 PerlIO_stdstreams(aTHX);
4936 }
a1ea730d 4937 return &PL_perlio[2];
9e353e3b
NIS
4938}
4939
9e353e3b 4940PerlIO *
e87a358a 4941Perl_PerlIO_stderr(pTHX)
9e353e3b 4942{
97aff369 4943 dVAR;
a1ea730d 4944 if (!PL_perlio) {
14a5cf38
JH
4945 PerlIO_stdstreams(aTHX);
4946 }
a1ea730d 4947 return &PL_perlio[3];
9e353e3b
NIS
4948}
4949
4950/*--------------------------------------------------------------------------------------*/
4951
9e353e3b
NIS
4952char *
4953PerlIO_getname(PerlIO *f, char *buf)
4954{
14a5cf38 4955 dTHX;
a15cef0c 4956#ifdef VMS
73d840c0 4957 char *name = NULL;
7659f319 4958 bool exported = FALSE;
14a5cf38 4959 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
7659f319
CB
4960 if (!stdio) {
4961 stdio = PerlIO_exportFILE(f,0);
4962 exported = TRUE;
4963 }
4964 if (stdio) {
14a5cf38 4965 name = fgetname(stdio, buf);
7659f319
CB
4966 if (exported) PerlIO_releaseFILE(f,stdio);
4967 }
73d840c0 4968 return name;
a15cef0c 4969#else
8772537c
AL
4970 PERL_UNUSED_ARG(f);
4971 PERL_UNUSED_ARG(buf);
14a5cf38 4972 Perl_croak(aTHX_ "Don't know how to get file name");
bd61b366 4973 return NULL;
a15cef0c 4974#endif
9e353e3b
NIS
4975}
4976
4977
4978/*--------------------------------------------------------------------------------------*/
14a5cf38
JH
4979/*
4980 * Functions which can be called on any kind of PerlIO implemented in
71200d45 4981 * terms of above
14a5cf38 4982 */
9e353e3b 4983
e87a358a
NIS
4984#undef PerlIO_fdopen
4985PerlIO *
4986PerlIO_fdopen(int fd, const char *mode)
4987{
4988 dTHX;
bd61b366 4989 return PerlIO_openn(aTHX_ NULL, mode, fd, 0, 0, NULL, 0, NULL);
e87a358a
NIS
4990}
4991
4992#undef PerlIO_open
4993PerlIO *
4994PerlIO_open(const char *path, const char *mode)
4995{
4996 dTHX;
42d9b98d 4997 SV *name = sv_2mortal(newSVpv(path, 0));
bd61b366 4998 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, NULL, 1, &name);
e87a358a
NIS
4999}
5000
5001#undef Perlio_reopen
5002PerlIO *
5003PerlIO_reopen(const char *path, const char *mode, PerlIO *f)
5004{
5005 dTHX;
42d9b98d 5006 SV *name = sv_2mortal(newSVpv(path,0));
bd61b366 5007 return PerlIO_openn(aTHX_ NULL, mode, -1, 0, 0, f, 1, &name);
e87a358a
NIS
5008}
5009
9e353e3b 5010#undef PerlIO_getc
6f9d8c32 5011int
9e353e3b 5012PerlIO_getc(PerlIO *f)
760ac839 5013{
e87a358a 5014 dTHX;
14a5cf38 5015 STDCHAR buf[1];
de009b76 5016 if ( 1 == PerlIO_read(f, buf, 1) ) {
14a5cf38
JH
5017 return (unsigned char) buf[0];
5018 }
5019 return EOF;
313ca112
NIS
5020}
5021
5022#undef PerlIO_ungetc
5023int
5024PerlIO_ungetc(PerlIO *f, int ch)
5025{
e87a358a 5026 dTHX;
14a5cf38
JH
5027 if (ch != EOF) {
5028 STDCHAR buf = ch;
5029 if (PerlIO_unread(f, &buf, 1) == 1)
5030 return ch;
5031 }
5032 return EOF;
760ac839
LW
5033}
5034
9e353e3b
NIS
5035#undef PerlIO_putc
5036int
5037PerlIO_putc(PerlIO *f, int ch)
760ac839 5038{
e87a358a 5039 dTHX;
14a5cf38
JH
5040 STDCHAR buf = ch;
5041 return PerlIO_write(f, &buf, 1);
760ac839
LW
5042}
5043
9e353e3b 5044#undef PerlIO_puts
760ac839 5045int
9e353e3b 5046PerlIO_puts(PerlIO *f, const char *s)
760ac839 5047{
e87a358a 5048 dTHX;
dcda55fc 5049 return PerlIO_write(f, s, strlen(s));
760ac839
LW
5050}
5051
5052#undef PerlIO_rewind
5053void
c78749f2 5054PerlIO_rewind(PerlIO *f)
760ac839 5055{
e87a358a 5056 dTHX;
14a5cf38
JH
5057 PerlIO_seek(f, (Off_t) 0, SEEK_SET);
5058 PerlIO_clearerr(f);
6f9d8c32
NIS
5059}
5060
5061#undef PerlIO_vprintf
5062int
5063PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap)
5064{
14a5cf38 5065 dTHX;
53ce71d3 5066 SV * sv;
b83604b4 5067 const char *s;
14a5cf38
JH
5068 STRLEN len;
5069 SSize_t wrote;
2cc61e15 5070#ifdef NEED_VA_COPY
14a5cf38
JH
5071 va_list apc;
5072 Perl_va_copy(ap, apc);
53ce71d3 5073 sv = vnewSVpvf(fmt, &apc);
2cc61e15 5074#else
53ce71d3 5075 sv = vnewSVpvf(fmt, &ap);
2cc61e15 5076#endif
b83604b4 5077 s = SvPV_const(sv, len);
14a5cf38
JH
5078 wrote = PerlIO_write(f, s, len);
5079 SvREFCNT_dec(sv);
5080 return wrote;
760ac839
LW
5081}
5082
5083#undef PerlIO_printf
6f9d8c32 5084int
14a5cf38 5085PerlIO_printf(PerlIO *f, const char *fmt, ...)
760ac839 5086{
14a5cf38
JH
5087 va_list ap;
5088 int result;
5089 va_start(ap, fmt);
5090 result = PerlIO_vprintf(f, fmt, ap);
5091 va_end(ap);
5092 return result;
760ac839
LW
5093}
5094
5095#undef PerlIO_stdoutf
6f9d8c32 5096int
14a5cf38 5097PerlIO_stdoutf(const char *fmt, ...)
760ac839 5098{
e87a358a 5099 dTHX;
14a5cf38
JH
5100 va_list ap;
5101 int result;
5102 va_start(ap, fmt);
5103 result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap);
5104 va_end(ap);
5105 return result;
760ac839
LW
5106}
5107
5108#undef PerlIO_tmpfile
5109PerlIO *
c78749f2 5110PerlIO_tmpfile(void)
760ac839 5111{
2941a2e1
JH
5112 dTHX;
5113 PerlIO *f = NULL;
2941a2e1 5114#ifdef WIN32
de009b76 5115 const int fd = win32_tmpfd();
2941a2e1
JH
5116 if (fd >= 0)
5117 f = PerlIO_fdopen(fd, "w+b");
5118#else /* WIN32 */
460c8493 5119# if defined(HAS_MKSTEMP) && ! defined(VMS) && ! defined(OS2)
396482e1 5120 SV * const sv = newSVpvs("/tmp/PerlIO_XXXXXX");
2941a2e1
JH
5121 /*
5122 * I have no idea how portable mkstemp() is ... NI-S
5123 */
de009b76 5124 const int fd = mkstemp(SvPVX(sv));
2941a2e1
JH
5125 if (fd >= 0) {
5126 f = PerlIO_fdopen(fd, "w+");
5127 if (f)
5128 PerlIOBase(f)->flags |= PERLIO_F_TEMP;
b15aece3 5129 PerlLIO_unlink(SvPVX_const(sv));
2941a2e1 5130 }
b2166d27 5131 SvREFCNT_dec(sv);
2941a2e1 5132# else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */
c4420975 5133 FILE * const stdio = PerlSIO_tmpfile();
2941a2e1 5134
085e731f
CB
5135 if (stdio)
5136 f = PerlIO_fdopen(fileno(stdio), "w+");
5137
2941a2e1
JH
5138# endif /* else HAS_MKSTEMP */
5139#endif /* else WIN32 */
5140 return f;
760ac839
LW
5141}
5142
6f9d8c32
NIS
5143#undef HAS_FSETPOS
5144#undef HAS_FGETPOS
5145
22569500
NIS
5146#endif /* USE_SFIO */
5147#endif /* PERLIO_IS_STDIO */
760ac839 5148
9e353e3b 5149/*======================================================================================*/
14a5cf38 5150/*
71200d45
NIS
5151 * Now some functions in terms of above which may be needed even if we are
5152 * not in true PerlIO mode
9e353e3b 5153 */
188f0c84
YO
5154const char *
5155Perl_PerlIO_context_layers(pTHX_ const char *mode)
5156{
5157 dVAR;
8b850bd5
NC
5158 const char *direction = NULL;
5159 SV *layers;
188f0c84
YO
5160 /*
5161 * Need to supply default layer info from open.pm
5162 */
8b850bd5
NC
5163
5164 if (!PL_curcop)
5165 return NULL;
5166
5167 if (mode && mode[0] != 'r') {
5168 if (PL_curcop->cop_hints & HINT_LEXICAL_IO_OUT)
5169 direction = "open>";
5170 } else {
5171 if (PL_curcop->cop_hints & HINT_LEXICAL_IO_IN)
5172 direction = "open<";
188f0c84 5173 }
8b850bd5
NC
5174 if (!direction)
5175 return NULL;
5176
5177 layers = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
5178 0, direction, 5, 0, 0);
5179
5180 assert(layers);
5181 return SvOK(layers) ? SvPV_nolen_const(layers) : NULL;
188f0c84
YO
5182}
5183
9e353e3b 5184
760ac839
LW
5185#ifndef HAS_FSETPOS
5186#undef PerlIO_setpos
5187int
766a733e 5188PerlIO_setpos(PerlIO *f, SV *pos)
760ac839 5189{
14a5cf38
JH
5190 dTHX;
5191 if (SvOK(pos)) {
5192 STRLEN len;
c4420975 5193 const Off_t * const posn = (Off_t *) SvPV(pos, len);
14a5cf38
JH
5194 if (f && len == sizeof(Off_t))
5195 return PerlIO_seek(f, *posn, SEEK_SET);
5196 }
93189314 5197 SETERRNO(EINVAL, SS_IVCHAN);
14a5cf38 5198 return -1;
760ac839 5199}
c411622e 5200#else
c411622e 5201#undef PerlIO_setpos
5202int
766a733e 5203PerlIO_setpos(PerlIO *f, SV *pos)
c411622e 5204{
14a5cf38
JH
5205 dTHX;
5206 if (SvOK(pos)) {
5207 STRLEN len;
c4420975 5208 Fpos_t * const fpos = (Fpos_t *) SvPV(pos, len);
14a5cf38 5209 if (f && len == sizeof(Fpos_t)) {
2d4389e4 5210#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
14a5cf38 5211 return fsetpos64(f, fpos);
d9b3e12d 5212#else
14a5cf38 5213 return fsetpos(f, fpos);
d9b3e12d 5214#endif
14a5cf38 5215 }
766a733e 5216 }
93189314 5217 SETERRNO(EINVAL, SS_IVCHAN);
14a5cf38 5218 return -1;
c411622e 5219}
5220#endif
760ac839
LW
5221
5222#ifndef HAS_FGETPOS
5223#undef PerlIO_getpos
5224int
766a733e 5225PerlIO_getpos(PerlIO *f, SV *pos)
760ac839 5226{
14a5cf38
JH
5227 dTHX;
5228 Off_t posn = PerlIO_tell(f);
5229 sv_setpvn(pos, (char *) &posn, sizeof(posn));
5230 return (posn == (Off_t) - 1) ? -1 : 0;
760ac839 5231}
c411622e 5232#else
c411622e 5233#undef PerlIO_getpos
5234int
766a733e 5235PerlIO_getpos(PerlIO *f, SV *pos)
c411622e 5236{
14a5cf38
JH
5237 dTHX;
5238 Fpos_t fpos;
5239 int code;
2d4389e4 5240#if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64)
14a5cf38 5241 code = fgetpos64(f, &fpos);
d9b3e12d 5242#else
14a5cf38 5243 code = fgetpos(f, &fpos);
d9b3e12d 5244#endif
14a5cf38
JH
5245 sv_setpvn(pos, (char *) &fpos, sizeof(fpos));
5246 return code;
c411622e 5247}
5248#endif
760ac839
LW
5249
5250#if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF)
5251
5252int
c78749f2 5253vprintf(char *pat, char *args)
662a7e3f
CS
5254{
5255 _doprnt(pat, args, stdout);
22569500 5256 return 0; /* wrong, but perl doesn't use the return
14a5cf38 5257 * value */
662a7e3f
CS
5258}
5259
5260int
c78749f2 5261vfprintf(FILE *fd, char *pat, char *args)
760ac839
LW
5262{
5263 _doprnt(pat, args, fd);
22569500 5264 return 0; /* wrong, but perl doesn't use the return
14a5cf38 5265 * value */
760ac839
LW
5266}
5267
5268#endif
5269
5270#ifndef PerlIO_vsprintf
6f9d8c32 5271int
8ac85365 5272PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
760ac839 5273{
8ff9a42b 5274 dTHX;
d9fad198 5275 const int val = my_vsnprintf(s, n > 0 ? n : 0, fmt, ap);
14333449
AL
5276 PERL_UNUSED_CONTEXT;
5277
1208b3dd
JH
5278#ifndef PERL_MY_VSNPRINTF_GUARDED
5279 if (val < 0 || (n > 0 ? val >= n : 0)) {
37405f90 5280 Perl_croak(aTHX_ "panic: my_vsnprintf overflow in PerlIO_vsprintf\n");
760ac839 5281 }
1208b3dd 5282#endif
14a5cf38 5283 return val;
760ac839
LW
5284}
5285#endif
5286
5287#ifndef PerlIO_sprintf
6f9d8c32 5288int
14a5cf38 5289PerlIO_sprintf(char *s, int n, const char *fmt, ...)
760ac839 5290{
14a5cf38
JH
5291 va_list ap;
5292 int result;
5293 va_start(ap, fmt);
5294 result = PerlIO_vsprintf(s, n, fmt, ap);
5295 va_end(ap);
5296 return result;
760ac839
LW
5297}
5298#endif
9cfa90c0
NC
5299
5300/*
5301 * Local variables:
5302 * c-indentation-style: bsd
5303 * c-basic-offset: 4
5304 * indent-tabs-mode: t
5305 * End:
5306 *
37442d52
RGS
5307 * ex: set ts=8 sts=4 sw=4 noet:
5308 */