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