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