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