This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate 8.3 fix for PPPort fix
[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
NIS
450{
451 if (f && *f) {
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
JH
1006 */
1007 if (f && *f) {
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{
14a5cf38 1098 if (f && *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
JH
1109 int code = -1;
1110 if (f && *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{
14a5cf38 1122 if (f && *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;
1250 if (f && *f) {
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
JH
1257 while (l) {
1258 SV *arg =
1259 (l->tab->Getarg) ? (*l->tab->
ecdeb87c 1260 Getarg) (aTHX_ &l, NULL, 0) : &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{
14a5cf38 1314 if (f && *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{
14a5cf38 1325 if (f && *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{
14a5cf38 1336 if (f && *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{
14a5cf38 1347 if (f && *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{
14a5cf38 1358 if (f && *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{
14a5cf38 1432 if (f && *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{
14a5cf38
JH
1443 if (f && *f)
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{
14a5cf38 1454 if (f && *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{
14a5cf38 1465 if (f && *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{
14a5cf38 1476 if (f && *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{
14a5cf38 1485 if (f && *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{
14a5cf38
JH
1494 if (f && *f) {
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{
14a5cf38
JH
1503 if (f && *f && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) {
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{
14a5cf38
JH
1513 if (f && *f) {
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{
14a5cf38
JH
1523 if (f && *f) {
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{
14a5cf38 1533 if (f && *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{
14a5cf38 1541 if (f && *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{
14a5cf38
JH
1549 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1550 if (tab->Get_ptr == NULL)
1551 return NULL;
f62ce20a 1552 return (*tab->Get_ptr) (aTHX_ f);
9e353e3b
NIS
1553}
1554
05d1247b 1555int
e87a358a 1556Perl_PerlIO_get_cnt(pTHX_ PerlIO *f)
9e353e3b 1557{
14a5cf38
JH
1558 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1559 if (tab->Get_cnt == NULL)
1560 return 0;
f62ce20a 1561 return (*tab->Get_cnt) (aTHX_ f);
9e353e3b
NIS
1562}
1563
9e353e3b 1564void
e87a358a 1565Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt)
9e353e3b 1566{
f62ce20a 1567 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, NULL, cnt);
9e353e3b
NIS
1568}
1569
9e353e3b 1570void
e87a358a 1571Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt)
9e353e3b 1572{
14a5cf38
JH
1573 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1574 if (tab->Set_ptrcnt == NULL) {
14a5cf38
JH
1575 Perl_croak(aTHX_ "PerlIO buffer snooping abuse");
1576 }
f62ce20a 1577 (*PerlIOBase(f)->tab->Set_ptrcnt) (aTHX_ f, ptr, cnt);
9e353e3b
NIS
1578}
1579
1580/*--------------------------------------------------------------------------------------*/
14a5cf38 1581/*
71200d45 1582 * utf8 and raw dummy layers
14a5cf38 1583 */
dfebf958 1584
26fb694e 1585IV
f62ce20a 1586PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
26fb694e 1587{
14a5cf38 1588 if (PerlIONext(f)) {
14a5cf38
JH
1589 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1590 PerlIO_pop(aTHX_ f);
1591 if (tab->kind & PERLIO_K_UTF8)
1592 PerlIOBase(f)->flags |= PERLIO_F_UTF8;
1593 else
1594 PerlIOBase(f)->flags &= ~PERLIO_F_UTF8;
1595 return 0;
1596 }
1597 return -1;
26fb694e
NIS
1598}
1599
dfebf958 1600PerlIO_funcs PerlIO_utf8 = {
14a5cf38
JH
1601 "utf8",
1602 sizeof(PerlIOl),
1603 PERLIO_K_DUMMY | PERLIO_F_UTF8,
1604 PerlIOUtf8_pushed,
1605 NULL,
1606 NULL,
1607 NULL,
1608 NULL,
1609 NULL,
1610 NULL,
1611 NULL,
1612 NULL,
1613 NULL,
1614 NULL,
1615 NULL, /* flush */
1616 NULL, /* fill */
1617 NULL,
1618 NULL,
1619 NULL,
1620 NULL,
1621 NULL, /* get_base */
1622 NULL, /* get_bufsiz */
1623 NULL, /* get_ptr */
1624 NULL, /* get_cnt */
1625 NULL, /* set_ptrcnt */
26fb694e
NIS
1626};
1627
1628PerlIO_funcs PerlIO_byte = {
14a5cf38
JH
1629 "bytes",
1630 sizeof(PerlIOl),
1631 PERLIO_K_DUMMY,
1632 PerlIOUtf8_pushed,
1633 NULL,
1634 NULL,
1635 NULL,
1636 NULL,
1637 NULL,
1638 NULL,
1639 NULL,
1640 NULL,
1641 NULL,
1642 NULL,
1643 NULL, /* flush */
1644 NULL, /* fill */
1645 NULL,
1646 NULL,
1647 NULL,
1648 NULL,
1649 NULL, /* get_base */
1650 NULL, /* get_bufsiz */
1651 NULL, /* get_ptr */
1652 NULL, /* get_cnt */
1653 NULL, /* set_ptrcnt */
dfebf958
NIS
1654};
1655
1656PerlIO *
14a5cf38
JH
1657PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
1658 IV n, const char *mode, int fd, int imode, int perm,
1659 PerlIO *old, int narg, SV **args)
dfebf958 1660{
14a5cf38
JH
1661 PerlIO_funcs *tab = PerlIO_default_btm();
1662 return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
1663 old, narg, args);
dfebf958
NIS
1664}
1665
1666PerlIO_funcs PerlIO_raw = {
14a5cf38
JH
1667 "raw",
1668 sizeof(PerlIOl),
1669 PERLIO_K_DUMMY,
1670 PerlIORaw_pushed,
1671 PerlIOBase_popped,
1672 PerlIORaw_open,
1673 NULL,
1674 NULL,
1675 NULL,
1676 NULL,
1677 NULL,
1678 NULL,
1679 NULL,
1680 NULL,
1681 NULL, /* flush */
1682 NULL, /* fill */
1683 NULL,
1684 NULL,
1685 NULL,
1686 NULL,
1687 NULL, /* get_base */
1688 NULL, /* get_bufsiz */
1689 NULL, /* get_ptr */
1690 NULL, /* get_cnt */
1691 NULL, /* set_ptrcnt */
dfebf958
NIS
1692};
1693/*--------------------------------------------------------------------------------------*/
1694/*--------------------------------------------------------------------------------------*/
14a5cf38 1695/*
71200d45 1696 * "Methods" of the "base class"
14a5cf38 1697 */
9e353e3b
NIS
1698
1699IV
f62ce20a 1700PerlIOBase_fileno(pTHX_ PerlIO *f)
9e353e3b 1701{
14a5cf38 1702 return PerlIO_fileno(PerlIONext(f));
9e353e3b
NIS
1703}
1704
f5b9d040 1705char *
14a5cf38
JH
1706PerlIO_modestr(PerlIO *f, char *buf)
1707{
1708 char *s = buf;
1709 IV flags = PerlIOBase(f)->flags;
1710 if (flags & PERLIO_F_APPEND) {
1711 *s++ = 'a';
1712 if (flags & PERLIO_F_CANREAD) {
1713 *s++ = '+';
1714 }
1715 }
1716 else if (flags & PERLIO_F_CANREAD) {
1717 *s++ = 'r';
1718 if (flags & PERLIO_F_CANWRITE)
1719 *s++ = '+';
1720 }
1721 else if (flags & PERLIO_F_CANWRITE) {
1722 *s++ = 'w';
1723 if (flags & PERLIO_F_CANREAD) {
1724 *s++ = '+';
1725 }
1726 }
35990314 1727#ifdef PERLIO_USING_CRLF
14a5cf38
JH
1728 if (!(flags & PERLIO_F_CRLF))
1729 *s++ = 'b';
5f1a76d0 1730#endif
14a5cf38
JH
1731 *s = '\0';
1732 return buf;
f5b9d040
NIS
1733}
1734
76ced9ad 1735IV
f62ce20a 1736PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
9e353e3b 1737{
14a5cf38 1738 PerlIOl *l = PerlIOBase(f);
b7953727 1739#if 0
14a5cf38
JH
1740 const char *omode = mode;
1741 char temp[8];
b7953727 1742#endif
14a5cf38
JH
1743 PerlIO_funcs *tab = PerlIOBase(f)->tab;
1744 l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE |
1745 PERLIO_F_TRUNCATE | PERLIO_F_APPEND);
1746 if (tab->Set_ptrcnt != NULL)
1747 l->flags |= PERLIO_F_FASTGETS;
1748 if (mode) {
1749 if (*mode == '#' || *mode == 'I')
1750 mode++;
1751 switch (*mode++) {
1752 case 'r':
1753 l->flags |= PERLIO_F_CANREAD;
1754 break;
1755 case 'a':
1756 l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE;
1757 break;
1758 case 'w':
1759 l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE;
1760 break;
1761 default:
1762 SETERRNO(EINVAL, LIB$_INVARG);
1763 return -1;
1764 }
1765 while (*mode) {
1766 switch (*mode++) {
1767 case '+':
1768 l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE;
1769 break;
1770 case 'b':
1771 l->flags &= ~PERLIO_F_CRLF;
1772 break;
1773 case 't':
1774 l->flags |= PERLIO_F_CRLF;
1775 break;
1776 default:
1777 SETERRNO(EINVAL, LIB$_INVARG);
1778 return -1;
1779 }
1780 }
1781 }
1782 else {
1783 if (l->next) {
1784 l->flags |= l->next->flags &
1785 (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE |
1786 PERLIO_F_APPEND);
1787 }
1788 }
5e2ab84b 1789#if 0
14a5cf38
JH
1790 PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n",
1791 f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)",
1792 l->flags, PerlIO_modestr(f, temp));
5e2ab84b 1793#endif
14a5cf38 1794 return 0;
76ced9ad
NIS
1795}
1796
1797IV
f62ce20a 1798PerlIOBase_popped(pTHX_ PerlIO *f)
76ced9ad 1799{
14a5cf38 1800 return 0;
760ac839
LW
1801}
1802
9e353e3b 1803SSize_t
f62ce20a 1804PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 1805{
14a5cf38 1806 /*
71200d45 1807 * Save the position as current head considers it
14a5cf38
JH
1808 */
1809 Off_t old = PerlIO_tell(f);
1810 SSize_t done;
1811 PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv);
1812 PerlIOSelf(f, PerlIOBuf)->posn = old;
f62ce20a 1813 done = PerlIOBuf_unread(aTHX_ f, vbuf, count);
14a5cf38 1814 return done;
9e353e3b
NIS
1815}
1816
f6c77cf1 1817SSize_t
f62ce20a 1818PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
f6c77cf1 1819{
14a5cf38
JH
1820 STDCHAR *buf = (STDCHAR *) vbuf;
1821 if (f) {
1822 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
1823 return 0;
1824 while (count > 0) {
1825 SSize_t avail = PerlIO_get_cnt(f);
1826 SSize_t take = 0;
1827 if (avail > 0)
1828 take = (count < avail) ? count : avail;
1829 if (take > 0) {
1830 STDCHAR *ptr = PerlIO_get_ptr(f);
1831 Copy(ptr, buf, take, STDCHAR);
1832 PerlIO_set_ptrcnt(f, ptr + take, (avail -= take));
1833 count -= take;
1834 buf += take;
1835 }
1836 if (count > 0 && avail <= 0) {
1837 if (PerlIO_fill(f) != 0)
1838 break;
1839 }
1840 }
1841 return (buf - (STDCHAR *) vbuf);
1842 }
f6c77cf1 1843 return 0;
f6c77cf1
NIS
1844}
1845
9e353e3b 1846IV
f62ce20a 1847PerlIOBase_noop_ok(pTHX_ PerlIO *f)
9e353e3b 1848{
14a5cf38 1849 return 0;
9e353e3b
NIS
1850}
1851
1852IV
f62ce20a 1853PerlIOBase_noop_fail(pTHX_ PerlIO *f)
06da4f11 1854{
14a5cf38 1855 return -1;
06da4f11
NIS
1856}
1857
1858IV
f62ce20a 1859PerlIOBase_close(pTHX_ PerlIO *f)
9e353e3b 1860{
14a5cf38
JH
1861 IV code = 0;
1862 PerlIO *n = PerlIONext(f);
1863 if (PerlIO_flush(f) != 0)
1864 code = -1;
f62ce20a 1865 if (n && *n && (*PerlIOBase(n)->tab->Close)(aTHX_ n) != 0)
14a5cf38
JH
1866 code = -1;
1867 PerlIOBase(f)->flags &=
1868 ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN);
1869 return code;
9e353e3b
NIS
1870}
1871
1872IV
f62ce20a 1873PerlIOBase_eof(pTHX_ PerlIO *f)
9e353e3b 1874{
14a5cf38
JH
1875 if (f && *f) {
1876 return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0;
1877 }
1878 return 1;
9e353e3b
NIS
1879}
1880
1881IV
f62ce20a 1882PerlIOBase_error(pTHX_ PerlIO *f)
9e353e3b 1883{
14a5cf38
JH
1884 if (f && *f) {
1885 return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0;
1886 }
1887 return 1;
9e353e3b
NIS
1888}
1889
1890void
f62ce20a 1891PerlIOBase_clearerr(pTHX_ PerlIO *f)
9e353e3b 1892{
14a5cf38
JH
1893 if (f && *f) {
1894 PerlIO *n = PerlIONext(f);
1895 PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF);
1896 if (n)
1897 PerlIO_clearerr(n);
1898 }
9e353e3b
NIS
1899}
1900
1901void
f62ce20a 1902PerlIOBase_setlinebuf(pTHX_ PerlIO *f)
9e353e3b 1903{
14a5cf38
JH
1904 if (f) {
1905 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF;
1906 }
9e353e3b
NIS
1907}
1908
93a8090d
NIS
1909SV *
1910PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param)
1911{
1912 if (!arg)
1913 return Nullsv;
1914#ifdef sv_dup
1915 if (param) {
1916 return sv_dup(arg, param);
1917 }
1918 else {
1919 return newSVsv(arg);
1920 }
1921#else
1922 return newSVsv(arg);
1923#endif
1924}
1925
1926PerlIO *
ecdeb87c 1927PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
93a8090d
NIS
1928{
1929 PerlIO *nexto = PerlIONext(o);
1930 if (*nexto) {
1931 PerlIO_funcs *tab = PerlIOBase(nexto)->tab;
ecdeb87c 1932 f = (*tab->Dup)(aTHX_ f, nexto, param, flags);
93a8090d
NIS
1933 }
1934 if (f) {
1935 PerlIO_funcs *self = PerlIOBase(o)->tab;
1936 SV *arg = Nullsv;
1937 char buf[8];
fe5a182c
JH
1938 PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n",
1939 self->name, (void*)f, (void*)o, (void*)param);
93a8090d 1940 if (self->Getarg) {
ecdeb87c 1941 arg = (*self->Getarg)(aTHX_ o,param,flags);
93a8090d
NIS
1942 }
1943 f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg);
ecdeb87c 1944 if (arg) {
93a8090d
NIS
1945 SvREFCNT_dec(arg);
1946 }
1947 }
1948 return f;
1949}
1950
1951#define PERLIO_MAX_REFCOUNTABLE_FD 2048
168d5872 1952#ifdef USE_THREADS
93a8090d 1953perl_mutex PerlIO_mutex;
93a8090d 1954#endif
168d5872 1955int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD];
93a8090d
NIS
1956
1957void
1958PerlIO_init(pTHX)
1959{
1960 /* Place holder for stdstreams call ??? */
168d5872 1961#ifdef USE_THREADS
93a8090d
NIS
1962 MUTEX_INIT(&PerlIO_mutex);
1963#endif
1964}
1965
168d5872
NIS
1966void
1967PerlIOUnix_refcnt_inc(int fd)
1968{
1969 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
1970#ifdef USE_THREADS
1971 MUTEX_LOCK(&PerlIO_mutex);
1972#endif
1973 PerlIO_fd_refcnt[fd]++;
1974 PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]);
1975#ifdef USE_THREADS
1976 MUTEX_UNLOCK(&PerlIO_mutex);
1977#endif
1978 }
1979}
1980
168d5872
NIS
1981int
1982PerlIOUnix_refcnt_dec(int fd)
1983{
1984 int cnt = 0;
1985 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
1986#ifdef USE_THREADS
1987 MUTEX_LOCK(&PerlIO_mutex);
1988#endif
1989 cnt = --PerlIO_fd_refcnt[fd];
1990 PerlIO_debug("fd %d refcnt=%d\n",fd,cnt);
1991#ifdef USE_THREADS
1992 MUTEX_UNLOCK(&PerlIO_mutex);
1993#endif
1994 }
1995 return cnt;
1996}
1997
694c95cf
JH
1998void
1999PerlIO_cleanup(pTHX)
2000{
2001 int i;
2002#ifdef USE_ITHREADS
2003 PerlIO_debug("Cleanup %p\n",aTHX);
2004#endif
2005 /* Raise STDIN..STDERR refcount so we don't close them */
2006 for (i=0; i < 3; i++)
2007 PerlIOUnix_refcnt_inc(i);
2008 PerlIO_cleantable(aTHX_ &PL_perlio);
2009 /* Restore STDIN..STDERR refcount */
2010 for (i=0; i < 3; i++)
2011 PerlIOUnix_refcnt_dec(i);
2012}
2013
2014
2015
9e353e3b 2016/*--------------------------------------------------------------------------------------*/
14a5cf38 2017/*
71200d45 2018 * Bottom-most level for UNIX-like case
14a5cf38 2019 */
9e353e3b 2020
14a5cf38
JH
2021typedef struct {
2022 struct _PerlIO base; /* The generic part */
2023 int fd; /* UNIX like file descriptor */
2024 int oflags; /* open/fcntl flags */
9e353e3b
NIS
2025} PerlIOUnix;
2026
6f9d8c32 2027int
9e353e3b 2028PerlIOUnix_oflags(const char *mode)
760ac839 2029{
14a5cf38
JH
2030 int oflags = -1;
2031 if (*mode == 'I' || *mode == '#')
2032 mode++;
2033 switch (*mode) {
2034 case 'r':
2035 oflags = O_RDONLY;
2036 if (*++mode == '+') {
2037 oflags = O_RDWR;
2038 mode++;
2039 }
2040 break;
2041
2042 case 'w':
2043 oflags = O_CREAT | O_TRUNC;
2044 if (*++mode == '+') {
2045 oflags |= O_RDWR;
2046 mode++;
2047 }
2048 else
2049 oflags |= O_WRONLY;
2050 break;
2051
2052 case 'a':
2053 oflags = O_CREAT | O_APPEND;
2054 if (*++mode == '+') {
2055 oflags |= O_RDWR;
2056 mode++;
2057 }
2058 else
2059 oflags |= O_WRONLY;
2060 break;
2061 }
2062 if (*mode == 'b') {
2063 oflags |= O_BINARY;
2064 oflags &= ~O_TEXT;
2065 mode++;
2066 }
2067 else if (*mode == 't') {
2068 oflags |= O_TEXT;
2069 oflags &= ~O_BINARY;
2070 mode++;
2071 }
2072 /*
71200d45 2073 * Always open in binary mode
14a5cf38
JH
2074 */
2075 oflags |= O_BINARY;
2076 if (*mode || oflags == -1) {
2077 SETERRNO(EINVAL, LIB$_INVARG);
2078 oflags = -1;
2079 }
2080 return oflags;
9e353e3b
NIS
2081}
2082
2083IV
f62ce20a 2084PerlIOUnix_fileno(pTHX_ PerlIO *f)
9e353e3b 2085{
14a5cf38 2086 return PerlIOSelf(f, PerlIOUnix)->fd;
9e353e3b
NIS
2087}
2088
4b803d04 2089IV
f62ce20a 2090PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
4b803d04 2091{
f62ce20a 2092 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
93a8090d 2093 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
14a5cf38 2094 if (*PerlIONext(f)) {
14a5cf38
JH
2095 s->fd = PerlIO_fileno(PerlIONext(f));
2096 /*
71200d45 2097 * XXX could (or should) we retrieve the oflags from the open file
14a5cf38 2098 * handle rather than believing the "mode" we are passed in? XXX
71200d45 2099 * Should the value on NULL mode be 0 or -1?
14a5cf38
JH
2100 */
2101 s->oflags = mode ? PerlIOUnix_oflags(mode) : -1;
2102 }
2103 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
2104 return code;
4b803d04
NIS
2105}
2106
9e353e3b 2107PerlIO *
14a5cf38
JH
2108PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2109 IV n, const char *mode, int fd, int imode,
2110 int perm, PerlIO *f, int narg, SV **args)
2111{
2112 if (f) {
2113 if (PerlIOBase(f)->flags & PERLIO_F_OPEN)
f62ce20a 2114 (*PerlIOBase(f)->tab->Close)(aTHX_ f);
14a5cf38
JH
2115 }
2116 if (narg > 0) {
2117 char *path = SvPV_nolen(*args);
2118 if (*mode == '#')
2119 mode++;
2120 else {
2121 imode = PerlIOUnix_oflags(mode);
2122 perm = 0666;
2123 }
2124 if (imode != -1) {
2125 fd = PerlLIO_open3(path, imode, perm);
2126 }
2127 }
2128 if (fd >= 0) {
2129 PerlIOUnix *s;
2130 if (*mode == 'I')
2131 mode++;
2132 if (!f) {
2133 f = PerlIO_allocate(aTHX);
2134 s = PerlIOSelf(PerlIO_push(aTHX_ f, self, mode, PerlIOArg),
2135 PerlIOUnix);
2136 }
2137 else
2138 s = PerlIOSelf(f, PerlIOUnix);
2139 s->fd = fd;
2140 s->oflags = imode;
2141 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
93a8090d 2142 PerlIOUnix_refcnt_inc(fd);
14a5cf38
JH
2143 return f;
2144 }
2145 else {
2146 if (f) {
2147 /*
71200d45 2148 * FIXME: pop layers ???
14a5cf38
JH
2149 */
2150 }
2151 return NULL;
2152 }
9e353e3b
NIS
2153}
2154
71200d45 2155PerlIO *
ecdeb87c 2156PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45
NIS
2157{
2158 PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix);
93a8090d 2159 int fd = os->fd;
ecdeb87c
NIS
2160 if (flags & PERLIO_DUP_FD) {
2161 fd = PerlLIO_dup(fd);
2162 }
93a8090d 2163 if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) {
ecdeb87c 2164 f = PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
2165 if (f) {
2166 /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */
2167 PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix);
2168 s->fd = fd;
93a8090d 2169 PerlIOUnix_refcnt_inc(fd);
71200d45
NIS
2170 return f;
2171 }
71200d45
NIS
2172 }
2173 return NULL;
2174}
2175
2176
9e353e3b 2177SSize_t
f62ce20a 2178PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2179{
14a5cf38
JH
2180 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2181 if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD))
2182 return 0;
2183 while (1) {
2184 SSize_t len = PerlLIO_read(fd, vbuf, count);
2185 if (len >= 0 || errno != EINTR) {
2186 if (len < 0)
2187 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2188 else if (len == 0 && count != 0)
2189 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2190 return len;
2191 }
2192 PERL_ASYNC_CHECK();
2193 }
9e353e3b
NIS
2194}
2195
2196SSize_t
f62ce20a 2197PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2198{
14a5cf38
JH
2199 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2200 while (1) {
2201 SSize_t len = PerlLIO_write(fd, vbuf, count);
2202 if (len >= 0 || errno != EINTR) {
2203 if (len < 0)
2204 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2205 return len;
2206 }
2207 PERL_ASYNC_CHECK();
06da4f11 2208 }
9e353e3b
NIS
2209}
2210
2211IV
f62ce20a 2212PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 2213{
14a5cf38
JH
2214 Off_t new =
2215 PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, offset, whence);
2216 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
2217 return (new == (Off_t) - 1) ? -1 : 0;
9e353e3b
NIS
2218}
2219
2220Off_t
f62ce20a 2221PerlIOUnix_tell(pTHX_ PerlIO *f)
9e353e3b 2222{
14a5cf38 2223 return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR);
9e353e3b
NIS
2224}
2225
71200d45 2226
9e353e3b 2227IV
f62ce20a 2228PerlIOUnix_close(pTHX_ PerlIO *f)
9e353e3b 2229{
14a5cf38
JH
2230 int fd = PerlIOSelf(f, PerlIOUnix)->fd;
2231 int code = 0;
168d5872
NIS
2232 if (PerlIOBase(f)->flags & PERLIO_F_OPEN) {
2233 if (PerlIOUnix_refcnt_dec(fd) > 0) {
93a8090d
NIS
2234 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2235 return 0;
2236 }
93a8090d
NIS
2237 }
2238 else {
2239 SETERRNO(EBADF,SS$_IVCHAN);
2240 return -1;
2241 }
14a5cf38
JH
2242 while (PerlLIO_close(fd) != 0) {
2243 if (errno != EINTR) {
2244 code = -1;
2245 break;
2246 }
2247 PERL_ASYNC_CHECK();
2248 }
2249 if (code == 0) {
2250 PerlIOBase(f)->flags &= ~PERLIO_F_OPEN;
2251 }
2252 return code;
9e353e3b
NIS
2253}
2254
2255PerlIO_funcs PerlIO_unix = {
14a5cf38
JH
2256 "unix",
2257 sizeof(PerlIOUnix),
2258 PERLIO_K_RAW,
2259 PerlIOUnix_pushed,
2260 PerlIOBase_noop_ok,
2261 PerlIOUnix_open,
2262 NULL,
2263 PerlIOUnix_fileno,
71200d45 2264 PerlIOUnix_dup,
14a5cf38
JH
2265 PerlIOUnix_read,
2266 PerlIOBase_unread,
2267 PerlIOUnix_write,
2268 PerlIOUnix_seek,
2269 PerlIOUnix_tell,
2270 PerlIOUnix_close,
2271 PerlIOBase_noop_ok, /* flush */
2272 PerlIOBase_noop_fail, /* fill */
2273 PerlIOBase_eof,
2274 PerlIOBase_error,
2275 PerlIOBase_clearerr,
2276 PerlIOBase_setlinebuf,
2277 NULL, /* get_base */
2278 NULL, /* get_bufsiz */
2279 NULL, /* get_ptr */
2280 NULL, /* get_cnt */
2281 NULL, /* set_ptrcnt */
9e353e3b
NIS
2282};
2283
2284/*--------------------------------------------------------------------------------------*/
14a5cf38 2285/*
71200d45 2286 * stdio as a layer
14a5cf38 2287 */
9e353e3b 2288
14a5cf38
JH
2289typedef struct {
2290 struct _PerlIO base;
2291 FILE *stdio; /* The stream */
9e353e3b
NIS
2292} PerlIOStdio;
2293
2294IV
f62ce20a 2295PerlIOStdio_fileno(pTHX_ PerlIO *f)
9e353e3b 2296{
14a5cf38 2297 return PerlSIO_fileno(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2298}
2299
766a733e 2300char *
14a5cf38
JH
2301PerlIOStdio_mode(const char *mode, char *tmode)
2302{
2303 char *ret = tmode;
2304 while (*mode) {
2305 *tmode++ = *mode++;
2306 }
35990314 2307#ifdef PERLIO_USING_CRLF
6ce75a77
JH
2308 *tmode++ = 'b';
2309#endif
14a5cf38
JH
2310 *tmode = '\0';
2311 return ret;
2312}
2313
2314/*
71200d45 2315 * This isn't used yet ...
14a5cf38 2316 */
4b803d04 2317IV
f62ce20a 2318PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
4b803d04 2319{
14a5cf38 2320 if (*PerlIONext(f)) {
14a5cf38
JH
2321 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
2322 char tmode[8];
2323 FILE *stdio =
2324 PerlSIO_fdopen(PerlIO_fileno(PerlIONext(f)), mode =
2325 PerlIOStdio_mode(mode, tmode));
2326 if (stdio)
2327 s->stdio = stdio;
2328 else
2329 return -1;
2330 }
f62ce20a 2331 return PerlIOBase_pushed(aTHX_ f, mode, arg);
4b803d04
NIS
2332}
2333
9e353e3b
NIS
2334PerlIO *
2335PerlIO_importFILE(FILE *stdio, int fl)
2336{
14a5cf38
JH
2337 dTHX;
2338 PerlIO *f = NULL;
2339 if (stdio) {
2340 PerlIOStdio *s =
2341 PerlIOSelf(PerlIO_push
2342 (aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio,
2343 "r+", Nullsv), PerlIOStdio);
2344 s->stdio = stdio;
2345 }
2346 return f;
9e353e3b
NIS
2347}
2348
2349PerlIO *
14a5cf38
JH
2350PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2351 IV n, const char *mode, int fd, int imode,
2352 int perm, PerlIO *f, int narg, SV **args)
2353{
2354 char tmode[8];
2355 if (f) {
2356 char *path = SvPV_nolen(*args);
2357 PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio);
1751d015
NIS
2358 FILE *stdio;
2359 PerlIOUnix_refcnt_dec(fileno(s->stdio));
2360 stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)),
14a5cf38
JH
2361 s->stdio);
2362 if (!s->stdio)
2363 return NULL;
2364 s->stdio = stdio;
1751d015 2365 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2366 return f;
2367 }
2368 else {
2369 if (narg > 0) {
2370 char *path = SvPV_nolen(*args);
2371 if (*mode == '#') {
2372 mode++;
2373 fd = PerlLIO_open3(path, imode, perm);
2374 }
2375 else {
2376 FILE *stdio = PerlSIO_fopen(path, mode);
2377 if (stdio) {
2378 PerlIOStdio *s =
2379 PerlIOSelf(PerlIO_push
2380 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2381 (mode = PerlIOStdio_mode(mode, tmode)),
2382 PerlIOArg),
2383 PerlIOStdio);
2384 s->stdio = stdio;
1751d015 2385 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2386 }
2387 return f;
2388 }
2389 }
2390 if (fd >= 0) {
2391 FILE *stdio = NULL;
2392 int init = 0;
2393 if (*mode == 'I') {
2394 init = 1;
2395 mode++;
2396 }
2397 if (init) {
2398 switch (fd) {
2399 case 0:
2400 stdio = PerlSIO_stdin;
2401 break;
2402 case 1:
2403 stdio = PerlSIO_stdout;
2404 break;
2405 case 2:
2406 stdio = PerlSIO_stderr;
2407 break;
2408 }
2409 }
2410 else {
2411 stdio = PerlSIO_fdopen(fd, mode =
2412 PerlIOStdio_mode(mode, tmode));
2413 }
2414 if (stdio) {
2415 PerlIOStdio *s =
2416 PerlIOSelf(PerlIO_push
2417 (aTHX_(f = PerlIO_allocate(aTHX)), self,
2418 mode, PerlIOArg), PerlIOStdio);
2419 s->stdio = stdio;
1751d015 2420 PerlIOUnix_refcnt_inc(fileno(s->stdio));
14a5cf38
JH
2421 return f;
2422 }
2423 }
2424 }
ee518936 2425 return NULL;
9e353e3b
NIS
2426}
2427
1751d015 2428PerlIO *
ecdeb87c 2429PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2430{
2431 /* This assumes no layers underneath - which is what
2432 happens, but is not how I remember it. NI-S 2001/10/16
2433 */
ecdeb87c 2434 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2435 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
ecdeb87c
NIS
2436 if (flags & PERLIO_DUP_FD) {
2437 int fd = PerlLIO_dup(fileno(stdio));
2438 if (fd >= 0) {
2439 char mode[8];
293ed4d2 2440 stdio = fdopen(fd, PerlIO_modestr(o,mode));
ecdeb87c
NIS
2441 }
2442 else {
2443 /* FIXME: To avoid messy error recovery if dup fails
2444 re-use the existing stdio as though flag was not set
2445 */
2446 }
2447 }
694c95cf
JH
2448 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2449 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2450 }
2451 return f;
2452}
2453
2454IV
f62ce20a 2455PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 2456{
1751d015
NIS
2457#ifdef SOCKS5_VERSION_NAME
2458 int optval;
2459 Sock_size_t optlen = sizeof(int);
2460#endif
2461 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2462 if (PerlIOUnix_refcnt_dec(fileno(stdio)) > 0) {
694c95cf
JH
2463 /* Do not close it but do flush any buffers */
2464 PerlIO_flush(f);
1751d015
NIS
2465 return 0;
2466 }
2467 return (
2468#ifdef SOCKS5_VERSION_NAME
2469 (getsockopt
2470 (PerlIO_fileno(f), SOL_SOCKET, SO_TYPE, (void *) &optval,
2471 &optlen) <
2472 0) ? PerlSIO_fclose(stdio) : close(PerlIO_fileno(f))
2473#else
2474 PerlSIO_fclose(stdio)
2475#endif
2476 );
2477
2478}
2479
2480
2481
9e353e3b 2482SSize_t
f62ce20a 2483PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2484{
14a5cf38
JH
2485 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2486 SSize_t got = 0;
2487 if (count == 1) {
2488 STDCHAR *buf = (STDCHAR *) vbuf;
2489 /*
2490 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
71200d45 2491 * stdio does not do that for fread()
14a5cf38
JH
2492 */
2493 int ch = PerlSIO_fgetc(s);
2494 if (ch != EOF) {
2495 *buf = ch;
2496 got = 1;
2497 }
2498 }
2499 else
2500 got = PerlSIO_fread(vbuf, 1, count, s);
2501 return got;
9e353e3b
NIS
2502}
2503
2504SSize_t
f62ce20a 2505PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2506{
14a5cf38
JH
2507 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2508 STDCHAR *buf = ((STDCHAR *) vbuf) + count - 1;
2509 SSize_t unread = 0;
2510 while (count > 0) {
2511 int ch = *buf-- & 0xff;
2512 if (PerlSIO_ungetc(ch, s) != ch)
2513 break;
2514 unread++;
2515 count--;
2516 }
2517 return unread;
9e353e3b
NIS
2518}
2519
2520SSize_t
f62ce20a 2521PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2522{
14a5cf38
JH
2523 return PerlSIO_fwrite(vbuf, 1, count,
2524 PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2525}
2526
2527IV
f62ce20a 2528PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 2529{
14a5cf38
JH
2530 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2531 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
2532}
2533
2534Off_t
f62ce20a 2535PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 2536{
14a5cf38
JH
2537 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2538 return PerlSIO_ftell(stdio);
9e353e3b
NIS
2539}
2540
2541IV
f62ce20a 2542PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 2543{
14a5cf38
JH
2544 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2545 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
2546 return PerlSIO_fflush(stdio);
2547 }
2548 else {
88b61e10 2549#if 0
14a5cf38
JH
2550 /*
2551 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 2552 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 2553 * design is to do _this_ but not have layer above flush this
71200d45 2554 * layer read-to-read
14a5cf38
JH
2555 */
2556 /*
71200d45 2557 * Not writeable - sync by attempting a seek
14a5cf38
JH
2558 */
2559 int err = errno;
2560 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
2561 errno = err;
88b61e10 2562#endif
14a5cf38
JH
2563 }
2564 return 0;
9e353e3b
NIS
2565}
2566
2567IV
f62ce20a 2568PerlIOStdio_fill(pTHX_ PerlIO *f)
06da4f11 2569{
14a5cf38
JH
2570 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2571 int c;
2572 /*
71200d45 2573 * fflush()ing read-only streams can cause trouble on some stdio-s
14a5cf38
JH
2574 */
2575 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
2576 if (PerlSIO_fflush(stdio) != 0)
2577 return EOF;
2578 }
2579 c = PerlSIO_fgetc(stdio);
2580 if (c == EOF || PerlSIO_ungetc(c, stdio) != c)
2581 return EOF;
2582 return 0;
06da4f11
NIS
2583}
2584
2585IV
f62ce20a 2586PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 2587{
14a5cf38 2588 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2589}
2590
2591IV
f62ce20a 2592PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 2593{
14a5cf38 2594 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2595}
2596
2597void
f62ce20a 2598PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 2599{
14a5cf38 2600 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
2601}
2602
2603void
f62ce20a 2604PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b
NIS
2605{
2606#ifdef HAS_SETLINEBUF
14a5cf38 2607 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 2608#else
14a5cf38 2609 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
9e353e3b
NIS
2610#endif
2611}
2612
2613#ifdef FILE_base
2614STDCHAR *
f62ce20a 2615PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 2616{
14a5cf38 2617 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 2618 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
2619}
2620
2621Size_t
f62ce20a 2622PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 2623{
14a5cf38
JH
2624 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2625 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
2626}
2627#endif
2628
2629#ifdef USE_STDIO_PTR
2630STDCHAR *
f62ce20a 2631PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 2632{
14a5cf38 2633 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 2634 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
2635}
2636
2637SSize_t
f62ce20a 2638PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 2639{
14a5cf38
JH
2640 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
2641 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
2642}
2643
2644void
f62ce20a 2645PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 2646{
14a5cf38 2647 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 2648 if (ptr != NULL) {
9e353e3b 2649#ifdef STDIO_PTR_LVALUE
f62ce20a 2650 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 2651#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 2652 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
2653 assert(PerlSIO_get_cnt(stdio) == (cnt));
2654 }
9e353e3b
NIS
2655#endif
2656#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 2657 /*
71200d45 2658 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
2659 */
2660 return;
9e353e3b 2661#endif
14a5cf38
JH
2662#else /* STDIO_PTR_LVALUE */
2663 PerlProc_abort();
2664#endif /* STDIO_PTR_LVALUE */
2665 }
2666 /*
71200d45 2667 * Now (or only) set cnt
14a5cf38 2668 */
9e353e3b 2669#ifdef STDIO_CNT_LVALUE
14a5cf38
JH
2670 PerlSIO_set_cnt(stdio, cnt);
2671#else /* STDIO_CNT_LVALUE */
9e353e3b 2672#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
2673 PerlSIO_set_ptr(stdio,
2674 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
2675 cnt));
2676#else /* STDIO_PTR_LVAL_SETS_CNT */
2677 PerlProc_abort();
2678#endif /* STDIO_PTR_LVAL_SETS_CNT */
2679#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
2680}
2681
2682#endif
2683
2684PerlIO_funcs PerlIO_stdio = {
14a5cf38
JH
2685 "stdio",
2686 sizeof(PerlIOStdio),
2687 PERLIO_K_BUFFERED,
2688 PerlIOBase_pushed,
2689 PerlIOBase_noop_ok,
2690 PerlIOStdio_open,
2691 NULL,
2692 PerlIOStdio_fileno,
71200d45 2693 PerlIOStdio_dup,
14a5cf38
JH
2694 PerlIOStdio_read,
2695 PerlIOStdio_unread,
2696 PerlIOStdio_write,
2697 PerlIOStdio_seek,
2698 PerlIOStdio_tell,
2699 PerlIOStdio_close,
2700 PerlIOStdio_flush,
2701 PerlIOStdio_fill,
2702 PerlIOStdio_eof,
2703 PerlIOStdio_error,
2704 PerlIOStdio_clearerr,
2705 PerlIOStdio_setlinebuf,
9e353e3b 2706#ifdef FILE_base
14a5cf38
JH
2707 PerlIOStdio_get_base,
2708 PerlIOStdio_get_bufsiz,
9e353e3b 2709#else
14a5cf38
JH
2710 NULL,
2711 NULL,
9e353e3b
NIS
2712#endif
2713#ifdef USE_STDIO_PTR
14a5cf38
JH
2714 PerlIOStdio_get_ptr,
2715 PerlIOStdio_get_cnt,
0eb1d8a4 2716#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
14a5cf38
JH
2717 PerlIOStdio_set_ptrcnt
2718#else /* STDIO_PTR_LVALUE */
2719 NULL
2720#endif /* STDIO_PTR_LVALUE */
2721#else /* USE_STDIO_PTR */
2722 NULL,
2723 NULL,
2724 NULL
2725#endif /* USE_STDIO_PTR */
9e353e3b
NIS
2726};
2727
9e353e3b
NIS
2728FILE *
2729PerlIO_exportFILE(PerlIO *f, int fl)
2730{
e87a358a 2731 dTHX;
14a5cf38
JH
2732 FILE *stdio;
2733 PerlIO_flush(f);
2734 stdio = fdopen(PerlIO_fileno(f), "r+");
2735 if (stdio) {
14a5cf38
JH
2736 PerlIOStdio *s =
2737 PerlIOSelf(PerlIO_push(aTHX_ f, &PerlIO_stdio, "r+", Nullsv),
2738 PerlIOStdio);
2739 s->stdio = stdio;
2740 }
2741 return stdio;
9e353e3b
NIS
2742}
2743
9e353e3b
NIS
2744FILE *
2745PerlIO_findFILE(PerlIO *f)
2746{
14a5cf38
JH
2747 PerlIOl *l = *f;
2748 while (l) {
2749 if (l->tab == &PerlIO_stdio) {
2750 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
2751 return s->stdio;
2752 }
2753 l = *PerlIONext(&l);
f7e7eb72 2754 }
14a5cf38 2755 return PerlIO_exportFILE(f, 0);
9e353e3b
NIS
2756}
2757
9e353e3b
NIS
2758void
2759PerlIO_releaseFILE(PerlIO *p, FILE *f)
2760{
2761}
2762
2763/*--------------------------------------------------------------------------------------*/
14a5cf38 2764/*
71200d45 2765 * perlio buffer layer
14a5cf38 2766 */
9e353e3b 2767
5e2ab84b 2768IV
f62ce20a 2769PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
5e2ab84b 2770{
14a5cf38
JH
2771 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2772 int fd = PerlIO_fileno(f);
2773 Off_t posn;
2774 if (fd >= 0 && PerlLIO_isatty(fd)) {
2775 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
2776 }
2777 posn = PerlIO_tell(PerlIONext(f));
2778 if (posn != (Off_t) - 1) {
2779 b->posn = posn;
2780 }
f62ce20a 2781 return PerlIOBase_pushed(aTHX_ f, mode, arg);
5e2ab84b
NIS
2782}
2783
9e353e3b 2784PerlIO *
14a5cf38
JH
2785PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
2786 IV n, const char *mode, int fd, int imode, int perm,
2787 PerlIO *f, int narg, SV **args)
2788{
2789 if (f) {
2790 PerlIO *next = PerlIONext(f);
2791 PerlIO_funcs *tab =
2792 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
2793 next =
2794 (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2795 next, narg, args);
2796 if (!next
f62ce20a 2797 || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg) != 0) {
14a5cf38
JH
2798 return NULL;
2799 }
2800 }
2801 else {
2802 PerlIO_funcs *tab =
2803 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
2804 int init = 0;
2805 if (*mode == 'I') {
2806 init = 1;
2807 /*
71200d45 2808 * mode++;
14a5cf38
JH
2809 */
2810 }
2811 f = (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
2812 NULL, narg, args);
2813 if (f) {
b26b1ab5
NC
2814 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
2815 /*
2816 * if push fails during open, open fails. close will pop us.
2817 */
2818 PerlIO_close (f);
2819 return NULL;
2820 } else {
2821 fd = PerlIO_fileno(f);
35990314 2822#ifdef PERLIO_USING_CRLF
14a5cf38 2823 /*
b26b1ab5 2824 * do something about failing setmode()? --jhi
14a5cf38 2825 */
b26b1ab5
NC
2826 PerlLIO_setmode(fd, O_BINARY);
2827#endif
2828 if (init && fd == 2) {
2829 /*
2830 * Initial stderr is unbuffered
2831 */
2832 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
2833 }
14a5cf38
JH
2834 }
2835 }
ee518936 2836 }
14a5cf38 2837 return f;
9e353e3b
NIS
2838}
2839
14a5cf38
JH
2840/*
2841 * This "flush" is akin to sfio's sync in that it handles files in either
71200d45 2842 * read or write state
14a5cf38 2843 */
9e353e3b 2844IV
f62ce20a 2845PerlIOBuf_flush(pTHX_ PerlIO *f)
6f9d8c32 2846{
14a5cf38
JH
2847 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2848 int code = 0;
2849 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
2850 /*
71200d45 2851 * write() the buffer
14a5cf38
JH
2852 */
2853 STDCHAR *buf = b->buf;
2854 STDCHAR *p = buf;
2855 PerlIO *n = PerlIONext(f);
2856 while (p < b->ptr) {
2857 SSize_t count = PerlIO_write(n, p, b->ptr - p);
2858 if (count > 0) {
2859 p += count;
2860 }
2861 else if (count < 0 || PerlIO_error(n)) {
2862 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2863 code = -1;
2864 break;
2865 }
2866 }
2867 b->posn += (p - buf);
2868 }
2869 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2870 STDCHAR *buf = PerlIO_get_base(f);
2871 /*
71200d45 2872 * Note position change
14a5cf38
JH
2873 */
2874 b->posn += (b->ptr - buf);
2875 if (b->ptr < b->end) {
2876 /*
71200d45 2877 * We did not consume all of it
14a5cf38
JH
2878 */
2879 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) == 0) {
2880 b->posn = PerlIO_tell(PerlIONext(f));
2881 }
2882 }
2883 }
2884 b->ptr = b->end = b->buf;
2885 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
2886 /*
71200d45 2887 * FIXME: Is this right for read case ?
14a5cf38
JH
2888 */
2889 if (PerlIO_flush(PerlIONext(f)) != 0)
2890 code = -1;
2891 return code;
6f9d8c32
NIS
2892}
2893
06da4f11 2894IV
f62ce20a 2895PerlIOBuf_fill(pTHX_ PerlIO *f)
06da4f11 2896{
14a5cf38
JH
2897 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2898 PerlIO *n = PerlIONext(f);
2899 SSize_t avail;
2900 /*
2901 * FIXME: doing the down-stream flush is a bad idea if it causes
2902 * pre-read data in stdio buffer to be discarded but this is too
2903 * simplistic - as it skips _our_ hosekeeping and breaks tell tests.
71200d45 2904 * if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { }
14a5cf38
JH
2905 */
2906 if (PerlIO_flush(f) != 0)
2907 return -1;
2908 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
f62ce20a 2909 PerlIOBase_flush_linebuf(aTHX);
14a5cf38
JH
2910
2911 if (!b->buf)
2912 PerlIO_get_base(f); /* allocate via vtable */
2913
2914 b->ptr = b->end = b->buf;
2915 if (PerlIO_fast_gets(n)) {
2916 /*
2917 * Layer below is also buffered We do _NOT_ want to call its
2918 * ->Read() because that will loop till it gets what we asked for
2919 * which may hang on a pipe etc. Instead take anything it has to
71200d45 2920 * hand, or ask it to fill _once_.
14a5cf38
JH
2921 */
2922 avail = PerlIO_get_cnt(n);
2923 if (avail <= 0) {
2924 avail = PerlIO_fill(n);
2925 if (avail == 0)
2926 avail = PerlIO_get_cnt(n);
2927 else {
2928 if (!PerlIO_error(n) && PerlIO_eof(n))
2929 avail = 0;
2930 }
2931 }
2932 if (avail > 0) {
2933 STDCHAR *ptr = PerlIO_get_ptr(n);
2934 SSize_t cnt = avail;
2935 if (avail > b->bufsiz)
2936 avail = b->bufsiz;
2937 Copy(ptr, b->buf, avail, STDCHAR);
2938 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
2939 }
2940 }
2941 else {
2942 avail = PerlIO_read(n, b->ptr, b->bufsiz);
2943 }
2944 if (avail <= 0) {
2945 if (avail == 0)
2946 PerlIOBase(f)->flags |= PERLIO_F_EOF;
2947 else
2948 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
2949 return -1;
2950 }
2951 b->end = b->buf + avail;
2952 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2953 return 0;
06da4f11
NIS
2954}
2955
6f9d8c32 2956SSize_t
f62ce20a 2957PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 2958{
14a5cf38
JH
2959 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2960 if (f) {
2961 if (!b->ptr)
2962 PerlIO_get_base(f);
f62ce20a 2963 return PerlIOBase_read(aTHX_ f, vbuf, count);
14a5cf38
JH
2964 }
2965 return 0;
6f9d8c32
NIS
2966}
2967
9e353e3b 2968SSize_t
f62ce20a 2969PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 2970{
14a5cf38
JH
2971 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
2972 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
2973 SSize_t unread = 0;
2974 SSize_t avail;
2975 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
2976 PerlIO_flush(f);
2977 if (!b->buf)
2978 PerlIO_get_base(f);
2979 if (b->buf) {
2980 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
2981 /*
2982 * Buffer is already a read buffer, we can overwrite any chars
71200d45 2983 * which have been read back to buffer start
14a5cf38
JH
2984 */
2985 avail = (b->ptr - b->buf);
2986 }
2987 else {
2988 /*
2989 * Buffer is idle, set it up so whole buffer is available for
71200d45 2990 * unread
14a5cf38
JH
2991 */
2992 avail = b->bufsiz;
2993 b->end = b->buf + avail;
2994 b->ptr = b->end;
2995 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
2996 /*
71200d45 2997 * Buffer extends _back_ from where we are now
14a5cf38
JH
2998 */
2999 b->posn -= b->bufsiz;
3000 }
3001 if (avail > (SSize_t) count) {
3002 /*
71200d45 3003 * If we have space for more than count, just move count
14a5cf38
JH
3004 */
3005 avail = count;
3006 }
3007 if (avail > 0) {
3008 b->ptr -= avail;
3009 buf -= avail;
3010 /*
3011 * In simple stdio-like ungetc() case chars will be already
71200d45 3012 * there
14a5cf38
JH
3013 */
3014 if (buf != b->ptr) {
3015 Copy(buf, b->ptr, avail, STDCHAR);
3016 }
3017 count -= avail;
3018 unread += avail;
3019 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3020 }
3021 }
3022 return unread;
760ac839
LW
3023}
3024
9e353e3b 3025SSize_t
f62ce20a 3026PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3027{
14a5cf38
JH
3028 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3029 const STDCHAR *buf = (const STDCHAR *) vbuf;
3030 Size_t written = 0;
3031 if (!b->buf)
3032 PerlIO_get_base(f);
3033 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3034 return 0;
3035 while (count > 0) {
3036 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3037 if ((SSize_t) count < avail)
3038 avail = count;
3039 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3040 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3041 while (avail > 0) {
3042 int ch = *buf++;
3043 *(b->ptr)++ = ch;
3044 count--;
3045 avail--;
3046 written++;
3047 if (ch == '\n') {
3048 PerlIO_flush(f);
3049 break;
3050 }
3051 }
3052 }
3053 else {
3054 if (avail) {
3055 Copy(buf, b->ptr, avail, STDCHAR);
3056 count -= avail;
3057 buf += avail;
3058 written += avail;
3059 b->ptr += avail;
3060 }
3061 }
3062 if (b->ptr >= (b->buf + b->bufsiz))
3063 PerlIO_flush(f);
3064 }
3065 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3066 PerlIO_flush(f);
3067 return written;
9e353e3b
NIS
3068}
3069
3070IV
f62ce20a 3071PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3072{
14a5cf38
JH
3073 IV code;
3074 if ((code = PerlIO_flush(f)) == 0) {
3075 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3076 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3077 code = PerlIO_seek(PerlIONext(f), offset, whence);
3078 if (code == 0) {
3079 b->posn = PerlIO_tell(PerlIONext(f));
3080 }
9e353e3b 3081 }
14a5cf38 3082 return code;
9e353e3b
NIS
3083}
3084
3085Off_t
f62ce20a 3086PerlIOBuf_tell(pTHX_ PerlIO *f)
9e353e3b 3087{
14a5cf38
JH
3088 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3089 /*
71200d45 3090 * b->posn is file position where b->buf was read, or will be written
14a5cf38
JH
3091 */
3092 Off_t posn = b->posn;
3093 if (b->buf) {
3094 /*
71200d45 3095 * If buffer is valid adjust position by amount in buffer
14a5cf38
JH
3096 */
3097 posn += (b->ptr - b->buf);
3098 }
3099 return posn;
9e353e3b
NIS
3100}
3101
3102IV
f62ce20a 3103PerlIOBuf_close(pTHX_ PerlIO *f)
9e353e3b 3104{
f62ce20a 3105 IV code = PerlIOBase_close(aTHX_ f);
14a5cf38
JH
3106 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3107 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 3108 Safefree(b->buf);
14a5cf38
JH
3109 }
3110 b->buf = NULL;
3111 b->ptr = b->end = b->buf;
3112 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3113 return code;
760ac839
LW
3114}
3115
9e353e3b 3116STDCHAR *
f62ce20a 3117PerlIOBuf_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3118{
14a5cf38
JH
3119 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3120 if (!b->buf)
3121 PerlIO_get_base(f);
3122 return b->ptr;
9e353e3b
NIS
3123}
3124
05d1247b 3125SSize_t
f62ce20a 3126PerlIOBuf_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3127{
14a5cf38
JH
3128 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3129 if (!b->buf)
3130 PerlIO_get_base(f);
3131 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF)
3132 return (b->end - b->ptr);
3133 return 0;
9e353e3b
NIS
3134}
3135
3136STDCHAR *
f62ce20a 3137PerlIOBuf_get_base(pTHX_ PerlIO *f)
9e353e3b 3138{
14a5cf38
JH
3139 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3140 if (!b->buf) {
3141 if (!b->bufsiz)
3142 b->bufsiz = 4096;
a1ea730d 3143 b->buf =
7fcdafbd 3144 Newz('B',b->buf,b->bufsiz, STDCHAR);
14a5cf38
JH
3145 if (!b->buf) {
3146 b->buf = (STDCHAR *) & b->oneword;
3147 b->bufsiz = sizeof(b->oneword);
3148 }
3149 b->ptr = b->buf;
3150 b->end = b->ptr;
06da4f11 3151 }
14a5cf38 3152 return b->buf;
9e353e3b
NIS
3153}
3154
3155Size_t
f62ce20a 3156PerlIOBuf_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3157{
14a5cf38
JH
3158 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3159 if (!b->buf)
3160 PerlIO_get_base(f);
3161 return (b->end - b->buf);
9e353e3b
NIS
3162}
3163
3164void
f62ce20a 3165PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3166{
14a5cf38
JH
3167 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3168 if (!b->buf)
3169 PerlIO_get_base(f);
3170 b->ptr = ptr;
3171 if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) {
14a5cf38
JH
3172 assert(PerlIO_get_cnt(f) == cnt);
3173 assert(b->ptr >= b->buf);
3174 }
3175 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
760ac839
LW
3176}
3177
71200d45 3178PerlIO *
ecdeb87c 3179PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
71200d45 3180{
ecdeb87c 3181 return PerlIOBase_dup(aTHX_ f, o, param, flags);
71200d45
NIS
3182}
3183
3184
3185
9e353e3b 3186PerlIO_funcs PerlIO_perlio = {
14a5cf38
JH
3187 "perlio",
3188 sizeof(PerlIOBuf),
3189 PERLIO_K_BUFFERED,
3190 PerlIOBuf_pushed,
3191 PerlIOBase_noop_ok,
3192 PerlIOBuf_open,
3193 NULL,
3194 PerlIOBase_fileno,
71200d45 3195 PerlIOBuf_dup,
14a5cf38
JH
3196 PerlIOBuf_read,
3197 PerlIOBuf_unread,
3198 PerlIOBuf_write,
3199 PerlIOBuf_seek,
3200 PerlIOBuf_tell,
3201 PerlIOBuf_close,
3202 PerlIOBuf_flush,
3203 PerlIOBuf_fill,
3204 PerlIOBase_eof,
3205 PerlIOBase_error,
3206 PerlIOBase_clearerr,
3207 PerlIOBase_setlinebuf,
3208 PerlIOBuf_get_base,
3209 PerlIOBuf_bufsiz,
3210 PerlIOBuf_get_ptr,
3211 PerlIOBuf_get_cnt,
3212 PerlIOBuf_set_ptrcnt,
9e353e3b
NIS
3213};
3214
66ecd56b 3215/*--------------------------------------------------------------------------------------*/
14a5cf38 3216/*
71200d45 3217 * Temp layer to hold unread chars when cannot do it any other way
14a5cf38 3218 */
5e2ab84b
NIS
3219
3220IV
f62ce20a 3221PerlIOPending_fill(pTHX_ PerlIO *f)
5e2ab84b 3222{
14a5cf38 3223 /*
71200d45 3224 * Should never happen
14a5cf38
JH
3225 */
3226 PerlIO_flush(f);
3227 return 0;
5e2ab84b
NIS
3228}
3229
3230IV
f62ce20a 3231PerlIOPending_close(pTHX_ PerlIO *f)
5e2ab84b 3232{
14a5cf38 3233 /*
71200d45 3234 * A tad tricky - flush pops us, then we close new top
14a5cf38
JH
3235 */
3236 PerlIO_flush(f);
3237 return PerlIO_close(f);
5e2ab84b
NIS
3238}
3239
3240IV
f62ce20a 3241PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
5e2ab84b 3242{
14a5cf38 3243 /*
71200d45 3244 * A tad tricky - flush pops us, then we seek new top
14a5cf38
JH
3245 */
3246 PerlIO_flush(f);
3247 return PerlIO_seek(f, offset, whence);
5e2ab84b
NIS
3248}
3249
3250
3251IV
f62ce20a 3252PerlIOPending_flush(pTHX_ PerlIO *f)
5e2ab84b 3253{
14a5cf38
JH
3254 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3255 if (b->buf && b->buf != (STDCHAR *) & b->oneword) {
3a1ee7e8 3256 Safefree(b->buf);
14a5cf38
JH
3257 b->buf = NULL;
3258 }
3259 PerlIO_pop(aTHX_ f);
3260 return 0;
5e2ab84b
NIS
3261}
3262
3263void
f62ce20a 3264PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
5e2ab84b 3265{
14a5cf38
JH
3266 if (cnt <= 0) {
3267 PerlIO_flush(f);
3268 }
3269 else {
f62ce20a 3270 PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt);
14a5cf38 3271 }
5e2ab84b
NIS
3272}
3273
3274IV
f62ce20a 3275PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
5e2ab84b 3276{
f62ce20a 3277 IV code = PerlIOBase_pushed(aTHX_ f, mode, arg);
14a5cf38
JH
3278 PerlIOl *l = PerlIOBase(f);
3279 /*
71200d45
NIS
3280 * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets()
3281 * etc. get muddled when it changes mid-string when we auto-pop.
14a5cf38
JH
3282 */
3283 l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) |
3284 (PerlIOBase(PerlIONext(f))->
3285 flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8));
3286 return code;
5e2ab84b
NIS
3287}
3288
3289SSize_t
f62ce20a 3290PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
5e2ab84b 3291{
14a5cf38
JH
3292 SSize_t avail = PerlIO_get_cnt(f);
3293 SSize_t got = 0;
3294 if (count < avail)
3295 avail = count;
3296 if (avail > 0)
f62ce20a 3297 got = PerlIOBuf_read(aTHX_ f, vbuf, avail);
14a5cf38
JH
3298 if (got >= 0 && got < count) {
3299 SSize_t more =
3300 PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got);
3301 if (more >= 0 || got == 0)
3302 got += more;
3303 }
3304 return got;
5e2ab84b
NIS
3305}
3306
5e2ab84b 3307PerlIO_funcs PerlIO_pending = {
14a5cf38
JH
3308 "pending",
3309 sizeof(PerlIOBuf),
3310 PERLIO_K_BUFFERED,
3311 PerlIOPending_pushed,
3312 PerlIOBase_noop_ok,
3313 NULL,
3314 NULL,
3315 PerlIOBase_fileno,
71200d45 3316 PerlIOBuf_dup,
14a5cf38
JH
3317 PerlIOPending_read,
3318 PerlIOBuf_unread,
3319 PerlIOBuf_write,
3320 PerlIOPending_seek,
3321 PerlIOBuf_tell,
3322 PerlIOPending_close,
3323 PerlIOPending_flush,
3324 PerlIOPending_fill,
3325 PerlIOBase_eof,
3326 PerlIOBase_error,
3327 PerlIOBase_clearerr,
3328 PerlIOBase_setlinebuf,
3329 PerlIOBuf_get_base,
3330 PerlIOBuf_bufsiz,
3331 PerlIOBuf_get_ptr,
3332 PerlIOBuf_get_cnt,
3333 PerlIOPending_set_ptrcnt,
5e2ab84b
NIS
3334};
3335
3336
3337
3338/*--------------------------------------------------------------------------------------*/
14a5cf38
JH
3339/*
3340 * crlf - translation On read translate CR,LF to "\n" we do this by
3341 * overriding ptr/cnt entries to hand back a line at a time and keeping a
71200d45 3342 * record of which nl we "lied" about. On write translate "\n" to CR,LF
66ecd56b
NIS
3343 */
3344
14a5cf38
JH
3345typedef struct {
3346 PerlIOBuf base; /* PerlIOBuf stuff */
71200d45 3347 STDCHAR *nl; /* Position of crlf we "lied" about in the
14a5cf38 3348 * buffer */
99efab12
NIS
3349} PerlIOCrlf;
3350
f5b9d040 3351IV
f62ce20a 3352PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg)
f5b9d040 3353{
14a5cf38
JH
3354 IV code;
3355 PerlIOBase(f)->flags |= PERLIO_F_CRLF;
f62ce20a 3356 code = PerlIOBuf_pushed(aTHX_ f, mode, arg);
5e2ab84b 3357#if 0
14a5cf38
JH
3358 PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n",
3359 f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
3360 PerlIOBase(f)->flags);
5e2ab84b 3361#endif
14a5cf38 3362 return code;
f5b9d040
NIS
3363}
3364
3365
99efab12 3366SSize_t
f62ce20a 3367PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
99efab12 3368{
14a5cf38
JH
3369 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3370 if (c->nl) {
3371 *(c->nl) = 0xd;
3372 c->nl = NULL;
3373 }
3374 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
f62ce20a 3375 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3376 else {
3377 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3378 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3379 SSize_t unread = 0;
3380 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3381 PerlIO_flush(f);
3382 if (!b->buf)
3383 PerlIO_get_base(f);
3384 if (b->buf) {
3385 if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3386 b->end = b->ptr = b->buf + b->bufsiz;
3387 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3388 b->posn -= b->bufsiz;
3389 }
3390 while (count > 0 && b->ptr > b->buf) {
3391 int ch = *--buf;
3392 if (ch == '\n') {
3393 if (b->ptr - 2 >= b->buf) {
3394 *--(b->ptr) = 0xa;
3395 *--(b->ptr) = 0xd;
3396 unread++;
3397 count--;
3398 }
3399 else {
3400 buf++;
3401 break;
3402 }
3403 }
3404 else {
3405 *--(b->ptr) = ch;
3406 unread++;
3407 count--;
3408 }
3409 }
3410 }
3411 return unread;
3412 }
99efab12
NIS
3413}
3414
3415SSize_t
f62ce20a 3416PerlIOCrlf_get_cnt(pTHX_ PerlIO *f)
99efab12 3417{
14a5cf38
JH
3418 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3419 if (!b->buf)
3420 PerlIO_get_base(f);
3421 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3422 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3423 if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && !c->nl) {
3424 STDCHAR *nl = b->ptr;
3425 scan:
3426 while (nl < b->end && *nl != 0xd)
3427 nl++;
3428 if (nl < b->end && *nl == 0xd) {
3429 test:
3430 if (nl + 1 < b->end) {
3431 if (nl[1] == 0xa) {
3432 *nl = '\n';
3433 c->nl = nl;
3434 }
3435 else {
3436 /*
71200d45 3437 * Not CR,LF but just CR
14a5cf38
JH
3438 */
3439 nl++;
3440 goto scan;
3441 }
3442 }
3443 else {
3444 /*
71200d45 3445 * Blast - found CR as last char in buffer
14a5cf38 3446 */
e87a358a 3447
14a5cf38
JH
3448 if (b->ptr < nl) {
3449 /*
3450 * They may not care, defer work as long as
71200d45 3451 * possible
14a5cf38 3452 */
a0d1d361 3453 c->nl = nl;
14a5cf38
JH
3454 return (nl - b->ptr);
3455 }
3456 else {
3457 int code;
3458 b->ptr++; /* say we have read it as far as
3459 * flush() is concerned */
d1be9408 3460 b->buf++; /* Leave space in front of buffer */
14a5cf38
JH
3461 b->bufsiz--; /* Buffer is thus smaller */
3462 code = PerlIO_fill(f); /* Fetch some more */
3463 b->bufsiz++; /* Restore size for next time */
3464 b->buf--; /* Point at space */
3465 b->ptr = nl = b->buf; /* Which is what we hand
3466 * off */
3467 b->posn--; /* Buffer starts here */
3468 *nl = 0xd; /* Fill in the CR */
3469 if (code == 0)
3470 goto test; /* fill() call worked */
3471 /*
71200d45 3472 * CR at EOF - just fall through
14a5cf38 3473 */
a0d1d361 3474 /* Should we clear EOF though ??? */
14a5cf38
JH
3475 }
3476 }
3477 }
3478 }
3479 return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr);
3480 }
3481 return 0;
99efab12
NIS
3482}
3483
3484void
f62ce20a 3485PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
14a5cf38
JH
3486{
3487 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3488 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3489 IV flags = PerlIOBase(f)->flags;
3490 if (!b->buf)
3491 PerlIO_get_base(f);
3492 if (!ptr) {
a0d1d361 3493 if (c->nl) {
14a5cf38 3494 ptr = c->nl + 1;
a0d1d361
NIS
3495 if (ptr == b->end && *c->nl == 0xd) {
3496 /* Defered CR at end of buffer case - we lied about count */
3497 ptr--;
3498 }
3499 }
14a5cf38
JH
3500 else {
3501 ptr = b->end;
14a5cf38
JH
3502 }
3503 ptr -= cnt;
3504 }
3505 else {
3506 /*
71200d45 3507 * Test code - delete when it works ...
14a5cf38 3508 */
ba7abf9d 3509 STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end;
a0d1d361
NIS
3510 if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) {
3511 /* Defered CR at end of buffer case - we lied about count */
3512 chk--;
e87a358a 3513 }
14a5cf38
JH
3514 chk -= cnt;
3515
a0d1d361
NIS
3516 if (ptr != chk ) {
3517 Perl_warn(aTHX_ "ptr wrong %p != %p fl=%08" UVxf
14a5cf38
JH
3518 " nl=%p e=%p for %d", ptr, chk, flags, c->nl,
3519 b->end, cnt);
3520 }
3521 }
3522 if (c->nl) {
3523 if (ptr > c->nl) {
3524 /*
71200d45 3525 * They have taken what we lied about
14a5cf38
JH
3526 */
3527 *(c->nl) = 0xd;
3528 c->nl = NULL;
3529 ptr++;
3530 }
3531 }
3532 b->ptr = ptr;
3533 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
99efab12
NIS
3534}
3535
3536SSize_t
f62ce20a 3537PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
99efab12 3538{
14a5cf38 3539 if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF))
f62ce20a 3540 return PerlIOBuf_write(aTHX_ f, vbuf, count);
14a5cf38
JH
3541 else {
3542 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3543 const STDCHAR *buf = (const STDCHAR *) vbuf;
3544 const STDCHAR *ebuf = buf + count;
3545 if (!b->buf)
3546 PerlIO_get_base(f);
3547 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3548 return 0;
3549 while (buf < ebuf) {
3550 STDCHAR *eptr = b->buf + b->bufsiz;
3551 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3552 while (buf < ebuf && b->ptr < eptr) {
3553 if (*buf == '\n') {
3554 if ((b->ptr + 2) > eptr) {
3555 /*
71200d45 3556 * Not room for both
14a5cf38
JH
3557 */
3558 PerlIO_flush(f);
3559 break;
3560 }
3561 else {
3562 *(b->ptr)++ = 0xd; /* CR */
3563 *(b->ptr)++ = 0xa; /* LF */
3564 buf++;
3565 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3566 PerlIO_flush(f);
3567 break;
3568 }
3569 }
3570 }
3571 else {
3572 int ch = *buf++;
3573 *(b->ptr)++ = ch;
3574 }
3575 if (b->ptr >= eptr) {
3576 PerlIO_flush(f);
3577 break;
3578 }
3579 }
3580 }
3581 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3582 PerlIO_flush(f);
3583 return (buf - (STDCHAR *) vbuf);
3584 }
99efab12
NIS
3585}
3586
3587IV
f62ce20a 3588PerlIOCrlf_flush(pTHX_ PerlIO *f)
99efab12 3589{
14a5cf38
JH
3590 PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf);
3591 if (c->nl) {
3592 *(c->nl) = 0xd;
3593 c->nl = NULL;
3594 }
f62ce20a 3595 return PerlIOBuf_flush(aTHX_ f);
99efab12
NIS
3596}
3597
66ecd56b 3598PerlIO_funcs PerlIO_crlf = {
14a5cf38
JH
3599 "crlf",
3600 sizeof(PerlIOCrlf),
3601 PERLIO_K_BUFFERED | PERLIO_K_CANCRLF,
3602 PerlIOCrlf_pushed,
3603 PerlIOBase_noop_ok, /* popped */
3604 PerlIOBuf_open,
3605 NULL,
3606 PerlIOBase_fileno,
71200d45 3607 PerlIOBuf_dup,
14a5cf38
JH
3608 PerlIOBuf_read, /* generic read works with ptr/cnt lies
3609 * ... */
3610 PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */
3611 PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */
3612 PerlIOBuf_seek,
3613 PerlIOBuf_tell,
3614 PerlIOBuf_close,
3615 PerlIOCrlf_flush,
3616 PerlIOBuf_fill,
3617 PerlIOBase_eof,
3618 PerlIOBase_error,
3619 PerlIOBase_clearerr,
3620 PerlIOBase_setlinebuf,
3621 PerlIOBuf_get_base,
3622 PerlIOBuf_bufsiz,
3623 PerlIOBuf_get_ptr,
3624 PerlIOCrlf_get_cnt,
3625 PerlIOCrlf_set_ptrcnt,
66ecd56b
NIS
3626};
3627
06da4f11
NIS
3628#ifdef HAS_MMAP
3629/*--------------------------------------------------------------------------------------*/
14a5cf38 3630/*
71200d45 3631 * mmap as "buffer" layer
14a5cf38 3632 */
06da4f11 3633
14a5cf38
JH
3634typedef struct {
3635 PerlIOBuf base; /* PerlIOBuf stuff */
3636 Mmap_t mptr; /* Mapped address */
3637 Size_t len; /* mapped length */
3638 STDCHAR *bbuf; /* malloced buffer if map fails */
06da4f11
NIS
3639} PerlIOMmap;
3640
c3d7c7c9
NIS
3641static size_t page_size = 0;
3642
06da4f11 3643IV
f62ce20a 3644PerlIOMmap_map(pTHX_ PerlIO *f)
06da4f11 3645{
14a5cf38
JH
3646 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3647 IV flags = PerlIOBase(f)->flags;
3648 IV code = 0;
3649 if (m->len)
3650 abort();
3651 if (flags & PERLIO_F_CANREAD) {
3652 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3653 int fd = PerlIO_fileno(f);
10eefe7f
CB
3654 Stat_t st;
3655 code = Fstat(fd, &st);
14a5cf38
JH
3656 if (code == 0 && S_ISREG(st.st_mode)) {
3657 SSize_t len = st.st_size - b->posn;
3658 if (len > 0) {
3659 Off_t posn;
3660 if (!page_size) {
68d873c6 3661#if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE))
14a5cf38
JH
3662 {
3663 SETERRNO(0, SS$_NORMAL);
68d873c6 3664# ifdef _SC_PAGESIZE
14a5cf38 3665 page_size = sysconf(_SC_PAGESIZE);
68d873c6 3666# else
14a5cf38 3667 page_size = sysconf(_SC_PAGE_SIZE);
14aaf8e8 3668# endif
14a5cf38
JH
3669 if ((long) page_size < 0) {
3670 if (errno) {
3671 SV *error = ERRSV;
3672 char *msg;
3673 STRLEN n_a;
3674 (void) SvUPGRADE(error, SVt_PV);
3675 msg = SvPVx(error, n_a);
3676 Perl_croak(aTHX_ "panic: sysconf: %s",
3677 msg);
3678 }
3679 else
3680 Perl_croak(aTHX_
3681 "panic: sysconf: pagesize unknown");
3682 }
3683 }
68d873c6
JH
3684#else
3685# ifdef HAS_GETPAGESIZE
14a5cf38 3686 page_size = getpagesize();
68d873c6
JH
3687# else
3688# if defined(I_SYS_PARAM) && defined(PAGESIZE)
14a5cf38 3689 page_size = PAGESIZE; /* compiletime, bad */
68d873c6
JH
3690# endif
3691# endif
3692#endif
14a5cf38
JH
3693 if ((IV) page_size <= 0)
3694 Perl_croak(aTHX_ "panic: bad pagesize %" IVdf,
3695 (IV) page_size);
3696 }
3697 if (b->posn < 0) {
3698 /*
3699 * This is a hack - should never happen - open should
71200d45 3700 * have set it !
14a5cf38
JH
3701 */
3702 b->posn = PerlIO_tell(PerlIONext(f));
3703 }
3704 posn = (b->posn / page_size) * page_size;
3705 len = st.st_size - posn;
3706 m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn);
3707 if (m->mptr && m->mptr != (Mmap_t) - 1) {
a5262162 3708#if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL)
14a5cf38 3709 madvise(m->mptr, len, MADV_SEQUENTIAL);
06da4f11 3710#endif
a5262162 3711#if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED)
14a5cf38 3712 madvise(m->mptr, len, MADV_WILLNEED);
a5262162 3713#endif
14a5cf38
JH
3714 PerlIOBase(f)->flags =
3715 (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF;
3716 b->end = ((STDCHAR *) m->mptr) + len;
3717 b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn);
3718 b->ptr = b->buf;
3719 m->len = len;
3720 }
3721 else {
3722 b->buf = NULL;
3723 }
3724 }
3725 else {
3726 PerlIOBase(f)->flags =
3727 flags | PERLIO_F_EOF | PERLIO_F_RDBUF;
3728 b->buf = NULL;
3729 b->ptr = b->end = b->ptr;
3730 code = -1;
3731 }
3732 }
3733 }
3734 return code;
06da4f11
NIS
3735}
3736
3737IV
e87a358a 3738PerlIOMmap_unmap(pTHX_ PerlIO *f)
06da4f11 3739{
14a5cf38
JH
3740 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3741 PerlIOBuf *b = &m->base;
3742 IV code = 0;
3743 if (m->len) {
3744 if (b->buf) {
3745 code = munmap(m->mptr, m->len);
3746 b->buf = NULL;
3747 m->len = 0;
3748 m->mptr = NULL;
3749 if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0)
3750 code = -1;
3751 }
3752 b->ptr = b->end = b->buf;
3753 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
3754 }
3755 return code;
06da4f11
NIS
3756}
3757
3758STDCHAR *
f62ce20a 3759PerlIOMmap_get_base(pTHX_ PerlIO *f)
06da4f11 3760{
14a5cf38
JH
3761 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3762 PerlIOBuf *b = &m->base;
3763 if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) {
3764 /*
71200d45 3765 * Already have a readbuffer in progress
14a5cf38
JH
3766 */
3767 return b->buf;
3768 }
3769 if (b->buf) {
3770 /*
71200d45 3771 * We have a write buffer or flushed PerlIOBuf read buffer
14a5cf38
JH
3772 */
3773 m->bbuf = b->buf; /* save it in case we need it again */
3774 b->buf = NULL; /* Clear to trigger below */
3775 }
3776 if (!b->buf) {
f62ce20a 3777 PerlIOMmap_map(aTHX_ f); /* Try and map it */
14a5cf38
JH
3778 if (!b->buf) {
3779 /*
71200d45 3780 * Map did not work - recover PerlIOBuf buffer if we have one
14a5cf38
JH
3781 */
3782 b->buf = m->bbuf;
3783 }
3784 }
3785 b->ptr = b->end = b->buf;
3786 if (b->buf)
3787 return b->buf;
f62ce20a 3788 return PerlIOBuf_get_base(aTHX_ f);
06da4f11
NIS
3789}
3790
3791SSize_t
f62ce20a 3792PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
06da4f11 3793{
14a5cf38
JH
3794 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3795 PerlIOBuf *b = &m->base;
3796 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3797 PerlIO_flush(f);
3798 if (b->ptr && (b->ptr - count) >= b->buf
3799 && memEQ(b->ptr - count, vbuf, count)) {
3800 b->ptr -= count;
3801 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3802 return count;
3803 }
3804 if (m->len) {
3805 /*
71200d45 3806 * Loose the unwritable mapped buffer
14a5cf38
JH
3807 */
3808 PerlIO_flush(f);
3809 /*
71200d45 3810 * If flush took the "buffer" see if we have one from before
14a5cf38
JH
3811 */
3812 if (!b->buf && m->bbuf)
3813 b->buf = m->bbuf;
3814 if (!b->buf) {
f62ce20a 3815 PerlIOBuf_get_base(aTHX_ f);
14a5cf38
JH
3816 m->bbuf = b->buf;
3817 }
3818 }
f62ce20a 3819 return PerlIOBuf_unread(aTHX_ f, vbuf, count);
06da4f11
NIS
3820}
3821
3822SSize_t
f62ce20a 3823PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
06da4f11 3824{
14a5cf38
JH
3825 PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap);
3826 PerlIOBuf *b = &m->base;
3827 if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) {
3828 /*
71200d45 3829 * No, or wrong sort of, buffer
14a5cf38
JH
3830 */
3831 if (m->len) {
e87a358a 3832 if (PerlIOMmap_unmap(aTHX_ f) != 0)
14a5cf38
JH
3833 return 0;
3834 }