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