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