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