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