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