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