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