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