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