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