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