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