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