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