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