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