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