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