This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update copyright year.
[perl5.git] / perlio.c
CommitLineData
14a5cf38 1/*
cbdf9ef8 2 * perlio.c Copyright (c) 1996-2005, 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
27da23d5
JH
59#define PERLIO_MAX_REFCOUNTABLE_FD 2048
60
9cffb111
OS
61#ifdef __Lynx__
62/* Missing proto on LynxOS */
63int mkstemp(char*);
64#endif
65
1b7a0411 66/* Call the callback or PerlIOBase, and return failure. */
b32dd47e 67#define Perl_PerlIO_or_Base(f, callback, base, failure, args) \
1b7a0411 68 if (PerlIOValid(f)) { \
46c461b5 69 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
70 if (tab && tab->callback) \
71 return (*tab->callback) args; \
72 else \
73 return PerlIOBase_ ## base args; \
74 } \
75 else \
76 SETERRNO(EBADF, SS_IVCHAN); \
77 return failure
78
79/* Call the callback or fail, and return failure. */
b32dd47e 80#define Perl_PerlIO_or_fail(f, callback, failure, args) \
1b7a0411 81 if (PerlIOValid(f)) { \
46c461b5 82 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
83 if (tab && tab->callback) \
84 return (*tab->callback) args; \
85 SETERRNO(EINVAL, LIB_INVARG); \
86 } \
87 else \
88 SETERRNO(EBADF, SS_IVCHAN); \
89 return failure
90
91/* Call the callback or PerlIOBase, and be void. */
b32dd47e 92#define Perl_PerlIO_or_Base_void(f, callback, base, args) \
1b7a0411 93 if (PerlIOValid(f)) { \
46c461b5 94 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
95 if (tab && tab->callback) \
96 (*tab->callback) args; \
97 else \
98 PerlIOBase_ ## base args; \
1b7a0411
JH
99 } \
100 else \
101 SETERRNO(EBADF, SS_IVCHAN)
102
103/* Call the callback or fail, and be void. */
b32dd47e 104#define Perl_PerlIO_or_fail_void(f, callback, args) \
1b7a0411 105 if (PerlIOValid(f)) { \
46c461b5 106 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;\
1b7a0411
JH
107 if (tab && tab->callback) \
108 (*tab->callback) args; \
37725cdc
NIS
109 else \
110 SETERRNO(EINVAL, LIB_INVARG); \
1b7a0411
JH
111 } \
112 else \
113 SETERRNO(EBADF, SS_IVCHAN)
114
71ab4674
SP
115#ifndef USE_SFIO
116int
117perlsio_binmode(FILE *fp, int iotype, int mode)
118{
119 /*
120 * This used to be contents of do_binmode in doio.c
121 */
122#ifdef DOSISH
123# if defined(atarist) || defined(__MINT__)
124 if (!fflush(fp)) {
125 if (mode & O_BINARY)
126 ((FILE *) fp)->_flag |= _IOBIN;
127 else
128 ((FILE *) fp)->_flag &= ~_IOBIN;
129 return 1;
130 }
131 return 0;
132# else
133 dTHX;
134#ifdef NETWARE
135 if (PerlLIO_setmode(fp, mode) != -1) {
136#else
137 if (PerlLIO_setmode(fileno(fp), mode) != -1) {
138#endif
139# if defined(WIN32) && defined(__BORLANDC__)
140 /*
141 * The translation mode of the stream is maintained independent
142of
143 * the translation mode of the fd in the Borland RTL (heavy
144 * digging through their runtime sources reveal). User has to
145set
146 * the mode explicitly for the stream (though they don't
147document
148 * this anywhere). GSAR 97-5-24
149 */
150 fseek(fp, 0L, 0);
151 if (mode & O_BINARY)
152 fp->flags |= _F_BIN;
153 else
154 fp->flags &= ~_F_BIN;
155# endif
156 return 1;
157 }
158 else
159 return 0;
160# endif
161#else
162# if defined(USEMYBINMODE)
163 dTHX;
164 if (my_binmode(fp, iotype, mode) != FALSE)
165 return 1;
166 else
167 return 0;
168# else
169 PERL_UNUSED_ARG(fp);
170 PERL_UNUSED_ARG(iotype);
171 PERL_UNUSED_ARG(mode);
172 return 1;
173# endif
174#endif
175}
176#endif /* sfio */
177
06c7082d 178#ifndef O_ACCMODE
22569500 179#define O_ACCMODE 3 /* Assume traditional implementation */
06c7082d
NIS
180#endif
181
182int
183PerlIO_intmode2str(int rawmode, char *mode, int *writing)
184{
de009b76 185 const int result = rawmode & O_ACCMODE;
06c7082d
NIS
186 int ix = 0;
187 int ptype;
188 switch (result) {
189 case O_RDONLY:
190 ptype = IoTYPE_RDONLY;
191 break;
192 case O_WRONLY:
193 ptype = IoTYPE_WRONLY;
194 break;
195 case O_RDWR:
196 default:
197 ptype = IoTYPE_RDWR;
198 break;
199 }
200 if (writing)
201 *writing = (result != O_RDONLY);
202
203 if (result == O_RDONLY) {
204 mode[ix++] = 'r';
205 }
206#ifdef O_APPEND
207 else if (rawmode & O_APPEND) {
208 mode[ix++] = 'a';
209 if (result != O_WRONLY)
210 mode[ix++] = '+';
211 }
212#endif
213 else {
214 if (result == O_WRONLY)
215 mode[ix++] = 'w';
216 else {
217 mode[ix++] = 'r';
218 mode[ix++] = '+';
219 }
220 }
221 if (rawmode & O_BINARY)
222 mode[ix++] = 'b';
223 mode[ix] = '\0';
224 return ptype;
225}
226
eb73beca
NIS
227#ifndef PERLIO_LAYERS
228int
229PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
230{
6874a2de
NIS
231 if (!names || !*names
232 || strEQ(names, ":crlf")
233 || strEQ(names, ":raw")
234 || strEQ(names, ":bytes")
235 ) {
14a5cf38
JH
236 return 0;
237 }
238 Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names);
239 /*
71200d45 240 * NOTREACHED
14a5cf38
JH
241 */
242 return -1;
eb73beca
NIS
243}
244
13621cfb
NIS
245void
246PerlIO_destruct(pTHX)
247{
248}
249
f5b9d040
NIS
250int
251PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names)
252{
92bff44d 253#ifdef USE_SFIO
8772537c
AL
254 PERL_UNUSED_ARG(iotype);
255 PERL_UNUSED_ARG(mode);
256 PERL_UNUSED_ARG(names);
14a5cf38 257 return 1;
92bff44d 258#else
14a5cf38 259 return perlsio_binmode(fp, iotype, mode);
92bff44d 260#endif
f5b9d040 261}
60382766 262
e0fa5af2 263PerlIO *
ecdeb87c 264PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
e0fa5af2 265{
a0fd4948 266#if defined(PERL_MICRO) || defined(__SYMBIAN32__)
0553478e
NIS
267 return NULL;
268#else
269#ifdef PERL_IMPLICIT_SYS
22569500 270 return PerlSIO_fdupopen(f);
0553478e 271#else
30753f56
NIS
272#ifdef WIN32
273 return win32_fdupopen(f);
274#else
e0fa5af2 275 if (f) {
504618e9 276 const int fd = PerlLIO_dup(PerlIO_fileno(f));
e0fa5af2 277 if (fd >= 0) {
06c7082d 278 char mode[8];
a5936e02 279#ifdef DJGPP
dcda55fc
AL
280 const int omode = djgpp_get_stream_mode(f);
281#else
282 const int omode = fcntl(fd, F_GETFL);
a5936e02 283#endif
06c7082d 284 PerlIO_intmode2str(omode,mode,NULL);
e0fa5af2 285 /* the r+ is a hack */
06c7082d 286 return PerlIO_fdopen(fd, mode);
e0fa5af2
NIS
287 }
288 return NULL;
289 }
290 else {
93189314 291 SETERRNO(EBADF, SS_IVCHAN);
e0fa5af2 292 }
7114a2d2 293#endif
e0fa5af2 294 return NULL;
0553478e 295#endif
30753f56 296#endif
e0fa5af2
NIS
297}
298
299
14a5cf38 300/*
71200d45 301 * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries
14a5cf38 302 */
ee518936
NIS
303
304PerlIO *
14a5cf38
JH
305PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
306 int imode, int perm, PerlIO *old, int narg, SV **args)
307{
7cf31beb
NIS
308 if (narg) {
309 if (narg > 1) {
3b8752bb 310 Perl_croak(aTHX_ "More than one argument to open");
7cf31beb 311 }
14a5cf38
JH
312 if (*args == &PL_sv_undef)
313 return PerlIO_tmpfile();
314 else {
e62f0680 315 const char *name = SvPV_nolen_const(*args);
3b6c1aba 316 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
317 fd = PerlLIO_open3(name, imode, perm);
318 if (fd >= 0)
de009b76 319 return PerlIO_fdopen(fd, mode + 1);
14a5cf38
JH
320 }
321 else if (old) {
322 return PerlIO_reopen(name, mode, old);
323 }
324 else {
325 return PerlIO_open(name, mode);
326 }
327 }
328 }
329 else {
330 return PerlIO_fdopen(fd, (char *) mode);
331 }
332 return NULL;
ee518936
NIS
333}
334
0c4f7ff0
NIS
335XS(XS_PerlIO__Layer__find)
336{
14a5cf38
JH
337 dXSARGS;
338 if (items < 2)
339 Perl_croak(aTHX_ "Usage class->find(name[,load])");
340 else {
dcda55fc 341 const char * const name = SvPV_nolen_const(ST(1));
14a5cf38
JH
342 ST(0) = (strEQ(name, "crlf")
343 || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef;
344 XSRETURN(1);
345 }
0c4f7ff0
NIS
346}
347
348
349void
350Perl_boot_core_PerlIO(pTHX)
351{
14a5cf38 352 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
0c4f7ff0
NIS
353}
354
ac27b0f5
NIS
355#endif
356
32e30700 357
6f9d8c32 358#ifdef PERLIO_IS_STDIO
760ac839
LW
359
360void
e8632036 361PerlIO_init(pTHX)
760ac839 362{
14a5cf38
JH
363 /*
364 * Does nothing (yet) except force this file to be included in perl
71200d45 365 * binary. That allows this file to force inclusion of other functions
14a5cf38 366 * that may be required by loadable extensions e.g. for
71200d45 367 * FileHandle::tmpfile
14a5cf38 368 */
760ac839
LW
369}
370
33dcbb9a
PP
371#undef PerlIO_tmpfile
372PerlIO *
8ac85365 373PerlIO_tmpfile(void)
33dcbb9a 374{
14a5cf38 375 return tmpfile();
33dcbb9a
PP
376}
377
22569500 378#else /* PERLIO_IS_STDIO */
760ac839
LW
379
380#ifdef USE_SFIO
381
382#undef HAS_FSETPOS
383#undef HAS_FGETPOS
384
14a5cf38
JH
385/*
386 * This section is just to make sure these functions get pulled in from
71200d45 387 * libsfio.a
14a5cf38 388 */
760ac839
LW
389
390#undef PerlIO_tmpfile
391PerlIO *
c78749f2 392PerlIO_tmpfile(void)
760ac839 393{
14a5cf38 394 return sftmp(0);
760ac839
LW
395}
396
397void
e8632036 398PerlIO_init(pTHX)
760ac839 399{
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{
14a5cf38 510 /*
71200d45 511 * Find a free slot in the table, allocating new table as necessary
14a5cf38
JH
512 */
513 PerlIO **last;
514 PerlIO *f;
a1ea730d 515 last = &PL_perlio;
14a5cf38
JH
516 while ((f = *last)) {
517 int i;
518 last = (PerlIO **) (f);
519 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
520 if (!*++f) {
521 return f;
522 }
523 }
524 }
a02a5408 525 Newxz(f,PERLIO_TABLE_SIZE,PerlIO);
14a5cf38
JH
526 if (!f) {
527 return NULL;
528 }
529 *last = f;
530 return f + 1;
05d1247b
NIS
531}
532
a1ea730d
NIS
533#undef PerlIO_fdupopen
534PerlIO *
ecdeb87c 535PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
a1ea730d 536{
04892f78 537 if (PerlIOValid(f)) {
de009b76 538 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
fe5a182c 539 PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param);
210e727c
JH
540 if (tab && tab->Dup)
541 return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
37725cdc
NIS
542 else {
543 return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags);
544 }
a1ea730d 545 }
210e727c
JH
546 else
547 SETERRNO(EBADF, SS_IVCHAN);
548
549 return NULL;
a1ea730d
NIS
550}
551
552void
5f1a76d0 553PerlIO_cleantable(pTHX_ PerlIO **tablep)
05d1247b 554{
dcda55fc 555 PerlIO * const table = *tablep;
14a5cf38
JH
556 if (table) {
557 int i;
558 PerlIO_cleantable(aTHX_(PerlIO **) & (table[0]));
559 for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) {
dcda55fc 560 PerlIO * const f = table + i;
14a5cf38
JH
561 if (*f) {
562 PerlIO_close(f);
563 }
564 }
3a1ee7e8 565 Safefree(table);
14a5cf38 566 *tablep = NULL;
05d1247b 567 }
05d1247b
NIS
568}
569
fcf2db38
NIS
570
571PerlIO_list_t *
3a1ee7e8 572PerlIO_list_alloc(pTHX)
fcf2db38 573{
14a5cf38 574 PerlIO_list_t *list;
a02a5408 575 Newxz(list, 1, PerlIO_list_t);
14a5cf38
JH
576 list->refcnt = 1;
577 return list;
fcf2db38
NIS
578}
579
580void
3a1ee7e8 581PerlIO_list_free(pTHX_ PerlIO_list_t *list)
fcf2db38 582{
14a5cf38
JH
583 if (list) {
584 if (--list->refcnt == 0) {
585 if (list->array) {
14a5cf38
JH
586 IV i;
587 for (i = 0; i < list->cur; i++) {
588 if (list->array[i].arg)
589 SvREFCNT_dec(list->array[i].arg);
590 }
591 Safefree(list->array);
592 }
593 Safefree(list);
594 }
595 }
fcf2db38
NIS
596}
597
598void
3a1ee7e8 599PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg)
14a5cf38 600{
14a5cf38
JH
601 PerlIO_pair_t *p;
602 if (list->cur >= list->len) {
603 list->len += 8;
604 if (list->array)
605 Renew(list->array, list->len, PerlIO_pair_t);
606 else
a02a5408 607 Newx(list->array, list->len, PerlIO_pair_t);
14a5cf38
JH
608 }
609 p = &(list->array[list->cur++]);
610 p->funcs = funcs;
611 if ((p->arg = arg)) {
7fc63493 612 (void)SvREFCNT_inc(arg);
14a5cf38 613 }
fcf2db38
NIS
614}
615
3a1ee7e8
NIS
616PerlIO_list_t *
617PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param)
618{
694c95cf
JH
619 PerlIO_list_t *list = (PerlIO_list_t *) NULL;
620 if (proto) {
621 int i;
622 list = PerlIO_list_alloc(aTHX);
623 for (i=0; i < proto->cur; i++) {
624 SV *arg = Nullsv;
625 if (proto->array[i].arg)
626 arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param);
627 PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg);
628 }
3a1ee7e8
NIS
629 }
630 return list;
631}
4a4a6116 632
05d1247b 633void
3a1ee7e8 634PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param)
9a6404c5 635{
3aaf42a7 636#ifdef USE_ITHREADS
3a1ee7e8
NIS
637 PerlIO **table = &proto->Iperlio;
638 PerlIO *f;
639 PL_perlio = NULL;
640 PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param);
641 PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param);
642 PerlIO_allocate(aTHX); /* root slot is never used */
694c95cf 643 PerlIO_debug("Clone %p from %p\n",aTHX,proto);
3a1ee7e8
NIS
644 while ((f = *table)) {
645 int i;
646 table = (PerlIO **) (f++);
647 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
648 if (*f) {
93a8090d 649 (void) fp_dup(f, 0, param);
3a1ee7e8
NIS
650 }
651 f++;
652 }
653 }
1b6737cc
AL
654#else
655 PERL_UNUSED_ARG(proto);
656 PERL_UNUSED_ARG(param);
3aaf42a7 657#endif
9a6404c5
DM
658}
659
660void
13621cfb
NIS
661PerlIO_destruct(pTHX)
662{
a1ea730d 663 PerlIO **table = &PL_perlio;
14a5cf38 664 PerlIO *f;
694c95cf
JH
665#ifdef USE_ITHREADS
666 PerlIO_debug("Destruct %p\n",aTHX);
667#endif
14a5cf38
JH
668 while ((f = *table)) {
669 int i;
670 table = (PerlIO **) (f++);
671 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
672 PerlIO *x = f;
dcda55fc 673 const PerlIOl *l;
14a5cf38
JH
674 while ((l = *x)) {
675 if (l->tab->kind & PERLIO_K_DESTRUCT) {
676 PerlIO_debug("Destruct popping %s\n", l->tab->name);
677 PerlIO_flush(x);
678 PerlIO_pop(aTHX_ x);
679 }
680 else {
681 x = PerlIONext(x);
682 }
683 }
684 f++;
685 }
686 }
13621cfb
NIS
687}
688
689void
a999f61b 690PerlIO_pop(pTHX_ PerlIO *f)
760ac839 691{
dcda55fc 692 const PerlIOl *l = *f;
14a5cf38 693 if (l) {
fe5a182c 694 PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name);
14a5cf38
JH
695 if (l->tab->Popped) {
696 /*
697 * If popped returns non-zero do not free its layer structure
698 * it has either done so itself, or it is shared and still in
71200d45 699 * use
14a5cf38 700 */
f62ce20a 701 if ((*l->tab->Popped) (aTHX_ f) != 0)
14a5cf38
JH
702 return;
703 }
b47cad08 704 *f = l->next;
3a1ee7e8 705 Safefree(l);
a8c08ecd 706 }
6f9d8c32
NIS
707}
708
39f7a870
JH
709/* Return as an array the stack of layers on a filehandle. Note that
710 * the stack is returned top-first in the array, and there are three
711 * times as many array elements as there are layers in the stack: the
712 * first element of a layer triplet is the name, the second one is the
713 * arguments, and the third one is the flags. */
714
715AV *
716PerlIO_get_layers(pTHX_ PerlIO *f)
717{
dcda55fc 718 AV * const av = newAV();
39f7a870 719
dcda55fc
AL
720 if (PerlIOValid(f)) {
721 PerlIOl *l = PerlIOBase(f);
722
723 while (l) {
724 SV * const name = l->tab && l->tab->name ?
725 newSVpv(l->tab->name, 0) : &PL_sv_undef;
726 SV * const arg = l->tab && l->tab->Getarg ?
727 (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef;
728 av_push(av, name);
729 av_push(av, arg);
730 av_push(av, newSViv((IV)l->flags));
731 l = l->next;
732 }
733 }
39f7a870 734
dcda55fc 735 return av;
39f7a870
JH
736}
737
9e353e3b 738/*--------------------------------------------------------------------------------------*/
14a5cf38 739/*
71200d45 740 * XS Interface for perl code
14a5cf38 741 */
9e353e3b 742
fcf2db38 743PerlIO_funcs *
2edd7e44 744PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load)
f3862f8b 745{
27da23d5 746 dVAR;
14a5cf38
JH
747 IV i;
748 if ((SSize_t) len <= 0)
749 len = strlen(name);
3a1ee7e8 750 for (i = 0; i < PL_known_layers->cur; i++) {
46c461b5 751 PerlIO_funcs * const f = PL_known_layers->array[i].funcs;
a9f76400 752 if (memEQ(f->name, name, len) && f->name[len] == 0) {
fe5a182c 753 PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f);
14a5cf38
JH
754 return f;
755 }
756 }
3a1ee7e8
NIS
757 if (load && PL_subname && PL_def_layerlist
758 && PL_def_layerlist->cur >= 2) {
d7a09b41
SR
759 if (PL_in_load_module) {
760 Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer");
761 return NULL;
762 } else {
396482e1 763 SV * const pkgsv = newSVpvs("PerlIO");
46c461b5
AL
764 SV * const layer = newSVpvn(name, len);
765 CV * const cv = get_cv("PerlIO::Layer::NoWarnings", FALSE);
766 ENTER;
d7a09b41 767 SAVEINT(PL_in_load_module);
c9bca74a 768 if (cv) {
9cfa90c0 769 SAVEGENERICSV(PL_warnhook);
504618e9 770 (void)SvREFCNT_inc(cv);
c9bca74a
NIS
771 PL_warnhook = (SV *) cv;
772 }
d7a09b41
SR
773 PL_in_load_module++;
774 /*
775 * The two SVs are magically freed by load_module
776 */
777 Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv);
778 PL_in_load_module--;
779 LEAVE;
780 return PerlIO_find_layer(aTHX_ name, len, 0);
781 }
14a5cf38
JH
782 }
783 PerlIO_debug("Cannot find %.*s\n", (int) len, name);
784 return NULL;
f3862f8b
NIS
785}
786
2a1bc955 787#ifdef USE_ATTRIBUTES_FOR_PERLIO
b13b2135
NIS
788
789static int
790perlio_mg_set(pTHX_ SV *sv, MAGIC *mg)
791{
14a5cf38 792 if (SvROK(sv)) {
dcda55fc
AL
793 IO * const io = GvIOn((GV *) SvRV(sv));
794 PerlIO * const ifp = IoIFP(io);
795 PerlIO * const ofp = IoOFP(io);
14a5cf38
JH
796 Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp);
797 }
798 return 0;
b13b2135
NIS
799}
800
801static int
802perlio_mg_get(pTHX_ SV *sv, MAGIC *mg)
803{
14a5cf38 804 if (SvROK(sv)) {
dcda55fc
AL
805 IO * const io = GvIOn((GV *) SvRV(sv));
806 PerlIO * const ifp = IoIFP(io);
807 PerlIO * const ofp = IoOFP(io);
14a5cf38
JH
808 Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp);
809 }
810 return 0;
b13b2135
NIS
811}
812
813static int
814perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg)
815{
14a5cf38
JH
816 Perl_warn(aTHX_ "clear %" SVf, sv);
817 return 0;
b13b2135
NIS
818}
819
820static int
821perlio_mg_free(pTHX_ SV *sv, MAGIC *mg)
822{
14a5cf38
JH
823 Perl_warn(aTHX_ "free %" SVf, sv);
824 return 0;
b13b2135
NIS
825}
826
827MGVTBL perlio_vtab = {
14a5cf38
JH
828 perlio_mg_get,
829 perlio_mg_set,
22569500 830 NULL, /* len */
14a5cf38
JH
831 perlio_mg_clear,
832 perlio_mg_free
b13b2135
NIS
833};
834
835XS(XS_io_MODIFY_SCALAR_ATTRIBUTES)
836{
14a5cf38 837 dXSARGS;
dcda55fc
AL
838 SV * const sv = SvRV(ST(1));
839 AV * const av = newAV();
14a5cf38
JH
840 MAGIC *mg;
841 int count = 0;
842 int i;
843 sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0);
844 SvRMAGICAL_off(sv);
845 mg = mg_find(sv, PERL_MAGIC_ext);
846 mg->mg_virtual = &perlio_vtab;
847 mg_magical(sv);
848 Perl_warn(aTHX_ "attrib %" SVf, sv);
849 for (i = 2; i < items; i++) {
850 STRLEN len;
dcda55fc
AL
851 const char * const name = SvPV_const(ST(i), len);
852 SV * const layer = PerlIO_find_layer(aTHX_ name, len, 1);
14a5cf38
JH
853 if (layer) {
854 av_push(av, SvREFCNT_inc(layer));
855 }
856 else {
857 ST(count) = ST(i);
858 count++;
859 }
860 }
861 SvREFCNT_dec(av);
862 XSRETURN(count);
863}
864
22569500 865#endif /* USE_ATTIBUTES_FOR_PERLIO */
2a1bc955 866
e3f3bf95
NIS
867SV *
868PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab)
f3862f8b 869{
1c5d1ca7 870 HV * const stash = gv_stashpvn(STR_WITH_LEN("PerlIO::Layer"), TRUE);
46c461b5 871 SV * const sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash);
14a5cf38 872 return sv;
e3f3bf95
NIS
873}
874
5ca1d77f 875XS(XS_PerlIO__Layer__NoWarnings)
c9bca74a 876{
37725cdc 877 /* This is used as a %SIG{__WARN__} handler to supress warnings
c9bca74a
NIS
878 during loading of layers.
879 */
880 dXSARGS;
881 if (items)
e62f0680 882 PerlIO_debug("warning:%s\n",SvPV_nolen_const(ST(0)));
c9bca74a
NIS
883 XSRETURN(0);
884}
885
5ca1d77f 886XS(XS_PerlIO__Layer__find)
0c4f7ff0 887{
14a5cf38
JH
888 dXSARGS;
889 if (items < 2)
890 Perl_croak(aTHX_ "Usage class->find(name[,load])");
891 else {
de009b76 892 STRLEN len;
46c461b5 893 const char * const name = SvPV_const(ST(1), len);
de009b76 894 const bool load = (items > 2) ? SvTRUE(ST(2)) : 0;
46c461b5 895 PerlIO_funcs * const layer = PerlIO_find_layer(aTHX_ name, len, load);
14a5cf38
JH
896 ST(0) =
897 (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) :
898 &PL_sv_undef;
899 XSRETURN(1);
900 }
0c4f7ff0
NIS
901}
902
e3f3bf95
NIS
903void
904PerlIO_define_layer(pTHX_ PerlIO_funcs *tab)
905{
3a1ee7e8
NIS
906 if (!PL_known_layers)
907 PL_known_layers = PerlIO_list_alloc(aTHX);
908 PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv);
fe5a182c 909 PerlIO_debug("define %s %p\n", tab->name, (void*)tab);
f3862f8b
NIS
910}
911
1141d9f8 912int
fcf2db38 913PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names)
1141d9f8 914{
14a5cf38
JH
915 if (names) {
916 const char *s = names;
917 while (*s) {
918 while (isSPACE(*s) || *s == ':')
919 s++;
920 if (*s) {
921 STRLEN llen = 0;
922 const char *e = s;
923 const char *as = Nullch;
924 STRLEN alen = 0;
925 if (!isIDFIRST(*s)) {
926 /*
927 * Message is consistent with how attribute lists are
928 * passed. Even though this means "foo : : bar" is
71200d45 929 * seen as an invalid separator character.
14a5cf38 930 */
de009b76 931 const char q = ((*s == '\'') ? '"' : '\'');
99ef548b 932 if (ckWARN(WARN_LAYER))
22569500 933 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 934 "Invalid separator character %c%c%c in PerlIO layer specification %s",
1e616cf5 935 q, *s, q, s);
93189314 936 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
937 return -1;
938 }
939 do {
940 e++;
941 } while (isALNUM(*e));
942 llen = e - s;
943 if (*e == '(') {
944 int nesting = 1;
945 as = ++e;
946 while (nesting) {
947 switch (*e++) {
948 case ')':
949 if (--nesting == 0)
950 alen = (e - 1) - as;
951 break;
952 case '(':
953 ++nesting;
954 break;
955 case '\\':
956 /*
957 * It's a nul terminated string, not allowed
958 * to \ the terminating null. Anything other
71200d45 959 * character is passed over.
14a5cf38
JH
960 */
961 if (*e++) {
962 break;
963 }
964 /*
71200d45 965 * Drop through
14a5cf38
JH
966 */
967 case '\0':
968 e--;
22569500
NIS
969 if (ckWARN(WARN_LAYER))
970 Perl_warner(aTHX_ packWARN(WARN_LAYER),
b4581f09 971 "Argument list not closed for PerlIO layer \"%.*s\"",
14a5cf38
JH
972 (int) (e - s), s);
973 return -1;
974 default:
975 /*
71200d45 976 * boring.
14a5cf38
JH
977 */
978 break;
979 }
980 }
981 }
982 if (e > s) {
46c461b5 983 PerlIO_funcs * const layer =
14a5cf38
JH
984 PerlIO_find_layer(aTHX_ s, llen, 1);
985 if (layer) {
3a1ee7e8 986 PerlIO_list_push(aTHX_ av, layer,
14a5cf38
JH
987 (as) ? newSVpvn(as,
988 alen) :
989 &PL_sv_undef);
990 }
991 else {
041457d9 992 if (ckWARN(WARN_LAYER))
b4581f09 993 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Unknown PerlIO layer \"%.*s\"",
14a5cf38
JH
994 (int) llen, s);
995 return -1;
996 }
997 }
998 s = e;
999 }
1000 }
1001 }
1002 return 0;
1141d9f8
NIS
1003}
1004
dfebf958 1005void
fcf2db38 1006PerlIO_default_buffer(pTHX_ PerlIO_list_t *av)
dfebf958 1007{
27da23d5 1008 PERLIO_FUNCS_DECL(*tab) = &PerlIO_perlio;
35990314 1009#ifdef PERLIO_USING_CRLF
6ce75a77 1010 tab = &PerlIO_crlf;
846be114 1011#else
6ce75a77 1012 if (PerlIO_stdio.Set_ptrcnt)
22569500 1013 tab = &PerlIO_stdio;
846be114 1014#endif
14a5cf38 1015 PerlIO_debug("Pushing %s\n", tab->name);
3a1ee7e8 1016 PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0),
14a5cf38 1017 &PL_sv_undef);
dfebf958
NIS
1018}
1019
e3f3bf95 1020SV *
14a5cf38 1021PerlIO_arg_fetch(PerlIO_list_t *av, IV n)
e3f3bf95 1022{
14a5cf38 1023 return av->array[n].arg;
e3f3bf95
NIS
1024}
1025
f3862f8b 1026PerlIO_funcs *
14a5cf38 1027PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def)
f3862f8b 1028{
14a5cf38
JH
1029 if (n >= 0 && n < av->cur) {
1030 PerlIO_debug("Layer %" IVdf " is %s\n", n,
1031 av->array[n].funcs->name);
1032 return av->array[n].funcs;
1033 }
1034 if (!def)
1035 Perl_croak(aTHX_ "panic: PerlIO layer array corrupt");
1036 return def;
e3f3bf95
NIS
1037}
1038
4ec2216f
NIS
1039IV
1040PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1041{
8772537c
AL
1042 PERL_UNUSED_ARG(mode);
1043 PERL_UNUSED_ARG(arg);
1044 PERL_UNUSED_ARG(tab);
4ec2216f
NIS
1045 if (PerlIOValid(f)) {
1046 PerlIO_flush(f);
1047 PerlIO_pop(aTHX_ f);
1048 return 0;
1049 }
1050 return -1;
1051}
1052
27da23d5 1053PERLIO_FUNCS_DECL(PerlIO_remove) = {
4ec2216f
NIS
1054 sizeof(PerlIO_funcs),
1055 "pop",
1056 0,
1057 PERLIO_K_DUMMY | PERLIO_K_UTF8,
1058 PerlIOPop_pushed,
1059 NULL,
1060 NULL,
1061 NULL,
1062 NULL,
1063 NULL,
1064 NULL,
1065 NULL,
1066 NULL,
1067 NULL,
1068 NULL,
de009b76
AL
1069 NULL,
1070 NULL,
4ec2216f
NIS
1071 NULL, /* flush */
1072 NULL, /* fill */
1073 NULL,
1074 NULL,
1075 NULL,
1076 NULL,
1077 NULL, /* get_base */
1078 NULL, /* get_bufsiz */
1079 NULL, /* get_ptr */
1080 NULL, /* get_cnt */
1081 NULL, /* set_ptrcnt */
1082};
1083
fcf2db38 1084PerlIO_list_t *
e3f3bf95
NIS
1085PerlIO_default_layers(pTHX)
1086{
3a1ee7e8 1087 if (!PL_def_layerlist) {
dcda55fc 1088 const char * const s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO");
27da23d5 1089 PERLIO_FUNCS_DECL(*osLayer) = &PerlIO_unix;
3a1ee7e8 1090 PL_def_layerlist = PerlIO_list_alloc(aTHX);
27da23d5 1091 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_unix));
979e2c82 1092#if defined(WIN32)
27da23d5 1093 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_win32));
2f8118af 1094#if 0
14a5cf38 1095 osLayer = &PerlIO_win32;
0c4128ad 1096#endif
2f8118af 1097#endif
27da23d5
JH
1098 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_raw));
1099 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_perlio));
1100 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_stdio));
1101 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_crlf));
06da4f11 1102#ifdef HAS_MMAP
27da23d5 1103 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_mmap));
06da4f11 1104#endif
27da23d5
JH
1105 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_utf8));
1106 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_remove));
1107 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_byte));
3a1ee7e8 1108 PerlIO_list_push(aTHX_ PL_def_layerlist,
14a5cf38
JH
1109 PerlIO_find_layer(aTHX_ osLayer->name, 0, 0),
1110 &PL_sv_undef);
1111 if (s) {
3a1ee7e8 1112 PerlIO_parse_layers(aTHX_ PL_def_layerlist, s);
14a5cf38
JH
1113 }
1114 else {
3a1ee7e8 1115 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
14a5cf38 1116 }
1141d9f8 1117 }
3a1ee7e8
NIS
1118 if (PL_def_layerlist->cur < 2) {
1119 PerlIO_default_buffer(aTHX_ PL_def_layerlist);
f3862f8b 1120 }
3a1ee7e8 1121 return PL_def_layerlist;
e3f3bf95
NIS
1122}
1123
0c4f7ff0
NIS
1124void
1125Perl_boot_core_PerlIO(pTHX)
1126{
1127#ifdef USE_ATTRIBUTES_FOR_PERLIO
14a5cf38
JH
1128 newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES,
1129 __FILE__);
0c4f7ff0 1130#endif
14a5cf38 1131 newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__);
c9bca74a 1132 newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__);
0c4f7ff0 1133}
e3f3bf95
NIS
1134
1135PerlIO_funcs *
1136PerlIO_default_layer(pTHX_ I32 n)
1137{
46c461b5 1138 PerlIO_list_t * const av = PerlIO_default_layers(aTHX);
14a5cf38
JH
1139 if (n < 0)
1140 n += av->cur;
27da23d5 1141 return PerlIO_layer_fetch(aTHX_ av, n, PERLIO_FUNCS_CAST(&PerlIO_stdio));
f3862f8b
NIS
1142}
1143
a999f61b
NIS
1144#define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1)
1145#define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0)
60382766
NIS
1146
1147void
1141d9f8 1148PerlIO_stdstreams(pTHX)
60382766 1149{
a1ea730d 1150 if (!PL_perlio) {
14a5cf38
JH
1151 PerlIO_allocate(aTHX);
1152 PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT);
1153 PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT);
1154 PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT);
1155 }
60382766
NIS
1156}
1157
1158PerlIO *
27da23d5 1159PerlIO_push(pTHX_ PerlIO *f, PERLIO_FUNCS_DECL(*tab), const char *mode, SV *arg)
14a5cf38 1160{
2dc2558e
NIS
1161 if (tab->fsize != sizeof(PerlIO_funcs)) {
1162 mismatch:
1163 Perl_croak(aTHX_ "Layer does not match this perl");
1164 }
1165 if (tab->size) {
b464bac0 1166 PerlIOl *l;
2dc2558e
NIS
1167 if (tab->size < sizeof(PerlIOl)) {
1168 goto mismatch;
1169 }
1170 /* Real layer with a data area */
a02a5408 1171 Newxc(l,tab->size,char,PerlIOl);
2dc2558e
NIS
1172 if (l && f) {
1173 Zero(l, tab->size, char);
1174 l->next = *f;
27da23d5 1175 l->tab = (PerlIO_funcs*) tab;
2dc2558e
NIS
1176 *f = l;
1177 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1178 (mode) ? mode : "(Null)", (void*)arg);
210e727c 1179 if (*l->tab->Pushed &&
27da23d5 1180 (*l->tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
2dc2558e
NIS
1181 PerlIO_pop(aTHX_ f);
1182 return NULL;
1183 }
1184 }
1185 }
1186 else if (f) {
1187 /* Pseudo-layer where push does its own stack adjust */
00f51856
NIS
1188 PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name,
1189 (mode) ? mode : "(Null)", (void*)arg);
210e727c 1190 if (tab->Pushed &&
27da23d5 1191 (*tab->Pushed) (aTHX_ f, mode, arg, (PerlIO_funcs*) tab) != 0) {
210e727c 1192 return NULL;
14a5cf38
JH
1193 }
1194 }
1195 return f;
60382766
NIS
1196}
1197
dfebf958 1198IV
86e05cf2
NIS
1199PerlIOBase_binmode(pTHX_ PerlIO *f)
1200{
1201 if (PerlIOValid(f)) {
1202 /* Is layer suitable for raw stream ? */
1203 if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) {
1204 /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */
1205 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1206 }
1207 else {
1208 /* Not suitable - pop it */
1209 PerlIO_pop(aTHX_ f);
1210 }
1211 return 0;
1212 }
1213 return -1;
1214}
1215
1216IV
2dc2558e 1217PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
dfebf958 1218{
8772537c
AL
1219 PERL_UNUSED_ARG(mode);
1220 PERL_UNUSED_ARG(arg);
1221 PERL_UNUSED_ARG(tab);
86e05cf2 1222
04892f78 1223 if (PerlIOValid(f)) {
86e05cf2 1224 PerlIO *t;
de009b76 1225 const PerlIOl *l;
14a5cf38 1226 PerlIO_flush(f);
86e05cf2
NIS
1227 /*
1228 * Strip all layers that are not suitable for a raw stream
1229 */
1230 t = f;
1231 while (t && (l = *t)) {
1232 if (l->tab->Binmode) {
1233 /* Has a handler - normal case */
1234 if ((*l->tab->Binmode)(aTHX_ f) == 0) {
1235 if (*t == l) {
1236 /* Layer still there - move down a layer */
1237 t = PerlIONext(t);
1238 }
1239 }
1240 else {
1241 return -1;
1242 }
14a5cf38
JH
1243 }
1244 else {
86e05cf2
NIS
1245 /* No handler - pop it */
1246 PerlIO_pop(aTHX_ t);
14a5cf38
JH
1247 }
1248 }
86e05cf2
NIS
1249 if (PerlIOValid(f)) {
1250 PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name);
1251 return 0;
1252 }
14a5cf38
JH
1253 }
1254 return -1;
dfebf958
NIS
1255}
1256
ac27b0f5 1257int
14a5cf38 1258PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode,
d9dac8cd 1259 PerlIO_list_t *layers, IV n, IV max)
14a5cf38 1260{
14a5cf38
JH
1261 int code = 0;
1262 while (n < max) {
8772537c 1263 PerlIO_funcs * const tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL);
14a5cf38
JH
1264 if (tab) {
1265 if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) {
1266 code = -1;
1267 break;
1268 }
1269 }
1270 n++;
1271 }
1272 return code;
e3f3bf95
NIS
1273}
1274
1275int
ac27b0f5
NIS
1276PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
1277{
14a5cf38 1278 int code = 0;
53f1b6d2 1279 if (f && names) {
8772537c 1280 PerlIO_list_t * const layers = PerlIO_list_alloc(aTHX);
14a5cf38
JH
1281 code = PerlIO_parse_layers(aTHX_ layers, names);
1282 if (code == 0) {
d9dac8cd 1283 code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur);
14a5cf38 1284 }
3a1ee7e8 1285 PerlIO_list_free(aTHX_ layers);
ac27b0f5 1286 }
14a5cf38 1287 return code;
ac27b0f5
NIS
1288}
1289
f3862f8b 1290
60382766 1291/*--------------------------------------------------------------------------------------*/
14a5cf38 1292/*
71200d45 1293 * Given the abstraction above the public API functions
14a5cf38 1294 */
60382766
NIS
1295
1296int
f5b9d040 1297PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names)
76ced9ad 1298{
68b5363f
PD
1299 PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", (void*)f,
1300 (PerlIOBase(f)) ? PerlIOBase(f)->tab->name : "(Null)",
1301 iotype, mode, (names) ? names : "(Null)");
1302
03c0554d
NIS
1303 if (names) {
1304 /* Do not flush etc. if (e.g.) switching encodings.
1305 if a pushed layer knows it needs to flush lower layers
1306 (for example :unix which is never going to call them)
1307 it can do the flush when it is pushed.
1308 */
1309 return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE;
1310 }
1311 else {
86e05cf2 1312 /* Fake 5.6 legacy of using this call to turn ON O_TEXT */
35990314 1313#ifdef PERLIO_USING_CRLF
03c0554d
NIS
1314 /* Legacy binmode only has meaning if O_TEXT has a value distinct from
1315 O_BINARY so we can look for it in mode.
1316 */
1317 if (!(mode & O_BINARY)) {
1318 /* Text mode */
86e05cf2
NIS
1319 /* FIXME?: Looking down the layer stack seems wrong,
1320 but is a way of reaching past (say) an encoding layer
1321 to flip CRLF-ness of the layer(s) below
1322 */
03c0554d
NIS
1323 while (*f) {
1324 /* Perhaps we should turn on bottom-most aware layer
1325 e.g. Ilya's idea that UNIX TTY could serve
1326 */
1327 if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) {
1328 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) {
1329 /* Not in text mode - flush any pending stuff and flip it */
1330 PerlIO_flush(f);
1331 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
1332 }
1333 /* Only need to turn it on in one layer so we are done */
1334 return TRUE;
ed53a2bb 1335 }
03c0554d 1336 f = PerlIONext(f);
14a5cf38 1337 }
03c0554d
NIS
1338 /* Not finding a CRLF aware layer presumably means we are binary
1339 which is not what was requested - so we failed
1340 We _could_ push :crlf layer but so could caller
1341 */
1342 return FALSE;
14a5cf38 1343 }
6ce75a77 1344#endif
86e05cf2
NIS
1345 /* Legacy binmode is now _defined_ as being equivalent to pushing :raw
1346 So code that used to be here is now in PerlIORaw_pushed().
03c0554d 1347 */
27da23d5 1348 return PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_raw), Nullch, Nullsv) ? TRUE : FALSE;
14a5cf38 1349 }
f5b9d040
NIS
1350}
1351
f5b9d040 1352int
e87a358a 1353PerlIO__close(pTHX_ PerlIO *f)
f5b9d040 1354{
37725cdc 1355 if (PerlIOValid(f)) {
46c461b5 1356 PerlIO_funcs * const tab = PerlIOBase(f)->tab;
37725cdc
NIS
1357 if (tab && tab->Close)
1358 return (*tab->Close)(aTHX_ f);
1359 else
1360 return PerlIOBase_close(aTHX_ f);
1361 }
14a5cf38 1362 else {
93189314 1363 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1364 return -1;
1365 }
76ced9ad
NIS
1366}
1367
b931b1d9 1368int
e87a358a 1369Perl_PerlIO_close(pTHX_ PerlIO *f)
b931b1d9 1370{
de009b76 1371 const int code = PerlIO__close(aTHX_ f);
37725cdc
NIS
1372 while (PerlIOValid(f)) {
1373 PerlIO_pop(aTHX_ f);
f6c77cf1 1374 }
14a5cf38 1375 return code;
b931b1d9
NIS
1376}
1377
b931b1d9 1378int
e87a358a 1379Perl_PerlIO_fileno(pTHX_ PerlIO *f)
b931b1d9 1380{
b32dd47e 1381 Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f));
b931b1d9
NIS
1382}
1383
1141d9f8
NIS
1384static const char *
1385PerlIO_context_layers(pTHX_ const char *mode)
1386{
14a5cf38
JH
1387 const char *type = NULL;
1388 /*
71200d45 1389 * Need to supply default layer info from open.pm
14a5cf38
JH
1390 */
1391 if (PL_curcop) {
dcda55fc 1392 SV * const layers = PL_curcop->cop_io;
14a5cf38
JH
1393 if (layers) {
1394 STRLEN len;
b83604b4 1395 type = SvPV_const(layers, len);
14a5cf38
JH
1396 if (type && mode[0] != 'r') {
1397 /*
71200d45 1398 * Skip to write part
14a5cf38 1399 */
dcda55fc 1400 const char * const s = strchr(type, 0);
eb160463 1401 if (s && (STRLEN)(s - type) < len) {
14a5cf38
JH
1402 type = s + 1;
1403 }
1404 }
1405 }
1406 }
1407 return type;
1141d9f8
NIS
1408}
1409
fcf2db38 1410static PerlIO_funcs *
2edd7e44
NIS
1411PerlIO_layer_from_ref(pTHX_ SV *sv)
1412{
14a5cf38 1413 /*
71200d45 1414 * For any scalar type load the handler which is bundled with perl
14a5cf38
JH
1415 */
1416 if (SvTYPE(sv) < SVt_PVAV)
e934609f 1417 return PerlIO_find_layer(aTHX_ "scalar", 6, 1);
14a5cf38
JH
1418
1419 /*
71200d45 1420 * For other types allow if layer is known but don't try and load it
14a5cf38
JH
1421 */
1422 switch (SvTYPE(sv)) {
1423 case SVt_PVAV:
1424 return PerlIO_find_layer(aTHX_ "Array", 5, 0);
1425 case SVt_PVHV:
1426 return PerlIO_find_layer(aTHX_ "Hash", 4, 0);
1427 case SVt_PVCV:
1428 return PerlIO_find_layer(aTHX_ "Code", 4, 0);
1429 case SVt_PVGV:
1430 return PerlIO_find_layer(aTHX_ "Glob", 4, 0);
1431 }
1432 return NULL;
2edd7e44
NIS
1433}
1434
fcf2db38 1435PerlIO_list_t *
14a5cf38
JH
1436PerlIO_resolve_layers(pTHX_ const char *layers,
1437 const char *mode, int narg, SV **args)
1438{
1439 PerlIO_list_t *def = PerlIO_default_layers(aTHX);
1440 int incdef = 1;
a1ea730d 1441 if (!PL_perlio)
14a5cf38
JH
1442 PerlIO_stdstreams(aTHX);
1443 if (narg) {
dcda55fc 1444 SV * const arg = *args;
14a5cf38 1445 /*
71200d45
NIS
1446 * If it is a reference but not an object see if we have a handler
1447 * for it
14a5cf38
JH
1448 */
1449 if (SvROK(arg) && !sv_isobject(arg)) {
46c461b5 1450 PerlIO_funcs * const handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg));
14a5cf38 1451 if (handler) {
3a1ee7e8
NIS
1452 def = PerlIO_list_alloc(aTHX);
1453 PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef);
14a5cf38
JH
1454 incdef = 0;
1455 }
1456 /*
e934609f 1457 * Don't fail if handler cannot be found :via(...) etc. may do
14a5cf38 1458 * something sensible else we will just stringfy and open
71200d45 1459 * resulting string.
14a5cf38
JH
1460 */
1461 }
1462 }
9fe371da 1463 if (!layers || !*layers)
14a5cf38
JH
1464 layers = PerlIO_context_layers(aTHX_ mode);
1465 if (layers && *layers) {
1466 PerlIO_list_t *av;
1467 if (incdef) {
de009b76 1468 IV i;
3a1ee7e8 1469 av = PerlIO_list_alloc(aTHX);
14a5cf38 1470 for (i = 0; i < def->cur; i++) {
3a1ee7e8 1471 PerlIO_list_push(aTHX_ av, def->array[i].funcs,
14a5cf38
JH
1472 def->array[i].arg);
1473 }
1474 }
1475 else {
1476 av = def;
1477 }
0cff2cf3
NIS
1478 if (PerlIO_parse_layers(aTHX_ av, layers) == 0) {
1479 return av;
1480 }
1481 else {
1482 PerlIO_list_free(aTHX_ av);
1483 return (PerlIO_list_t *) NULL;
1484 }
14a5cf38
JH
1485 }
1486 else {
1487 if (incdef)
1488 def->refcnt++;
1489 return def;
1490 }
ee518936
NIS
1491}
1492
1493PerlIO *
14a5cf38
JH
1494PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
1495 int imode, int perm, PerlIO *f, int narg, SV **args)
1496{
1497 if (!f && narg == 1 && *args == &PL_sv_undef) {
1498 if ((f = PerlIO_tmpfile())) {
9fe371da 1499 if (!layers || !*layers)
14a5cf38
JH
1500 layers = PerlIO_context_layers(aTHX_ mode);
1501 if (layers && *layers)
1502 PerlIO_apply_layers(aTHX_ f, mode, layers);
1503 }
1504 }
1505 else {
de009b76 1506 PerlIO_list_t *layera;
14a5cf38
JH
1507 IV n;
1508 PerlIO_funcs *tab = NULL;
04892f78 1509 if (PerlIOValid(f)) {
14a5cf38 1510 /*
71200d45
NIS
1511 * This is "reopen" - it is not tested as perl does not use it
1512 * yet
14a5cf38
JH
1513 */
1514 PerlIOl *l = *f;
3a1ee7e8 1515 layera = PerlIO_list_alloc(aTHX);
14a5cf38 1516 while (l) {
46c461b5 1517 SV * const arg = (l->tab->Getarg)
04892f78
NIS
1518 ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0)
1519 : &PL_sv_undef;
3a1ee7e8 1520 PerlIO_list_push(aTHX_ layera, l->tab, arg);
14a5cf38
JH
1521 l = *PerlIONext(&l);
1522 }
1523 }
1524 else {
1525 layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args);
0cff2cf3
NIS
1526 if (!layera) {
1527 return NULL;
1528 }
14a5cf38
JH
1529 }
1530 /*
71200d45 1531 * Start at "top" of layer stack
14a5cf38
JH
1532 */
1533 n = layera->cur - 1;
1534 while (n >= 0) {
46c461b5 1535 PerlIO_funcs * const t = PerlIO_layer_fetch(aTHX_ layera, n, NULL);
14a5cf38
JH
1536 if (t && t->Open) {
1537 tab = t;
1538 break;
1539 }
1540 n--;
1541 }
1542 if (tab) {
1543 /*
71200d45 1544 * Found that layer 'n' can do opens - call it
14a5cf38 1545 */
7cf31beb 1546 if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) {
3b8752bb 1547 Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name);
7cf31beb 1548 }
14a5cf38 1549 PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n",
355d3743
PD
1550 tab->name, layers ? layers : "(Null)", mode, fd,
1551 imode, perm, (void*)f, narg, (void*)args);
210e727c
JH
1552 if (tab->Open)
1553 f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm,
1554 f, narg, args);
1555 else {
1556 SETERRNO(EINVAL, LIB_INVARG);
1557 f = NULL;
1558 }
14a5cf38
JH
1559 if (f) {
1560 if (n + 1 < layera->cur) {
1561 /*
1562 * More layers above the one that we used to open -
71200d45 1563 * apply them now
14a5cf38 1564 */
d9dac8cd
NIS
1565 if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) {
1566 /* If pushing layers fails close the file */
1567 PerlIO_close(f);
14a5cf38
JH
1568 f = NULL;
1569 }
1570 }
1571 }
1572 }
3a1ee7e8 1573 PerlIO_list_free(aTHX_ layera);
14a5cf38
JH
1574 }
1575 return f;
ee518936 1576}
b931b1d9
NIS
1577
1578
9e353e3b 1579SSize_t
e87a358a 1580Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 1581{
b32dd47e 1582 Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1583}
1584
313ca112 1585SSize_t
e87a358a 1586Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1587{
b32dd47e 1588 Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1589}
1590
9e353e3b 1591SSize_t
e87a358a 1592Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 1593{
b32dd47e 1594 Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count));
760ac839
LW
1595}
1596
6f9d8c32 1597int
e87a358a 1598Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
760ac839 1599{
b32dd47e 1600 Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence));
760ac839
LW
1601}
1602
9e353e3b 1603Off_t
e87a358a 1604Perl_PerlIO_tell(pTHX_ PerlIO *f)
760ac839 1605{
b32dd47e 1606 Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f));
760ac839
LW
1607}
1608
6f9d8c32 1609int
e87a358a 1610Perl_PerlIO_flush(pTHX_ PerlIO *f)
760ac839 1611{
14a5cf38
JH
1612 if (f) {
1613 if (*f) {
de009b76 1614 const PerlIO_funcs *tab = PerlIOBase(f)->tab;
1b7a0411
JH
1615
1616 if (tab && tab->Flush)
f62ce20a 1617 return (*tab->Flush) (aTHX_ f);
1b7a0411
JH
1618 else
1619 return 0; /* If no Flush defined, silently succeed. */
14a5cf38
JH
1620 }
1621 else {
fe5a182c 1622 PerlIO_debug("Cannot flush f=%p\n", (void*)f);
93189314 1623 SETERRNO(EBADF, SS_IVCHAN);
14a5cf38
JH
1624 return -1;
1625 }
1626 }
1627 else {
1628 /*
1629 * Is it good API design to do flush-all on NULL, a potentially
1630 * errorneous input? Maybe some magical value (PerlIO*
1631 * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar
1632 * things on fflush(NULL), but should we be bound by their design
71200d45 1633 * decisions? --jhi
14a5cf38 1634 */
a1ea730d 1635 PerlIO **table = &PL_perlio;
14a5cf38
JH
1636 int code = 0;
1637 while ((f = *table)) {
1638 int i;
1639 table = (PerlIO **) (f++);
1640 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1641 if (*f && PerlIO_flush(f) != 0)
1642 code = -1;
1643 f++;
1644 }
1645 }
1646 return code;
1647 }
760ac839
LW
1648}
1649
a9c883f6 1650void
f62ce20a 1651PerlIOBase_flush_linebuf(pTHX)
a9c883f6 1652{
a1ea730d 1653 PerlIO **table = &PL_perlio;
14a5cf38
JH
1654 PerlIO *f;
1655 while ((f = *table)) {
1656 int i;
1657 table = (PerlIO **) (f++);
1658 for (i = 1; i < PERLIO_TABLE_SIZE; i++) {
1659 if (*f
1660 && (PerlIOBase(f)->
1661 flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1662 == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE))
1663 PerlIO_flush(f);
1664 f++;
1665 }
a9c883f6 1666 }
a9c883f6
NIS
1667}
1668
06da4f11 1669int
e87a358a 1670Perl_PerlIO_fill(pTHX_ PerlIO *f)
06da4f11 1671{
b32dd47e 1672 Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f));
06da4f11
NIS
1673}
1674
f3862f8b
NIS
1675int
1676PerlIO_isutf8(PerlIO *f)
1677{
1b7a0411
JH
1678 if (PerlIOValid(f))
1679 return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0;
1680 else
1681 SETERRNO(EBADF, SS_IVCHAN);
37725cdc 1682
1b7a0411 1683 return -1;
f3862f8b
NIS
1684}
1685
6f9d8c32 1686int
e87a358a 1687Perl_PerlIO_eof(pTHX_ PerlIO *f)
760ac839 1688{
b32dd47e 1689 Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f));
9e353e3b
NIS
1690}
1691
9e353e3b 1692int
e87a358a 1693Perl_PerlIO_error(pTHX_ PerlIO *f)
9e353e3b 1694{
b32dd47e 1695 Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f));
9e353e3b
NIS
1696}
1697
9e353e3b 1698void
e87a358a 1699Perl_PerlIO_clearerr(pTHX_ PerlIO *f)
9e353e3b 1700{
b32dd47e 1701 Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f));
9e353e3b
NIS
1702}
1703
9e353e3b 1704void
e87a358a 1705Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1706{
b32dd47e 1707 Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f));
9e353e3b
NIS
1708}
1709
9e353e3b
NIS
1710int
1711PerlIO_has_base(PerlIO *f)
1712{
1b7a0411 1713 if (PerlIOValid(f)) {
46c461b5 1714 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1715
1716 if (tab)
1717 return (tab->Get_base != NULL);
1718 SETERRNO(EINVAL, LIB_INVARG);
1719 }
1720 else
1721 SETERRNO(EBADF, SS_IVCHAN);
1722
1723 return 0;
760ac839
LW
1724}
1725
9e353e3b
NIS
1726int
1727PerlIO_fast_gets(PerlIO *f)
760ac839 1728{
04892f78 1729 if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
46c461b5 1730 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1731
1732 if (tab)
1733 return (tab->Set_ptrcnt != NULL);
1734 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1735 }
1b7a0411
JH
1736 else
1737 SETERRNO(EBADF, SS_IVCHAN);
1738
14a5cf38 1739 return 0;
9e353e3b
NIS
1740}
1741
9e353e3b
NIS
1742int
1743PerlIO_has_cntptr(PerlIO *f)
1744{
04892f78 1745 if (PerlIOValid(f)) {
46c461b5 1746 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1747
1748 if (tab)
1749 return (tab->Get_ptr != NULL && tab->Get_cnt != NULL);
1750 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1751 }
1b7a0411
JH
1752 else
1753 SETERRNO(EBADF, SS_IVCHAN);
1754
14a5cf38 1755 return 0;
9e353e3b
NIS
1756}
1757
9e353e3b
NIS
1758int
1759PerlIO_canset_cnt(PerlIO *f)
1760{
04892f78 1761 if (PerlIOValid(f)) {
46c461b5 1762 const PerlIO_funcs * const tab = PerlIOBase(f)->tab;
1b7a0411
JH
1763
1764 if (tab)
1765 return (tab->Set_ptrcnt != NULL);
1766 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 1767 }
1b7a0411
JH
1768 else
1769 SETERRNO(EBADF, SS_IVCHAN);
1770
14a5cf38 1771 return 0;
760ac839
LW
1772}
1773
888911fc 1774STDCHAR *
e87a358a 1775Perl_PerlIO_get_base(pTHX_ PerlIO *f)
760ac839 1776{
b32dd47e 1777 Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f));
9e353e3b
NIS
1778}
1779
9e353e3b 1780int
e87a358a 1781Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 1782{
b32dd47e 1783 Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f));
9e353e3b
NIS
1784}
1785
9e353e3b 1786STDCHAR *
e87a358a 1787Perl_PerlIO_get_ptr(pTHX_ PerlIO *f)
9e353e3b 1788{
b32dd47e 1789 Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f));
9e353e3b
NIS
1790}
1791
05d1247b 1792int
e87a358a 1793Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1794{
b32dd47e 1795 Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f));
9e353e3b
NIS
1796}
1797
9e353e3b 1798void
e87a358a 1799Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1800{
b32dd47e 1801 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt));
9e353e3b
NIS
1802}
1803
9e353e3b 1804void
e87a358a 1805Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1806{
b32dd47e 1807 Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt));
9e353e3b
NIS
1808}
1809
4ec2216f 1810
9e353e3b 1811/*--------------------------------------------------------------------------------------*/
14a5cf38 1812/*
71200d45 1813 * utf8 and raw dummy layers
14a5cf38 1814 */
dfebf958 1815
26fb694e 1816IV
2dc2558e 1817PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
26fb694e 1818{
8772537c
AL
1819 PERL_UNUSED_ARG(mode);
1820 PERL_UNUSED_ARG(arg);
00f51856 1821 if (PerlIOValid(f)) {
14a5cf38
JH
1822 if (tab->kind & PERLIO_K_UTF8)
1823 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1824 else
1825 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1826 return 0;
1827 }
1828 return -1;
26fb694e
NIS
1829}
1830
27da23d5 1831PERLIO_FUNCS_DECL(PerlIO_utf8) = {
2dc2558e 1832 sizeof(PerlIO_funcs),
14a5cf38 1833 "utf8",
2dc2558e 1834 0,
6874a2de 1835 PERLIO_K_DUMMY | PERLIO_K_UTF8,
14a5cf38
JH
1836 PerlIOUtf8_pushed,
1837 NULL,
1838 NULL,
1839 NULL,
1840 NULL,
1841 NULL,
1842 NULL,
1843 NULL,
1844 NULL,
1845 NULL,
1846 NULL,
de009b76
AL
1847 NULL,
1848 NULL,
22569500
NIS
1849 NULL, /* flush */
1850 NULL, /* fill */
14a5cf38
JH
1851 NULL,
1852 NULL,
1853 NULL,
1854 NULL,
22569500
NIS
1855 NULL, /* get_base */
1856 NULL, /* get_bufsiz */
1857 NULL, /* get_ptr */
1858 NULL, /* get_cnt */
1859 NULL, /* set_ptrcnt */
26fb694e
NIS
1860};
1861
27da23d5 1862PERLIO_FUNCS_DECL(PerlIO_byte) = {
2dc2558e 1863 sizeof(PerlIO_funcs),
14a5cf38 1864 "bytes",
2dc2558e 1865 0,
14a5cf38
JH
1866 PERLIO_K_DUMMY,
1867 PerlIOUtf8_pushed,
1868 NULL,
1869 NULL,
1870 NULL,
1871 NULL,
1872 NULL,
1873 NULL,
1874 NULL,
1875 NULL,
1876 NULL,
1877 NULL,
de009b76
AL
1878 NULL,
1879 NULL,
22569500
NIS
1880 NULL, /* flush */
1881 NULL, /* fill */
14a5cf38
JH
1882 NULL,
1883 NULL,
1884 NULL,
1885 NULL,
22569500
NIS
1886 NULL, /* get_base */
1887 NULL, /* get_bufsiz */
1888 NULL, /* get_ptr */
1889 NULL, /* get_cnt */
1890 NULL, /* set_ptrcnt */
dfebf958
NIS
1891};
1892
1893PerlIO *
14a5cf38
JH
1894PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1895 IV n, const char *mode, int fd, int imode, int perm,
1896 PerlIO *old, int narg, SV **args)
dfebf958 1897{
8772537c
AL
1898 PerlIO_funcs * const tab = PerlIO_default_btm();
1899 PERL_UNUSED_ARG(self);
210e727c
JH
1900 if (tab && tab->Open)
1901 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1902 old, narg, args);
1903 SETERRNO(EINVAL, LIB_INVARG);
1904 return NULL;
dfebf958
NIS
1905}
1906
27da23d5 1907PERLIO_FUNCS_DECL(PerlIO_raw) = {
2dc2558e 1908 sizeof(PerlIO_funcs),
14a5cf38 1909 "raw",
2dc2558e 1910 0,
14a5cf38
JH
1911 PERLIO_K_DUMMY,
1912 PerlIORaw_pushed,
1913 PerlIOBase_popped,
1914 PerlIORaw_open,
1915 NULL,
1916 NULL,
1917 NULL,
1918 NULL,
1919 NULL,
1920 NULL,
1921 NULL,
1922 NULL,
de009b76
AL
1923 NULL,
1924 NULL,
22569500
NIS
1925 NULL, /* flush */
1926 NULL, /* fill */
14a5cf38
JH
1927 NULL,
1928 NULL,
1929 NULL,
1930 NULL,
22569500
NIS
1931 NULL, /* get_base */
1932 NULL, /* get_bufsiz */
1933 NULL, /* get_ptr */
1934 NULL, /* get_cnt */
1935 NULL, /* set_ptrcnt */
dfebf958
NIS
1936};
1937/*--------------------------------------------------------------------------------------*/
1938/*--------------------------------------------------------------------------------------*/
14a5cf38 1939/*
71200d45 1940 * "Methods" of the "base class"
14a5cf38 1941 */
9e353e3b
NIS
1942
1943IV
f62ce20a 1944PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1945{
04892f78 1946 return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1;
9e353e3b
NIS
1947}
1948
f5b9d040 1949char *
81428673 1950PerlIO_modestr(PerlIO * f, char *buf)
14a5cf38
JH
1951{
1952 char *s = buf;
81428673 1953 if (PerlIOValid(f)) {
de009b76 1954 const IV flags = PerlIOBase(f)->flags;
81428673
NIS
1955 if (flags & PERLIO_F_APPEND) {
1956 *s++ = 'a';
1957 if (flags & PERLIO_F_CANREAD) {
1958 *s++ = '+';
1959 }
14a5cf38 1960 }
81428673
NIS
1961 else if (flags & PERLIO_F_CANREAD) {
1962 *s++ = 'r';
1963 if (flags & PERLIO_F_CANWRITE)
1964 *s++ = '+';
1965 }
1966 else if (flags & PERLIO_F_CANWRITE) {
1967 *s++ = 'w';
1968 if (flags & PERLIO_F_CANREAD) {
1969 *s++ = '+';
1970 }
14a5cf38 1971 }
35990314 1972#ifdef PERLIO_USING_CRLF
81428673
NIS
1973 if (!(flags & PERLIO_F_CRLF))
1974 *s++ = 'b';
5f1a76d0 1975#endif
81428673 1976 }
14a5cf38
JH
1977 *s = '\0';
1978 return buf;
f5b9d040
NIS
1979}
1980
81428673 1981
76ced9ad 1982IV
2dc2558e 1983PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
9e353e3b 1984{
de009b76 1985 PerlIOl * const l = PerlIOBase(f);
8772537c 1986 PERL_UNUSED_ARG(arg);
de009b76 1987
14a5cf38
JH
1988 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1989 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1990 if (tab->Set_ptrcnt != NULL)
1991 l->flags |= PERLIO_F_FASTGETS;
1992 if (mode) {
3b6c1aba 1993 if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
14a5cf38
JH
1994 mode++;
1995 switch (*mode++) {
1996 case 'r':
1997 l->flags |= PERLIO_F_CANREAD;
1998 break;
1999 case 'a':
2000 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
2001 break;
2002 case 'w':
2003 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
2004 break;
2005 default:
93189314 2006 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2007 return -1;
2008 }
2009 while (*mode) {
2010 switch (*mode++) {
2011 case '+':
2012 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
2013 break;
2014 case 'b':
2015 l->flags &= ~PERLIO_F_CRLF;
2016 break;
2017 case 't':
2018 l->flags |= PERLIO_F_CRLF;
2019 break;
2020 default:
93189314 2021 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2022 return -1;
2023 }
2024 }
2025 }
2026 else {
2027 if (l->next) {
2028 l->flags |= l->next->flags &
2029 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
2030 PERLIO_F_APPEND);
2031 }
2032 }
5e2ab84b 2033#if 0
14a5cf38
JH
2034 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
2035 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
2036 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 2037#endif
14a5cf38 2038 return 0;
76ced9ad
NIS
2039}
2040
2041IV
f62ce20a 2042PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 2043{
8772537c 2044 PERL_UNUSED_ARG(f);
14a5cf38 2045 return 0;
760ac839
LW
2046}
2047
9e353e3b 2048SSize_t
f62ce20a 2049PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2050{
14a5cf38 2051 /*
71200d45 2052 * Save the position as current head considers it
14a5cf38 2053 */
de009b76 2054 const Off_t old = PerlIO_tell(f);
27da23d5 2055 PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_pending), "r", Nullsv);
14a5cf38 2056 PerlIOSelf(f, PerlIOBuf)->posn = old;
de009b76 2057 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
9e353e3b
NIS
2058}
2059
f6c77cf1 2060SSize_t
f62ce20a 2061PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 2062{
14a5cf38
JH
2063 STDCHAR *buf = (STDCHAR *) vbuf;
2064 if (f) {
263df5f1
JH
2065 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD)) {
2066 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2067 SETERRNO(EBADF, SS_IVCHAN);
2068 return 0;
2069 }
14a5cf38 2070 while (count > 0) {
93c2c2ec
IZ
2071 get_cnt:
2072 {
14a5cf38
JH
2073 SSize_t avail = PerlIO_get_cnt(f);
2074 SSize_t take = 0;
2075 if (avail > 0)
eb160463 2076 take = ((SSize_t)count < avail) ? count : avail;
14a5cf38
JH
2077 if (take > 0) {
2078 STDCHAR *ptr = PerlIO_get_ptr(f);
2079 Copy(ptr, buf, take, STDCHAR);
2080 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
2081 count -= take;
2082 buf += take;
93c2c2ec
IZ
2083 if (avail == 0) /* set_ptrcnt could have reset avail */
2084 goto get_cnt;
14a5cf38
JH
2085 }
2086 if (count > 0 && avail <= 0) {
2087 if (PerlIO_fill(f) != 0)
2088 break;
2089 }
93c2c2ec 2090 }
14a5cf38
JH
2091 }
2092 return (buf - (STDCHAR *) vbuf);
2093 }
f6c77cf1 2094 return 0;
f6c77cf1
NIS
2095}
2096
9e353e3b 2097IV
f62ce20a 2098PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 2099{
8772537c 2100 PERL_UNUSED_ARG(f);
14a5cf38 2101 return 0;
9e353e3b
NIS
2102}
2103
2104IV
f62ce20a 2105PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 2106{
8772537c 2107 PERL_UNUSED_ARG(f);
14a5cf38 2108 return -1;
06da4f11
NIS
2109}
2110
2111IV
f62ce20a 2112PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 2113{
37725cdc
NIS
2114 IV code = -1;
2115 if (PerlIOValid(f)) {
2116 PerlIO *n = PerlIONext(f);
2117 code = PerlIO_flush(f);
2118 PerlIOBase(f)->flags &=
2119 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2120 while (PerlIOValid(n)) {
de009b76 2121 const PerlIO_funcs * const tab = PerlIOBase(n)->tab;
37725cdc
NIS
2122 if (tab && tab->Close) {
2123 if ((*tab->Close)(aTHX_ n) != 0)
2124 code = -1;
2125 break;
2126 }
2127 else {
2128 PerlIOBase(n)->flags &=
2129 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
2130 }
2131 n = PerlIONext(n);
2132 }
2133 }
2134 else {
2135 SETERRNO(EBADF, SS_IVCHAN);
2136 }
14a5cf38 2137 return code;
9e353e3b
NIS
2138}
2139
2140IV
f62ce20a 2141PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 2142{
04892f78 2143 if (PerlIOValid(f)) {
14a5cf38
JH
2144 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
2145 }
2146 return 1;
9e353e3b
NIS
2147}
2148
2149IV
f62ce20a 2150PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 2151{
04892f78 2152 if (PerlIOValid(f)) {
14a5cf38
JH
2153 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
2154 }
2155 return 1;
9e353e3b
NIS
2156}
2157
2158void
f62ce20a 2159PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 2160{
04892f78 2161 if (PerlIOValid(f)) {
dcda55fc 2162 PerlIO * const n = PerlIONext(f);
14a5cf38 2163 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
04892f78 2164 if (PerlIOValid(n))
14a5cf38
JH
2165 PerlIO_clearerr(n);
2166 }
9e353e3b
NIS
2167}
2168
2169void
f62ce20a 2170PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 2171{
04892f78 2172 if (PerlIOValid(f)) {
14a5cf38
JH
2173 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
2174 }
9e353e3b
NIS
2175}
2176
93a8090d
NIS
2177SV *
2178PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
2179{
2180 if (!arg)
2181 return Nullsv;
2182#ifdef sv_dup
2183 if (param) {
2184 return sv_dup(arg, param);
2185 }
2186 else {
2187 return newSVsv(arg);
2188 }
2189#else
1b6737cc 2190 PERL_UNUSED_ARG(param);
93a8090d
NIS
2191 return newSVsv(arg);
2192#endif
2193}
2194
2195PerlIO *
ecdeb87c 2196PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d 2197{
1b6737cc 2198 PerlIO * const nexto = PerlIONext(o);
04892f78 2199 if (PerlIOValid(nexto)) {
de009b76 2200 const PerlIO_funcs * const tab = PerlIOBase(nexto)->tab;
37725cdc
NIS
2201 if (tab && tab->Dup)
2202 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
2203 else
2204 f = PerlIOBase_dup(aTHX_ f, nexto, param, flags);
93a8090d
NIS
2205 }
2206 if (f) {
dcda55fc 2207 PerlIO_funcs * const self = PerlIOBase(o)->tab;
210e727c 2208 SV *arg;
93a8090d 2209 char buf[8];
fe5a182c
JH
2210 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
2211 self->name, (void*)f, (void*)o, (void*)param);
210e727c
JH
2212 if (self->Getarg)
2213 arg = (*self->Getarg)(aTHX_ o, param, flags);
2214 else {
37725cdc 2215 arg = Nullsv;
93a8090d
NIS
2216 }
2217 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
ecdeb87c 2218 if (arg) {
93a8090d
NIS
2219 SvREFCNT_dec(arg);
2220 }
2221 }
2222 return f;
2223}
2224
168d5872 2225#ifdef USE_THREADS
93a8090d 2226perl_mutex PerlIO_mutex;
93a8090d 2227#endif
27da23d5
JH
2228
2229/* PL_perlio_fd_refcnt[] is in intrpvar.h */
93a8090d
NIS
2230
2231void
2232PerlIO_init(pTHX)
2233{
2234 /* Place holder for stdstreams call ??? */
168d5872 2235#ifdef USE_THREADS
27da23d5 2236 MUTEX_INIT(&PerlIO_mutex);
93a8090d
NIS
2237#endif
2238}
2239
168d5872
NIS
2240void
2241PerlIOUnix_refcnt_inc(int fd)
2242{
27da23d5 2243 dTHX;
168d5872
NIS
2244 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2245#ifdef USE_THREADS
2246 MUTEX_LOCK(&PerlIO_mutex);
2247#endif
27da23d5
JH
2248 PL_perlio_fd_refcnt[fd]++;
2249 PerlIO_debug("fd %d refcnt=%d\n",fd,PL_perlio_fd_refcnt[fd]);
168d5872
NIS
2250#ifdef USE_THREADS
2251 MUTEX_UNLOCK(&PerlIO_mutex);
2252#endif
2253 }
2254}
2255
168d5872
NIS
2256int
2257PerlIOUnix_refcnt_dec(int fd)
2258{
27da23d5 2259 dTHX;
168d5872
NIS
2260 int cnt = 0;
2261 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
2262#ifdef USE_THREADS
2263 MUTEX_LOCK(&PerlIO_mutex);
2264#endif
27da23d5 2265 cnt = --PL_perlio_fd_refcnt[fd];
168d5872
NIS
2266 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
2267#ifdef USE_THREADS
2268 MUTEX_UNLOCK(&PerlIO_mutex);
2269#endif
2270 }
2271 return cnt;
2272}
2273
694c95cf
JH
2274void
2275PerlIO_cleanup(pTHX)
2276{
2277 int i;
2278#ifdef USE_ITHREADS
9f4bd222
NIS
2279 PerlIO_debug("Cleanup layers for %p\n",aTHX);
2280#else
2281 PerlIO_debug("Cleanup layers\n");
694c95cf
JH
2282#endif
2283 /* Raise STDIN..STDERR refcount so we don't close them */
2284 for (i=0; i < 3; i++)
2285 PerlIOUnix_refcnt_inc(i);
2286 PerlIO_cleantable(aTHX_ &PL_perlio);
2287 /* Restore STDIN..STDERR refcount */
2288 for (i=0; i < 3; i++)
2289 PerlIOUnix_refcnt_dec(i);
9f4bd222
NIS
2290
2291 if (PL_known_layers) {
2292 PerlIO_list_free(aTHX_ PL_known_layers);
2293 PL_known_layers = NULL;
2294 }
27da23d5 2295 if (PL_def_layerlist) {
9f4bd222
NIS
2296 PerlIO_list_free(aTHX_ PL_def_layerlist);
2297 PL_def_layerlist = NULL;
2298 }
694c95cf
JH
2299}
2300
2301
2302
9e353e3b 2303/*--------------------------------------------------------------------------------------*/
14a5cf38 2304/*
71200d45 2305 * Bottom-most level for UNIX-like case
14a5cf38 2306 */
9e353e3b 2307
14a5cf38 2308typedef struct {
22569500
NIS
2309 struct _PerlIO base; /* The generic part */
2310 int fd; /* UNIX like file descriptor */
2311 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2312} PerlIOUnix;
2313
6f9d8c32 2314int
9e353e3b 2315PerlIOUnix_oflags(const char *mode)
760ac839 2316{
14a5cf38 2317 int oflags = -1;
3b6c1aba 2318 if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
14a5cf38
JH
2319 mode++;
2320 switch (*mode) {
2321 case 'r':
2322 oflags = O_RDONLY;
2323 if (*++mode == '+') {
2324 oflags = O_RDWR;
2325 mode++;
2326 }
2327 break;
2328
2329 case 'w':
2330 oflags = O_CREAT | O_TRUNC;
2331 if (*++mode == '+') {
2332 oflags |= O_RDWR;
2333 mode++;
2334 }
2335 else
2336 oflags |= O_WRONLY;
2337 break;
2338
2339 case 'a':
2340 oflags = O_CREAT | O_APPEND;
2341 if (*++mode == '+') {
2342 oflags |= O_RDWR;
2343 mode++;
2344 }
2345 else
2346 oflags |= O_WRONLY;
2347 break;
2348 }
2349 if (*mode == 'b') {
2350 oflags |= O_BINARY;
2351 oflags &= ~O_TEXT;
2352 mode++;
2353 }
2354 else if (*mode == 't') {
2355 oflags |= O_TEXT;
2356 oflags &= ~O_BINARY;
2357 mode++;
2358 }
2359 /*
71200d45 2360 * Always open in binary mode
14a5cf38
JH
2361 */
2362 oflags |= O_BINARY;
2363 if (*mode || oflags == -1) {
93189314 2364 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38
JH
2365 oflags = -1;
2366 }
2367 return oflags;
9e353e3b
NIS
2368}
2369
2370IV
f62ce20a 2371PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2372{
14a5cf38 2373 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2374}
2375
aa063c35
NIS
2376static void
2377PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode)
4b803d04 2378{
de009b76 2379 PerlIOUnix * const s = PerlIOSelf(f, PerlIOUnix);
6caa5a9c 2380#if defined(WIN32)
aa063c35
NIS
2381 Stat_t st;
2382 if (PerlLIO_fstat(fd, &st) == 0) {
6caa5a9c 2383 if (!S_ISREG(st.st_mode)) {
aa063c35 2384 PerlIO_debug("%d is not regular file\n",fd);
6caa5a9c
NIS
2385 PerlIOBase(f)->flags |= PERLIO_F_NOTREG;
2386 }
aa063c35
NIS
2387 else {
2388 PerlIO_debug("%d _is_ a regular file\n",fd);
2389 }
6caa5a9c
NIS
2390 }
2391#endif
aa063c35
NIS
2392 s->fd = fd;
2393 s->oflags = imode;
2394 PerlIOUnix_refcnt_inc(fd);
2395}
2396
2397IV
2398PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2399{
2400 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
14a5cf38 2401 if (*PerlIONext(f)) {
4b069b44 2402 /* We never call down so do any pending stuff now */
03c0554d 2403 PerlIO_flush(PerlIONext(f));
14a5cf38 2404 /*
71200d45 2405 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2406 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2407 * Should the value on NULL mode be 0 or -1?
14a5cf38 2408 */
acbd16bf 2409 PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)),
aa063c35 2410 mode ? PerlIOUnix_oflags(mode) : -1);
14a5cf38
JH
2411 }
2412 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
6caa5a9c 2413
14a5cf38 2414 return code;
4b803d04
NIS
2415}
2416
c2fcde81
JH
2417IV
2418PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
2419{
de009b76 2420 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
0723351e 2421 Off_t new_loc;
c2fcde81
JH
2422 if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
2423#ifdef ESPIPE
2424 SETERRNO(ESPIPE, LIB_INVARG);
2425#else
2426 SETERRNO(EINVAL, LIB_INVARG);
2427#endif
2428 return -1;
2429 }
0723351e
NC
2430 new_loc = PerlLIO_lseek(fd, offset, whence);
2431 if (new_loc == (Off_t) - 1)
dcda55fc 2432 return -1;
c2fcde81
JH
2433 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2434 return 0;
2435}
2436
9e353e3b 2437PerlIO *
14a5cf38
JH
2438PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2439 IV n, const char *mode, int fd, int imode,
2440 int perm, PerlIO *f, int narg, SV **args)
2441{
d9dac8cd 2442 if (PerlIOValid(f)) {
14a5cf38 2443 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2444 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2445 }
2446 if (narg > 0) {
3b6c1aba 2447 if (*mode == IoTYPE_NUMERIC)
14a5cf38
JH
2448 mode++;
2449 else {
2450 imode = PerlIOUnix_oflags(mode);
2451 perm = 0666;
2452 }
2453 if (imode != -1) {
e62f0680 2454 const char *path = SvPV_nolen_const(*args);
14a5cf38
JH
2455 fd = PerlLIO_open3(path, imode, perm);
2456 }
2457 }
2458 if (fd >= 0) {
3b6c1aba 2459 if (*mode == IoTYPE_IMPLICIT)
14a5cf38
JH
2460 mode++;
2461 if (!f) {
2462 f = PerlIO_allocate(aTHX);
d9dac8cd
NIS
2463 }
2464 if (!PerlIOValid(f)) {
a33cf58c
NIS
2465 if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2466 return NULL;
2467 }
d9dac8cd 2468 }
aa063c35 2469 PerlIOUnix_setfd(aTHX_ f, fd, imode);
14a5cf38 2470 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
c2fcde81
JH
2471 if (*mode == IoTYPE_APPEND)
2472 PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
14a5cf38
JH
2473 return f;
2474 }
2475 else {
2476 if (f) {
2477 /*
71200d45 2478 * FIXME: pop layers ???
14a5cf38
JH
2479 */
2480 }
2481 return NULL;
2482 }
9e353e3b
NIS
2483}
2484
71200d45 2485PerlIO *
ecdeb87c 2486PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 2487{
dcda55fc 2488 const PerlIOUnix * const os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2489 int fd = os->fd;
ecdeb87c
NIS
2490 if (flags & PERLIO_DUP_FD) {
2491 fd = PerlLIO_dup(fd);
2492 }
93a8090d 2493 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
ecdeb87c 2494 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2495 if (f) {
2496 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
aa063c35 2497 PerlIOUnix_setfd(aTHX_ f, fd, os->oflags);
71200d45
NIS
2498 return f;
2499 }
71200d45
NIS
2500 }
2501 return NULL;
2502}
2503
2504
9e353e3b 2505SSize_t
f62ce20a 2506PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2507{
de009b76 2508 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2509#ifdef PERLIO_STD_SPECIAL
2510 if (fd == 0)
2511 return PERLIO_STD_IN(fd, vbuf, count);
2512#endif
81428673 2513 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) ||
1fd8f4ce 2514 PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) {
263df5f1 2515 return 0;
1fd8f4ce 2516 }
14a5cf38 2517 while (1) {
b464bac0 2518 const SSize_t len = PerlLIO_read(fd, vbuf, count);
14a5cf38 2519 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2520 if (len < 0) {
2521 if (errno != EAGAIN) {
2522 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2523 }
2524 }
2525 else if (len == 0 && count != 0) {
14a5cf38 2526 PerlIOBase(f)->flags |= PERLIO_F_EOF;
ba85f2ea
NIS
2527 SETERRNO(0,0);
2528 }
14a5cf38
JH
2529 return len;
2530 }
2531 PERL_ASYNC_CHECK();
2532 }
b464bac0 2533 /*NOTREACHED*/
9e353e3b
NIS
2534}
2535
2536SSize_t
f62ce20a 2537PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2538{
de009b76 2539 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
27da23d5
JH
2540#ifdef PERLIO_STD_SPECIAL
2541 if (fd == 1 || fd == 2)
2542 return PERLIO_STD_OUT(fd, vbuf, count);
2543#endif
14a5cf38 2544 while (1) {
de009b76 2545 const SSize_t len = PerlLIO_write(fd, vbuf, count);
14a5cf38 2546 if (len >= 0 || errno != EINTR) {
ba85f2ea
NIS
2547 if (len < 0) {
2548 if (errno != EAGAIN) {
2549 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2550 }
2551 }
14a5cf38
JH
2552 return len;
2553 }
2554 PERL_ASYNC_CHECK();
06da4f11 2555 }
1b6737cc 2556 /*NOTREACHED*/
9e353e3b
NIS
2557}
2558
9e353e3b 2559Off_t
f62ce20a 2560PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2561{
14a5cf38 2562 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2563}
2564
71200d45 2565
9e353e3b 2566IV
f62ce20a 2567PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2568{
de009b76 2569 const int fd = PerlIOSelf(f, PerlIOUnix)->fd;
14a5cf38 2570 int code = 0;
168d5872
NIS
2571 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2572 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2573 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2574 return 0;
22569500 2575 }
93a8090d
NIS
2576 }
2577 else {
93189314 2578 SETERRNO(EBADF,SS_IVCHAN);
93a8090d
NIS
2579 return -1;
2580 }
14a5cf38
JH
2581 while (PerlLIO_close(fd) != 0) {
2582 if (errno != EINTR) {
2583 code = -1;
2584 break;
2585 }
2586 PERL_ASYNC_CHECK();
2587 }
2588 if (code == 0) {
2589 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2590 }
2591 return code;
9e353e3b
NIS
2592}
2593
27da23d5 2594PERLIO_FUNCS_DECL(PerlIO_unix) = {
2dc2558e 2595 sizeof(PerlIO_funcs),
14a5cf38
JH
2596 "unix",
2597 sizeof(PerlIOUnix),
2598 PERLIO_K_RAW,
2599 PerlIOUnix_pushed,
44798d05 2600 PerlIOBase_popped,
14a5cf38 2601 PerlIOUnix_open,
86e05cf2 2602 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
2603 NULL,
2604 PerlIOUnix_fileno,
71200d45 2605 PerlIOUnix_dup,
14a5cf38
JH
2606 PerlIOUnix_read,
2607 PerlIOBase_unread,
2608 PerlIOUnix_write,
2609 PerlIOUnix_seek,
2610 PerlIOUnix_tell,
2611 PerlIOUnix_close,
22569500
NIS
2612 PerlIOBase_noop_ok, /* flush */
2613 PerlIOBase_noop_fail, /* fill */
14a5cf38
JH
2614 PerlIOBase_eof,
2615 PerlIOBase_error,
2616 PerlIOBase_clearerr,
2617 PerlIOBase_setlinebuf,
22569500
NIS
2618 NULL, /* get_base */
2619 NULL, /* get_bufsiz */
2620 NULL, /* get_ptr */
2621 NULL, /* get_cnt */
2622 NULL, /* set_ptrcnt */
9e353e3b
NIS
2623};
2624
2625/*--------------------------------------------------------------------------------------*/
14a5cf38 2626/*
71200d45 2627 * stdio as a layer
14a5cf38 2628 */
9e353e3b 2629
313e59c8
NIS
2630#if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE)
2631/* perl5.8 - This ensures the last minute VMS ungetc fix is not
2632 broken by the last second glibc 2.3 fix
2633 */
2634#define STDIO_BUFFER_WRITABLE
2635#endif
2636
2637
14a5cf38
JH
2638typedef struct {
2639 struct _PerlIO base;
22569500 2640 FILE *stdio; /* The stream */
9e353e3b
NIS
2641} PerlIOStdio;
2642
2643IV
f62ce20a 2644PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2645{
c4420975
AL
2646 if (PerlIOValid(f)) {
2647 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
2648 if (s)
2649 return PerlSIO_fileno(s);
439ba545
NIS
2650 }
2651 errno = EBADF;
2652 return -1;
9e353e3b
NIS
2653}
2654
766a733e 2655char *
14a5cf38
JH
2656PerlIOStdio_mode(const char *mode, char *tmode)
2657{
de009b76 2658 char * const ret = tmode;
a0625d38
SR
2659 if (mode) {
2660 while (*mode) {
2661 *tmode++ = *mode++;
2662 }
14a5cf38 2663 }
95005ad8 2664#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
6ce75a77
JH
2665 *tmode++ = 'b';
2666#endif
14a5cf38
JH
2667 *tmode = '\0';
2668 return ret;
2669}
2670
4b803d04 2671IV
2dc2558e 2672PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
4b803d04 2673{
1fd8f4ce
NIS
2674 PerlIO *n;
2675 if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) {
46c461b5 2676 PerlIO_funcs * const toptab = PerlIOBase(n)->tab;
1fd8f4ce
NIS
2677 if (toptab == tab) {
2678 /* Top is already stdio - pop self (duplicate) and use original */
2679 PerlIO_pop(aTHX_ f);
2680 return 0;
2681 } else {
de009b76 2682 const int fd = PerlIO_fileno(n);
1fd8f4ce
NIS
2683 char tmode[8];
2684 FILE *stdio;
81428673 2685 if (fd >= 0 && (stdio = PerlSIO_fdopen(fd,
1fd8f4ce
NIS
2686 mode = PerlIOStdio_mode(mode, tmode)))) {
2687 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2688 /* We never call down so do any pending stuff now */
2689 PerlIO_flush(PerlIONext(f));
81428673 2690 }
1fd8f4ce
NIS
2691 else {
2692 return -1;
2693 }
2694 }
14a5cf38 2695 }
2dc2558e 2696 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
4b803d04
NIS
2697}
2698
22569500 2699
9e353e3b 2700PerlIO *
4b069b44 2701PerlIO_importFILE(FILE *stdio, const char *mode)
9e353e3b 2702{
14a5cf38
JH
2703 dTHX;
2704 PerlIO *f = NULL;
2705 if (stdio) {
22569500 2706 PerlIOStdio *s;
4b069b44
NIS
2707 if (!mode || !*mode) {
2708 /* We need to probe to see how we can open the stream
2709 so start with read/write and then try write and read
2710 we dup() so that we can fclose without loosing the fd.
2711
2712 Note that the errno value set by a failing fdopen
2713 varies between stdio implementations.
2714 */
de009b76 2715 const int fd = PerlLIO_dup(fileno(stdio));
a33cf58c 2716 FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+"));
4b069b44 2717 if (!f2) {
a33cf58c 2718 f2 = PerlSIO_fdopen(fd, (mode = "w"));
4b069b44
NIS
2719 }
2720 if (!f2) {
a33cf58c 2721 f2 = PerlSIO_fdopen(fd, (mode = "r"));
4b069b44
NIS
2722 }
2723 if (!f2) {
2724 /* Don't seem to be able to open */
2725 PerlLIO_close(fd);
2726 return f;
2727 }
2728 fclose(f2);
22569500 2729 }
27da23d5 2730 if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), PERLIO_FUNCS_CAST(&PerlIO_stdio), mode, Nullsv))) {
a33cf58c
NIS
2731 s = PerlIOSelf(f, PerlIOStdio);
2732 s->stdio = stdio;
2733 }
14a5cf38
JH
2734 }
2735 return f;
9e353e3b
NIS
2736}
2737
2738PerlIO *
14a5cf38
JH
2739PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2740 IV n, const char *mode, int fd, int imode,
2741 int perm, PerlIO *f, int narg, SV **args)
2742{
2743 char tmode[8];
d9dac8cd 2744 if (PerlIOValid(f)) {
dcda55fc
AL
2745 const char * const path = SvPV_nolen_const(*args);
2746 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2747 FILE *stdio;
2748 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2749 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2750 s->stdio);
2751 if (!s->stdio)
2752 return NULL;
2753 s->stdio = stdio;
1751d015 2754 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2755 return f;
2756 }
2757 else {
2758 if (narg > 0) {
dcda55fc 2759 const char * const path = SvPV_nolen_const(*args);
3b6c1aba 2760 if (*mode == IoTYPE_NUMERIC) {
14a5cf38
JH
2761 mode++;
2762 fd = PerlLIO_open3(path, imode, perm);
2763 }
2764 else {
95005ad8
GH
2765 FILE *stdio;
2766 bool appended = FALSE;
2767#ifdef __CYGWIN__
2768 /* Cygwin wants its 'b' early. */
2769 appended = TRUE;
2770 mode = PerlIOStdio_mode(mode, tmode);
2771#endif
2772 stdio = PerlSIO_fopen(path, mode);
6f0313ac
JH
2773 if (stdio) {
2774 PerlIOStdio *s;
2775 if (!f) {
2776 f = PerlIO_allocate(aTHX);
2777 }
95005ad8
GH
2778 if (!appended)
2779 mode = PerlIOStdio_mode(mode, tmode);
2780 f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
2781 if (f) {
6f0313ac
JH
2782 s = PerlIOSelf(f, PerlIOStdio);
2783 s->stdio = stdio;
2784 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2785 }
2786 return f;
2787 }
2788 else {
2789 return NULL;
2790 }
14a5cf38
JH
2791 }
2792 }
2793 if (fd >= 0) {
2794 FILE *stdio = NULL;
2795 int init = 0;
3b6c1aba 2796 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
2797 init = 1;
2798 mode++;
2799 }
2800 if (init) {
2801 switch (fd) {
2802 case 0:
2803 stdio = PerlSIO_stdin;
2804 break;
2805 case 1:
2806 stdio = PerlSIO_stdout;
2807 break;
2808 case 2:
2809 stdio = PerlSIO_stderr;
2810 break;
2811 }
2812 }
2813 else {
2814 stdio = PerlSIO_fdopen(fd, mode =
2815 PerlIOStdio_mode(mode, tmode));
2816 }
2817 if (stdio) {
d9dac8cd
NIS
2818 if (!f) {
2819 f = PerlIO_allocate(aTHX);
2820 }
a33cf58c 2821 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
dcda55fc 2822 PerlIOStdio * const s = PerlIOSelf(f, PerlIOStdio);
a33cf58c
NIS
2823 s->stdio = stdio;
2824 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2825 }
14a5cf38
JH
2826 return f;
2827 }
2828 }
2829 }
ee518936 2830 return NULL;
9e353e3b
NIS
2831}
2832
1751d015 2833PerlIO *
ecdeb87c 2834PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2835{
2836 /* This assumes no layers underneath - which is what
2837 happens, but is not how I remember it. NI-S 2001/10/16
2838 */
ecdeb87c 2839 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2840 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
de009b76 2841 const int fd = fileno(stdio);
9217ff3f 2842 char mode[8];
ecdeb87c 2843 if (flags & PERLIO_DUP_FD) {
de009b76 2844 const int dfd = PerlLIO_dup(fileno(stdio));
9217ff3f
NIS
2845 if (dfd >= 0) {
2846 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2847 goto set_this;
ecdeb87c
NIS
2848 }
2849 else {
2850 /* FIXME: To avoid messy error recovery if dup fails
2851 re-use the existing stdio as though flag was not set
2852 */
2853 }
2854 }
9217ff3f
NIS
2855 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2856 set_this:
694c95cf
JH
2857 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2858 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2859 }
2860 return f;
2861}
2862
0d7a5398
NIS
2863static int
2864PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2865{
2866 /* XXX this could use PerlIO_canset_fileno() and
37725cdc 2867 * PerlIO_set_fileno() support from Configure
0d7a5398 2868 */
ef8eacb8
AT
2869# if defined(__UCLIBC__)
2870 /* uClibc must come before glibc because it defines __GLIBC__ as well. */
2871 f->__filedes = -1;
2872 return 1;
2873# elif defined(__GLIBC__)
0d7a5398 2874 /* There may be a better way for GLIBC:
37725cdc 2875 - libio.h defines a flag to not close() on cleanup
0d7a5398
NIS
2876 */
2877 f->_fileno = -1;
2878 return 1;
2879# elif defined(__sun__)
2880# if defined(_LP64)
2881 /* On solaris, if _LP64 is defined, the FILE structure is this:
2882 *
2883 * struct FILE {
2884 * long __pad[16];
2885 * };
2886 *
37725cdc 2887 * It turns out that the fd is stored in the top 32 bits of
0d7a5398
NIS
2888 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2889 * to contain a pointer or offset into another structure. All the
2890 * remaining fields are zero.
2891 *
2892 * We set the top bits to -1 (0xFFFFFFFF).
2893 */
2894 f->__pad[4] |= 0xffffffff00000000L;
2895 assert(fileno(f) == 0xffffffff);
2896# else /* !defined(_LP64) */
37725cdc
NIS
2897 /* _file is just a unsigned char :-(
2898 Not clear why we dup() rather than using -1
2899 even if that would be treated as 0xFF - so will
0d7a5398
NIS
2900 a dup fail ...
2901 */
673c27c1 2902 f->_file = PerlLIO_dup(fileno(f));
0d7a5398
NIS
2903# endif /* defined(_LP64) */
2904 return 1;
2905# elif defined(__hpux)
2906 f->__fileH = 0xff;
2907 f->__fileL = 0xff;
2908 return 1;
9837d373 2909 /* Next one ->_file seems to be a reasonable fallback, i.e. if
37725cdc 2910 your platform does not have special entry try this one.
9837d373
NIS
2911 [For OSF only have confirmation for Tru64 (alpha)
2912 but assume other OSFs will be similar.]
37725cdc 2913 */
9837d373 2914# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
0d7a5398
NIS
2915 f->_file = -1;
2916 return 1;
2917# elif defined(__FreeBSD__)
2918 /* There may be a better way on FreeBSD:
37725cdc
NIS
2919 - we could insert a dummy func in the _close function entry
2920 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2921 */
2922 f->_file = -1;
0c49ea6a
SU
2923 return 1;
2924# elif defined(__OpenBSD__)
2925 /* There may be a better way on OpenBSD:
2926 - we could insert a dummy func in the _close function entry
2927 f->_close = (int (*)(void *)) dummy_close;
2928 */
2929 f->_file = -1;
0d7a5398 2930 return 1;
59ad941d
IZ
2931# elif defined(__EMX__)
2932 /* f->_flags &= ~_IOOPEN; */ /* Will leak stream->_buffer */
2933 f->_handle = -1;
2934 return 1;
0d7a5398
NIS
2935# elif defined(__CYGWIN__)
2936 /* There may be a better way on CYGWIN:
37725cdc
NIS
2937 - we could insert a dummy func in the _close function entry
2938 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2939 */
2940 f->_file = -1;
2941 return 1;
2942# elif defined(WIN32)
2943# if defined(__BORLANDC__)
2944 f->fd = PerlLIO_dup(fileno(f));
b475b3e6
JH
2945# elif defined(UNDER_CE)
2946 /* WIN_CE does not have access to FILE internals, it hardly has FILE
2947 structure at all
2948 */
0d7a5398
NIS
2949# else
2950 f->_file = -1;
2951# endif
2952 return 1;
2953# else
2954#if 0
37725cdc 2955 /* Sarathy's code did this - we fall back to a dup/dup2 hack
0d7a5398 2956 (which isn't thread safe) instead
37725cdc 2957 */
0d7a5398
NIS
2958# error "Don't know how to set FILE.fileno on your platform"
2959#endif
8772537c 2960 PERL_UNUSED_ARG(f);
0d7a5398
NIS
2961 return 0;
2962# endif
2963}
2964
1751d015 2965IV
f62ce20a 2966PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 2967{
c4420975 2968 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
2969 if (!stdio) {
2970 errno = EBADF;
2971 return -1;
2972 }
9217ff3f 2973 else {
de009b76 2974 const int fd = fileno(stdio);
0d7a5398
NIS
2975 int socksfd = 0;
2976 int invalidate = 0;
bbfd922f 2977 IV result = 0;
0d7a5398
NIS
2978 int saveerr = 0;
2979 int dupfd = 0;
2980#ifdef SOCKS5_VERSION_NAME
37725cdc
NIS
2981 /* Socks lib overrides close() but stdio isn't linked to
2982 that library (though we are) - so we must call close()
2983 on sockets on stdio's behalf.
2984 */
0d7a5398
NIS
2985 int optval;
2986 Sock_size_t optlen = sizeof(int);
2987 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
2988 socksfd = 1;
37725cdc 2989 invalidate = 1;
0d7a5398
NIS
2990 }
2991#endif
9217ff3f
NIS
2992 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2993 /* File descriptor still in use */
0d7a5398
NIS
2994 invalidate = 1;
2995 socksfd = 0;
37725cdc 2996 }
0d7a5398 2997 if (invalidate) {
37725cdc
NIS
2998 /* For STD* handles don't close the stdio at all
2999 this is because we have shared the FILE * too
0d7a5398
NIS
3000 */
3001 if (stdio == stdin) {
3002 /* Some stdios are buggy fflush-ing inputs */
3003 return 0;
3004 }
3005 else if (stdio == stdout || stdio == stderr) {
9217ff3f
NIS
3006 return PerlIO_flush(f);
3007 }
37725cdc
NIS
3008 /* Tricky - must fclose(stdio) to free memory but not close(fd)
3009 Use Sarathy's trick from maint-5.6 to invalidate the
3010 fileno slot of the FILE *
3011 */
bbfd922f 3012 result = PerlIO_flush(f);
0d7a5398
NIS
3013 saveerr = errno;
3014 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
9217ff3f
NIS
3015 dupfd = PerlLIO_dup(fd);
3016 }
37725cdc 3017 }
0d7a5398 3018 result = PerlSIO_fclose(stdio);
37725cdc
NIS
3019 /* We treat error from stdio as success if we invalidated
3020 errno may NOT be expected EBADF
e8529473
NIS
3021 */
3022 if (invalidate && result != 0) {
0d7a5398
NIS
3023 errno = saveerr;
3024 result = 0;
37725cdc 3025 }
0d7a5398
NIS
3026 if (socksfd) {
3027 /* in SOCKS case let close() determine return value */
3028 result = close(fd);
3029 }
3030 if (dupfd) {
3031 PerlLIO_dup2(dupfd,fd);
8a521f28 3032 PerlLIO_close(dupfd);
9217ff3f
NIS
3033 }
3034 return result;
37725cdc 3035 }
1751d015
NIS
3036}
3037
9e353e3b 3038SSize_t
f62ce20a 3039PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 3040{
c4420975 3041 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3042 SSize_t got = 0;
4d948241
NIS
3043 for (;;) {
3044 if (count == 1) {
3045 STDCHAR *buf = (STDCHAR *) vbuf;
3046 /*
3047 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
3048 * stdio does not do that for fread()
3049 */
de009b76 3050 const int ch = PerlSIO_fgetc(s);
4d948241
NIS
3051 if (ch != EOF) {
3052 *buf = ch;
3053 got = 1;
3054 }
14a5cf38 3055 }
4d948241
NIS
3056 else
3057 got = PerlSIO_fread(vbuf, 1, count, s);
b1d8b47a
CS
3058 if (got == 0 && PerlSIO_ferror(s))
3059 got = -1;
42a7a32f 3060 if (got >= 0 || errno != EINTR)
4d948241
NIS
3061 break;
3062 PERL_ASYNC_CHECK();
42a7a32f 3063 SETERRNO(0,0); /* just in case */
14a5cf38 3064 }
14a5cf38 3065 return got;
9e353e3b
NIS
3066}
3067
3068SSize_t
f62ce20a 3069PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3070{
14a5cf38 3071 SSize_t unread = 0;
c4420975 3072 FILE * const s = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785 3073
313e59c8 3074#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3075 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3076 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3077 STDCHAR *base = PerlIO_get_base(f);
3078 SSize_t cnt = PerlIO_get_cnt(f);
3079 STDCHAR *ptr = PerlIO_get_ptr(f);
3080 SSize_t avail = ptr - base;
3081 if (avail > 0) {
3082 if (avail > count) {
3083 avail = count;
3084 }
3085 ptr -= avail;
3086 Move(buf-avail,ptr,avail,STDCHAR);
3087 count -= avail;
3088 unread += avail;
3089 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
3090 if (PerlSIO_feof(s) && unread >= 0)
3091 PerlSIO_clearerr(s);
3092 }
3093 }
313e59c8
NIS
3094 else
3095#endif
3096 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3097 /* We can get pointer to buffer but not its base
3098 Do ungetc() but check chars are ending up in the
3099 buffer
3100 */
3101 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3102 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3103 while (count > 0) {
de009b76 3104 const int ch = *--buf & 0xFF;
9f7cd136
NIS
3105 if (ungetc(ch,s) != ch) {
3106 /* ungetc did not work */
3107 break;
3108 }
3109 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3110 /* Did not change pointer as expected */
3111 fgetc(s); /* get char back again */
3112 break;
3113 }
3114 /* It worked ! */
3115 count--;
3116 unread++;
93679785
NIS
3117 }
3118 }
3119
3120 if (count > 0) {
3121 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3122 }
3123 return unread;
9e353e3b
NIS
3124}
3125
3126SSize_t
f62ce20a 3127PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3128{
4d948241
NIS
3129 SSize_t got;
3130 for (;;) {
3131 got = PerlSIO_fwrite(vbuf, 1, count,
3132 PerlIOSelf(f, PerlIOStdio)->stdio);
42a7a32f 3133 if (got >= 0 || errno != EINTR)
4d948241
NIS
3134 break;
3135 PERL_ASYNC_CHECK();
42a7a32f 3136 SETERRNO(0,0); /* just in case */
4d948241
NIS
3137 }
3138 return got;
9e353e3b
NIS
3139}
3140
94a175e1 3141IV
f62ce20a 3142PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3143{
c4420975 3144 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3145 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
3146}
3147
3148Off_t
f62ce20a 3149PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 3150{
c4420975 3151 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3152 return PerlSIO_ftell(stdio);
9e353e3b
NIS
3153}
3154
3155IV
f62ce20a 3156PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 3157{
c4420975 3158 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38
JH
3159 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3160 return PerlSIO_fflush(stdio);
3161 }
3162 else {
88b61e10 3163#if 0
14a5cf38
JH
3164 /*
3165 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 3166 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 3167 * design is to do _this_ but not have layer above flush this
71200d45 3168 * layer read-to-read
14a5cf38
JH
3169 */
3170 /*
71200d45 3171 * Not writeable - sync by attempting a seek
14a5cf38 3172 */
79852593 3173 const int err = errno;
14a5cf38
JH
3174 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3175 errno = err;
88b61e10 3176#endif
14a5cf38
JH
3177 }
3178 return 0;
9e353e3b
NIS
3179}
3180
3181IV
f62ce20a 3182PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 3183{
14a5cf38 3184 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3185}
3186
3187IV
f62ce20a 3188PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 3189{
263df5f1 3190 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3191}
3192
3193void
f62ce20a 3194PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 3195{
14a5cf38 3196 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3197}
3198
3199void
f62ce20a 3200PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b
NIS
3201{
3202#ifdef HAS_SETLINEBUF
14a5cf38 3203 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 3204#else
14a5cf38 3205 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
9e353e3b
NIS
3206#endif
3207}
3208
3209#ifdef FILE_base
3210STDCHAR *
f62ce20a 3211PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 3212{
c4420975 3213 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3214 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
3215}
3216
3217Size_t
f62ce20a 3218PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3219{
c4420975 3220 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3221 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
3222}
3223#endif
3224
3225#ifdef USE_STDIO_PTR
3226STDCHAR *
f62ce20a 3227PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3228{
c4420975 3229 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3230 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
3231}
3232
3233SSize_t
f62ce20a 3234PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3235{
c4420975 3236 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3237 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
3238}
3239
3240void
f62ce20a 3241PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3242{
c4420975 3243 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3244 if (ptr != NULL) {
9e353e3b 3245#ifdef STDIO_PTR_LVALUE
22569500 3246 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 3247#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 3248 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
3249 assert(PerlSIO_get_cnt(stdio) == (cnt));
3250 }
9e353e3b
NIS
3251#endif
3252#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 3253 /*
71200d45 3254 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
3255 */
3256 return;
9e353e3b 3257#endif
22569500 3258#else /* STDIO_PTR_LVALUE */
14a5cf38 3259 PerlProc_abort();
22569500 3260#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
3261 }
3262 /*
71200d45 3263 * Now (or only) set cnt
14a5cf38 3264 */
9e353e3b 3265#ifdef STDIO_CNT_LVALUE
14a5cf38 3266 PerlSIO_set_cnt(stdio, cnt);
22569500 3267#else /* STDIO_CNT_LVALUE */
9e353e3b 3268#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
3269 PerlSIO_set_ptr(stdio,
3270 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3271 cnt));
22569500 3272#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 3273 PerlProc_abort();
22569500
NIS
3274#endif /* STDIO_PTR_LVAL_SETS_CNT */
3275#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
3276}
3277
93679785 3278
9e353e3b
NIS
3279#endif
3280
93679785
NIS
3281IV
3282PerlIOStdio_fill(pTHX_ PerlIO *f)
3283{
c4420975 3284 FILE * const stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
93679785
NIS
3285 int c;
3286 /*
3287 * fflush()ing read-only streams can cause trouble on some stdio-s
3288 */
3289 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3290 if (PerlSIO_fflush(stdio) != 0)
3291 return EOF;
3292 }
3293 c = PerlSIO_fgetc(stdio);
3294 if (c == EOF)
3295 return EOF;
3296
3297#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
3298
3299#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3300 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3301 /* Fake ungetc() to the real buffer in case system's ungetc
3302 goes elsewhere
3303 */
3304 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3305 SSize_t cnt = PerlSIO_get_cnt(stdio);
3306 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3307 if (ptr == base+1) {
3308 *--ptr = (STDCHAR) c;
3309 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3310 if (PerlSIO_feof(stdio))
3311 PerlSIO_clearerr(stdio);
3312 return 0;
3313 }
3314 }
313e59c8
NIS
3315 else
3316#endif
3317 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3318 STDCHAR ch = c;
3319 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3320 return 0;
3321 }
3322 }
93679785
NIS
3323#endif
3324
3325#if defined(VMS)
3326 /* An ungetc()d char is handled separately from the regular
3327 * buffer, so we stuff it in the buffer ourselves.
3328 * Should never get called as should hit code above
3329 */
bad9695d
NIS
3330 *(--((*stdio)->_ptr)) = (unsigned char) c;
3331 (*stdio)->_cnt++;
93679785
NIS
3332#else
3333 /* If buffer snoop scheme above fails fall back to
9f7cd136 3334 using ungetc().
93679785
NIS
3335 */
3336 if (PerlSIO_ungetc(c, stdio) != c)
3337 return EOF;
3338#endif
3339 return 0;
3340}
3341
3342
3343
27da23d5 3344PERLIO_FUNCS_DECL(PerlIO_stdio) = {
2dc2558e 3345 sizeof(PerlIO_funcs),
14a5cf38
JH
3346 "stdio",
3347 sizeof(PerlIOStdio),
86e05cf2 3348 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 3349 PerlIOStdio_pushed,
44798d05 3350 PerlIOBase_popped,
14a5cf38 3351 PerlIOStdio_open,
86e05cf2 3352 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3353 NULL,
3354 PerlIOStdio_fileno,
71200d45 3355 PerlIOStdio_dup,
14a5cf38
JH
3356 PerlIOStdio_read,
3357 PerlIOStdio_unread,
3358 PerlIOStdio_write,
3359 PerlIOStdio_seek,
3360 PerlIOStdio_tell,
3361 PerlIOStdio_close,
3362 PerlIOStdio_flush,
3363 PerlIOStdio_fill,
3364 PerlIOStdio_eof,
3365 PerlIOStdio_error,
3366 PerlIOStdio_clearerr,
3367 PerlIOStdio_setlinebuf,
9e353e3b 3368#ifdef FILE_base
14a5cf38
JH
3369 PerlIOStdio_get_base,
3370 PerlIOStdio_get_bufsiz,
9e353e3b 3371#else
14a5cf38
JH
3372 NULL,
3373 NULL,
9e353e3b
NIS
3374#endif
3375#ifdef USE_STDIO_PTR
14a5cf38
JH
3376 PerlIOStdio_get_ptr,
3377 PerlIOStdio_get_cnt,
15b61c98 3378# if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
79ed0f43 3379 PerlIOStdio_set_ptrcnt,
15b61c98
JH
3380# else
3381 NULL,
3382# endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3383#else
3384 NULL,
14a5cf38
JH
3385 NULL,
3386 NULL,
15b61c98 3387#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3388};
3389
b9d6bf13
JH
3390/* Note that calls to PerlIO_exportFILE() are reversed using
3391 * PerlIO_releaseFILE(), not importFILE. */
9e353e3b 3392FILE *
81428673 3393PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3394{
e87a358a 3395 dTHX;
81428673
NIS
3396 FILE *stdio = NULL;
3397 if (PerlIOValid(f)) {
3398 char buf[8];
3399 PerlIO_flush(f);
3400 if (!mode || !*mode) {
3401 mode = PerlIO_modestr(f, buf);
3402 }
3403 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3404 if (stdio) {
3405 PerlIOl *l = *f;
9f75cc58 3406 PerlIO *f2;
81428673
NIS
3407 /* De-link any lower layers so new :stdio sticks */
3408 *f = NULL;
27da23d5 3409 if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, Nullsv))) {
9f75cc58 3410 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
81428673
NIS
3411 s->stdio = stdio;
3412 /* Link previous lower layers under new one */
3413 *PerlIONext(f) = l;
3414 }
3415 else {
3416 /* restore layers list */
3417 *f = l;
3418 }
a33cf58c 3419 }
14a5cf38
JH
3420 }
3421 return stdio;
9e353e3b
NIS
3422}
3423
81428673 3424
9e353e3b
NIS
3425FILE *
3426PerlIO_findFILE(PerlIO *f)
3427{
14a5cf38
JH
3428 PerlIOl *l = *f;
3429 while (l) {
3430 if (l->tab == &PerlIO_stdio) {
3431 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3432 return s->stdio;
3433 }
3434 l = *PerlIONext(&l);
f7e7eb72 3435 }
4b069b44
NIS
3436 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3437 return PerlIO_exportFILE(f, Nullch);
9e353e3b
NIS
3438}
3439
b9d6bf13 3440/* Use this to reverse PerlIO_exportFILE calls. */
9e353e3b
NIS
3441void
3442PerlIO_releaseFILE(PerlIO *p, FILE *f)
3443{
27da23d5 3444 dVAR;
22569500
NIS
3445 PerlIOl *l;
3446 while ((l = *p)) {
3447 if (l->tab == &PerlIO_stdio) {
3448 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3449 if (s->stdio == f) {
3450 dTHX;
3451 PerlIO_pop(aTHX_ p);
3452 return;
3453 }
3454 }
3455 p = PerlIONext(p);
3456 }
3457 return;
9e353e3b
NIS
3458}
3459
3460/*--------------------------------------------------------------------------------------*/
14a5cf38 3461/*
71200d45 3462 * perlio buffer layer
14a5cf38 3463 */
9e353e3b 3464
5e2ab84b 3465IV
2dc2558e 3466PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3467{
14a5cf38 3468 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
de009b76 3469 const int fd = PerlIO_fileno(f);
14a5cf38
JH
3470 if (fd >= 0 && PerlLIO_isatty(fd)) {
3471 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3472 }
4b069b44 3473 if (*PerlIONext(f)) {
de009b76 3474 const Off_t posn = PerlIO_tell(PerlIONext(f));
4b069b44
NIS
3475 if (posn != (Off_t) - 1) {
3476 b->posn = posn;
3477 }
14a5cf38 3478 }
2dc2558e 3479 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3480}
3481
9e353e3b 3482PerlIO *
14a5cf38
JH
3483PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3484 IV n, const char *mode, int fd, int imode, int perm,
3485 PerlIO *f, int narg, SV **args)
3486{
04892f78 3487 if (PerlIOValid(f)) {
14a5cf38 3488 PerlIO *next = PerlIONext(f);
67363c0d
JH
3489 PerlIO_funcs *tab =
3490 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3491 if (tab && tab->Open)
3492 next =
3493 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3494 next, narg, args);
2dc2558e 3495 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3496 return NULL;
3497 }
3498 }
3499 else {
04892f78 3500 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38 3501 int init = 0;
3b6c1aba 3502 if (*mode == IoTYPE_IMPLICIT) {
14a5cf38
JH
3503 init = 1;
3504 /*
71200d45 3505 * mode++;
14a5cf38
JH
3506 */
3507 }
67363c0d
JH
3508 if (tab && tab->Open)
3509 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3510 f, narg, args);
3511 else
3512 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 3513 if (f) {
22569500 3514 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
b26b1ab5
NC
3515 /*
3516 * if push fails during open, open fails. close will pop us.
3517 */
3518 PerlIO_close (f);
3519 return NULL;
3520 } else {
3521 fd = PerlIO_fileno(f);
b26b1ab5
NC
3522 if (init && fd == 2) {
3523 /*
3524 * Initial stderr is unbuffered
3525 */
3526 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3527 }
23b84778
IZ
3528#ifdef PERLIO_USING_CRLF
3529# ifdef PERLIO_IS_BINMODE_FD
3530 if (PERLIO_IS_BINMODE_FD(fd))
5c728af0 3531 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
23b84778
IZ
3532 else
3533# endif
3534 /*
3535 * do something about failing setmode()? --jhi
3536 */
3537 PerlLIO_setmode(fd, O_BINARY);
3538#endif
14a5cf38
JH
3539 }
3540 }
ee518936 3541 }
14a5cf38 3542 return f;
9e353e3b
NIS
3543}
3544
14a5cf38
JH
3545/*
3546 * This "flush" is akin to sfio's sync in that it handles files in either
93c2c2ec
IZ
3547 * read or write state. For write state, we put the postponed data through
3548 * the next layers. For read state, we seek() the next layers to the
3549 * offset given by current position in the buffer, and discard the buffer
3550 * state (XXXX supposed to be for seek()able buffers only, but now it is done
3551 * in any case?). Then the pass the stick further in chain.
14a5cf38 3552 */
9e353e3b 3553IV
f62ce20a 3554PerlIOBuf_flush(pTHX_ PerlIO *f)
6f9d8c32 3555{
dcda55fc 3556 PerlIOBuf * const b = PerlIOSelf(f, PerlIOBuf);
14a5cf38 3557 int code = 0;
04892f78 3558 PerlIO *n = PerlIONext(f);
14a5cf38
JH
3559 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3560 /*
71200d45 3561 * write() the buffer
14a5cf38 3562 */
de009b76
AL
3563 const STDCHAR *buf = b->buf;
3564 const STDCHAR *p = buf;
14a5cf38
JH
3565 while (p < b->ptr) {
3566 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3567 if (count > 0) {
3568 p += count;
3569 }
3570 else if (count < 0 || PerlIO_error(n)) {
3571 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3572 code = -1;
3573 break;
3574 }
3575 }
3576 b->posn += (p - buf);
3577 }
3578 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3579 STDCHAR *buf = PerlIO_get_base(f);
3580 /*
71200d45 3581 * Note position change
14a5cf38
JH
3582 */
3583 b->posn += (b->ptr - buf);
3584 if (b->ptr < b->end) {
4b069b44
NIS
3585 /* We did not consume all of it - try and seek downstream to
3586 our logical position
14a5cf38 3587 */
4b069b44 3588 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
04892f78
NIS
3589 /* Reload n as some layers may pop themselves on seek */
3590 b->posn = PerlIO_tell(n = PerlIONext(f));
14a5cf38 3591 }
ba5c3fe9 3592 else {
4b069b44
NIS
3593 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3594 data is lost for good - so return saying "ok" having undone
3595 the position adjust
3596 */
3597 b->posn -= (b->ptr - buf);
ba5c3fe9
NIS
3598 return code;
3599 }
14a5cf38
JH
3600 }
3601 }
3602 b->ptr = b->end = b->buf;
3603 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
04892f78 3604 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
04892f78 3605 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
14a5cf38
JH
3606 code = -1;
3607 return code;
6f9d8c32
NIS
3608}
3609
93c2c2ec
IZ