This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to ExtUtils::MakeMaker 6.12.
[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
PP
346#undef PerlIO_tmpfile
347PerlIO *
8ac85365 348PerlIO_tmpfile(void)
33dcbb9a 349{
14a5cf38 350 return tmpfile();
33dcbb9a
PP
351}
352
22569500 353#else /* PERLIO_IS_STDIO */
760ac839
LW
354
355#ifdef USE_SFIO
356
357#undef HAS_FSETPOS
358#undef HAS_FGETPOS
359
14a5cf38
JH
360/*
361 * This section is just to make sure these functions get pulled in from
71200d45 362 * libsfio.a
14a5cf38 363 */
760ac839
LW
364
365#undef PerlIO_tmpfile
366PerlIO *
c78749f2 367PerlIO_tmpfile(void)
760ac839 368{
14a5cf38 369 return sftmp(0);
760ac839
LW
370}
371
372void
e8632036 373PerlIO_init(pTHX)
760ac839 374{
14a5cf38
JH
375 /*
376 * Force this file to be included in perl binary. Which allows this
377 * file to force inclusion of other functions that may be required by
71200d45 378 * loadable extensions e.g. for FileHandle::tmpfile
14a5cf38 379 */
760ac839 380
14a5cf38 381 /*
71200d45 382 * Hack sfio does its own 'autoflush' on stdout in common cases. Flush
14a5cf38 383 * results in a lot of lseek()s to regular files and lot of small
71200d45 384 * writes to pipes.
14a5cf38
JH
385 */
386 sfset(sfstdout, SF_SHARE, 0);
760ac839
LW
387}
388
b9d6bf13 389/* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */
92bff44d 390PerlIO *
4b069b44 391PerlIO_importFILE(FILE *stdio, const char *mode)
92bff44d 392{
14a5cf38 393 int fd = fileno(stdio);
4b069b44 394 if (!mode || !*mode) {
81428673 395 mode = "r+";
4b069b44
NIS
396 }
397 return PerlIO_fdopen(fd, mode);
92bff44d
NIS
398}
399
400FILE *
401PerlIO_findFILE(PerlIO *pio)
402{
14a5cf38
JH
403 int fd = PerlIO_fileno(pio);
404 FILE *f = fdopen(fd, "r+");
405 PerlIO_flush(pio);
406 if (!f && errno == EINVAL)
407 f = fdopen(fd, "w");
408 if (!f && errno == EINVAL)
409 f = fdopen(fd, "r");
410 return f;
92bff44d
NIS
411}
412
413
22569500 414#else /* USE_SFIO */
6f9d8c32 415/*======================================================================================*/
14a5cf38 416/*
71200d45 417 * Implement all the PerlIO interface ourselves.
9e353e3b 418 */
760ac839 419
76ced9ad
NIS
420#include "perliol.h"
421
14a5cf38
JH
422/*
423 * We _MUST_ have <unistd.h> if we are using lseek() and may have large
71200d45 424 * files
14a5cf38 425 */
02f66e2f
NIS
426#ifdef I_UNISTD
427#include <unistd.h>
428#endif
06da4f11
NIS
429#ifdef HAS_MMAP
430#include <sys/mman.h>
431#endif
432
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);
e1caacb4 1073#if defined(WIN32) && !defined(UNDER_CE)
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 }
35990314 2612#ifdef PERLIO_USING_CRLF
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 {
2713 FILE *stdio = PerlSIO_fopen(path, mode);
2714 if (stdio) {
d9dac8cd
NIS
2715 PerlIOStdio *s;
2716 if (!f) {
2717 f = PerlIO_allocate(aTHX);
2718 }
a33cf58c 2719 if ((f = PerlIO_push(aTHX_ f, self,
14a5cf38 2720 (mode = PerlIOStdio_mode(mode, tmode)),
a33cf58c
NIS
2721 PerlIOArg))) {
2722 s = PerlIOSelf(f, PerlIOStdio);
2723 s->stdio = stdio;
2724 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2725 }
602c3c4b
NIS
2726 return f;
2727 }
2728 else {
2729 return NULL;
14a5cf38 2730 }
14a5cf38
JH
2731 }
2732 }
2733 if (fd >= 0) {
2734 FILE *stdio = NULL;
2735 int init = 0;
2736 if (*mode == 'I') {
2737 init = 1;
2738 mode++;
2739 }
2740 if (init) {
2741 switch (fd) {
2742 case 0:
2743 stdio = PerlSIO_stdin;
2744 break;
2745 case 1:
2746 stdio = PerlSIO_stdout;
2747 break;
2748 case 2:
2749 stdio = PerlSIO_stderr;
2750 break;
2751 }
2752 }
2753 else {
2754 stdio = PerlSIO_fdopen(fd, mode =
2755 PerlIOStdio_mode(mode, tmode));
2756 }
2757 if (stdio) {
d9dac8cd
NIS
2758 PerlIOStdio *s;
2759 if (!f) {
2760 f = PerlIO_allocate(aTHX);
2761 }
a33cf58c
NIS
2762 if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) {
2763 s = PerlIOSelf(f, PerlIOStdio);
2764 s->stdio = stdio;
2765 PerlIOUnix_refcnt_inc(fileno(s->stdio));
2766 }
14a5cf38
JH
2767 return f;
2768 }
2769 }
2770 }
ee518936 2771 return NULL;
9e353e3b
NIS
2772}
2773
1751d015 2774PerlIO *
ecdeb87c 2775PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
1751d015
NIS
2776{
2777 /* This assumes no layers underneath - which is what
2778 happens, but is not how I remember it. NI-S 2001/10/16
2779 */
ecdeb87c 2780 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
694c95cf 2781 FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio;
9217ff3f
NIS
2782 int fd = fileno(stdio);
2783 char mode[8];
ecdeb87c 2784 if (flags & PERLIO_DUP_FD) {
9217ff3f
NIS
2785 int dfd = PerlLIO_dup(fileno(stdio));
2786 if (dfd >= 0) {
2787 stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode));
2788 goto set_this;
ecdeb87c
NIS
2789 }
2790 else {
2791 /* FIXME: To avoid messy error recovery if dup fails
2792 re-use the existing stdio as though flag was not set
2793 */
2794 }
2795 }
9217ff3f
NIS
2796 stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode));
2797 set_this:
694c95cf
JH
2798 PerlIOSelf(f, PerlIOStdio)->stdio = stdio;
2799 PerlIOUnix_refcnt_inc(fileno(stdio));
1751d015
NIS
2800 }
2801 return f;
2802}
2803
0d7a5398
NIS
2804static int
2805PerlIOStdio_invalidate_fileno(pTHX_ FILE *f)
2806{
2807 /* XXX this could use PerlIO_canset_fileno() and
37725cdc 2808 * PerlIO_set_fileno() support from Configure
0d7a5398
NIS
2809 */
2810# if defined(__GLIBC__)
2811 /* There may be a better way for GLIBC:
37725cdc 2812 - libio.h defines a flag to not close() on cleanup
0d7a5398
NIS
2813 */
2814 f->_fileno = -1;
2815 return 1;
2816# elif defined(__sun__)
2817# if defined(_LP64)
2818 /* On solaris, if _LP64 is defined, the FILE structure is this:
2819 *
2820 * struct FILE {
2821 * long __pad[16];
2822 * };
2823 *
37725cdc 2824 * It turns out that the fd is stored in the top 32 bits of
0d7a5398
NIS
2825 * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears
2826 * to contain a pointer or offset into another structure. All the
2827 * remaining fields are zero.
2828 *
2829 * We set the top bits to -1 (0xFFFFFFFF).
2830 */
2831 f->__pad[4] |= 0xffffffff00000000L;
2832 assert(fileno(f) == 0xffffffff);
2833# else /* !defined(_LP64) */
37725cdc
NIS
2834 /* _file is just a unsigned char :-(
2835 Not clear why we dup() rather than using -1
2836 even if that would be treated as 0xFF - so will
0d7a5398
NIS
2837 a dup fail ...
2838 */
673c27c1 2839 f->_file = PerlLIO_dup(fileno(f));
0d7a5398
NIS
2840# endif /* defined(_LP64) */
2841 return 1;
2842# elif defined(__hpux)
2843 f->__fileH = 0xff;
2844 f->__fileL = 0xff;
2845 return 1;
9837d373 2846 /* Next one ->_file seems to be a reasonable fallback, i.e. if
37725cdc 2847 your platform does not have special entry try this one.
9837d373
NIS
2848 [For OSF only have confirmation for Tru64 (alpha)
2849 but assume other OSFs will be similar.]
37725cdc 2850 */
9837d373 2851# elif defined(_AIX) || defined(__osf__) || defined(__irix__)
0d7a5398
NIS
2852 f->_file = -1;
2853 return 1;
2854# elif defined(__FreeBSD__)
2855 /* There may be a better way on FreeBSD:
37725cdc
NIS
2856 - we could insert a dummy func in the _close function entry
2857 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2858 */
2859 f->_file = -1;
2860 return 1;
2861# elif defined(__CYGWIN__)
2862 /* There may be a better way on CYGWIN:
37725cdc
NIS
2863 - we could insert a dummy func in the _close function entry
2864 f->_close = (int (*)(void *)) dummy_close;
0d7a5398
NIS
2865 */
2866 f->_file = -1;
2867 return 1;
2868# elif defined(WIN32)
2869# if defined(__BORLANDC__)
2870 f->fd = PerlLIO_dup(fileno(f));
b475b3e6
JH
2871# elif defined(UNDER_CE)
2872 /* WIN_CE does not have access to FILE internals, it hardly has FILE
2873 structure at all
2874 */
0d7a5398
NIS
2875# else
2876 f->_file = -1;
2877# endif
2878 return 1;
2879# else
2880#if 0
37725cdc 2881 /* Sarathy's code did this - we fall back to a dup/dup2 hack
0d7a5398 2882 (which isn't thread safe) instead
37725cdc 2883 */
0d7a5398
NIS
2884# error "Don't know how to set FILE.fileno on your platform"
2885#endif
2886 return 0;
2887# endif
2888}
2889
1751d015 2890IV
f62ce20a 2891PerlIOStdio_close(pTHX_ PerlIO *f)
1751d015 2892{
1751d015 2893 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
439ba545
NIS
2894 if (!stdio) {
2895 errno = EBADF;
2896 return -1;
2897 }
9217ff3f
NIS
2898 else {
2899 int fd = fileno(stdio);
0d7a5398
NIS
2900 int socksfd = 0;
2901 int invalidate = 0;
bbfd922f 2902 IV result = 0;
0d7a5398
NIS
2903 int saveerr = 0;
2904 int dupfd = 0;
2905#ifdef SOCKS5_VERSION_NAME
37725cdc
NIS
2906 /* Socks lib overrides close() but stdio isn't linked to
2907 that library (though we are) - so we must call close()
2908 on sockets on stdio's behalf.
2909 */
0d7a5398
NIS
2910 int optval;
2911 Sock_size_t optlen = sizeof(int);
2912 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) {
2913 socksfd = 1;
37725cdc 2914 invalidate = 1;
0d7a5398
NIS
2915 }
2916#endif
9217ff3f
NIS
2917 if (PerlIOUnix_refcnt_dec(fd) > 0) {
2918 /* File descriptor still in use */
0d7a5398
NIS
2919 invalidate = 1;
2920 socksfd = 0;
37725cdc 2921 }
0d7a5398 2922 if (invalidate) {
37725cdc
NIS
2923 /* For STD* handles don't close the stdio at all
2924 this is because we have shared the FILE * too
0d7a5398
NIS
2925 */
2926 if (stdio == stdin) {
2927 /* Some stdios are buggy fflush-ing inputs */
2928 return 0;
2929 }
2930 else if (stdio == stdout || stdio == stderr) {
9217ff3f
NIS
2931 return PerlIO_flush(f);
2932 }
37725cdc
NIS
2933 /* Tricky - must fclose(stdio) to free memory but not close(fd)
2934 Use Sarathy's trick from maint-5.6 to invalidate the
2935 fileno slot of the FILE *
2936 */
bbfd922f 2937 result = PerlIO_flush(f);
0d7a5398
NIS
2938 saveerr = errno;
2939 if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) {
9217ff3f
NIS
2940 dupfd = PerlLIO_dup(fd);
2941 }
37725cdc 2942 }
0d7a5398 2943 result = PerlSIO_fclose(stdio);
37725cdc
NIS
2944 /* We treat error from stdio as success if we invalidated
2945 errno may NOT be expected EBADF
e8529473
NIS
2946 */
2947 if (invalidate && result != 0) {
0d7a5398
NIS
2948 errno = saveerr;
2949 result = 0;
37725cdc 2950 }
0d7a5398
NIS
2951 if (socksfd) {
2952 /* in SOCKS case let close() determine return value */
2953 result = close(fd);
2954 }
2955 if (dupfd) {
2956 PerlLIO_dup2(dupfd,fd);
8a521f28 2957 PerlLIO_close(dupfd);
9217ff3f
NIS
2958 }
2959 return result;
37725cdc 2960 }
1751d015
NIS
2961}
2962
9e353e3b 2963SSize_t
f62ce20a 2964PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
9e353e3b 2965{
14a5cf38
JH
2966 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2967 SSize_t got = 0;
4d948241
NIS
2968 for (;;) {
2969 if (count == 1) {
2970 STDCHAR *buf = (STDCHAR *) vbuf;
2971 /*
2972 * Perl is expecting PerlIO_getc() to fill the buffer Linux's
2973 * stdio does not do that for fread()
2974 */
2975 int ch = PerlSIO_fgetc(s);
2976 if (ch != EOF) {
2977 *buf = ch;
2978 got = 1;
2979 }
14a5cf38 2980 }
4d948241
NIS
2981 else
2982 got = PerlSIO_fread(vbuf, 1, count, s);
42a7a32f 2983 if (got >= 0 || errno != EINTR)
4d948241
NIS
2984 break;
2985 PERL_ASYNC_CHECK();
42a7a32f 2986 SETERRNO(0,0); /* just in case */
14a5cf38 2987 }
14a5cf38 2988 return got;
9e353e3b
NIS
2989}
2990
2991SSize_t
f62ce20a 2992PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 2993{
14a5cf38 2994 SSize_t unread = 0;
93679785
NIS
2995 FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio;
2996
313e59c8 2997#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 2998 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
2999 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3000 STDCHAR *base = PerlIO_get_base(f);
3001 SSize_t cnt = PerlIO_get_cnt(f);
3002 STDCHAR *ptr = PerlIO_get_ptr(f);
3003 SSize_t avail = ptr - base;
3004 if (avail > 0) {
3005 if (avail > count) {
3006 avail = count;
3007 }
3008 ptr -= avail;
3009 Move(buf-avail,ptr,avail,STDCHAR);
3010 count -= avail;
3011 unread += avail;
3012 PerlIO_set_ptrcnt(f,ptr,cnt+avail);
9f7cd136
NIS
3013 if (PerlSIO_feof(s) && unread >= 0)
3014 PerlSIO_clearerr(s);
3015 }
3016 }
313e59c8
NIS
3017 else
3018#endif
3019 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3020 /* We can get pointer to buffer but not its base
3021 Do ungetc() but check chars are ending up in the
3022 buffer
3023 */
3024 STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s);
3025 STDCHAR *buf = ((STDCHAR *) vbuf) + count;
3026 while (count > 0) {
3027 int ch = *--buf & 0xFF;
3028 if (ungetc(ch,s) != ch) {
3029 /* ungetc did not work */
3030 break;
3031 }
3032 if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) {
3033 /* Did not change pointer as expected */
3034 fgetc(s); /* get char back again */
3035 break;
3036 }
3037 /* It worked ! */
3038 count--;
3039 unread++;
93679785
NIS
3040 }
3041 }
3042
3043 if (count > 0) {
3044 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
14a5cf38
JH
3045 }
3046 return unread;
9e353e3b
NIS
3047}
3048
3049SSize_t
f62ce20a 3050PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
9e353e3b 3051{
4d948241
NIS
3052 SSize_t got;
3053 for (;;) {
3054 got = PerlSIO_fwrite(vbuf, 1, count,
3055 PerlIOSelf(f, PerlIOStdio)->stdio);
42a7a32f 3056 if (got >= 0 || errno != EINTR)
4d948241
NIS
3057 break;
3058 PERL_ASYNC_CHECK();
42a7a32f 3059 SETERRNO(0,0); /* just in case */
4d948241
NIS
3060 }
3061 return got;
9e353e3b
NIS
3062}
3063
94a175e1 3064IV
f62ce20a 3065PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3066{
14a5cf38 3067 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3068 return PerlSIO_fseek(stdio, offset, whence);
9e353e3b
NIS
3069}
3070
3071Off_t
f62ce20a 3072PerlIOStdio_tell(pTHX_ PerlIO *f)
9e353e3b 3073{
14a5cf38 3074 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
94a175e1 3075 return PerlSIO_ftell(stdio);
9e353e3b
NIS
3076}
3077
3078IV
f62ce20a 3079PerlIOStdio_flush(pTHX_ PerlIO *f)
9e353e3b 3080{
14a5cf38
JH
3081 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3082 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
3083 return PerlSIO_fflush(stdio);
3084 }
3085 else {
88b61e10 3086#if 0
14a5cf38
JH
3087 /*
3088 * FIXME: This discards ungetc() and pre-read stuff which is not
71200d45 3089 * right if this is just a "sync" from a layer above Suspect right
14a5cf38 3090 * design is to do _this_ but not have layer above flush this
71200d45 3091 * layer read-to-read
14a5cf38
JH
3092 */
3093 /*
71200d45 3094 * Not writeable - sync by attempting a seek
14a5cf38
JH
3095 */
3096 int err = errno;
3097 if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0)
3098 errno = err;
88b61e10 3099#endif
14a5cf38
JH
3100 }
3101 return 0;
9e353e3b
NIS
3102}
3103
3104IV
f62ce20a 3105PerlIOStdio_eof(pTHX_ PerlIO *f)
9e353e3b 3106{
14a5cf38 3107 return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3108}
3109
3110IV
f62ce20a 3111PerlIOStdio_error(pTHX_ PerlIO *f)
9e353e3b 3112{
263df5f1 3113 return PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3114}
3115
3116void
f62ce20a 3117PerlIOStdio_clearerr(pTHX_ PerlIO *f)
9e353e3b 3118{
14a5cf38 3119 PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b
NIS
3120}
3121
3122void
f62ce20a 3123PerlIOStdio_setlinebuf(pTHX_ PerlIO *f)
9e353e3b
NIS
3124{
3125#ifdef HAS_SETLINEBUF
14a5cf38 3126 PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio);
9e353e3b 3127#else
14a5cf38 3128 PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0);
9e353e3b
NIS
3129#endif
3130}
3131
3132#ifdef FILE_base
3133STDCHAR *
f62ce20a 3134PerlIOStdio_get_base(pTHX_ PerlIO *f)
9e353e3b 3135{
14a5cf38 3136 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3137 return (STDCHAR*)PerlSIO_get_base(stdio);
9e353e3b
NIS
3138}
3139
3140Size_t
f62ce20a 3141PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f)
9e353e3b 3142{
14a5cf38
JH
3143 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3144 return PerlSIO_get_bufsiz(stdio);
9e353e3b
NIS
3145}
3146#endif
3147
3148#ifdef USE_STDIO_PTR
3149STDCHAR *
f62ce20a 3150PerlIOStdio_get_ptr(pTHX_ PerlIO *f)
9e353e3b 3151{
14a5cf38 3152 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
cc00df79 3153 return (STDCHAR*)PerlSIO_get_ptr(stdio);
9e353e3b
NIS
3154}
3155
3156SSize_t
f62ce20a 3157PerlIOStdio_get_cnt(pTHX_ PerlIO *f)
9e353e3b 3158{
14a5cf38
JH
3159 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3160 return PerlSIO_get_cnt(stdio);
9e353e3b
NIS
3161}
3162
3163void
f62ce20a 3164PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt)
9e353e3b 3165{
14a5cf38 3166 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
14a5cf38 3167 if (ptr != NULL) {
9e353e3b 3168#ifdef STDIO_PTR_LVALUE
22569500 3169 PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */
9e353e3b 3170#ifdef STDIO_PTR_LVAL_SETS_CNT
14a5cf38 3171 if (PerlSIO_get_cnt(stdio) != (cnt)) {
14a5cf38
JH
3172 assert(PerlSIO_get_cnt(stdio) == (cnt));
3173 }
9e353e3b
NIS
3174#endif
3175#if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT))
14a5cf38 3176 /*
71200d45 3177 * Setting ptr _does_ change cnt - we are done
14a5cf38
JH
3178 */
3179 return;
9e353e3b 3180#endif
22569500 3181#else /* STDIO_PTR_LVALUE */
14a5cf38 3182 PerlProc_abort();
22569500 3183#endif /* STDIO_PTR_LVALUE */
14a5cf38
JH
3184 }
3185 /*
71200d45 3186 * Now (or only) set cnt
14a5cf38 3187 */
9e353e3b 3188#ifdef STDIO_CNT_LVALUE
14a5cf38 3189 PerlSIO_set_cnt(stdio, cnt);
22569500 3190#else /* STDIO_CNT_LVALUE */
9e353e3b 3191#if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT))
14a5cf38
JH
3192 PerlSIO_set_ptr(stdio,
3193 PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) -
3194 cnt));
22569500 3195#else /* STDIO_PTR_LVAL_SETS_CNT */
14a5cf38 3196 PerlProc_abort();
22569500
NIS
3197#endif /* STDIO_PTR_LVAL_SETS_CNT */
3198#endif /* STDIO_CNT_LVALUE */
9e353e3b
NIS
3199}
3200
93679785 3201
9e353e3b
NIS
3202#endif
3203
93679785
NIS
3204IV
3205PerlIOStdio_fill(pTHX_ PerlIO *f)
3206{
3207 FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio;
3208 int c;
3209 /*
3210 * fflush()ing read-only streams can cause trouble on some stdio-s
3211 */
3212 if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) {
3213 if (PerlSIO_fflush(stdio) != 0)
3214 return EOF;
3215 }
3216 c = PerlSIO_fgetc(stdio);
3217 if (c == EOF)
3218 return EOF;
3219
3220#if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT)))
313e59c8
NIS
3221
3222#ifdef STDIO_BUFFER_WRITABLE
9f7cd136 3223 if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) {
93679785
NIS
3224 /* Fake ungetc() to the real buffer in case system's ungetc
3225 goes elsewhere
3226 */
3227 STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio);
3228 SSize_t cnt = PerlSIO_get_cnt(stdio);
3229 STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio);
3230 if (ptr == base+1) {
3231 *--ptr = (STDCHAR) c;
3232 PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1);
3233 if (PerlSIO_feof(stdio))
3234 PerlSIO_clearerr(stdio);
3235 return 0;
3236 }
3237 }
313e59c8
NIS
3238 else
3239#endif
3240 if (PerlIO_has_cntptr(f)) {
9f7cd136
NIS
3241 STDCHAR ch = c;
3242 if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) {
3243 return 0;
3244 }
3245 }
93679785
NIS
3246#endif
3247
3248#if defined(VMS)
3249 /* An ungetc()d char is handled separately from the regular
3250 * buffer, so we stuff it in the buffer ourselves.
3251 * Should never get called as should hit code above
3252 */
bad9695d
NIS
3253 *(--((*stdio)->_ptr)) = (unsigned char) c;
3254 (*stdio)->_cnt++;
93679785
NIS
3255#else
3256 /* If buffer snoop scheme above fails fall back to
9f7cd136 3257 using ungetc().
93679785
NIS
3258 */
3259 if (PerlSIO_ungetc(c, stdio) != c)
3260 return EOF;
3261#endif
3262 return 0;
3263}
3264
3265
3266
9e353e3b 3267PerlIO_funcs PerlIO_stdio = {
2dc2558e 3268 sizeof(PerlIO_funcs),
14a5cf38
JH
3269 "stdio",
3270 sizeof(PerlIOStdio),
86e05cf2 3271 PERLIO_K_BUFFERED|PERLIO_K_RAW,
1fd8f4ce 3272 PerlIOStdio_pushed,
44798d05 3273 PerlIOBase_popped,
14a5cf38 3274 PerlIOStdio_open,
86e05cf2 3275 PerlIOBase_binmode, /* binmode */
14a5cf38
JH
3276 NULL,
3277 PerlIOStdio_fileno,
71200d45 3278 PerlIOStdio_dup,
14a5cf38
JH
3279 PerlIOStdio_read,
3280 PerlIOStdio_unread,
3281 PerlIOStdio_write,
3282 PerlIOStdio_seek,
3283 PerlIOStdio_tell,
3284 PerlIOStdio_close,
3285 PerlIOStdio_flush,
3286 PerlIOStdio_fill,
3287 PerlIOStdio_eof,
3288 PerlIOStdio_error,
3289 PerlIOStdio_clearerr,
3290 PerlIOStdio_setlinebuf,
9e353e3b 3291#ifdef FILE_base
14a5cf38
JH
3292 PerlIOStdio_get_base,
3293 PerlIOStdio_get_bufsiz,
9e353e3b 3294#else
14a5cf38
JH
3295 NULL,
3296 NULL,
9e353e3b
NIS
3297#endif
3298#ifdef USE_STDIO_PTR
14a5cf38
JH
3299 PerlIOStdio_get_ptr,
3300 PerlIOStdio_get_cnt,
15b61c98 3301# if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO)
79ed0f43 3302 PerlIOStdio_set_ptrcnt,
15b61c98
JH
3303# else
3304 NULL,
3305# endif /* HAS_FAST_STDIO && USE_FAST_STDIO */
3306#else
3307 NULL,
14a5cf38
JH
3308 NULL,
3309 NULL,
15b61c98 3310#endif /* USE_STDIO_PTR */
9e353e3b
NIS
3311};
3312
b9d6bf13
JH
3313/* Note that calls to PerlIO_exportFILE() are reversed using
3314 * PerlIO_releaseFILE(), not importFILE. */
9e353e3b 3315FILE *
81428673 3316PerlIO_exportFILE(PerlIO * f, const char *mode)
9e353e3b 3317{
e87a358a 3318 dTHX;
81428673
NIS
3319 FILE *stdio = NULL;
3320 if (PerlIOValid(f)) {
3321 char buf[8];
3322 PerlIO_flush(f);
3323 if (!mode || !*mode) {
3324 mode = PerlIO_modestr(f, buf);
3325 }
3326 stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode);
3327 if (stdio) {
3328 PerlIOl *l = *f;
9f75cc58 3329 PerlIO *f2;
81428673
NIS
3330 /* De-link any lower layers so new :stdio sticks */
3331 *f = NULL;
9f75cc58
RGS
3332 if ((f2 = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) {
3333 PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio);
81428673
NIS
3334 s->stdio = stdio;
3335 /* Link previous lower layers under new one */
3336 *PerlIONext(f) = l;
3337 }
3338 else {
3339 /* restore layers list */
3340 *f = l;
3341 }
a33cf58c 3342 }
14a5cf38
JH
3343 }
3344 return stdio;
9e353e3b
NIS
3345}
3346
81428673 3347
9e353e3b
NIS
3348FILE *
3349PerlIO_findFILE(PerlIO *f)
3350{
14a5cf38
JH
3351 PerlIOl *l = *f;
3352 while (l) {
3353 if (l->tab == &PerlIO_stdio) {
3354 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3355 return s->stdio;
3356 }
3357 l = *PerlIONext(&l);
f7e7eb72 3358 }
4b069b44
NIS
3359 /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */
3360 return PerlIO_exportFILE(f, Nullch);
9e353e3b
NIS
3361}
3362
b9d6bf13 3363/* Use this to reverse PerlIO_exportFILE calls. */
9e353e3b
NIS
3364void
3365PerlIO_releaseFILE(PerlIO *p, FILE *f)
3366{
22569500
NIS
3367 PerlIOl *l;
3368 while ((l = *p)) {
3369 if (l->tab == &PerlIO_stdio) {
3370 PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio);
3371 if (s->stdio == f) {
3372 dTHX;
3373 PerlIO_pop(aTHX_ p);
3374 return;
3375 }
3376 }
3377 p = PerlIONext(p);
3378 }
3379 return;
9e353e3b
NIS
3380}
3381
3382/*--------------------------------------------------------------------------------------*/
14a5cf38 3383/*
71200d45 3384 * perlio buffer layer
14a5cf38 3385 */
9e353e3b 3386
5e2ab84b 3387IV
2dc2558e 3388PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
5e2ab84b 3389{
14a5cf38
JH
3390 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3391 int fd = PerlIO_fileno(f);
14a5cf38
JH
3392 if (fd >= 0 && PerlLIO_isatty(fd)) {
3393 PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY;
3394 }
4b069b44
NIS
3395 if (*PerlIONext(f)) {
3396 Off_t posn = PerlIO_tell(PerlIONext(f));
3397 if (posn != (Off_t) - 1) {
3398 b->posn = posn;
3399 }
14a5cf38 3400 }
2dc2558e 3401 return PerlIOBase_pushed(aTHX_ f, mode, arg, tab);
5e2ab84b
NIS
3402}
3403
9e353e3b 3404PerlIO *
14a5cf38
JH
3405PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
3406 IV n, const char *mode, int fd, int imode, int perm,
3407 PerlIO *f, int narg, SV **args)
3408{
04892f78 3409 if (PerlIOValid(f)) {
14a5cf38 3410 PerlIO *next = PerlIONext(f);
67363c0d
JH
3411 PerlIO_funcs *tab =
3412 PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab);
3413 if (tab && tab->Open)
3414 next =
3415 (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3416 next, narg, args);
2dc2558e 3417 if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) {
14a5cf38
JH
3418 return NULL;
3419 }
3420 }
3421 else {
04892f78 3422 PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
14a5cf38
JH
3423 int init = 0;
3424 if (*mode == 'I') {
3425 init = 1;
3426 /*
71200d45 3427 * mode++;
14a5cf38
JH
3428 */
3429 }
67363c0d
JH
3430 if (tab && tab->Open)
3431 f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm,
3432 f, narg, args);
3433 else
3434 SETERRNO(EINVAL, LIB_INVARG);
14a5cf38 3435 if (f) {
22569500 3436 if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) {
b26b1ab5
NC
3437 /*
3438 * if push fails during open, open fails. close will pop us.
3439 */
3440 PerlIO_close (f);
3441 return NULL;
3442 } else {
3443 fd = PerlIO_fileno(f);
b26b1ab5
NC
3444 if (init && fd == 2) {
3445 /*
3446 * Initial stderr is unbuffered
3447 */
3448 PerlIOBase(f)->flags |= PERLIO_F_UNBUF;
3449 }
23b84778
IZ
3450#ifdef PERLIO_USING_CRLF
3451# ifdef PERLIO_IS_BINMODE_FD
3452 if (PERLIO_IS_BINMODE_FD(fd))
5c728af0 3453 PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch);
23b84778
IZ
3454 else
3455# endif
3456 /*
3457 * do something about failing setmode()? --jhi
3458 */
3459 PerlLIO_setmode(fd, O_BINARY);
3460#endif
14a5cf38
JH
3461 }
3462 }
ee518936 3463 }
14a5cf38 3464 return f;
9e353e3b
NIS
3465}
3466
14a5cf38
JH
3467/*
3468 * This "flush" is akin to sfio's sync in that it handles files in either
71200d45 3469 * read or write state
14a5cf38 3470 */
9e353e3b 3471IV
f62ce20a 3472PerlIOBuf_flush(pTHX_ PerlIO *f)
6f9d8c32 3473{
14a5cf38
JH
3474 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3475 int code = 0;
04892f78 3476 PerlIO *n = PerlIONext(f);
14a5cf38
JH
3477 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) {
3478 /*
71200d45 3479 * write() the buffer
14a5cf38
JH
3480 */
3481 STDCHAR *buf = b->buf;
3482 STDCHAR *p = buf;
14a5cf38
JH
3483 while (p < b->ptr) {
3484 SSize_t count = PerlIO_write(n, p, b->ptr - p);
3485 if (count > 0) {
3486 p += count;
3487 }
3488 else if (count < 0 || PerlIO_error(n)) {
3489 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3490 code = -1;
3491 break;
3492 }
3493 }
3494 b->posn += (p - buf);
3495 }
3496 else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3497 STDCHAR *buf = PerlIO_get_base(f);
3498 /*
71200d45 3499 * Note position change
14a5cf38
JH
3500 */
3501 b->posn += (b->ptr - buf);
3502 if (b->ptr < b->end) {
4b069b44
NIS
3503 /* We did not consume all of it - try and seek downstream to
3504 our logical position
14a5cf38 3505 */
4b069b44 3506 if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) {
04892f78
NIS
3507 /* Reload n as some layers may pop themselves on seek */
3508 b->posn = PerlIO_tell(n = PerlIONext(f));
14a5cf38 3509 }
ba5c3fe9 3510 else {
4b069b44
NIS
3511 /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read
3512 data is lost for good - so return saying "ok" having undone
3513 the position adjust
3514 */
3515 b->posn -= (b->ptr - buf);
ba5c3fe9
NIS
3516 return code;
3517 }
14a5cf38
JH
3518 }
3519 }
3520 b->ptr = b->end = b->buf;
3521 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
04892f78 3522 /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */
04892f78 3523 if (PerlIOValid(n) && PerlIO_flush(n) != 0)
14a5cf38
JH
3524 code = -1;
3525 return code;
6f9d8c32
NIS
3526}
3527
06da4f11 3528IV
f62ce20a 3529PerlIOBuf_fill(pTHX_ PerlIO *f)
06da4f11 3530{
14a5cf38
JH
3531 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3532 PerlIO *n = PerlIONext(f);
3533 SSize_t avail;
3534 /*
4b069b44
NIS
3535 * Down-stream flush is defined not to loose read data so is harmless.
3536 * we would not normally be fill'ing if there was data left in anycase.
14a5cf38
JH
3537 */
3538 if (PerlIO_flush(f) != 0)
3539 return -1;
3540 if (PerlIOBase(f)->flags & PERLIO_F_TTY)
f62ce20a 3541 PerlIOBase_flush_linebuf(aTHX);
14a5cf38
JH
3542
3543 if (!b->buf)
22569500 3544 PerlIO_get_base(f); /* allocate via vtable */
14a5cf38
JH
3545
3546 b->ptr = b->end = b->buf;
4b069b44
NIS
3547
3548 if (!PerlIOValid(n)) {
3549 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3550 return -1;
3551 }
3552
14a5cf38
JH
3553 if (PerlIO_fast_gets(n)) {
3554 /*
04892f78 3555 * Layer below is also buffered. We do _NOT_ want to call its
14a5cf38
JH
3556 * ->Read() because that will loop till it gets what we asked for
3557 * which may hang on a pipe etc. Instead take anything it has to
71200d45 3558 * hand, or ask it to fill _once_.
14a5cf38
JH
3559 */
3560 avail = PerlIO_get_cnt(n);
3561 if (avail <= 0) {
3562 avail = PerlIO_fill(n);
3563 if (avail == 0)
3564 avail = PerlIO_get_cnt(n);
3565 else {
3566 if (!PerlIO_error(n) && PerlIO_eof(n))
3567 avail = 0;
3568 }
3569 }
3570 if (avail > 0) {
3571 STDCHAR *ptr = PerlIO_get_ptr(n);
3572 SSize_t cnt = avail;
eb160463 3573 if (avail > (SSize_t)b->bufsiz)
14a5cf38
JH
3574 avail = b->bufsiz;
3575 Copy(ptr, b->buf, avail, STDCHAR);
3576 PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail);
3577 }
3578 }
3579 else {
3580 avail = PerlIO_read(n, b->ptr, b->bufsiz);
3581 }
3582 if (avail <= 0) {
3583 if (avail == 0)
3584 PerlIOBase(f)->flags |= PERLIO_F_EOF;
3585 else
3586 PerlIOBase(f)->flags |= PERLIO_F_ERROR;
3587 return -1;
3588 }
3589 b->end = b->buf + avail;
3590 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3591 return 0;
06da4f11
NIS
3592}
3593
6f9d8c32 3594SSize_t
f62ce20a 3595PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
6f9d8c32 3596{
14a5cf38 3597 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
04892f78 3598 if (PerlIOValid(f)) {
14a5cf38
JH
3599 if (!b->ptr)
3600 PerlIO_get_base(f);
f62ce20a 3601 return PerlIOBase_read(aTHX_ f, vbuf, count);
14a5cf38
JH
3602 }
3603 return 0;
6f9d8c32
NIS
3604}
3605
9e353e3b 3606SSize_t
f62ce20a 3607PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3608{
14a5cf38
JH
3609 const STDCHAR *buf = (const STDCHAR *) vbuf + count;
3610 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3611 SSize_t unread = 0;
3612 SSize_t avail;
3613 if (PerlIOBase(f)->flags & PERLIO_F_WRBUF)
3614 PerlIO_flush(f);
3615 if (!b->buf)
3616 PerlIO_get_base(f);
3617 if (b->buf) {
3618 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3619 /*
3620 * Buffer is already a read buffer, we can overwrite any chars
71200d45 3621 * which have been read back to buffer start
14a5cf38
JH
3622 */
3623 avail = (b->ptr - b->buf);
3624 }
3625 else {
3626 /*
3627 * Buffer is idle, set it up so whole buffer is available for
71200d45 3628 * unread
14a5cf38
JH
3629 */
3630 avail = b->bufsiz;
3631 b->end = b->buf + avail;
3632 b->ptr = b->end;
3633 PerlIOBase(f)->flags |= PERLIO_F_RDBUF;
3634 /*
71200d45 3635 * Buffer extends _back_ from where we are now
14a5cf38
JH
3636 */
3637 b->posn -= b->bufsiz;
3638 }
3639 if (avail > (SSize_t) count) {
3640 /*
71200d45 3641 * If we have space for more than count, just move count
14a5cf38
JH
3642 */
3643 avail = count;
3644 }
3645 if (avail > 0) {
3646 b->ptr -= avail;
3647 buf -= avail;
3648 /*
3649 * In simple stdio-like ungetc() case chars will be already
71200d45 3650 * there
14a5cf38
JH
3651 */
3652 if (buf != b->ptr) {
3653 Copy(buf, b->ptr, avail, STDCHAR);
3654 }
3655 count -= avail;
3656 unread += avail;
3657 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3658 }
3659 }
93679785
NIS
3660 if (count > 0) {
3661 unread += PerlIOBase_unread(aTHX_ f, vbuf, count);
3662 }
14a5cf38 3663 return unread;
760ac839
LW
3664}
3665
9e353e3b 3666SSize_t
f62ce20a 3667PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
760ac839 3668{
14a5cf38
JH
3669 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3670 const STDCHAR *buf = (const STDCHAR *) vbuf;
3671 Size_t written = 0;
3672 if (!b->buf)
3673 PerlIO_get_base(f);
3674 if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE))
3675 return 0;
0678cb22
NIS
3676 if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) {
3677 if (PerlIO_flush(f) != 0) {
3678 return 0;
3679 }
3680 }
14a5cf38
JH
3681 while (count > 0) {
3682 SSize_t avail = b->bufsiz - (b->ptr - b->buf);
3683 if ((SSize_t) count < avail)
3684 avail = count;
3685 PerlIOBase(f)->flags |= PERLIO_F_WRBUF;
3686 if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) {
3687 while (avail > 0) {
3688 int ch = *buf++;
3689 *(b->ptr)++ = ch;
3690 count--;
3691 avail--;
3692 written++;
3693 if (ch == '\n') {
3694 PerlIO_flush(f);
3695 break;
3696 }
3697 }
3698 }
3699 else {
3700 if (avail) {
3701 Copy(buf, b->ptr, avail, STDCHAR);
3702 count -= avail;
3703 buf += avail;
3704 written += avail;
3705 b->ptr += avail;
3706 }
3707 }
3708 if (b->ptr >= (b->buf + b->bufsiz))
3709 PerlIO_flush(f);
3710 }
3711 if (PerlIOBase(f)->flags & PERLIO_F_UNBUF)
3712 PerlIO_flush(f);
3713 return written;
9e353e3b
NIS
3714}
3715
94a175e1 3716IV
f62ce20a 3717PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
9e353e3b 3718{
14a5cf38
JH
3719 IV code;
3720 if ((code = PerlIO_flush(f)) == 0) {
3721 PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf);
3722 PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
3723 code = PerlIO_seek(PerlIONext(f), offset, whence);
3724 if (code == 0) {
3725 b->posn = PerlIO_tell(PerlIONext(f));
3726 }
9e353e3b 3727 }
14a5cf38 3728 return code;
9e353e3b
NIS
3729}
3730
3731Off_t