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