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