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