Commit | Line | Data |
---|---|---|
14a5cf38 | 1 | /* |
be3c0a43 | 2 | * perlio.c Copyright (c) 1996-2002, Nick Ing-Simmons You may distribute |
14a5cf38 | 3 | * under the terms of either the GNU General Public License or the |
71200d45 | 4 | * Artistic License, as specified in the README file. |
760ac839 LW |
5 | */ |
6 | ||
14a5cf38 | 7 | /* |
d31a8517 AT |
8 | * Hour after hour for nearly three weary days he had jogged up and down, |
9 | * over passes, and through long dales, and across many streams. | |
10 | */ | |
11 | ||
12 | /* | |
71200d45 | 13 | * If we have ActivePerl-like PERL_IMPLICIT_SYS then we need a dTHX to get |
14a5cf38 | 14 | * at the dispatch tables, even when we do not need it for other reasons. |
71200d45 | 15 | * Invent a dSYS macro to abstract this out |
14a5cf38 | 16 | */ |
7bcba3d4 NIS |
17 | #ifdef PERL_IMPLICIT_SYS |
18 | #define dSYS dTHX | |
19 | #else | |
20 | #define dSYS dNOOP | |
21 | #endif | |
22 | ||
760ac839 | 23 | #define VOIDUSED 1 |
12ae5dfc JH |
24 | #ifdef PERL_MICRO |
25 | # include "uconfig.h" | |
26 | #else | |
27 | # include "config.h" | |
28 | #endif | |
760ac839 | 29 | |
6f9d8c32 | 30 | #define PERLIO_NOT_STDIO 0 |
760ac839 | 31 | #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO) |
14a5cf38 | 32 | /* |
71200d45 | 33 | * #define PerlIO FILE |
14a5cf38 | 34 | */ |
760ac839 LW |
35 | #endif |
36 | /* | |
6f9d8c32 | 37 | * This file provides those parts of PerlIO abstraction |
88b61e10 | 38 | * which are not #defined in perlio.h. |
6f9d8c32 | 39 | * Which these are depends on various Configure #ifdef's |
760ac839 LW |
40 | */ |
41 | ||
42 | #include "EXTERN.h" | |
864dbfa3 | 43 | #define PERL_IN_PERLIO_C |
760ac839 LW |
44 | #include "perl.h" |
45 | ||
32af7c23 CL |
46 | #ifdef PERL_IMPLICIT_CONTEXT |
47 | #undef dSYS | |
48 | #define dSYS dTHX | |
49 | #endif | |
50 | ||
0c4f7ff0 NIS |
51 | #include "XSUB.h" |
52 | ||
1b7a0411 | 53 | /* Call the callback or PerlIOBase, and return failure. */ |
b32dd47e | 54 | #define Perl_PerlIO_or_Base(f, callback, base, failure, args) \ |
1b7a0411 JH |
55 | if (PerlIOValid(f)) { \ |
56 | PerlIO_funcs *tab = PerlIOBase(f)->tab; \ | |
57 | if (tab && tab->callback) \ | |
58 | return (*tab->callback) args; \ | |
59 | else \ | |
60 | return PerlIOBase_ ## base args; \ | |
61 | } \ | |
62 | else \ | |
63 | SETERRNO(EBADF, SS_IVCHAN); \ | |
64 | return failure | |
65 | ||
66 | /* Call the callback or fail, and return failure. */ | |
b32dd47e | 67 | #define Perl_PerlIO_or_fail(f, callback, failure, args) \ |
1b7a0411 JH |
68 | if (PerlIOValid(f)) { \ |
69 | PerlIO_funcs *tab = PerlIOBase(f)->tab; \ | |
70 | if (tab && tab->callback) \ | |
71 | return (*tab->callback) args; \ | |
72 | SETERRNO(EINVAL, LIB_INVARG); \ | |
73 | } \ | |
74 | else \ | |
75 | SETERRNO(EBADF, SS_IVCHAN); \ | |
76 | return failure | |
77 | ||
78 | /* Call the callback or PerlIOBase, and be void. */ | |
b32dd47e | 79 | #define Perl_PerlIO_or_Base_void(f, callback, base, args) \ |
1b7a0411 JH |
80 | if (PerlIOValid(f)) { \ |
81 | PerlIO_funcs *tab = PerlIOBase(f)->tab; \ | |
82 | if (tab && tab->callback) \ | |
83 | (*tab->callback) args; \ | |
84 | else \ | |
85 | PerlIOBase_ ## base args; \ | |
1b7a0411 JH |
86 | } \ |
87 | else \ | |
88 | SETERRNO(EBADF, SS_IVCHAN) | |
89 | ||
90 | /* Call the callback or fail, and be void. */ | |
b32dd47e | 91 | #define Perl_PerlIO_or_fail_void(f, callback, args) \ |
1b7a0411 JH |
92 | if (PerlIOValid(f)) { \ |
93 | PerlIO_funcs *tab = PerlIOBase(f)->tab; \ | |
94 | if (tab && tab->callback) \ | |
95 | (*tab->callback) args; \ | |
37725cdc NIS |
96 | else \ |
97 | SETERRNO(EINVAL, LIB_INVARG); \ | |
1b7a0411 JH |
98 | } \ |
99 | else \ | |
100 | SETERRNO(EBADF, SS_IVCHAN) | |
101 | ||
1f376c7c JH |
102 | #define Perl_PerlIO_fail_if_hasnot(f, able, ueno, veno, ret) \ |
103 | if (PerlIOValid(f) && (PerlIOBase(f)->flags & (able)) == 0) { \ | |
104 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; \ | |
105 | SETERRNO(ueno, veno); \ | |
106 | return ret; \ | |
107 | } | |
108 | ||
109 | #define Perl_PerlIO_fail_if_has(f, able, ueno, veno, ret) \ | |
110 | if (PerlIOValid(f) && (PerlIOBase(f)->flags & (able)) == able) { \ | |
111 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; \ | |
112 | SETERRNO(ueno, veno); \ | |
113 | return ret; \ | |
114 | } | |
115 | ||
60382766 | 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 | } | |
170 | ||
06c7082d | 171 | #ifndef O_ACCMODE |
22569500 | 172 | #define O_ACCMODE 3 /* Assume traditional implementation */ |
06c7082d NIS |
173 | #endif |
174 | ||
175 | int | |
176 | PerlIO_intmode2str(int rawmode, char *mode, int *writing) | |
177 | { | |
178 | int result = rawmode & O_ACCMODE; | |
179 | int ix = 0; | |
180 | int ptype; | |
181 | switch (result) { | |
182 | case O_RDONLY: | |
183 | ptype = IoTYPE_RDONLY; | |
184 | break; | |
185 | case O_WRONLY: | |
186 | ptype = IoTYPE_WRONLY; | |
187 | break; | |
188 | case O_RDWR: | |
189 | default: | |
190 | ptype = IoTYPE_RDWR; | |
191 | break; | |
192 | } | |
193 | if (writing) | |
194 | *writing = (result != O_RDONLY); | |
195 | ||
196 | if (result == O_RDONLY) { | |
197 | mode[ix++] = 'r'; | |
198 | } | |
199 | #ifdef O_APPEND | |
200 | else if (rawmode & O_APPEND) { | |
201 | mode[ix++] = 'a'; | |
202 | if (result != O_WRONLY) | |
203 | mode[ix++] = '+'; | |
204 | } | |
205 | #endif | |
206 | else { | |
207 | if (result == O_WRONLY) | |
208 | mode[ix++] = 'w'; | |
209 | else { | |
210 | mode[ix++] = 'r'; | |
211 | mode[ix++] = '+'; | |
212 | } | |
213 | } | |
214 | if (rawmode & O_BINARY) | |
215 | mode[ix++] = 'b'; | |
216 | mode[ix] = '\0'; | |
217 | return ptype; | |
218 | } | |
219 | ||
eb73beca NIS |
220 | #ifndef PERLIO_LAYERS |
221 | int | |
222 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) | |
223 | { | |
6874a2de NIS |
224 | if (!names || !*names |
225 | || strEQ(names, ":crlf") | |
226 | || strEQ(names, ":raw") | |
227 | || strEQ(names, ":bytes") | |
228 | ) { | |
14a5cf38 JH |
229 | return 0; |
230 | } | |
231 | Perl_croak(aTHX_ "Cannot apply \"%s\" in non-PerlIO perl", names); | |
232 | /* | |
71200d45 | 233 | * NOTREACHED |
14a5cf38 JH |
234 | */ |
235 | return -1; | |
eb73beca NIS |
236 | } |
237 | ||
13621cfb NIS |
238 | void |
239 | PerlIO_destruct(pTHX) | |
240 | { | |
241 | } | |
242 | ||
f5b9d040 NIS |
243 | int |
244 | PerlIO_binmode(pTHX_ PerlIO *fp, int iotype, int mode, const char *names) | |
245 | { | |
92bff44d | 246 | #ifdef USE_SFIO |
14a5cf38 | 247 | return 1; |
92bff44d | 248 | #else |
14a5cf38 | 249 | return perlsio_binmode(fp, iotype, mode); |
92bff44d | 250 | #endif |
f5b9d040 | 251 | } |
60382766 | 252 | |
e0fa5af2 | 253 | PerlIO * |
ecdeb87c | 254 | PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
e0fa5af2 | 255 | { |
0553478e NIS |
256 | #ifdef PERL_MICRO |
257 | return NULL; | |
258 | #else | |
259 | #ifdef PERL_IMPLICIT_SYS | |
22569500 | 260 | return PerlSIO_fdupopen(f); |
0553478e | 261 | #else |
30753f56 NIS |
262 | #ifdef WIN32 |
263 | return win32_fdupopen(f); | |
264 | #else | |
e0fa5af2 NIS |
265 | if (f) { |
266 | int fd = PerlLIO_dup(PerlIO_fileno(f)); | |
267 | if (fd >= 0) { | |
06c7082d NIS |
268 | char mode[8]; |
269 | int omode = fcntl(fd, F_GETFL); | |
a5936e02 JH |
270 | #ifdef DJGPP |
271 | omode = djgpp_get_stream_mode(f); | |
272 | #endif | |
06c7082d | 273 | PerlIO_intmode2str(omode,mode,NULL); |
e0fa5af2 | 274 | /* the r+ is a hack */ |
06c7082d | 275 | return PerlIO_fdopen(fd, mode); |
e0fa5af2 NIS |
276 | } |
277 | return NULL; | |
278 | } | |
279 | else { | |
93189314 | 280 | SETERRNO(EBADF, SS_IVCHAN); |
e0fa5af2 | 281 | } |
7114a2d2 | 282 | #endif |
e0fa5af2 | 283 | return NULL; |
0553478e | 284 | #endif |
30753f56 | 285 | #endif |
e0fa5af2 NIS |
286 | } |
287 | ||
288 | ||
14a5cf38 | 289 | /* |
71200d45 | 290 | * De-mux PerlIO_openn() into fdopen, freopen and fopen type entries |
14a5cf38 | 291 | */ |
ee518936 NIS |
292 | |
293 | PerlIO * | |
14a5cf38 JH |
294 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
295 | int imode, int perm, PerlIO *old, int narg, SV **args) | |
296 | { | |
7cf31beb NIS |
297 | if (narg) { |
298 | if (narg > 1) { | |
3b8752bb | 299 | Perl_croak(aTHX_ "More than one argument to open"); |
7cf31beb | 300 | } |
14a5cf38 JH |
301 | if (*args == &PL_sv_undef) |
302 | return PerlIO_tmpfile(); | |
303 | else { | |
304 | char *name = SvPV_nolen(*args); | |
305 | if (*mode == '#') { | |
306 | fd = PerlLIO_open3(name, imode, perm); | |
307 | if (fd >= 0) | |
308 | return PerlIO_fdopen(fd, (char *) mode + 1); | |
309 | } | |
310 | else if (old) { | |
311 | return PerlIO_reopen(name, mode, old); | |
312 | } | |
313 | else { | |
314 | return PerlIO_open(name, mode); | |
315 | } | |
316 | } | |
317 | } | |
318 | else { | |
319 | return PerlIO_fdopen(fd, (char *) mode); | |
320 | } | |
321 | return NULL; | |
ee518936 NIS |
322 | } |
323 | ||
0c4f7ff0 NIS |
324 | XS(XS_PerlIO__Layer__find) |
325 | { | |
14a5cf38 JH |
326 | dXSARGS; |
327 | if (items < 2) | |
328 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
329 | else { | |
330 | char *name = SvPV_nolen(ST(1)); | |
331 | ST(0) = (strEQ(name, "crlf") | |
332 | || strEQ(name, "raw")) ? &PL_sv_yes : &PL_sv_undef; | |
333 | XSRETURN(1); | |
334 | } | |
0c4f7ff0 NIS |
335 | } |
336 | ||
337 | ||
338 | void | |
339 | Perl_boot_core_PerlIO(pTHX) | |
340 | { | |
14a5cf38 | 341 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
0c4f7ff0 NIS |
342 | } |
343 | ||
ac27b0f5 NIS |
344 | #endif |
345 | ||
32e30700 | 346 | |
6f9d8c32 | 347 | #ifdef PERLIO_IS_STDIO |
760ac839 LW |
348 | |
349 | void | |
e8632036 | 350 | PerlIO_init(pTHX) |
760ac839 | 351 | { |
14a5cf38 JH |
352 | /* |
353 | * Does nothing (yet) except force this file to be included in perl | |
71200d45 | 354 | * binary. That allows this file to force inclusion of other functions |
14a5cf38 | 355 | * that may be required by loadable extensions e.g. for |
71200d45 | 356 | * FileHandle::tmpfile |
14a5cf38 | 357 | */ |
760ac839 LW |
358 | } |
359 | ||
33dcbb9a | 360 | #undef PerlIO_tmpfile |
361 | PerlIO * | |
8ac85365 | 362 | PerlIO_tmpfile(void) |
33dcbb9a | 363 | { |
14a5cf38 | 364 | return tmpfile(); |
33dcbb9a | 365 | } |
366 | ||
22569500 | 367 | #else /* PERLIO_IS_STDIO */ |
760ac839 LW |
368 | |
369 | #ifdef USE_SFIO | |
370 | ||
371 | #undef HAS_FSETPOS | |
372 | #undef HAS_FGETPOS | |
373 | ||
14a5cf38 JH |
374 | /* |
375 | * This section is just to make sure these functions get pulled in from | |
71200d45 | 376 | * libsfio.a |
14a5cf38 | 377 | */ |
760ac839 LW |
378 | |
379 | #undef PerlIO_tmpfile | |
380 | PerlIO * | |
c78749f2 | 381 | PerlIO_tmpfile(void) |
760ac839 | 382 | { |
14a5cf38 | 383 | return sftmp(0); |
760ac839 LW |
384 | } |
385 | ||
386 | void | |
e8632036 | 387 | PerlIO_init(pTHX) |
760ac839 | 388 | { |
14a5cf38 JH |
389 | /* |
390 | * Force this file to be included in perl binary. Which allows this | |
391 | * file to force inclusion of other functions that may be required by | |
71200d45 | 392 | * loadable extensions e.g. for FileHandle::tmpfile |
14a5cf38 | 393 | */ |
760ac839 | 394 | |
14a5cf38 | 395 | /* |
71200d45 | 396 | * Hack sfio does its own 'autoflush' on stdout in common cases. Flush |
14a5cf38 | 397 | * results in a lot of lseek()s to regular files and lot of small |
71200d45 | 398 | * writes to pipes. |
14a5cf38 JH |
399 | */ |
400 | sfset(sfstdout, SF_SHARE, 0); | |
760ac839 LW |
401 | } |
402 | ||
b9d6bf13 | 403 | /* This is not the reverse of PerlIO_exportFILE(), PerlIO_releaseFILE() is. */ |
92bff44d | 404 | PerlIO * |
4b069b44 | 405 | PerlIO_importFILE(FILE *stdio, const char *mode) |
92bff44d | 406 | { |
14a5cf38 | 407 | int fd = fileno(stdio); |
4b069b44 | 408 | if (!mode || !*mode) { |
81428673 | 409 | mode = "r+"; |
4b069b44 NIS |
410 | } |
411 | return PerlIO_fdopen(fd, mode); | |
92bff44d NIS |
412 | } |
413 | ||
414 | FILE * | |
415 | PerlIO_findFILE(PerlIO *pio) | |
416 | { | |
14a5cf38 JH |
417 | int fd = PerlIO_fileno(pio); |
418 | FILE *f = fdopen(fd, "r+"); | |
419 | PerlIO_flush(pio); | |
420 | if (!f && errno == EINVAL) | |
421 | f = fdopen(fd, "w"); | |
422 | if (!f && errno == EINVAL) | |
423 | f = fdopen(fd, "r"); | |
424 | return f; | |
92bff44d NIS |
425 | } |
426 | ||
427 | ||
22569500 | 428 | #else /* USE_SFIO */ |
6f9d8c32 | 429 | /*======================================================================================*/ |
14a5cf38 | 430 | /* |
71200d45 | 431 | * Implement all the PerlIO interface ourselves. |
9e353e3b | 432 | */ |
760ac839 | 433 | |
76ced9ad NIS |
434 | #include "perliol.h" |
435 | ||
14a5cf38 JH |
436 | /* |
437 | * We _MUST_ have <unistd.h> if we are using lseek() and may have large | |
71200d45 | 438 | * files |
14a5cf38 | 439 | */ |
02f66e2f NIS |
440 | #ifdef I_UNISTD |
441 | #include <unistd.h> | |
442 | #endif | |
06da4f11 NIS |
443 | #ifdef HAS_MMAP |
444 | #include <sys/mman.h> | |
445 | #endif | |
446 | ||
04fa0c70 RB |
447 | /* |
448 | * Why is this here - not in perlio.h? RMB | |
8a521f28 | 449 | */ |
14a5cf38 | 450 | void PerlIO_debug(const char *fmt, ...) |
287c8091 | 451 | __attribute__format__(__printf__, 1, 2); |
6f9d8c32 | 452 | |
6f9d8c32 | 453 | void |
14a5cf38 JH |
454 | PerlIO_debug(const char *fmt, ...) |
455 | { | |
456 | static int dbg = 0; | |
457 | va_list ap; | |
458 | dSYS; | |
459 | va_start(ap, fmt); | |
460 | if (!dbg) { | |
461 | char *s = PerlEnv_getenv("PERLIO_DEBUG"); | |
462 | if (s && *s) | |
463 | dbg = PerlLIO_open3(s, O_WRONLY | O_CREAT | O_APPEND, 0666); | |
464 | else | |
465 | dbg = -1; | |
466 | } | |
467 | if (dbg > 0) { | |
468 | dTHX; | |
70ace5da NIS |
469 | #ifdef USE_ITHREADS |
470 | /* Use fixed buffer as sv_catpvf etc. needs SVs */ | |
471 | char buffer[1024]; | |
472 | char *s; | |
473 | STRLEN len; | |
474 | s = CopFILE(PL_curcop); | |
475 | if (!s) | |
476 | s = "(none)"; | |
477 | sprintf(buffer, "%s:%" IVdf " ", s, (IV) CopLINE(PL_curcop)); | |
22569500 | 478 | len = strlen(buffer); |
70ace5da NIS |
479 | vsprintf(buffer+len, fmt, ap); |
480 | PerlLIO_write(dbg, buffer, strlen(buffer)); | |
481 | #else | |
14a5cf38 JH |
482 | SV *sv = newSVpvn("", 0); |
483 | char *s; | |
484 | STRLEN len; | |
485 | s = CopFILE(PL_curcop); | |
486 | if (!s) | |
487 | s = "(none)"; | |
488 | Perl_sv_catpvf(aTHX_ sv, "%s:%" IVdf " ", s, | |
489 | (IV) CopLINE(PL_curcop)); | |
490 | Perl_sv_vcatpvf(aTHX_ sv, fmt, &ap); | |
491 | ||
492 | s = SvPV(sv, len); | |
493 | PerlLIO_write(dbg, s, len); | |
494 | SvREFCNT_dec(sv); | |
70ace5da | 495 | #endif |
14a5cf38 JH |
496 | } |
497 | va_end(ap); | |
6f9d8c32 NIS |
498 | } |
499 | ||
9e353e3b NIS |
500 | /*--------------------------------------------------------------------------------------*/ |
501 | ||
14a5cf38 | 502 | /* |
71200d45 | 503 | * Inner level routines |
14a5cf38 | 504 | */ |
9e353e3b | 505 | |
14a5cf38 | 506 | /* |
71200d45 | 507 | * Table of pointers to the PerlIO structs (malloc'ed) |
14a5cf38 | 508 | */ |
05d1247b | 509 | #define PERLIO_TABLE_SIZE 64 |
6f9d8c32 | 510 | |
760ac839 | 511 | PerlIO * |
5f1a76d0 | 512 | PerlIO_allocate(pTHX) |
6f9d8c32 | 513 | { |
14a5cf38 | 514 | /* |
71200d45 | 515 | * Find a free slot in the table, allocating new table as necessary |
14a5cf38 JH |
516 | */ |
517 | PerlIO **last; | |
518 | PerlIO *f; | |
a1ea730d | 519 | last = &PL_perlio; |
14a5cf38 JH |
520 | while ((f = *last)) { |
521 | int i; | |
522 | last = (PerlIO **) (f); | |
523 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
524 | if (!*++f) { | |
525 | return f; | |
526 | } | |
527 | } | |
528 | } | |
3a1ee7e8 | 529 | Newz('I',f,PERLIO_TABLE_SIZE,PerlIO); |
14a5cf38 JH |
530 | if (!f) { |
531 | return NULL; | |
532 | } | |
533 | *last = f; | |
534 | return f + 1; | |
05d1247b NIS |
535 | } |
536 | ||
a1ea730d NIS |
537 | #undef PerlIO_fdupopen |
538 | PerlIO * | |
ecdeb87c | 539 | PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
a1ea730d | 540 | { |
04892f78 | 541 | if (PerlIOValid(f)) { |
a1ea730d | 542 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
fe5a182c | 543 | PerlIO_debug("fdupopen f=%p param=%p\n",(void*)f,(void*)param); |
210e727c JH |
544 | if (tab && tab->Dup) |
545 | return (*tab->Dup)(aTHX_ PerlIO_allocate(aTHX), f, param, flags); | |
37725cdc NIS |
546 | else { |
547 | return PerlIOBase_dup(aTHX_ PerlIO_allocate(aTHX), f, param, flags); | |
548 | } | |
a1ea730d | 549 | } |
210e727c JH |
550 | else |
551 | SETERRNO(EBADF, SS_IVCHAN); | |
552 | ||
553 | return NULL; | |
a1ea730d NIS |
554 | } |
555 | ||
556 | void | |
5f1a76d0 | 557 | PerlIO_cleantable(pTHX_ PerlIO **tablep) |
05d1247b | 558 | { |
14a5cf38 JH |
559 | PerlIO *table = *tablep; |
560 | if (table) { | |
561 | int i; | |
562 | PerlIO_cleantable(aTHX_(PerlIO **) & (table[0])); | |
563 | for (i = PERLIO_TABLE_SIZE - 1; i > 0; i--) { | |
564 | PerlIO *f = table + i; | |
565 | if (*f) { | |
566 | PerlIO_close(f); | |
567 | } | |
568 | } | |
3a1ee7e8 | 569 | Safefree(table); |
14a5cf38 | 570 | *tablep = NULL; |
05d1247b | 571 | } |
05d1247b NIS |
572 | } |
573 | ||
fcf2db38 NIS |
574 | |
575 | PerlIO_list_t * | |
3a1ee7e8 | 576 | PerlIO_list_alloc(pTHX) |
fcf2db38 | 577 | { |
14a5cf38 JH |
578 | PerlIO_list_t *list; |
579 | Newz('L', list, 1, PerlIO_list_t); | |
580 | list->refcnt = 1; | |
581 | return list; | |
fcf2db38 NIS |
582 | } |
583 | ||
584 | void | |
3a1ee7e8 | 585 | PerlIO_list_free(pTHX_ PerlIO_list_t *list) |
fcf2db38 | 586 | { |
14a5cf38 JH |
587 | if (list) { |
588 | if (--list->refcnt == 0) { | |
589 | if (list->array) { | |
14a5cf38 JH |
590 | IV i; |
591 | for (i = 0; i < list->cur; i++) { | |
592 | if (list->array[i].arg) | |
593 | SvREFCNT_dec(list->array[i].arg); | |
594 | } | |
595 | Safefree(list->array); | |
596 | } | |
597 | Safefree(list); | |
598 | } | |
599 | } | |
fcf2db38 NIS |
600 | } |
601 | ||
602 | void | |
3a1ee7e8 | 603 | PerlIO_list_push(pTHX_ PerlIO_list_t *list, PerlIO_funcs *funcs, SV *arg) |
14a5cf38 | 604 | { |
14a5cf38 JH |
605 | PerlIO_pair_t *p; |
606 | if (list->cur >= list->len) { | |
607 | list->len += 8; | |
608 | if (list->array) | |
609 | Renew(list->array, list->len, PerlIO_pair_t); | |
610 | else | |
611 | New('l', list->array, list->len, PerlIO_pair_t); | |
612 | } | |
613 | p = &(list->array[list->cur++]); | |
614 | p->funcs = funcs; | |
615 | if ((p->arg = arg)) { | |
616 | SvREFCNT_inc(arg); | |
617 | } | |
fcf2db38 NIS |
618 | } |
619 | ||
3a1ee7e8 NIS |
620 | PerlIO_list_t * |
621 | PerlIO_clone_list(pTHX_ PerlIO_list_t *proto, CLONE_PARAMS *param) | |
622 | { | |
694c95cf JH |
623 | PerlIO_list_t *list = (PerlIO_list_t *) NULL; |
624 | if (proto) { | |
625 | int i; | |
626 | list = PerlIO_list_alloc(aTHX); | |
627 | for (i=0; i < proto->cur; i++) { | |
628 | SV *arg = Nullsv; | |
629 | if (proto->array[i].arg) | |
630 | arg = PerlIO_sv_dup(aTHX_ proto->array[i].arg,param); | |
631 | PerlIO_list_push(aTHX_ list, proto->array[i].funcs, arg); | |
632 | } | |
3a1ee7e8 NIS |
633 | } |
634 | return list; | |
635 | } | |
4a4a6116 | 636 | |
05d1247b | 637 | void |
3a1ee7e8 | 638 | PerlIO_clone(pTHX_ PerlInterpreter *proto, CLONE_PARAMS *param) |
9a6404c5 | 639 | { |
3aaf42a7 | 640 | #ifdef USE_ITHREADS |
3a1ee7e8 NIS |
641 | PerlIO **table = &proto->Iperlio; |
642 | PerlIO *f; | |
643 | PL_perlio = NULL; | |
644 | PL_known_layers = PerlIO_clone_list(aTHX_ proto->Iknown_layers, param); | |
645 | PL_def_layerlist = PerlIO_clone_list(aTHX_ proto->Idef_layerlist, param); | |
646 | PerlIO_allocate(aTHX); /* root slot is never used */ | |
694c95cf | 647 | PerlIO_debug("Clone %p from %p\n",aTHX,proto); |
3a1ee7e8 NIS |
648 | while ((f = *table)) { |
649 | int i; | |
650 | table = (PerlIO **) (f++); | |
651 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
652 | if (*f) { | |
93a8090d | 653 | (void) fp_dup(f, 0, param); |
3a1ee7e8 NIS |
654 | } |
655 | f++; | |
656 | } | |
657 | } | |
3aaf42a7 | 658 | #endif |
9a6404c5 DM |
659 | } |
660 | ||
661 | void | |
13621cfb NIS |
662 | PerlIO_destruct(pTHX) |
663 | { | |
a1ea730d | 664 | PerlIO **table = &PL_perlio; |
14a5cf38 | 665 | PerlIO *f; |
694c95cf JH |
666 | #ifdef USE_ITHREADS |
667 | PerlIO_debug("Destruct %p\n",aTHX); | |
668 | #endif | |
14a5cf38 JH |
669 | while ((f = *table)) { |
670 | int i; | |
671 | table = (PerlIO **) (f++); | |
672 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
673 | PerlIO *x = f; | |
674 | PerlIOl *l; | |
675 | while ((l = *x)) { | |
676 | if (l->tab->kind & PERLIO_K_DESTRUCT) { | |
677 | PerlIO_debug("Destruct popping %s\n", l->tab->name); | |
678 | PerlIO_flush(x); | |
679 | PerlIO_pop(aTHX_ x); | |
680 | } | |
681 | else { | |
682 | x = PerlIONext(x); | |
683 | } | |
684 | } | |
685 | f++; | |
686 | } | |
687 | } | |
13621cfb NIS |
688 | } |
689 | ||
690 | void | |
a999f61b | 691 | PerlIO_pop(pTHX_ PerlIO *f) |
760ac839 | 692 | { |
14a5cf38 JH |
693 | PerlIOl *l = *f; |
694 | if (l) { | |
fe5a182c | 695 | PerlIO_debug("PerlIO_pop f=%p %s\n", (void*)f, l->tab->name); |
14a5cf38 JH |
696 | if (l->tab->Popped) { |
697 | /* | |
698 | * If popped returns non-zero do not free its layer structure | |
699 | * it has either done so itself, or it is shared and still in | |
71200d45 | 700 | * use |
14a5cf38 | 701 | */ |
f62ce20a | 702 | if ((*l->tab->Popped) (aTHX_ f) != 0) |
14a5cf38 JH |
703 | return; |
704 | } | |
b47cad08 | 705 | *f = l->next; |
3a1ee7e8 | 706 | Safefree(l); |
a8c08ecd | 707 | } |
6f9d8c32 NIS |
708 | } |
709 | ||
39f7a870 JH |
710 | /* Return as an array the stack of layers on a filehandle. Note that |
711 | * the stack is returned top-first in the array, and there are three | |
712 | * times as many array elements as there are layers in the stack: the | |
713 | * first element of a layer triplet is the name, the second one is the | |
714 | * arguments, and the third one is the flags. */ | |
715 | ||
716 | AV * | |
717 | PerlIO_get_layers(pTHX_ PerlIO *f) | |
718 | { | |
719 | AV *av = newAV(); | |
720 | ||
721 | if (PerlIOValid(f)) { | |
39f7a870 JH |
722 | PerlIOl *l = PerlIOBase(f); |
723 | ||
724 | while (l) { | |
725 | SV *name = l->tab && l->tab->name ? | |
726 | newSVpv(l->tab->name, 0) : &PL_sv_undef; | |
727 | SV *arg = l->tab && l->tab->Getarg ? | |
728 | (*l->tab->Getarg)(aTHX_ &l, 0, 0) : &PL_sv_undef; | |
729 | av_push(av, name); | |
730 | av_push(av, arg); | |
731 | av_push(av, newSViv((IV)l->flags)); | |
732 | l = l->next; | |
733 | } | |
734 | } | |
735 | ||
736 | return av; | |
737 | } | |
738 | ||
9e353e3b | 739 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 740 | /* |
71200d45 | 741 | * XS Interface for perl code |
14a5cf38 | 742 | */ |
9e353e3b | 743 | |
fcf2db38 | 744 | PerlIO_funcs * |
2edd7e44 | 745 | PerlIO_find_layer(pTHX_ const char *name, STRLEN len, int load) |
f3862f8b | 746 | { |
14a5cf38 JH |
747 | IV i; |
748 | if ((SSize_t) len <= 0) | |
749 | len = strlen(name); | |
3a1ee7e8 NIS |
750 | for (i = 0; i < PL_known_layers->cur; i++) { |
751 | PerlIO_funcs *f = PL_known_layers->array[i].funcs; | |
14a5cf38 | 752 | if (memEQ(f->name, name, len)) { |
fe5a182c | 753 | PerlIO_debug("%.*s => %p\n", (int) len, name, (void*)f); |
14a5cf38 JH |
754 | return f; |
755 | } | |
756 | } | |
3a1ee7e8 NIS |
757 | if (load && PL_subname && PL_def_layerlist |
758 | && PL_def_layerlist->cur >= 2) { | |
d7a09b41 SR |
759 | if (PL_in_load_module) { |
760 | Perl_croak(aTHX_ "Recursive call to Perl_load_module in PerlIO_find_layer"); | |
761 | return NULL; | |
762 | } else { | |
763 | SV *pkgsv = newSVpvn("PerlIO", 6); | |
764 | SV *layer = newSVpvn(name, len); | |
c9bca74a NIS |
765 | CV *cv = get_cv("PerlIO::Layer::NoWarnings", FALSE); |
766 | ENTER; | |
d7a09b41 | 767 | SAVEINT(PL_in_load_module); |
c9bca74a NIS |
768 | if (cv) { |
769 | SAVESPTR(PL_warnhook); | |
770 | PL_warnhook = (SV *) cv; | |
771 | } | |
d7a09b41 SR |
772 | PL_in_load_module++; |
773 | /* | |
774 | * The two SVs are magically freed by load_module | |
775 | */ | |
776 | Perl_load_module(aTHX_ 0, pkgsv, Nullsv, layer, Nullsv); | |
777 | PL_in_load_module--; | |
778 | LEAVE; | |
779 | return PerlIO_find_layer(aTHX_ name, len, 0); | |
780 | } | |
14a5cf38 JH |
781 | } |
782 | PerlIO_debug("Cannot find %.*s\n", (int) len, name); | |
783 | return NULL; | |
f3862f8b NIS |
784 | } |
785 | ||
2a1bc955 | 786 | #ifdef USE_ATTRIBUTES_FOR_PERLIO |
b13b2135 NIS |
787 | |
788 | static int | |
789 | perlio_mg_set(pTHX_ SV *sv, MAGIC *mg) | |
790 | { | |
14a5cf38 JH |
791 | if (SvROK(sv)) { |
792 | IO *io = GvIOn((GV *) SvRV(sv)); | |
793 | PerlIO *ifp = IoIFP(io); | |
794 | PerlIO *ofp = IoOFP(io); | |
795 | Perl_warn(aTHX_ "set %" SVf " %p %p %p", sv, io, ifp, ofp); | |
796 | } | |
797 | return 0; | |
b13b2135 NIS |
798 | } |
799 | ||
800 | static int | |
801 | perlio_mg_get(pTHX_ SV *sv, MAGIC *mg) | |
802 | { | |
14a5cf38 JH |
803 | if (SvROK(sv)) { |
804 | IO *io = GvIOn((GV *) SvRV(sv)); | |
805 | PerlIO *ifp = IoIFP(io); | |
806 | PerlIO *ofp = IoOFP(io); | |
807 | Perl_warn(aTHX_ "get %" SVf " %p %p %p", sv, io, ifp, ofp); | |
808 | } | |
809 | return 0; | |
b13b2135 NIS |
810 | } |
811 | ||
812 | static int | |
813 | perlio_mg_clear(pTHX_ SV *sv, MAGIC *mg) | |
814 | { | |
14a5cf38 JH |
815 | Perl_warn(aTHX_ "clear %" SVf, sv); |
816 | return 0; | |
b13b2135 NIS |
817 | } |
818 | ||
819 | static int | |
820 | perlio_mg_free(pTHX_ SV *sv, MAGIC *mg) | |
821 | { | |
14a5cf38 JH |
822 | Perl_warn(aTHX_ "free %" SVf, sv); |
823 | return 0; | |
b13b2135 NIS |
824 | } |
825 | ||
826 | MGVTBL perlio_vtab = { | |
14a5cf38 JH |
827 | perlio_mg_get, |
828 | perlio_mg_set, | |
22569500 | 829 | NULL, /* len */ |
14a5cf38 JH |
830 | perlio_mg_clear, |
831 | perlio_mg_free | |
b13b2135 NIS |
832 | }; |
833 | ||
834 | XS(XS_io_MODIFY_SCALAR_ATTRIBUTES) | |
835 | { | |
14a5cf38 JH |
836 | dXSARGS; |
837 | SV *sv = SvRV(ST(1)); | |
838 | AV *av = newAV(); | |
839 | MAGIC *mg; | |
840 | int count = 0; | |
841 | int i; | |
842 | sv_magic(sv, (SV *) av, PERL_MAGIC_ext, NULL, 0); | |
843 | SvRMAGICAL_off(sv); | |
844 | mg = mg_find(sv, PERL_MAGIC_ext); | |
845 | mg->mg_virtual = &perlio_vtab; | |
846 | mg_magical(sv); | |
847 | Perl_warn(aTHX_ "attrib %" SVf, sv); | |
848 | for (i = 2; i < items; i++) { | |
849 | STRLEN len; | |
850 | const char *name = SvPV(ST(i), len); | |
851 | SV *layer = PerlIO_find_layer(aTHX_ name, len, 1); | |
852 | if (layer) { | |
853 | av_push(av, SvREFCNT_inc(layer)); | |
854 | } | |
855 | else { | |
856 | ST(count) = ST(i); | |
857 | count++; | |
858 | } | |
859 | } | |
860 | SvREFCNT_dec(av); | |
861 | XSRETURN(count); | |
862 | } | |
863 | ||
22569500 | 864 | #endif /* USE_ATTIBUTES_FOR_PERLIO */ |
2a1bc955 | 865 | |
e3f3bf95 NIS |
866 | SV * |
867 | PerlIO_tab_sv(pTHX_ PerlIO_funcs *tab) | |
f3862f8b | 868 | { |
14a5cf38 JH |
869 | HV *stash = gv_stashpv("PerlIO::Layer", TRUE); |
870 | SV *sv = sv_bless(newRV_noinc(newSViv(PTR2IV(tab))), stash); | |
871 | return sv; | |
e3f3bf95 NIS |
872 | } |
873 | ||
c9bca74a NIS |
874 | XS(XS_PerlIO__Layer__NoWarnings) |
875 | { | |
37725cdc | 876 | /* This is used as a %SIG{__WARN__} handler to supress warnings |
c9bca74a NIS |
877 | during loading of layers. |
878 | */ | |
879 | dXSARGS; | |
880 | if (items) | |
881 | PerlIO_debug("warning:%s\n",SvPV_nolen(ST(0))); | |
882 | XSRETURN(0); | |
883 | } | |
884 | ||
0c4f7ff0 NIS |
885 | XS(XS_PerlIO__Layer__find) |
886 | { | |
14a5cf38 JH |
887 | dXSARGS; |
888 | if (items < 2) | |
889 | Perl_croak(aTHX_ "Usage class->find(name[,load])"); | |
890 | else { | |
891 | STRLEN len = 0; | |
892 | char *name = SvPV(ST(1), len); | |
893 | bool load = (items > 2) ? SvTRUE(ST(2)) : 0; | |
894 | PerlIO_funcs *layer = PerlIO_find_layer(aTHX_ name, len, load); | |
895 | ST(0) = | |
896 | (layer) ? sv_2mortal(PerlIO_tab_sv(aTHX_ layer)) : | |
897 | &PL_sv_undef; | |
898 | XSRETURN(1); | |
899 | } | |
0c4f7ff0 NIS |
900 | } |
901 | ||
e3f3bf95 NIS |
902 | void |
903 | PerlIO_define_layer(pTHX_ PerlIO_funcs *tab) | |
904 | { | |
3a1ee7e8 NIS |
905 | if (!PL_known_layers) |
906 | PL_known_layers = PerlIO_list_alloc(aTHX); | |
907 | PerlIO_list_push(aTHX_ PL_known_layers, tab, Nullsv); | |
fe5a182c | 908 | PerlIO_debug("define %s %p\n", tab->name, (void*)tab); |
f3862f8b NIS |
909 | } |
910 | ||
1141d9f8 | 911 | int |
fcf2db38 | 912 | PerlIO_parse_layers(pTHX_ PerlIO_list_t *av, const char *names) |
1141d9f8 | 913 | { |
14a5cf38 JH |
914 | if (names) { |
915 | const char *s = names; | |
916 | while (*s) { | |
917 | while (isSPACE(*s) || *s == ':') | |
918 | s++; | |
919 | if (*s) { | |
920 | STRLEN llen = 0; | |
921 | const char *e = s; | |
922 | const char *as = Nullch; | |
923 | STRLEN alen = 0; | |
924 | if (!isIDFIRST(*s)) { | |
925 | /* | |
926 | * Message is consistent with how attribute lists are | |
927 | * passed. Even though this means "foo : : bar" is | |
71200d45 | 928 | * seen as an invalid separator character. |
14a5cf38 JH |
929 | */ |
930 | char q = ((*s == '\'') ? '"' : '\''); | |
99ef548b | 931 | if (ckWARN(WARN_LAYER)) |
22569500 | 932 | Perl_warner(aTHX_ packWARN(WARN_LAYER), |
1e616cf5 JH |
933 | "perlio: invalid separator character %c%c%c in layer specification list %s", |
934 | q, *s, q, s); | |
93189314 | 935 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
936 | return -1; |
937 | } | |
938 | do { | |
939 | e++; | |
940 | } while (isALNUM(*e)); | |
941 | llen = e - s; | |
942 | if (*e == '(') { | |
943 | int nesting = 1; | |
944 | as = ++e; | |
945 | while (nesting) { | |
946 | switch (*e++) { | |
947 | case ')': | |
948 | if (--nesting == 0) | |
949 | alen = (e - 1) - as; | |
950 | break; | |
951 | case '(': | |
952 | ++nesting; | |
953 | break; | |
954 | case '\\': | |
955 | /* | |
956 | * It's a nul terminated string, not allowed | |
957 | * to \ the terminating null. Anything other | |
71200d45 | 958 | * character is passed over. |
14a5cf38 JH |
959 | */ |
960 | if (*e++) { | |
961 | break; | |
962 | } | |
963 | /* | |
71200d45 | 964 | * Drop through |
14a5cf38 JH |
965 | */ |
966 | case '\0': | |
967 | e--; | |
22569500 NIS |
968 | if (ckWARN(WARN_LAYER)) |
969 | Perl_warner(aTHX_ packWARN(WARN_LAYER), | |
14a5cf38 JH |
970 | "perlio: argument list not closed for layer \"%.*s\"", |
971 | (int) (e - s), s); | |
972 | return -1; | |
973 | default: | |
974 | /* | |
71200d45 | 975 | * boring. |
14a5cf38 JH |
976 | */ |
977 | break; | |
978 | } | |
979 | } | |
980 | } | |
981 | if (e > s) { | |
99ef548b | 982 | bool warn_layer = ckWARN(WARN_LAYER); |
14a5cf38 JH |
983 | PerlIO_funcs *layer = |
984 | PerlIO_find_layer(aTHX_ s, llen, 1); | |
985 | if (layer) { | |
3a1ee7e8 | 986 | PerlIO_list_push(aTHX_ av, layer, |
14a5cf38 JH |
987 | (as) ? newSVpvn(as, |
988 | alen) : | |
989 | &PL_sv_undef); | |
990 | } | |
991 | else { | |
22569500 | 992 | if (warn_layer) |
99ef548b | 993 | Perl_warner(aTHX_ packWARN(WARN_LAYER), "perlio: unknown layer \"%.*s\"", |
14a5cf38 JH |
994 | (int) llen, s); |
995 | return -1; | |
996 | } | |
997 | } | |
998 | s = e; | |
999 | } | |
1000 | } | |
1001 | } | |
1002 | return 0; | |
1141d9f8 NIS |
1003 | } |
1004 | ||
dfebf958 | 1005 | void |
fcf2db38 | 1006 | PerlIO_default_buffer(pTHX_ PerlIO_list_t *av) |
dfebf958 | 1007 | { |
14a5cf38 | 1008 | PerlIO_funcs *tab = &PerlIO_perlio; |
35990314 | 1009 | #ifdef PERLIO_USING_CRLF |
6ce75a77 | 1010 | tab = &PerlIO_crlf; |
846be114 | 1011 | #else |
6ce75a77 | 1012 | if (PerlIO_stdio.Set_ptrcnt) |
22569500 | 1013 | tab = &PerlIO_stdio; |
846be114 | 1014 | #endif |
14a5cf38 | 1015 | PerlIO_debug("Pushing %s\n", tab->name); |
3a1ee7e8 | 1016 | PerlIO_list_push(aTHX_ av, PerlIO_find_layer(aTHX_ tab->name, 0, 0), |
14a5cf38 | 1017 | &PL_sv_undef); |
dfebf958 NIS |
1018 | } |
1019 | ||
e3f3bf95 | 1020 | SV * |
14a5cf38 | 1021 | PerlIO_arg_fetch(PerlIO_list_t *av, IV n) |
e3f3bf95 | 1022 | { |
14a5cf38 | 1023 | return av->array[n].arg; |
e3f3bf95 NIS |
1024 | } |
1025 | ||
f3862f8b | 1026 | PerlIO_funcs * |
14a5cf38 | 1027 | PerlIO_layer_fetch(pTHX_ PerlIO_list_t *av, IV n, PerlIO_funcs *def) |
f3862f8b | 1028 | { |
14a5cf38 JH |
1029 | if (n >= 0 && n < av->cur) { |
1030 | PerlIO_debug("Layer %" IVdf " is %s\n", n, | |
1031 | av->array[n].funcs->name); | |
1032 | return av->array[n].funcs; | |
1033 | } | |
1034 | if (!def) | |
1035 | Perl_croak(aTHX_ "panic: PerlIO layer array corrupt"); | |
1036 | return def; | |
e3f3bf95 NIS |
1037 | } |
1038 | ||
4ec2216f NIS |
1039 | IV |
1040 | PerlIOPop_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) | |
1041 | { | |
1042 | if (PerlIOValid(f)) { | |
1043 | PerlIO_flush(f); | |
1044 | PerlIO_pop(aTHX_ f); | |
1045 | return 0; | |
1046 | } | |
1047 | return -1; | |
1048 | } | |
1049 | ||
1050 | PerlIO_funcs PerlIO_remove = { | |
1051 | sizeof(PerlIO_funcs), | |
1052 | "pop", | |
1053 | 0, | |
1054 | PERLIO_K_DUMMY | PERLIO_K_UTF8, | |
1055 | PerlIOPop_pushed, | |
1056 | NULL, | |
1057 | NULL, | |
1058 | NULL, | |
1059 | NULL, | |
1060 | NULL, | |
1061 | NULL, | |
1062 | NULL, | |
1063 | NULL, | |
1064 | NULL, | |
1065 | NULL, | |
1066 | NULL, /* flush */ | |
1067 | NULL, /* fill */ | |
1068 | NULL, | |
1069 | NULL, | |
1070 | NULL, | |
1071 | NULL, | |
1072 | NULL, /* get_base */ | |
1073 | NULL, /* get_bufsiz */ | |
1074 | NULL, /* get_ptr */ | |
1075 | NULL, /* get_cnt */ | |
1076 | NULL, /* set_ptrcnt */ | |
1077 | }; | |
1078 | ||
fcf2db38 | 1079 | PerlIO_list_t * |
e3f3bf95 NIS |
1080 | PerlIO_default_layers(pTHX) |
1081 | { | |
3a1ee7e8 | 1082 | if (!PL_def_layerlist) { |
14a5cf38 JH |
1083 | const char *s = (PL_tainting) ? Nullch : PerlEnv_getenv("PERLIO"); |
1084 | PerlIO_funcs *osLayer = &PerlIO_unix; | |
3a1ee7e8 | 1085 | PL_def_layerlist = PerlIO_list_alloc(aTHX); |
14a5cf38 | 1086 | PerlIO_define_layer(aTHX_ & PerlIO_unix); |
e1caacb4 | 1087 | #if defined(WIN32) && !defined(UNDER_CE) |
14a5cf38 | 1088 | PerlIO_define_layer(aTHX_ & PerlIO_win32); |
2f8118af | 1089 | #if 0 |
14a5cf38 | 1090 | osLayer = &PerlIO_win32; |
0c4128ad | 1091 | #endif |
2f8118af | 1092 | #endif |
14a5cf38 JH |
1093 | PerlIO_define_layer(aTHX_ & PerlIO_raw); |
1094 | PerlIO_define_layer(aTHX_ & PerlIO_perlio); | |
1095 | PerlIO_define_layer(aTHX_ & PerlIO_stdio); | |
1096 | PerlIO_define_layer(aTHX_ & PerlIO_crlf); | |
06da4f11 | 1097 | #ifdef HAS_MMAP |
14a5cf38 | 1098 | PerlIO_define_layer(aTHX_ & PerlIO_mmap); |
06da4f11 | 1099 | #endif |
14a5cf38 | 1100 | PerlIO_define_layer(aTHX_ & PerlIO_utf8); |
4ec2216f | 1101 | PerlIO_define_layer(aTHX_ & PerlIO_remove); |
14a5cf38 | 1102 | PerlIO_define_layer(aTHX_ & PerlIO_byte); |
3a1ee7e8 | 1103 | PerlIO_list_push(aTHX_ PL_def_layerlist, |
14a5cf38 JH |
1104 | PerlIO_find_layer(aTHX_ osLayer->name, 0, 0), |
1105 | &PL_sv_undef); | |
1106 | if (s) { | |
3a1ee7e8 | 1107 | PerlIO_parse_layers(aTHX_ PL_def_layerlist, s); |
14a5cf38 JH |
1108 | } |
1109 | else { | |
3a1ee7e8 | 1110 | PerlIO_default_buffer(aTHX_ PL_def_layerlist); |
14a5cf38 | 1111 | } |
1141d9f8 | 1112 | } |
3a1ee7e8 NIS |
1113 | if (PL_def_layerlist->cur < 2) { |
1114 | PerlIO_default_buffer(aTHX_ PL_def_layerlist); | |
f3862f8b | 1115 | } |
3a1ee7e8 | 1116 | return PL_def_layerlist; |
e3f3bf95 NIS |
1117 | } |
1118 | ||
0c4f7ff0 NIS |
1119 | void |
1120 | Perl_boot_core_PerlIO(pTHX) | |
1121 | { | |
1122 | #ifdef USE_ATTRIBUTES_FOR_PERLIO | |
14a5cf38 JH |
1123 | newXS("io::MODIFY_SCALAR_ATTRIBUTES", XS_io_MODIFY_SCALAR_ATTRIBUTES, |
1124 | __FILE__); | |
0c4f7ff0 | 1125 | #endif |
14a5cf38 | 1126 | newXS("PerlIO::Layer::find", XS_PerlIO__Layer__find, __FILE__); |
c9bca74a | 1127 | newXS("PerlIO::Layer::NoWarnings", XS_PerlIO__Layer__NoWarnings, __FILE__); |
0c4f7ff0 | 1128 | } |
e3f3bf95 NIS |
1129 | |
1130 | PerlIO_funcs * | |
1131 | PerlIO_default_layer(pTHX_ I32 n) | |
1132 | { | |
14a5cf38 JH |
1133 | PerlIO_list_t *av = PerlIO_default_layers(aTHX); |
1134 | if (n < 0) | |
1135 | n += av->cur; | |
1136 | return PerlIO_layer_fetch(aTHX_ av, n, &PerlIO_stdio); | |
f3862f8b NIS |
1137 | } |
1138 | ||
a999f61b NIS |
1139 | #define PerlIO_default_top() PerlIO_default_layer(aTHX_ -1) |
1140 | #define PerlIO_default_btm() PerlIO_default_layer(aTHX_ 0) | |
60382766 NIS |
1141 | |
1142 | void | |
1141d9f8 | 1143 | PerlIO_stdstreams(pTHX) |
60382766 | 1144 | { |
a1ea730d | 1145 | if (!PL_perlio) { |
14a5cf38 JH |
1146 | PerlIO_allocate(aTHX); |
1147 | PerlIO_fdopen(0, "Ir" PERLIO_STDTEXT); | |
1148 | PerlIO_fdopen(1, "Iw" PERLIO_STDTEXT); | |
1149 | PerlIO_fdopen(2, "Iw" PERLIO_STDTEXT); | |
1150 | } | |
60382766 NIS |
1151 | } |
1152 | ||
1153 | PerlIO * | |
14a5cf38 JH |
1154 | PerlIO_push(pTHX_ PerlIO *f, PerlIO_funcs *tab, const char *mode, SV *arg) |
1155 | { | |
2dc2558e NIS |
1156 | if (tab->fsize != sizeof(PerlIO_funcs)) { |
1157 | mismatch: | |
1158 | Perl_croak(aTHX_ "Layer does not match this perl"); | |
1159 | } | |
1160 | if (tab->size) { | |
1161 | PerlIOl *l = NULL; | |
1162 | if (tab->size < sizeof(PerlIOl)) { | |
1163 | goto mismatch; | |
1164 | } | |
1165 | /* Real layer with a data area */ | |
1166 | Newc('L',l,tab->size,char,PerlIOl); | |
1167 | if (l && f) { | |
1168 | Zero(l, tab->size, char); | |
1169 | l->next = *f; | |
1170 | l->tab = tab; | |
1171 | *f = l; | |
1172 | PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name, | |
1173 | (mode) ? mode : "(Null)", (void*)arg); | |
210e727c JH |
1174 | if (*l->tab->Pushed && |
1175 | (*l->tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) { | |
2dc2558e NIS |
1176 | PerlIO_pop(aTHX_ f); |
1177 | return NULL; | |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | else if (f) { | |
1182 | /* Pseudo-layer where push does its own stack adjust */ | |
00f51856 NIS |
1183 | PerlIO_debug("PerlIO_push f=%p %s %s %p\n", (void*)f, tab->name, |
1184 | (mode) ? mode : "(Null)", (void*)arg); | |
210e727c JH |
1185 | if (tab->Pushed && |
1186 | (*tab->Pushed) (aTHX_ f, mode, arg, tab) != 0) { | |
1187 | return NULL; | |
14a5cf38 JH |
1188 | } |
1189 | } | |
1190 | return f; | |
60382766 NIS |
1191 | } |
1192 | ||
dfebf958 | 1193 | IV |
86e05cf2 NIS |
1194 | PerlIOBase_binmode(pTHX_ PerlIO *f) |
1195 | { | |
1196 | if (PerlIOValid(f)) { | |
1197 | /* Is layer suitable for raw stream ? */ | |
1198 | if (PerlIOBase(f)->tab->kind & PERLIO_K_RAW) { | |
1199 | /* Yes - turn off UTF-8-ness, to undo UTF-8 locale effects */ | |
1200 | PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; | |
1201 | } | |
1202 | else { | |
1203 | /* Not suitable - pop it */ | |
1204 | PerlIO_pop(aTHX_ f); | |
1205 | } | |
1206 | return 0; | |
1207 | } | |
1208 | return -1; | |
1209 | } | |
1210 | ||
1211 | IV | |
2dc2558e | 1212 | PerlIORaw_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
dfebf958 | 1213 | { |
86e05cf2 | 1214 | |
04892f78 | 1215 | if (PerlIOValid(f)) { |
86e05cf2 NIS |
1216 | PerlIO *t; |
1217 | PerlIOl *l; | |
14a5cf38 | 1218 | PerlIO_flush(f); |
86e05cf2 NIS |
1219 | /* |
1220 | * Strip all layers that are not suitable for a raw stream | |
1221 | */ | |
1222 | t = f; | |
1223 | while (t && (l = *t)) { | |
1224 | if (l->tab->Binmode) { | |
1225 | /* Has a handler - normal case */ | |
1226 | if ((*l->tab->Binmode)(aTHX_ f) == 0) { | |
1227 | if (*t == l) { | |
1228 | /* Layer still there - move down a layer */ | |
1229 | t = PerlIONext(t); | |
1230 | } | |
1231 | } | |
1232 | else { | |
1233 | return -1; | |
1234 | } | |
14a5cf38 JH |
1235 | } |
1236 | else { | |
86e05cf2 NIS |
1237 | /* No handler - pop it */ |
1238 | PerlIO_pop(aTHX_ t); | |
14a5cf38 JH |
1239 | } |
1240 | } | |
86e05cf2 NIS |
1241 | if (PerlIOValid(f)) { |
1242 | PerlIO_debug(":raw f=%p :%s\n", (void*)f, PerlIOBase(f)->tab->name); | |
1243 | return 0; | |
1244 | } | |
14a5cf38 JH |
1245 | } |
1246 | return -1; | |
dfebf958 NIS |
1247 | } |
1248 | ||
ac27b0f5 | 1249 | int |
14a5cf38 | 1250 | PerlIO_apply_layera(pTHX_ PerlIO *f, const char *mode, |
d9dac8cd | 1251 | PerlIO_list_t *layers, IV n, IV max) |
14a5cf38 | 1252 | { |
14a5cf38 JH |
1253 | int code = 0; |
1254 | while (n < max) { | |
1255 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n, NULL); | |
1256 | if (tab) { | |
1257 | if (!PerlIO_push(aTHX_ f, tab, mode, PerlIOArg)) { | |
1258 | code = -1; | |
1259 | break; | |
1260 | } | |
1261 | } | |
1262 | n++; | |
1263 | } | |
1264 | return code; | |
e3f3bf95 NIS |
1265 | } |
1266 | ||
1267 | int | |
ac27b0f5 NIS |
1268 | PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names) |
1269 | { | |
14a5cf38 | 1270 | int code = 0; |
53f1b6d2 | 1271 | if (f && names) { |
3a1ee7e8 | 1272 | PerlIO_list_t *layers = PerlIO_list_alloc(aTHX); |
14a5cf38 JH |
1273 | code = PerlIO_parse_layers(aTHX_ layers, names); |
1274 | if (code == 0) { | |
d9dac8cd | 1275 | code = PerlIO_apply_layera(aTHX_ f, mode, layers, 0, layers->cur); |
14a5cf38 | 1276 | } |
3a1ee7e8 | 1277 | PerlIO_list_free(aTHX_ layers); |
ac27b0f5 | 1278 | } |
14a5cf38 | 1279 | return code; |
ac27b0f5 NIS |
1280 | } |
1281 | ||
f3862f8b | 1282 | |
60382766 | 1283 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 1284 | /* |
71200d45 | 1285 | * Given the abstraction above the public API functions |
14a5cf38 | 1286 | */ |
60382766 NIS |
1287 | |
1288 | int | |
f5b9d040 | 1289 | PerlIO_binmode(pTHX_ PerlIO *f, int iotype, int mode, const char *names) |
76ced9ad | 1290 | { |
14a5cf38 | 1291 | PerlIO_debug("PerlIO_binmode f=%p %s %c %x %s\n", |
fe5a182c | 1292 | (void*)f, PerlIOBase(f)->tab->name, iotype, mode, |
14a5cf38 | 1293 | (names) ? names : "(Null)"); |
03c0554d NIS |
1294 | if (names) { |
1295 | /* Do not flush etc. if (e.g.) switching encodings. | |
1296 | if a pushed layer knows it needs to flush lower layers | |
1297 | (for example :unix which is never going to call them) | |
1298 | it can do the flush when it is pushed. | |
1299 | */ | |
1300 | return PerlIO_apply_layers(aTHX_ f, NULL, names) == 0 ? TRUE : FALSE; | |
1301 | } | |
1302 | else { | |
86e05cf2 | 1303 | /* Fake 5.6 legacy of using this call to turn ON O_TEXT */ |
35990314 | 1304 | #ifdef PERLIO_USING_CRLF |
03c0554d NIS |
1305 | /* Legacy binmode only has meaning if O_TEXT has a value distinct from |
1306 | O_BINARY so we can look for it in mode. | |
1307 | */ | |
1308 | if (!(mode & O_BINARY)) { | |
1309 | /* Text mode */ | |
86e05cf2 NIS |
1310 | /* FIXME?: Looking down the layer stack seems wrong, |
1311 | but is a way of reaching past (say) an encoding layer | |
1312 | to flip CRLF-ness of the layer(s) below | |
1313 | */ | |
03c0554d NIS |
1314 | while (*f) { |
1315 | /* Perhaps we should turn on bottom-most aware layer | |
1316 | e.g. Ilya's idea that UNIX TTY could serve | |
1317 | */ | |
1318 | if (PerlIOBase(f)->tab->kind & PERLIO_K_CANCRLF) { | |
1319 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) { | |
1320 | /* Not in text mode - flush any pending stuff and flip it */ | |
1321 | PerlIO_flush(f); | |
1322 | PerlIOBase(f)->flags |= PERLIO_F_CRLF; | |
1323 | } | |
1324 | /* Only need to turn it on in one layer so we are done */ | |
1325 | return TRUE; | |
ed53a2bb | 1326 | } |
03c0554d | 1327 | f = PerlIONext(f); |
14a5cf38 | 1328 | } |
03c0554d NIS |
1329 | /* Not finding a CRLF aware layer presumably means we are binary |
1330 | which is not what was requested - so we failed | |
1331 | We _could_ push :crlf layer but so could caller | |
1332 | */ | |
1333 | return FALSE; | |
14a5cf38 | 1334 | } |
6ce75a77 | 1335 | #endif |
86e05cf2 NIS |
1336 | /* Legacy binmode is now _defined_ as being equivalent to pushing :raw |
1337 | So code that used to be here is now in PerlIORaw_pushed(). | |
03c0554d | 1338 | */ |
86e05cf2 | 1339 | return PerlIO_push(aTHX_ f, &PerlIO_raw, Nullch, Nullsv) ? TRUE : FALSE; |
14a5cf38 | 1340 | } |
f5b9d040 NIS |
1341 | } |
1342 | ||
f5b9d040 | 1343 | int |
e87a358a | 1344 | PerlIO__close(pTHX_ PerlIO *f) |
f5b9d040 | 1345 | { |
37725cdc NIS |
1346 | if (PerlIOValid(f)) { |
1347 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1348 | if (tab && tab->Close) | |
1349 | return (*tab->Close)(aTHX_ f); | |
1350 | else | |
1351 | return PerlIOBase_close(aTHX_ f); | |
1352 | } | |
14a5cf38 | 1353 | else { |
93189314 | 1354 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1355 | return -1; |
1356 | } | |
76ced9ad NIS |
1357 | } |
1358 | ||
b931b1d9 | 1359 | int |
e87a358a | 1360 | Perl_PerlIO_close(pTHX_ PerlIO *f) |
b931b1d9 | 1361 | { |
37725cdc NIS |
1362 | int code = PerlIO__close(aTHX_ f); |
1363 | while (PerlIOValid(f)) { | |
1364 | PerlIO_pop(aTHX_ f); | |
f6c77cf1 | 1365 | } |
14a5cf38 | 1366 | return code; |
b931b1d9 NIS |
1367 | } |
1368 | ||
b931b1d9 | 1369 | int |
e87a358a | 1370 | Perl_PerlIO_fileno(pTHX_ PerlIO *f) |
b931b1d9 | 1371 | { |
b32dd47e | 1372 | Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f)); |
b931b1d9 NIS |
1373 | } |
1374 | ||
1141d9f8 NIS |
1375 | static const char * |
1376 | PerlIO_context_layers(pTHX_ const char *mode) | |
1377 | { | |
14a5cf38 JH |
1378 | const char *type = NULL; |
1379 | /* | |
71200d45 | 1380 | * Need to supply default layer info from open.pm |
14a5cf38 JH |
1381 | */ |
1382 | if (PL_curcop) { | |
1383 | SV *layers = PL_curcop->cop_io; | |
1384 | if (layers) { | |
1385 | STRLEN len; | |
1386 | type = SvPV(layers, len); | |
1387 | if (type && mode[0] != 'r') { | |
1388 | /* | |
71200d45 | 1389 | * Skip to write part |
14a5cf38 JH |
1390 | */ |
1391 | const char *s = strchr(type, 0); | |
eb160463 | 1392 | if (s && (STRLEN)(s - type) < len) { |
14a5cf38 JH |
1393 | type = s + 1; |
1394 | } | |
1395 | } | |
1396 | } | |
1397 | } | |
1398 | return type; | |
1141d9f8 NIS |
1399 | } |
1400 | ||
fcf2db38 | 1401 | static PerlIO_funcs * |
2edd7e44 NIS |
1402 | PerlIO_layer_from_ref(pTHX_ SV *sv) |
1403 | { | |
14a5cf38 | 1404 | /* |
71200d45 | 1405 | * For any scalar type load the handler which is bundled with perl |
14a5cf38 JH |
1406 | */ |
1407 | if (SvTYPE(sv) < SVt_PVAV) | |
e934609f | 1408 | return PerlIO_find_layer(aTHX_ "scalar", 6, 1); |
14a5cf38 JH |
1409 | |
1410 | /* | |
71200d45 | 1411 | * For other types allow if layer is known but don't try and load it |
14a5cf38 JH |
1412 | */ |
1413 | switch (SvTYPE(sv)) { | |
1414 | case SVt_PVAV: | |
1415 | return PerlIO_find_layer(aTHX_ "Array", 5, 0); | |
1416 | case SVt_PVHV: | |
1417 | return PerlIO_find_layer(aTHX_ "Hash", 4, 0); | |
1418 | case SVt_PVCV: | |
1419 | return PerlIO_find_layer(aTHX_ "Code", 4, 0); | |
1420 | case SVt_PVGV: | |
1421 | return PerlIO_find_layer(aTHX_ "Glob", 4, 0); | |
1422 | } | |
1423 | return NULL; | |
2edd7e44 NIS |
1424 | } |
1425 | ||
fcf2db38 | 1426 | PerlIO_list_t * |
14a5cf38 JH |
1427 | PerlIO_resolve_layers(pTHX_ const char *layers, |
1428 | const char *mode, int narg, SV **args) | |
1429 | { | |
1430 | PerlIO_list_t *def = PerlIO_default_layers(aTHX); | |
1431 | int incdef = 1; | |
a1ea730d | 1432 | if (!PL_perlio) |
14a5cf38 JH |
1433 | PerlIO_stdstreams(aTHX); |
1434 | if (narg) { | |
1435 | SV *arg = *args; | |
1436 | /* | |
71200d45 NIS |
1437 | * If it is a reference but not an object see if we have a handler |
1438 | * for it | |
14a5cf38 JH |
1439 | */ |
1440 | if (SvROK(arg) && !sv_isobject(arg)) { | |
1441 | PerlIO_funcs *handler = PerlIO_layer_from_ref(aTHX_ SvRV(arg)); | |
1442 | if (handler) { | |
3a1ee7e8 NIS |
1443 | def = PerlIO_list_alloc(aTHX); |
1444 | PerlIO_list_push(aTHX_ def, handler, &PL_sv_undef); | |
14a5cf38 JH |
1445 | incdef = 0; |
1446 | } | |
1447 | /* | |
e934609f | 1448 | * Don't fail if handler cannot be found :via(...) etc. may do |
14a5cf38 | 1449 | * something sensible else we will just stringfy and open |
71200d45 | 1450 | * resulting string. |
14a5cf38 JH |
1451 | */ |
1452 | } | |
1453 | } | |
1454 | if (!layers) | |
1455 | layers = PerlIO_context_layers(aTHX_ mode); | |
1456 | if (layers && *layers) { | |
1457 | PerlIO_list_t *av; | |
1458 | if (incdef) { | |
1459 | IV i = def->cur; | |
3a1ee7e8 | 1460 | av = PerlIO_list_alloc(aTHX); |
14a5cf38 | 1461 | for (i = 0; i < def->cur; i++) { |
3a1ee7e8 | 1462 | PerlIO_list_push(aTHX_ av, def->array[i].funcs, |
14a5cf38 JH |
1463 | def->array[i].arg); |
1464 | } | |
1465 | } | |
1466 | else { | |
1467 | av = def; | |
1468 | } | |
0cff2cf3 NIS |
1469 | if (PerlIO_parse_layers(aTHX_ av, layers) == 0) { |
1470 | return av; | |
1471 | } | |
1472 | else { | |
1473 | PerlIO_list_free(aTHX_ av); | |
1474 | return (PerlIO_list_t *) NULL; | |
1475 | } | |
14a5cf38 JH |
1476 | } |
1477 | else { | |
1478 | if (incdef) | |
1479 | def->refcnt++; | |
1480 | return def; | |
1481 | } | |
ee518936 NIS |
1482 | } |
1483 | ||
1484 | PerlIO * | |
14a5cf38 JH |
1485 | PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, |
1486 | int imode, int perm, PerlIO *f, int narg, SV **args) | |
1487 | { | |
1488 | if (!f && narg == 1 && *args == &PL_sv_undef) { | |
1489 | if ((f = PerlIO_tmpfile())) { | |
1490 | if (!layers) | |
1491 | layers = PerlIO_context_layers(aTHX_ mode); | |
1492 | if (layers && *layers) | |
1493 | PerlIO_apply_layers(aTHX_ f, mode, layers); | |
1494 | } | |
1495 | } | |
1496 | else { | |
1497 | PerlIO_list_t *layera = NULL; | |
1498 | IV n; | |
1499 | PerlIO_funcs *tab = NULL; | |
04892f78 | 1500 | if (PerlIOValid(f)) { |
14a5cf38 | 1501 | /* |
71200d45 NIS |
1502 | * This is "reopen" - it is not tested as perl does not use it |
1503 | * yet | |
14a5cf38 JH |
1504 | */ |
1505 | PerlIOl *l = *f; | |
3a1ee7e8 | 1506 | layera = PerlIO_list_alloc(aTHX); |
14a5cf38 | 1507 | while (l) { |
04892f78 NIS |
1508 | SV *arg = (l->tab->Getarg) |
1509 | ? (*l->tab->Getarg) (aTHX_ &l, NULL, 0) | |
1510 | : &PL_sv_undef; | |
3a1ee7e8 | 1511 | PerlIO_list_push(aTHX_ layera, l->tab, arg); |
14a5cf38 JH |
1512 | l = *PerlIONext(&l); |
1513 | } | |
1514 | } | |
1515 | else { | |
1516 | layera = PerlIO_resolve_layers(aTHX_ layers, mode, narg, args); | |
0cff2cf3 NIS |
1517 | if (!layera) { |
1518 | return NULL; | |
1519 | } | |
14a5cf38 JH |
1520 | } |
1521 | /* | |
71200d45 | 1522 | * Start at "top" of layer stack |
14a5cf38 JH |
1523 | */ |
1524 | n = layera->cur - 1; | |
1525 | while (n >= 0) { | |
1526 | PerlIO_funcs *t = PerlIO_layer_fetch(aTHX_ layera, n, NULL); | |
1527 | if (t && t->Open) { | |
1528 | tab = t; | |
1529 | break; | |
1530 | } | |
1531 | n--; | |
1532 | } | |
1533 | if (tab) { | |
1534 | /* | |
71200d45 | 1535 | * Found that layer 'n' can do opens - call it |
14a5cf38 | 1536 | */ |
7cf31beb | 1537 | if (narg > 1 && !(tab->kind & PERLIO_K_MULTIARG)) { |
3b8752bb | 1538 | Perl_croak(aTHX_ "More than one argument to open(,':%s')",tab->name); |
7cf31beb | 1539 | } |
14a5cf38 | 1540 | PerlIO_debug("openn(%s,'%s','%s',%d,%x,%o,%p,%d,%p)\n", |
fe5a182c JH |
1541 | tab->name, layers, mode, fd, imode, perm, |
1542 | (void*)f, narg, (void*)args); | |
210e727c JH |
1543 | if (tab->Open) |
1544 | f = (*tab->Open) (aTHX_ tab, layera, n, mode, fd, imode, perm, | |
1545 | f, narg, args); | |
1546 | else { | |
1547 | SETERRNO(EINVAL, LIB_INVARG); | |
1548 | f = NULL; | |
1549 | } | |
14a5cf38 JH |
1550 | if (f) { |
1551 | if (n + 1 < layera->cur) { | |
1552 | /* | |
1553 | * More layers above the one that we used to open - | |
71200d45 | 1554 | * apply them now |
14a5cf38 | 1555 | */ |
d9dac8cd NIS |
1556 | if (PerlIO_apply_layera(aTHX_ f, mode, layera, n + 1, layera->cur) != 0) { |
1557 | /* If pushing layers fails close the file */ | |
1558 | PerlIO_close(f); | |
14a5cf38 JH |
1559 | f = NULL; |
1560 | } | |
1561 | } | |
1562 | } | |
1563 | } | |
3a1ee7e8 | 1564 | PerlIO_list_free(aTHX_ layera); |
14a5cf38 JH |
1565 | } |
1566 | return f; | |
ee518936 | 1567 | } |
b931b1d9 NIS |
1568 | |
1569 | ||
9e353e3b | 1570 | SSize_t |
e87a358a | 1571 | Perl_PerlIO_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
6f9d8c32 | 1572 | { |
1f376c7c | 1573 | Perl_PerlIO_fail_if_hasnot(f, PERLIO_F_CANREAD, EBADF, SS_IVCHAN, -1); |
b32dd47e | 1574 | Perl_PerlIO_or_Base(f, Read, read, -1, (aTHX_ f, vbuf, count)); |
760ac839 LW |
1575 | } |
1576 | ||
313ca112 | 1577 | SSize_t |
e87a358a | 1578 | Perl_PerlIO_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 1579 | { |
1f376c7c | 1580 | Perl_PerlIO_fail_if_hasnot(f, PERLIO_F_CANREAD, EBADF, SS_IVCHAN, -1); |
b32dd47e | 1581 | Perl_PerlIO_or_Base(f, Unread, unread, -1, (aTHX_ f, vbuf, count)); |
760ac839 LW |
1582 | } |
1583 | ||
9e353e3b | 1584 | SSize_t |
e87a358a | 1585 | Perl_PerlIO_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 1586 | { |
1f376c7c | 1587 | Perl_PerlIO_fail_if_hasnot(f, PERLIO_F_CANWRITE, EBADF, SS_IVCHAN, -1); |
b32dd47e | 1588 | Perl_PerlIO_or_fail(f, Write, -1, (aTHX_ f, vbuf, count)); |
760ac839 LW |
1589 | } |
1590 | ||
6f9d8c32 | 1591 | int |
e87a358a | 1592 | Perl_PerlIO_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
760ac839 | 1593 | { |
b32dd47e | 1594 | Perl_PerlIO_or_fail(f, Seek, -1, (aTHX_ f, offset, whence)); |
760ac839 LW |
1595 | } |
1596 | ||
9e353e3b | 1597 | Off_t |
e87a358a | 1598 | Perl_PerlIO_tell(pTHX_ PerlIO *f) |
760ac839 | 1599 | { |
b32dd47e | 1600 | Perl_PerlIO_or_fail(f, Tell, -1, (aTHX_ f)); |
760ac839 LW |
1601 | } |
1602 | ||
6f9d8c32 | 1603 | int |
e87a358a | 1604 | Perl_PerlIO_flush(pTHX_ PerlIO *f) |
760ac839 | 1605 | { |
14a5cf38 JH |
1606 | if (f) { |
1607 | if (*f) { | |
1608 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1b7a0411 JH |
1609 | |
1610 | if (tab && tab->Flush) | |
f62ce20a | 1611 | return (*tab->Flush) (aTHX_ f); |
1b7a0411 JH |
1612 | else |
1613 | return 0; /* If no Flush defined, silently succeed. */ | |
14a5cf38 JH |
1614 | } |
1615 | else { | |
fe5a182c | 1616 | PerlIO_debug("Cannot flush f=%p\n", (void*)f); |
93189314 | 1617 | SETERRNO(EBADF, SS_IVCHAN); |
14a5cf38 JH |
1618 | return -1; |
1619 | } | |
1620 | } | |
1621 | else { | |
1622 | /* | |
1623 | * Is it good API design to do flush-all on NULL, a potentially | |
1624 | * errorneous input? Maybe some magical value (PerlIO* | |
1625 | * PERLIO_FLUSH_ALL = (PerlIO*)-1;)? Yes, stdio does similar | |
1626 | * things on fflush(NULL), but should we be bound by their design | |
71200d45 | 1627 | * decisions? --jhi |
14a5cf38 | 1628 | */ |
a1ea730d | 1629 | PerlIO **table = &PL_perlio; |
14a5cf38 JH |
1630 | int code = 0; |
1631 | while ((f = *table)) { | |
1632 | int i; | |
1633 | table = (PerlIO **) (f++); | |
1634 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1635 | if (*f && PerlIO_flush(f) != 0) | |
1636 | code = -1; | |
1637 | f++; | |
1638 | } | |
1639 | } | |
1640 | return code; | |
1641 | } | |
760ac839 LW |
1642 | } |
1643 | ||
a9c883f6 | 1644 | void |
f62ce20a | 1645 | PerlIOBase_flush_linebuf(pTHX) |
a9c883f6 | 1646 | { |
a1ea730d | 1647 | PerlIO **table = &PL_perlio; |
14a5cf38 JH |
1648 | PerlIO *f; |
1649 | while ((f = *table)) { | |
1650 | int i; | |
1651 | table = (PerlIO **) (f++); | |
1652 | for (i = 1; i < PERLIO_TABLE_SIZE; i++) { | |
1653 | if (*f | |
1654 | && (PerlIOBase(f)-> | |
1655 | flags & (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1656 | == (PERLIO_F_LINEBUF | PERLIO_F_CANWRITE)) | |
1657 | PerlIO_flush(f); | |
1658 | f++; | |
1659 | } | |
a9c883f6 | 1660 | } |
a9c883f6 NIS |
1661 | } |
1662 | ||
06da4f11 | 1663 | int |
e87a358a | 1664 | Perl_PerlIO_fill(pTHX_ PerlIO *f) |
06da4f11 | 1665 | { |
b32dd47e | 1666 | Perl_PerlIO_or_fail(f, Fill, -1, (aTHX_ f)); |
06da4f11 NIS |
1667 | } |
1668 | ||
f3862f8b NIS |
1669 | int |
1670 | PerlIO_isutf8(PerlIO *f) | |
1671 | { | |
1b7a0411 JH |
1672 | if (PerlIOValid(f)) |
1673 | return (PerlIOBase(f)->flags & PERLIO_F_UTF8) != 0; | |
1674 | else | |
1675 | SETERRNO(EBADF, SS_IVCHAN); | |
37725cdc | 1676 | |
1b7a0411 | 1677 | return -1; |
f3862f8b NIS |
1678 | } |
1679 | ||
6f9d8c32 | 1680 | int |
e87a358a | 1681 | Perl_PerlIO_eof(pTHX_ PerlIO *f) |
760ac839 | 1682 | { |
b32dd47e | 1683 | Perl_PerlIO_or_Base(f, Eof, eof, -1, (aTHX_ f)); |
9e353e3b NIS |
1684 | } |
1685 | ||
9e353e3b | 1686 | int |
e87a358a | 1687 | Perl_PerlIO_error(pTHX_ PerlIO *f) |
9e353e3b | 1688 | { |
b32dd47e | 1689 | Perl_PerlIO_or_Base(f, Error, error, -1, (aTHX_ f)); |
9e353e3b NIS |
1690 | } |
1691 | ||
9e353e3b | 1692 | void |
e87a358a | 1693 | Perl_PerlIO_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 1694 | { |
b32dd47e | 1695 | Perl_PerlIO_or_Base_void(f, Clearerr, clearerr, (aTHX_ f)); |
9e353e3b NIS |
1696 | } |
1697 | ||
9e353e3b | 1698 | void |
e87a358a | 1699 | Perl_PerlIO_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b | 1700 | { |
b32dd47e | 1701 | Perl_PerlIO_or_Base_void(f, Setlinebuf, setlinebuf, (aTHX_ f)); |
9e353e3b NIS |
1702 | } |
1703 | ||
9e353e3b NIS |
1704 | int |
1705 | PerlIO_has_base(PerlIO *f) | |
1706 | { | |
1b7a0411 JH |
1707 | if (PerlIOValid(f)) { |
1708 | PerlIO_funcs *tab = PerlIOBase(f)->tab; | |
1709 | ||
1710 | if (tab) | |
1711 | return (tab->Get_base != NULL); | |
1712 | SETERRNO(EINVAL, LIB_INVARG); | |
1713 | } | |
1714 | else | |
1715 | SETERRNO(EBADF, SS_IVCHAN); | |
1716 | ||
1717 | return 0; | |
760ac839 LW |
1718 | } |
1719 | ||
9e353e3b NIS |
1720 | int |
1721 | PerlIO_fast_gets(PerlIO *f) | |
760ac839 | 1722 | { |
04892f78 | 1723 | if (PerlIOValid(f) && (PerlIOBase(f)->flags & PERLIO_F_FASTGETS)) { |
1b7a0411 JH |
1724 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1725 | ||
1726 | if (tab) | |
1727 | return (tab->Set_ptrcnt != NULL); | |
1728 | SETERRNO(EINVAL, LIB_INVARG); | |
14a5cf38 | 1729 | } |
1b7a0411 JH |
1730 | else |
1731 | SETERRNO(EBADF, SS_IVCHAN); | |
1732 | ||
14a5cf38 | 1733 | return 0; |
9e353e3b NIS |
1734 | } |
1735 | ||
9e353e3b NIS |
1736 | int |
1737 | PerlIO_has_cntptr(PerlIO *f) | |
1738 | { | |
04892f78 | 1739 | if (PerlIOValid(f)) { |
14a5cf38 | 1740 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1b7a0411 JH |
1741 | |
1742 | if (tab) | |
1743 | return (tab->Get_ptr != NULL && tab->Get_cnt != NULL); | |
1744 | SETERRNO(EINVAL, LIB_INVARG); | |
14a5cf38 | 1745 | } |
1b7a0411 JH |
1746 | else |
1747 | SETERRNO(EBADF, SS_IVCHAN); | |
1748 | ||
14a5cf38 | 1749 | return 0; |
9e353e3b NIS |
1750 | } |
1751 | ||
9e353e3b NIS |
1752 | int |
1753 | PerlIO_canset_cnt(PerlIO *f) | |
1754 | { | |
04892f78 | 1755 | if (PerlIOValid(f)) { |
1b7a0411 JH |
1756 | PerlIO_funcs *tab = PerlIOBase(f)->tab; |
1757 | ||
1758 | if (tab) | |
1759 | return (tab->Set_ptrcnt != NULL); | |
1760 | SETERRNO(EINVAL, LIB_INVARG); | |
14a5cf38 | 1761 | } |
1b7a0411 JH |
1762 | else |
1763 | SETERRNO(EBADF, SS_IVCHAN); | |
1764 | ||
14a5cf38 | 1765 | return 0; |
760ac839 LW |
1766 | } |
1767 | ||
888911fc | 1768 | STDCHAR * |
e87a358a | 1769 | Perl_PerlIO_get_base(pTHX_ PerlIO *f) |
760ac839 | 1770 | { |
b32dd47e | 1771 | Perl_PerlIO_or_fail(f, Get_base, NULL, (aTHX_ f)); |
9e353e3b NIS |
1772 | } |
1773 | ||
9e353e3b | 1774 | int |
e87a358a | 1775 | Perl_PerlIO_get_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 1776 | { |
b32dd47e | 1777 | Perl_PerlIO_or_fail(f, Get_bufsiz, -1, (aTHX_ f)); |
9e353e3b NIS |
1778 | } |
1779 | ||
9e353e3b | 1780 | STDCHAR * |
e87a358a | 1781 | Perl_PerlIO_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 1782 | { |
b32dd47e | 1783 | Perl_PerlIO_or_fail(f, Get_ptr, NULL, (aTHX_ f)); |
9e353e3b NIS |
1784 | } |
1785 | ||
05d1247b | 1786 | int |
e87a358a | 1787 | Perl_PerlIO_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 1788 | { |
b32dd47e | 1789 | Perl_PerlIO_or_fail(f, Get_cnt, -1, (aTHX_ f)); |
9e353e3b NIS |
1790 | } |
1791 | ||
9e353e3b | 1792 | void |
e87a358a | 1793 | Perl_PerlIO_set_cnt(pTHX_ PerlIO *f, int cnt) |
9e353e3b | 1794 | { |
b32dd47e | 1795 | Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, NULL, cnt)); |
9e353e3b NIS |
1796 | } |
1797 | ||
9e353e3b | 1798 | void |
e87a358a | 1799 | Perl_PerlIO_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, int cnt) |
9e353e3b | 1800 | { |
b32dd47e | 1801 | Perl_PerlIO_or_fail_void(f, Set_ptrcnt, (aTHX_ f, ptr, cnt)); |
9e353e3b NIS |
1802 | } |
1803 | ||
4ec2216f | 1804 | |
9e353e3b | 1805 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 1806 | /* |
71200d45 | 1807 | * utf8 and raw dummy layers |
14a5cf38 | 1808 | */ |
dfebf958 | 1809 | |
26fb694e | 1810 | IV |
2dc2558e | 1811 | PerlIOUtf8_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
26fb694e | 1812 | { |
00f51856 | 1813 | if (PerlIOValid(f)) { |
14a5cf38 JH |
1814 | if (tab->kind & PERLIO_K_UTF8) |
1815 | PerlIOBase(f)->flags |= PERLIO_F_UTF8; | |
1816 | else | |
1817 | PerlIOBase(f)->flags &= ~PERLIO_F_UTF8; | |
1818 | return 0; | |
1819 | } | |
1820 | return -1; | |
26fb694e NIS |
1821 | } |
1822 | ||
dfebf958 | 1823 | PerlIO_funcs PerlIO_utf8 = { |
2dc2558e | 1824 | sizeof(PerlIO_funcs), |
14a5cf38 | 1825 | "utf8", |
2dc2558e | 1826 | 0, |
6874a2de | 1827 | PERLIO_K_DUMMY | PERLIO_K_UTF8, |
14a5cf38 JH |
1828 | PerlIOUtf8_pushed, |
1829 | NULL, | |
1830 | NULL, | |
1831 | NULL, | |
1832 | NULL, | |
1833 | NULL, | |
1834 | NULL, | |
1835 | NULL, | |
1836 | NULL, | |
1837 | NULL, | |
1838 | NULL, | |
22569500 NIS |
1839 | NULL, /* flush */ |
1840 | NULL, /* fill */ | |
14a5cf38 JH |
1841 | NULL, |
1842 | NULL, | |
1843 | NULL, | |
1844 | NULL, | |
22569500 NIS |
1845 | NULL, /* get_base */ |
1846 | NULL, /* get_bufsiz */ | |
1847 | NULL, /* get_ptr */ | |
1848 | NULL, /* get_cnt */ | |
1849 | NULL, /* set_ptrcnt */ | |
26fb694e NIS |
1850 | }; |
1851 | ||
1852 | PerlIO_funcs PerlIO_byte = { | |
2dc2558e | 1853 | sizeof(PerlIO_funcs), |
14a5cf38 | 1854 | "bytes", |
2dc2558e | 1855 | 0, |
14a5cf38 JH |
1856 | PERLIO_K_DUMMY, |
1857 | PerlIOUtf8_pushed, | |
1858 | NULL, | |
1859 | NULL, | |
1860 | NULL, | |
1861 | NULL, | |
1862 | NULL, | |
1863 | NULL, | |
1864 | NULL, | |
1865 | NULL, | |
1866 | NULL, | |
1867 | NULL, | |
22569500 NIS |
1868 | NULL, /* flush */ |
1869 | NULL, /* fill */ | |
14a5cf38 JH |
1870 | NULL, |
1871 | NULL, | |
1872 | NULL, | |
1873 | NULL, | |
22569500 NIS |
1874 | NULL, /* get_base */ |
1875 | NULL, /* get_bufsiz */ | |
1876 | NULL, /* get_ptr */ | |
1877 | NULL, /* get_cnt */ | |
1878 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1879 | }; |
1880 | ||
1881 | PerlIO * | |
14a5cf38 JH |
1882 | PerlIORaw_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
1883 | IV n, const char *mode, int fd, int imode, int perm, | |
1884 | PerlIO *old, int narg, SV **args) | |
dfebf958 | 1885 | { |
14a5cf38 | 1886 | PerlIO_funcs *tab = PerlIO_default_btm(); |
210e727c JH |
1887 | if (tab && tab->Open) |
1888 | return (*tab->Open) (aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
1889 | old, narg, args); | |
1890 | SETERRNO(EINVAL, LIB_INVARG); | |
1891 | return NULL; | |
dfebf958 NIS |
1892 | } |
1893 | ||
1894 | PerlIO_funcs PerlIO_raw = { | |
2dc2558e | 1895 | sizeof(PerlIO_funcs), |
14a5cf38 | 1896 | "raw", |
2dc2558e | 1897 | 0, |
14a5cf38 JH |
1898 | PERLIO_K_DUMMY, |
1899 | PerlIORaw_pushed, | |
1900 | PerlIOBase_popped, | |
1901 | PerlIORaw_open, | |
1902 | NULL, | |
1903 | NULL, | |
1904 | NULL, | |
1905 | NULL, | |
1906 | NULL, | |
1907 | NULL, | |
1908 | NULL, | |
1909 | NULL, | |
22569500 NIS |
1910 | NULL, /* flush */ |
1911 | NULL, /* fill */ | |
14a5cf38 JH |
1912 | NULL, |
1913 | NULL, | |
1914 | NULL, | |
1915 | NULL, | |
22569500 NIS |
1916 | NULL, /* get_base */ |
1917 | NULL, /* get_bufsiz */ | |
1918 | NULL, /* get_ptr */ | |
1919 | NULL, /* get_cnt */ | |
1920 | NULL, /* set_ptrcnt */ | |
dfebf958 NIS |
1921 | }; |
1922 | /*--------------------------------------------------------------------------------------*/ | |
1923 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 1924 | /* |
71200d45 | 1925 | * "Methods" of the "base class" |
14a5cf38 | 1926 | */ |
9e353e3b NIS |
1927 | |
1928 | IV | |
f62ce20a | 1929 | PerlIOBase_fileno(pTHX_ PerlIO *f) |
9e353e3b | 1930 | { |
04892f78 | 1931 | return PerlIOValid(f) ? PerlIO_fileno(PerlIONext(f)) : -1; |
9e353e3b NIS |
1932 | } |
1933 | ||
f5b9d040 | 1934 | char * |
81428673 | 1935 | PerlIO_modestr(PerlIO * f, char *buf) |
14a5cf38 JH |
1936 | { |
1937 | char *s = buf; | |
81428673 NIS |
1938 | if (PerlIOValid(f)) { |
1939 | IV flags = PerlIOBase(f)->flags; | |
1940 | if (flags & PERLIO_F_APPEND) { | |
1941 | *s++ = 'a'; | |
1942 | if (flags & PERLIO_F_CANREAD) { | |
1943 | *s++ = '+'; | |
1944 | } | |
14a5cf38 | 1945 | } |
81428673 NIS |
1946 | else if (flags & PERLIO_F_CANREAD) { |
1947 | *s++ = 'r'; | |
1948 | if (flags & PERLIO_F_CANWRITE) | |
1949 | *s++ = '+'; | |
1950 | } | |
1951 | else if (flags & PERLIO_F_CANWRITE) { | |
1952 | *s++ = 'w'; | |
1953 | if (flags & PERLIO_F_CANREAD) { | |
1954 | *s++ = '+'; | |
1955 | } | |
14a5cf38 | 1956 | } |
35990314 | 1957 | #ifdef PERLIO_USING_CRLF |
81428673 NIS |
1958 | if (!(flags & PERLIO_F_CRLF)) |
1959 | *s++ = 'b'; | |
5f1a76d0 | 1960 | #endif |
81428673 | 1961 | } |
14a5cf38 JH |
1962 | *s = '\0'; |
1963 | return buf; | |
f5b9d040 NIS |
1964 | } |
1965 | ||
81428673 | 1966 | |
76ced9ad | 1967 | IV |
2dc2558e | 1968 | PerlIOBase_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
9e353e3b | 1969 | { |
14a5cf38 | 1970 | PerlIOl *l = PerlIOBase(f); |
b7953727 | 1971 | #if 0 |
14a5cf38 JH |
1972 | const char *omode = mode; |
1973 | char temp[8]; | |
b7953727 | 1974 | #endif |
14a5cf38 JH |
1975 | l->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | |
1976 | PERLIO_F_TRUNCATE | PERLIO_F_APPEND); | |
1977 | if (tab->Set_ptrcnt != NULL) | |
1978 | l->flags |= PERLIO_F_FASTGETS; | |
1979 | if (mode) { | |
1980 | if (*mode == '#' || *mode == 'I') | |
1981 | mode++; | |
1982 | switch (*mode++) { | |
1983 | case 'r': | |
1984 | l->flags |= PERLIO_F_CANREAD; | |
1985 | break; | |
1986 | case 'a': | |
1987 | l->flags |= PERLIO_F_APPEND | PERLIO_F_CANWRITE; | |
1988 | break; | |
1989 | case 'w': | |
1990 | l->flags |= PERLIO_F_TRUNCATE | PERLIO_F_CANWRITE; | |
1991 | break; | |
1992 | default: | |
93189314 | 1993 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
1994 | return -1; |
1995 | } | |
1996 | while (*mode) { | |
1997 | switch (*mode++) { | |
1998 | case '+': | |
1999 | l->flags |= PERLIO_F_CANREAD | PERLIO_F_CANWRITE; | |
2000 | break; | |
2001 | case 'b': | |
2002 | l->flags &= ~PERLIO_F_CRLF; | |
2003 | break; | |
2004 | case 't': | |
2005 | l->flags |= PERLIO_F_CRLF; | |
2006 | break; | |
2007 | default: | |
93189314 | 2008 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
2009 | return -1; |
2010 | } | |
2011 | } | |
2012 | } | |
2013 | else { | |
2014 | if (l->next) { | |
2015 | l->flags |= l->next->flags & | |
2016 | (PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_TRUNCATE | | |
2017 | PERLIO_F_APPEND); | |
2018 | } | |
2019 | } | |
5e2ab84b | 2020 | #if 0 |
14a5cf38 JH |
2021 | PerlIO_debug("PerlIOBase_pushed f=%p %s %s fl=%08" UVxf " (%s)\n", |
2022 | f, PerlIOBase(f)->tab->name, (omode) ? omode : "(Null)", | |
2023 | l->flags, PerlIO_modestr(f, temp)); | |
5e2ab84b | 2024 | #endif |
14a5cf38 | 2025 | return 0; |
76ced9ad NIS |
2026 | } |
2027 | ||
2028 | IV | |
f62ce20a | 2029 | PerlIOBase_popped(pTHX_ PerlIO *f) |
76ced9ad | 2030 | { |
14a5cf38 | 2031 | return 0; |
760ac839 LW |
2032 | } |
2033 | ||
9e353e3b | 2034 | SSize_t |
f62ce20a | 2035 | PerlIOBase_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 2036 | { |
14a5cf38 | 2037 | /* |
71200d45 | 2038 | * Save the position as current head considers it |
14a5cf38 JH |
2039 | */ |
2040 | Off_t old = PerlIO_tell(f); | |
2041 | SSize_t done; | |
2042 | PerlIO_push(aTHX_ f, &PerlIO_pending, "r", Nullsv); | |
2043 | PerlIOSelf(f, PerlIOBuf)->posn = old; | |
f62ce20a | 2044 | done = PerlIOBuf_unread(aTHX_ f, vbuf, count); |
14a5cf38 | 2045 | return done; |
9e353e3b NIS |
2046 | } |
2047 | ||
f6c77cf1 | 2048 | SSize_t |
f62ce20a | 2049 | PerlIOBase_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
f6c77cf1 | 2050 | { |
14a5cf38 JH |
2051 | STDCHAR *buf = (STDCHAR *) vbuf; |
2052 | if (f) { | |
14a5cf38 JH |
2053 | while (count > 0) { |
2054 | SSize_t avail = PerlIO_get_cnt(f); | |
2055 | SSize_t take = 0; | |
2056 | if (avail > 0) | |
eb160463 | 2057 | take = ((SSize_t)count < avail) ? count : avail; |
14a5cf38 JH |
2058 | if (take > 0) { |
2059 | STDCHAR *ptr = PerlIO_get_ptr(f); | |
2060 | Copy(ptr, buf, take, STDCHAR); | |
2061 | PerlIO_set_ptrcnt(f, ptr + take, (avail -= take)); | |
2062 | count -= take; | |
2063 | buf += take; | |
2064 | } | |
2065 | if (count > 0 && avail <= 0) { | |
2066 | if (PerlIO_fill(f) != 0) | |
2067 | break; | |
2068 | } | |
2069 | } | |
2070 | return (buf - (STDCHAR *) vbuf); | |
2071 | } | |
f6c77cf1 | 2072 | return 0; |
f6c77cf1 NIS |
2073 | } |
2074 | ||
9e353e3b | 2075 | IV |
f62ce20a | 2076 | PerlIOBase_noop_ok(pTHX_ PerlIO *f) |
9e353e3b | 2077 | { |
14a5cf38 | 2078 | return 0; |
9e353e3b NIS |
2079 | } |
2080 | ||
2081 | IV | |
f62ce20a | 2082 | PerlIOBase_noop_fail(pTHX_ PerlIO *f) |
06da4f11 | 2083 | { |
14a5cf38 | 2084 | return -1; |
06da4f11 NIS |
2085 | } |
2086 | ||
2087 | IV | |
f62ce20a | 2088 | PerlIOBase_close(pTHX_ PerlIO *f) |
9e353e3b | 2089 | { |
37725cdc NIS |
2090 | IV code = -1; |
2091 | if (PerlIOValid(f)) { | |
2092 | PerlIO *n = PerlIONext(f); | |
2093 | code = PerlIO_flush(f); | |
2094 | PerlIOBase(f)->flags &= | |
2095 | ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); | |
2096 | while (PerlIOValid(n)) { | |
2097 | PerlIO_funcs *tab = PerlIOBase(n)->tab; | |
2098 | if (tab && tab->Close) { | |
2099 | if ((*tab->Close)(aTHX_ n) != 0) | |
2100 | code = -1; | |
2101 | break; | |
2102 | } | |
2103 | else { | |
2104 | PerlIOBase(n)->flags &= | |
2105 | ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); | |
2106 | } | |
2107 | n = PerlIONext(n); | |
2108 | } | |
2109 | } | |
2110 | else { | |
2111 | SETERRNO(EBADF, SS_IVCHAN); | |
2112 | } | |
14a5cf38 | 2113 | return code; |
9e353e3b NIS |
2114 | } |
2115 | ||
2116 | IV | |
f62ce20a | 2117 | PerlIOBase_eof(pTHX_ PerlIO *f) |
9e353e3b | 2118 | { |
04892f78 | 2119 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2120 | return (PerlIOBase(f)->flags & PERLIO_F_EOF) != 0; |
2121 | } | |
2122 | return 1; | |
9e353e3b NIS |
2123 | } |
2124 | ||
2125 | IV | |
f62ce20a | 2126 | PerlIOBase_error(pTHX_ PerlIO *f) |
9e353e3b | 2127 | { |
04892f78 | 2128 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2129 | return (PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0; |
2130 | } | |
2131 | return 1; | |
9e353e3b NIS |
2132 | } |
2133 | ||
2134 | void | |
f62ce20a | 2135 | PerlIOBase_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 2136 | { |
04892f78 | 2137 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2138 | PerlIO *n = PerlIONext(f); |
2139 | PerlIOBase(f)->flags &= ~(PERLIO_F_ERROR | PERLIO_F_EOF); | |
04892f78 | 2140 | if (PerlIOValid(n)) |
14a5cf38 JH |
2141 | PerlIO_clearerr(n); |
2142 | } | |
9e353e3b NIS |
2143 | } |
2144 | ||
2145 | void | |
f62ce20a | 2146 | PerlIOBase_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b | 2147 | { |
04892f78 | 2148 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2149 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF; |
2150 | } | |
9e353e3b NIS |
2151 | } |
2152 | ||
93a8090d NIS |
2153 | SV * |
2154 | PerlIO_sv_dup(pTHX_ SV *arg, CLONE_PARAMS *param) | |
2155 | { | |
2156 | if (!arg) | |
2157 | return Nullsv; | |
2158 | #ifdef sv_dup | |
2159 | if (param) { | |
2160 | return sv_dup(arg, param); | |
2161 | } | |
2162 | else { | |
2163 | return newSVsv(arg); | |
2164 | } | |
2165 | #else | |
2166 | return newSVsv(arg); | |
2167 | #endif | |
2168 | } | |
2169 | ||
2170 | PerlIO * | |
ecdeb87c | 2171 | PerlIOBase_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
93a8090d NIS |
2172 | { |
2173 | PerlIO *nexto = PerlIONext(o); | |
04892f78 | 2174 | if (PerlIOValid(nexto)) { |
93a8090d | 2175 | PerlIO_funcs *tab = PerlIOBase(nexto)->tab; |
37725cdc NIS |
2176 | if (tab && tab->Dup) |
2177 | f = (*tab->Dup)(aTHX_ f, nexto, param, flags); | |
2178 | else | |
2179 | f = PerlIOBase_dup(aTHX_ f, nexto, param, flags); | |
93a8090d NIS |
2180 | } |
2181 | if (f) { | |
2182 | PerlIO_funcs *self = PerlIOBase(o)->tab; | |
210e727c | 2183 | SV *arg; |
93a8090d | 2184 | char buf[8]; |
fe5a182c JH |
2185 | PerlIO_debug("PerlIOBase_dup %s f=%p o=%p param=%p\n", |
2186 | self->name, (void*)f, (void*)o, (void*)param); | |
210e727c JH |
2187 | if (self->Getarg) |
2188 | arg = (*self->Getarg)(aTHX_ o, param, flags); | |
2189 | else { | |
37725cdc | 2190 | arg = Nullsv; |
93a8090d NIS |
2191 | } |
2192 | f = PerlIO_push(aTHX_ f, self, PerlIO_modestr(o,buf), arg); | |
ecdeb87c | 2193 | if (arg) { |
93a8090d NIS |
2194 | SvREFCNT_dec(arg); |
2195 | } | |
2196 | } | |
2197 | return f; | |
2198 | } | |
2199 | ||
2200 | #define PERLIO_MAX_REFCOUNTABLE_FD 2048 | |
168d5872 | 2201 | #ifdef USE_THREADS |
93a8090d | 2202 | perl_mutex PerlIO_mutex; |
93a8090d | 2203 | #endif |
168d5872 | 2204 | int PerlIO_fd_refcnt[PERLIO_MAX_REFCOUNTABLE_FD]; |
93a8090d NIS |
2205 | |
2206 | void | |
2207 | PerlIO_init(pTHX) | |
2208 | { | |
2209 | /* Place holder for stdstreams call ??? */ | |
168d5872 | 2210 | #ifdef USE_THREADS |
93a8090d NIS |
2211 | MUTEX_INIT(&PerlIO_mutex); |
2212 | #endif | |
2213 | } | |
2214 | ||
168d5872 NIS |
2215 | void |
2216 | PerlIOUnix_refcnt_inc(int fd) | |
2217 | { | |
2218 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { | |
2219 | #ifdef USE_THREADS | |
2220 | MUTEX_LOCK(&PerlIO_mutex); | |
2221 | #endif | |
2222 | PerlIO_fd_refcnt[fd]++; | |
2223 | PerlIO_debug("fd %d refcnt=%d\n",fd,PerlIO_fd_refcnt[fd]); | |
2224 | #ifdef USE_THREADS | |
2225 | MUTEX_UNLOCK(&PerlIO_mutex); | |
2226 | #endif | |
2227 | } | |
2228 | } | |
2229 | ||
168d5872 NIS |
2230 | int |
2231 | PerlIOUnix_refcnt_dec(int fd) | |
2232 | { | |
2233 | int cnt = 0; | |
2234 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { | |
2235 | #ifdef USE_THREADS | |
2236 | MUTEX_LOCK(&PerlIO_mutex); | |
2237 | #endif | |
2238 | cnt = --PerlIO_fd_refcnt[fd]; | |
2239 | PerlIO_debug("fd %d refcnt=%d\n",fd,cnt); | |
2240 | #ifdef USE_THREADS | |
2241 | MUTEX_UNLOCK(&PerlIO_mutex); | |
2242 | #endif | |
2243 | } | |
2244 | return cnt; | |
2245 | } | |
2246 | ||
694c95cf JH |
2247 | void |
2248 | PerlIO_cleanup(pTHX) | |
2249 | { | |
2250 | int i; | |
2251 | #ifdef USE_ITHREADS | |
9f4bd222 NIS |
2252 | PerlIO_debug("Cleanup layers for %p\n",aTHX); |
2253 | #else | |
2254 | PerlIO_debug("Cleanup layers\n"); | |
694c95cf JH |
2255 | #endif |
2256 | /* Raise STDIN..STDERR refcount so we don't close them */ | |
2257 | for (i=0; i < 3; i++) | |
2258 | PerlIOUnix_refcnt_inc(i); | |
2259 | PerlIO_cleantable(aTHX_ &PL_perlio); | |
2260 | /* Restore STDIN..STDERR refcount */ | |
2261 | for (i=0; i < 3; i++) | |
2262 | PerlIOUnix_refcnt_dec(i); | |
9f4bd222 NIS |
2263 | |
2264 | if (PL_known_layers) { | |
2265 | PerlIO_list_free(aTHX_ PL_known_layers); | |
2266 | PL_known_layers = NULL; | |
2267 | } | |
2268 | if(PL_def_layerlist) { | |
2269 | PerlIO_list_free(aTHX_ PL_def_layerlist); | |
2270 | PL_def_layerlist = NULL; | |
2271 | } | |
694c95cf JH |
2272 | } |
2273 | ||
2274 | ||
2275 | ||
9e353e3b | 2276 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 2277 | /* |
71200d45 | 2278 | * Bottom-most level for UNIX-like case |
14a5cf38 | 2279 | */ |
9e353e3b | 2280 | |
14a5cf38 | 2281 | typedef struct { |
22569500 NIS |
2282 | struct _PerlIO base; /* The generic part */ |
2283 | int fd; /* UNIX like file descriptor */ | |
2284 | int oflags; /* open/fcntl flags */ | |
9e353e3b NIS |
2285 | } PerlIOUnix; |
2286 | ||
6f9d8c32 | 2287 | int |
9e353e3b | 2288 | PerlIOUnix_oflags(const char *mode) |
760ac839 | 2289 | { |
14a5cf38 JH |
2290 | int oflags = -1; |
2291 | if (*mode == 'I' || *mode == '#') | |
2292 | mode++; | |
2293 | switch (*mode) { | |
2294 | case 'r': | |
2295 | oflags = O_RDONLY; | |
2296 | if (*++mode == '+') { | |
2297 | oflags = O_RDWR; | |
2298 | mode++; | |
2299 | } | |
2300 | break; | |
2301 | ||
2302 | case 'w': | |
2303 | oflags = O_CREAT | O_TRUNC; | |
2304 | if (*++mode == '+') { | |
2305 | oflags |= O_RDWR; | |
2306 | mode++; | |
2307 | } | |
2308 | else | |
2309 | oflags |= O_WRONLY; | |
2310 | break; | |
2311 | ||
2312 | case 'a': | |
2313 | oflags = O_CREAT | O_APPEND; | |
2314 | if (*++mode == '+') { | |
2315 | oflags |= O_RDWR; | |
2316 | mode++; | |
2317 | } | |
2318 | else | |
2319 | oflags |= O_WRONLY; | |
2320 | break; | |
2321 | } | |
2322 | if (*mode == 'b') { | |
2323 | oflags |= O_BINARY; | |
2324 | oflags &= ~O_TEXT; | |
2325 | mode++; | |
2326 | } | |
2327 | else if (*mode == 't') { | |
2328 | oflags |= O_TEXT; | |
2329 | oflags &= ~O_BINARY; | |
2330 | mode++; | |
2331 | } | |
2332 | /* | |
71200d45 | 2333 | * Always open in binary mode |
14a5cf38 JH |
2334 | */ |
2335 | oflags |= O_BINARY; | |
2336 | if (*mode || oflags == -1) { | |
93189314 | 2337 | SETERRNO(EINVAL, LIB_INVARG); |
14a5cf38 JH |
2338 | oflags = -1; |
2339 | } | |
2340 | return oflags; | |
9e353e3b NIS |
2341 | } |
2342 | ||
2343 | IV | |
f62ce20a | 2344 | PerlIOUnix_fileno(pTHX_ PerlIO *f) |
9e353e3b | 2345 | { |
14a5cf38 | 2346 | return PerlIOSelf(f, PerlIOUnix)->fd; |
9e353e3b NIS |
2347 | } |
2348 | ||
aa063c35 NIS |
2349 | static void |
2350 | PerlIOUnix_setfd(pTHX_ PerlIO *f, int fd, int imode) | |
4b803d04 | 2351 | { |
93a8090d | 2352 | PerlIOUnix *s = PerlIOSelf(f, PerlIOUnix); |
6caa5a9c | 2353 | #if defined(WIN32) |
aa063c35 NIS |
2354 | Stat_t st; |
2355 | if (PerlLIO_fstat(fd, &st) == 0) { | |
6caa5a9c | 2356 | if (!S_ISREG(st.st_mode)) { |
aa063c35 | 2357 | PerlIO_debug("%d is not regular file\n",fd); |
6caa5a9c NIS |
2358 | PerlIOBase(f)->flags |= PERLIO_F_NOTREG; |
2359 | } | |
aa063c35 NIS |
2360 | else { |
2361 | PerlIO_debug("%d _is_ a regular file\n",fd); | |
2362 | } | |
6caa5a9c NIS |
2363 | } |
2364 | #endif | |
aa063c35 NIS |
2365 | s->fd = fd; |
2366 | s->oflags = imode; | |
2367 | PerlIOUnix_refcnt_inc(fd); | |
2368 | } | |
2369 | ||
2370 | IV | |
2371 | PerlIOUnix_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) | |
2372 | { | |
2373 | IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab); | |
14a5cf38 | 2374 | if (*PerlIONext(f)) { |
4b069b44 | 2375 | /* We never call down so do any pending stuff now */ |
03c0554d | 2376 | PerlIO_flush(PerlIONext(f)); |
14a5cf38 | 2377 | /* |
71200d45 | 2378 | * XXX could (or should) we retrieve the oflags from the open file |
14a5cf38 | 2379 | * handle rather than believing the "mode" we are passed in? XXX |
71200d45 | 2380 | * Should the value on NULL mode be 0 or -1? |
14a5cf38 | 2381 | */ |
acbd16bf | 2382 | PerlIOUnix_setfd(aTHX_ f, PerlIO_fileno(PerlIONext(f)), |
aa063c35 | 2383 | mode ? PerlIOUnix_oflags(mode) : -1); |
14a5cf38 JH |
2384 | } |
2385 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; | |
6caa5a9c | 2386 | |
14a5cf38 | 2387 | return code; |
4b803d04 NIS |
2388 | } |
2389 | ||
9e353e3b | 2390 | PerlIO * |
14a5cf38 JH |
2391 | PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2392 | IV n, const char *mode, int fd, int imode, | |
2393 | int perm, PerlIO *f, int narg, SV **args) | |
2394 | { | |
d9dac8cd | 2395 | if (PerlIOValid(f)) { |
14a5cf38 | 2396 | if (PerlIOBase(f)->flags & PERLIO_F_OPEN) |
f62ce20a | 2397 | (*PerlIOBase(f)->tab->Close)(aTHX_ f); |
14a5cf38 JH |
2398 | } |
2399 | if (narg > 0) { | |
2400 | char *path = SvPV_nolen(*args); | |
2401 | if (*mode == '#') | |
2402 | mode++; | |
2403 | else { | |
2404 | imode = PerlIOUnix_oflags(mode); | |
2405 | perm = 0666; | |
2406 | } | |
2407 | if (imode != -1) { | |
2408 | fd = PerlLIO_open3(path, imode, perm); | |
2409 | } | |
2410 | } | |
2411 | if (fd >= 0) { | |
14a5cf38 JH |
2412 | if (*mode == 'I') |
2413 | mode++; | |
2414 | if (!f) { | |
2415 | f = PerlIO_allocate(aTHX); | |
d9dac8cd NIS |
2416 | } |
2417 | if (!PerlIOValid(f)) { | |
a33cf58c NIS |
2418 | if (!(f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) { |
2419 | return NULL; | |
2420 | } | |
d9dac8cd | 2421 | } |
aa063c35 | 2422 | PerlIOUnix_setfd(aTHX_ f, fd, imode); |
14a5cf38 JH |
2423 | PerlIOBase(f)->flags |= PERLIO_F_OPEN; |
2424 | return f; | |
2425 | } | |
2426 | else { | |
2427 | if (f) { | |
2428 | /* | |
71200d45 | 2429 | * FIXME: pop layers ??? |
14a5cf38 JH |
2430 | */ |
2431 | } | |
2432 | return NULL; | |
2433 | } | |
9e353e3b NIS |
2434 | } |
2435 | ||
71200d45 | 2436 | PerlIO * |
ecdeb87c | 2437 | PerlIOUnix_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
71200d45 NIS |
2438 | { |
2439 | PerlIOUnix *os = PerlIOSelf(o, PerlIOUnix); | |
93a8090d | 2440 | int fd = os->fd; |
ecdeb87c NIS |
2441 | if (flags & PERLIO_DUP_FD) { |
2442 | fd = PerlLIO_dup(fd); | |
2443 | } | |
93a8090d | 2444 | if (fd >= 0 && fd < PERLIO_MAX_REFCOUNTABLE_FD) { |
ecdeb87c | 2445 | f = PerlIOBase_dup(aTHX_ f, o, param, flags); |
71200d45 NIS |
2446 | if (f) { |
2447 | /* If all went well overwrite fd in dup'ed lay with the dup()'ed fd */ | |
aa063c35 | 2448 | PerlIOUnix_setfd(aTHX_ f, fd, os->oflags); |
71200d45 NIS |
2449 | return f; |
2450 | } | |
71200d45 NIS |
2451 | } |
2452 | return NULL; | |
2453 | } | |
2454 | ||
2455 | ||
9e353e3b | 2456 | SSize_t |
f62ce20a | 2457 | PerlIOUnix_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
9e353e3b | 2458 | { |
14a5cf38 | 2459 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
81428673 | 2460 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANREAD) || |
1fd8f4ce | 2461 | PerlIOBase(f)->flags & (PERLIO_F_EOF|PERLIO_F_ERROR)) { |
1f376c7c | 2462 | return -1; |
1fd8f4ce | 2463 | } |
14a5cf38 JH |
2464 | while (1) { |
2465 | SSize_t len = PerlLIO_read(fd, vbuf, count); | |
2466 | if (len >= 0 || errno != EINTR) { | |
ba85f2ea NIS |
2467 | if (len < 0) { |
2468 | if (errno != EAGAIN) { | |
2469 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2470 | } | |
2471 | } | |
2472 | else if (len == 0 && count != 0) { | |
14a5cf38 | 2473 | PerlIOBase(f)->flags |= PERLIO_F_EOF; |
ba85f2ea NIS |
2474 | SETERRNO(0,0); |
2475 | } | |
14a5cf38 JH |
2476 | return len; |
2477 | } | |
2478 | PERL_ASYNC_CHECK(); | |
2479 | } | |
9e353e3b NIS |
2480 | } |
2481 | ||
2482 | SSize_t | |
f62ce20a | 2483 | PerlIOUnix_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 2484 | { |
14a5cf38 JH |
2485 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
2486 | while (1) { | |
2487 | SSize_t len = PerlLIO_write(fd, vbuf, count); | |
2488 | if (len >= 0 || errno != EINTR) { | |
ba85f2ea NIS |
2489 | if (len < 0) { |
2490 | if (errno != EAGAIN) { | |
2491 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
2492 | } | |
2493 | } | |
14a5cf38 JH |
2494 | return len; |
2495 | } | |
2496 | PERL_ASYNC_CHECK(); | |
06da4f11 | 2497 | } |
9e353e3b NIS |
2498 | } |
2499 | ||
2500 | IV | |
f62ce20a | 2501 | PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 2502 | { |
1f376c7c | 2503 | int fd; |
6caa5a9c | 2504 | Off_t new; |
1f376c7c JH |
2505 | #ifdef ESPIPE |
2506 | Perl_PerlIO_fail_if_has(f, PERLIO_F_NOTREG, ESPIPE, SS_IVCHAN, -1); | |
6caa5a9c | 2507 | #else |
1f376c7c | 2508 | Perl_PerlIO_fail_if_has(f, PERLIO_F_NOTREG, EBADF, SS_IVCHAN, -1); |
6caa5a9c | 2509 | #endif |
1f376c7c JH |
2510 | fd = PerlIOSelf(f, PerlIOUnix)->fd; |
2511 | new = PerlLIO_lseek(fd, offset, whence); | |
6caa5a9c NIS |
2512 | if (new == (Off_t) - 1) |
2513 | { | |
2514 | return -1; | |
2515 | } | |
14a5cf38 | 2516 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; |
6caa5a9c | 2517 | return 0; |
9e353e3b NIS |
2518 | } |
2519 | ||
2520 | Off_t | |
f62ce20a | 2521 | PerlIOUnix_tell(pTHX_ PerlIO *f) |
9e353e3b | 2522 | { |
14a5cf38 | 2523 | return PerlLIO_lseek(PerlIOSelf(f, PerlIOUnix)->fd, 0, SEEK_CUR); |
9e353e3b NIS |
2524 | } |
2525 | ||
71200d45 | 2526 | |
9e353e3b | 2527 | IV |
f62ce20a | 2528 | PerlIOUnix_close(pTHX_ PerlIO *f) |
9e353e3b | 2529 | { |
14a5cf38 JH |
2530 | int fd = PerlIOSelf(f, PerlIOUnix)->fd; |
2531 | int code = 0; | |
168d5872 NIS |
2532 | if (PerlIOBase(f)->flags & PERLIO_F_OPEN) { |
2533 | if (PerlIOUnix_refcnt_dec(fd) > 0) { | |
93a8090d NIS |
2534 | PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; |
2535 | return 0; | |
22569500 | 2536 | } |
93a8090d NIS |
2537 | } |
2538 | else { | |
93189314 | 2539 | SETERRNO(EBADF,SS_IVCHAN); |
93a8090d NIS |
2540 | return -1; |
2541 | } | |
14a5cf38 JH |
2542 | while (PerlLIO_close(fd) != 0) { |
2543 | if (errno != EINTR) { | |
2544 | code = -1; | |
2545 | break; | |
2546 | } | |
2547 | PERL_ASYNC_CHECK(); | |
2548 | } | |
2549 | if (code == 0) { | |
2550 | PerlIOBase(f)->flags &= ~PERLIO_F_OPEN; | |
2551 | } | |
2552 | return code; | |
9e353e3b NIS |
2553 | } |
2554 | ||
2555 | PerlIO_funcs PerlIO_unix = { | |
2dc2558e | 2556 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
2557 | "unix", |
2558 | sizeof(PerlIOUnix), | |
2559 | PERLIO_K_RAW, | |
2560 | PerlIOUnix_pushed, | |
44798d05 | 2561 | PerlIOBase_popped, |
14a5cf38 | 2562 | PerlIOUnix_open, |
86e05cf2 | 2563 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
2564 | NULL, |
2565 | PerlIOUnix_fileno, | |
71200d45 | 2566 | PerlIOUnix_dup, |
14a5cf38 JH |
2567 | PerlIOUnix_read, |
2568 | PerlIOBase_unread, | |
2569 | PerlIOUnix_write, | |
2570 | PerlIOUnix_seek, | |
2571 | PerlIOUnix_tell, | |
2572 | PerlIOUnix_close, | |
22569500 NIS |
2573 | PerlIOBase_noop_ok, /* flush */ |
2574 | PerlIOBase_noop_fail, /* fill */ | |
14a5cf38 JH |
2575 | PerlIOBase_eof, |
2576 | PerlIOBase_error, | |
2577 | PerlIOBase_clearerr, | |
2578 | PerlIOBase_setlinebuf, | |
22569500 NIS |
2579 | NULL, /* get_base */ |
2580 | NULL, /* get_bufsiz */ | |
2581 | NULL, /* get_ptr */ | |
2582 | NULL, /* get_cnt */ | |
2583 | NULL, /* set_ptrcnt */ | |
9e353e3b NIS |
2584 | }; |
2585 | ||
2586 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 2587 | /* |
71200d45 | 2588 | * stdio as a layer |
14a5cf38 | 2589 | */ |
9e353e3b | 2590 | |
313e59c8 NIS |
2591 | #if defined(VMS) && !defined(STDIO_BUFFER_WRITABLE) |
2592 | /* perl5.8 - This ensures the last minute VMS ungetc fix is not | |
2593 | broken by the last second glibc 2.3 fix | |
2594 | */ | |
2595 | #define STDIO_BUFFER_WRITABLE | |
2596 | #endif | |
2597 | ||
2598 | ||
14a5cf38 JH |
2599 | typedef struct { |
2600 | struct _PerlIO base; | |
22569500 | 2601 | FILE *stdio; /* The stream */ |
9e353e3b NIS |
2602 | } PerlIOStdio; |
2603 | ||
2604 | IV | |
f62ce20a | 2605 | PerlIOStdio_fileno(pTHX_ PerlIO *f) |
9e353e3b | 2606 | { |
439ba545 | 2607 | FILE *s; |
81428673 | 2608 | if (PerlIOValid(f) && (s = PerlIOSelf(f, PerlIOStdio)->stdio)) { |
439ba545 NIS |
2609 | return PerlSIO_fileno(s); |
2610 | } | |
2611 | errno = EBADF; | |
2612 | return -1; | |
9e353e3b NIS |
2613 | } |
2614 | ||
766a733e | 2615 | char * |
14a5cf38 JH |
2616 | PerlIOStdio_mode(const char *mode, char *tmode) |
2617 | { | |
2618 | char *ret = tmode; | |
2619 | while (*mode) { | |
2620 | *tmode++ = *mode++; | |
2621 | } | |
35990314 | 2622 | #ifdef PERLIO_USING_CRLF |
6ce75a77 JH |
2623 | *tmode++ = 'b'; |
2624 | #endif | |
14a5cf38 JH |
2625 | *tmode = '\0'; |
2626 | return ret; | |
2627 | } | |
2628 | ||
4b803d04 | 2629 | IV |
2dc2558e | 2630 | PerlIOStdio_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
4b803d04 | 2631 | { |
1fd8f4ce NIS |
2632 | PerlIO *n; |
2633 | if (PerlIOValid(f) && PerlIOValid(n = PerlIONext(f))) { | |
2634 | PerlIO_funcs *toptab = PerlIOBase(n)->tab; | |
2635 | if (toptab == tab) { | |
2636 | /* Top is already stdio - pop self (duplicate) and use original */ | |
2637 | PerlIO_pop(aTHX_ f); | |
2638 | return 0; | |
2639 | } else { | |
2640 | int fd = PerlIO_fileno(n); | |
2641 | char tmode[8]; | |
2642 | FILE *stdio; | |
81428673 | 2643 | if (fd >= 0 && (stdio = PerlSIO_fdopen(fd, |
1fd8f4ce NIS |
2644 | mode = PerlIOStdio_mode(mode, tmode)))) { |
2645 | PerlIOSelf(f, PerlIOStdio)->stdio = stdio; | |
2646 | /* We never call down so do any pending stuff now */ | |
2647 | PerlIO_flush(PerlIONext(f)); | |
81428673 | 2648 | } |
1fd8f4ce NIS |
2649 | else { |
2650 | return -1; | |
2651 | } | |
2652 | } | |
14a5cf38 | 2653 | } |
2dc2558e | 2654 | return PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
4b803d04 NIS |
2655 | } |
2656 | ||
22569500 | 2657 | |
9e353e3b | 2658 | PerlIO * |
4b069b44 | 2659 | PerlIO_importFILE(FILE *stdio, const char *mode) |
9e353e3b | 2660 | { |
14a5cf38 JH |
2661 | dTHX; |
2662 | PerlIO *f = NULL; | |
2663 | if (stdio) { | |
22569500 | 2664 | PerlIOStdio *s; |
4b069b44 NIS |
2665 | if (!mode || !*mode) { |
2666 | /* We need to probe to see how we can open the stream | |
2667 | so start with read/write and then try write and read | |
2668 | we dup() so that we can fclose without loosing the fd. | |
2669 | ||
2670 | Note that the errno value set by a failing fdopen | |
2671 | varies between stdio implementations. | |
2672 | */ | |
2673 | int fd = PerlLIO_dup(fileno(stdio)); | |
a33cf58c | 2674 | FILE *f2 = PerlSIO_fdopen(fd, (mode = "r+")); |
4b069b44 | 2675 | if (!f2) { |
a33cf58c | 2676 | f2 = PerlSIO_fdopen(fd, (mode = "w")); |
4b069b44 NIS |
2677 | } |
2678 | if (!f2) { | |
a33cf58c | 2679 | f2 = PerlSIO_fdopen(fd, (mode = "r")); |
4b069b44 NIS |
2680 | } |
2681 | if (!f2) { | |
2682 | /* Don't seem to be able to open */ | |
2683 | PerlLIO_close(fd); | |
2684 | return f; | |
2685 | } | |
2686 | fclose(f2); | |
22569500 | 2687 | } |
a33cf58c NIS |
2688 | if ((f = PerlIO_push(aTHX_(f = PerlIO_allocate(aTHX)), &PerlIO_stdio, mode, Nullsv))) { |
2689 | s = PerlIOSelf(f, PerlIOStdio); | |
2690 | s->stdio = stdio; | |
2691 | } | |
14a5cf38 JH |
2692 | } |
2693 | return f; | |
9e353e3b NIS |
2694 | } |
2695 | ||
2696 | PerlIO * | |
14a5cf38 JH |
2697 | PerlIOStdio_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
2698 | IV n, const char *mode, int fd, int imode, | |
2699 | int perm, PerlIO *f, int narg, SV **args) | |
2700 | { | |
2701 | char tmode[8]; | |
d9dac8cd | 2702 | if (PerlIOValid(f)) { |
14a5cf38 JH |
2703 | char *path = SvPV_nolen(*args); |
2704 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
1751d015 NIS |
2705 | FILE *stdio; |
2706 | PerlIOUnix_refcnt_dec(fileno(s->stdio)); | |
2707 | stdio = PerlSIO_freopen(path, (mode = PerlIOStdio_mode(mode, tmode)), | |
14a5cf38 JH |
2708 | s->stdio); |
2709 | if (!s->stdio) | |
2710 | return NULL; | |
2711 | s->stdio = stdio; | |
1751d015 | 2712 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); |
14a5cf38 JH |
2713 | return f; |
2714 | } | |
2715 | else { | |
2716 | if (narg > 0) { | |
2717 | char *path = SvPV_nolen(*args); | |
2718 | if (*mode == '#') { | |
2719 | mode++; | |
2720 | fd = PerlLIO_open3(path, imode, perm); | |
2721 | } | |
2722 | else { | |
2723 | FILE *stdio = PerlSIO_fopen(path, mode); | |
2724 | if (stdio) { | |
d9dac8cd NIS |
2725 | PerlIOStdio *s; |
2726 | if (!f) { | |
2727 | f = PerlIO_allocate(aTHX); | |
2728 | } | |
a33cf58c | 2729 | if ((f = PerlIO_push(aTHX_ f, self, |
14a5cf38 | 2730 | (mode = PerlIOStdio_mode(mode, tmode)), |
a33cf58c NIS |
2731 | PerlIOArg))) { |
2732 | s = PerlIOSelf(f, PerlIOStdio); | |
2733 | s->stdio = stdio; | |
2734 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); | |
2735 | } | |
602c3c4b NIS |
2736 | return f; |
2737 | } | |
2738 | else { | |
2739 | return NULL; | |
14a5cf38 | 2740 | } |
14a5cf38 JH |
2741 | } |
2742 | } | |
2743 | if (fd >= 0) { | |
2744 | FILE *stdio = NULL; | |
2745 | int init = 0; | |
2746 | if (*mode == 'I') { | |
2747 | init = 1; | |
2748 | mode++; | |
2749 | } | |
2750 | if (init) { | |
2751 | switch (fd) { | |
2752 | case 0: | |
2753 | stdio = PerlSIO_stdin; | |
2754 | break; | |
2755 | case 1: | |
2756 | stdio = PerlSIO_stdout; | |
2757 | break; | |
2758 | case 2: | |
2759 | stdio = PerlSIO_stderr; | |
2760 | break; | |
2761 | } | |
2762 | } | |
2763 | else { | |
2764 | stdio = PerlSIO_fdopen(fd, mode = | |
2765 | PerlIOStdio_mode(mode, tmode)); | |
2766 | } | |
2767 | if (stdio) { | |
d9dac8cd NIS |
2768 | PerlIOStdio *s; |
2769 | if (!f) { | |
2770 | f = PerlIO_allocate(aTHX); | |
2771 | } | |
a33cf58c NIS |
2772 | if ((f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg))) { |
2773 | s = PerlIOSelf(f, PerlIOStdio); | |
2774 | s->stdio = stdio; | |
2775 | PerlIOUnix_refcnt_inc(fileno(s->stdio)); | |
2776 | } | |
14a5cf38 JH |
2777 | return f; |
2778 | } | |
2779 | } | |
2780 | } | |
ee518936 | 2781 | return NULL; |
9e353e3b NIS |
2782 | } |
2783 | ||
1751d015 | 2784 | PerlIO * |
ecdeb87c | 2785 | PerlIOStdio_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
1751d015 NIS |
2786 | { |
2787 | /* This assumes no layers underneath - which is what | |
2788 | happens, but is not how I remember it. NI-S 2001/10/16 | |
2789 | */ | |
ecdeb87c | 2790 | if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) { |
694c95cf | 2791 | FILE *stdio = PerlIOSelf(o, PerlIOStdio)->stdio; |
9217ff3f NIS |
2792 | int fd = fileno(stdio); |
2793 | char mode[8]; | |
ecdeb87c | 2794 | if (flags & PERLIO_DUP_FD) { |
9217ff3f NIS |
2795 | int dfd = PerlLIO_dup(fileno(stdio)); |
2796 | if (dfd >= 0) { | |
2797 | stdio = PerlSIO_fdopen(dfd, PerlIO_modestr(o,mode)); | |
2798 | goto set_this; | |
ecdeb87c NIS |
2799 | } |
2800 | else { | |
2801 | /* FIXME: To avoid messy error recovery if dup fails | |
2802 | re-use the existing stdio as though flag was not set | |
2803 | */ | |
2804 | } | |
2805 | } | |
9217ff3f NIS |
2806 | stdio = PerlSIO_fdopen(fd, PerlIO_modestr(o,mode)); |
2807 | set_this: | |
694c95cf JH |
2808 | PerlIOSelf(f, PerlIOStdio)->stdio = stdio; |
2809 | PerlIOUnix_refcnt_inc(fileno(stdio)); | |
1751d015 NIS |
2810 | } |
2811 | return f; | |
2812 | } | |
2813 | ||
0d7a5398 NIS |
2814 | static int |
2815 | PerlIOStdio_invalidate_fileno(pTHX_ FILE *f) | |
2816 | { | |
2817 | /* XXX this could use PerlIO_canset_fileno() and | |
37725cdc | 2818 | * PerlIO_set_fileno() support from Configure |
0d7a5398 NIS |
2819 | */ |
2820 | # if defined(__GLIBC__) | |
2821 | /* There may be a better way for GLIBC: | |
37725cdc | 2822 | - libio.h defines a flag to not close() on cleanup |
0d7a5398 NIS |
2823 | */ |
2824 | f->_fileno = -1; | |
2825 | return 1; | |
2826 | # elif defined(__sun__) | |
2827 | # if defined(_LP64) | |
2828 | /* On solaris, if _LP64 is defined, the FILE structure is this: | |
2829 | * | |
2830 | * struct FILE { | |
2831 | * long __pad[16]; | |
2832 | * }; | |
2833 | * | |
37725cdc | 2834 | * It turns out that the fd is stored in the top 32 bits of |
0d7a5398 NIS |
2835 | * file->__pad[4]. The lower 32 bits contain flags. file->pad[5] appears |
2836 | * to contain a pointer or offset into another structure. All the | |
2837 | * remaining fields are zero. | |
2838 | * | |
2839 | * We set the top bits to -1 (0xFFFFFFFF). | |
2840 | */ | |
2841 | f->__pad[4] |= 0xffffffff00000000L; | |
2842 | assert(fileno(f) == 0xffffffff); | |
2843 | # else /* !defined(_LP64) */ | |
37725cdc NIS |
2844 | /* _file is just a unsigned char :-( |
2845 | Not clear why we dup() rather than using -1 | |
2846 | even if that would be treated as 0xFF - so will | |
0d7a5398 NIS |
2847 | a dup fail ... |
2848 | */ | |
673c27c1 | 2849 | f->_file = PerlLIO_dup(fileno(f)); |
0d7a5398 NIS |
2850 | # endif /* defined(_LP64) */ |
2851 | return 1; | |
2852 | # elif defined(__hpux) | |
2853 | f->__fileH = 0xff; | |
2854 | f->__fileL = 0xff; | |
2855 | return 1; | |
9837d373 | 2856 | /* Next one ->_file seems to be a reasonable fallback, i.e. if |
37725cdc | 2857 | your platform does not have special entry try this one. |
9837d373 NIS |
2858 | [For OSF only have confirmation for Tru64 (alpha) |
2859 | but assume other OSFs will be similar.] | |
37725cdc | 2860 | */ |
9837d373 | 2861 | # elif defined(_AIX) || defined(__osf__) || defined(__irix__) |
0d7a5398 NIS |
2862 | f->_file = -1; |
2863 | return 1; | |
2864 | # elif defined(__FreeBSD__) | |
2865 | /* There may be a better way on FreeBSD: | |
37725cdc NIS |
2866 | - we could insert a dummy func in the _close function entry |
2867 | f->_close = (int (*)(void *)) dummy_close; | |
0d7a5398 NIS |
2868 | */ |
2869 | f->_file = -1; | |
2870 | return 1; | |
2871 | # elif defined(__CYGWIN__) | |
2872 | /* There may be a better way on CYGWIN: | |
37725cdc NIS |
2873 | - we could insert a dummy func in the _close function entry |
2874 | f->_close = (int (*)(void *)) dummy_close; | |
0d7a5398 NIS |
2875 | */ |
2876 | f->_file = -1; | |
2877 | return 1; | |
2878 | # elif defined(WIN32) | |
2879 | # if defined(__BORLANDC__) | |
2880 | f->fd = PerlLIO_dup(fileno(f)); | |
b475b3e6 JH |
2881 | # elif defined(UNDER_CE) |
2882 | /* WIN_CE does not have access to FILE internals, it hardly has FILE | |
2883 | structure at all | |
2884 | */ | |
0d7a5398 NIS |
2885 | # else |
2886 | f->_file = -1; | |
2887 | # endif | |
2888 | return 1; | |
2889 | # else | |
2890 | #if 0 | |
37725cdc | 2891 | /* Sarathy's code did this - we fall back to a dup/dup2 hack |
0d7a5398 | 2892 | (which isn't thread safe) instead |
37725cdc | 2893 | */ |
0d7a5398 NIS |
2894 | # error "Don't know how to set FILE.fileno on your platform" |
2895 | #endif | |
2896 | return 0; | |
2897 | # endif | |
2898 | } | |
2899 | ||
1751d015 | 2900 | IV |
f62ce20a | 2901 | PerlIOStdio_close(pTHX_ PerlIO *f) |
1751d015 | 2902 | { |
1751d015 | 2903 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
439ba545 NIS |
2904 | if (!stdio) { |
2905 | errno = EBADF; | |
2906 | return -1; | |
2907 | } | |
9217ff3f NIS |
2908 | else { |
2909 | int fd = fileno(stdio); | |
0d7a5398 NIS |
2910 | int socksfd = 0; |
2911 | int invalidate = 0; | |
bbfd922f | 2912 | IV result = 0; |
0d7a5398 NIS |
2913 | int saveerr = 0; |
2914 | int dupfd = 0; | |
2915 | #ifdef SOCKS5_VERSION_NAME | |
37725cdc NIS |
2916 | /* Socks lib overrides close() but stdio isn't linked to |
2917 | that library (though we are) - so we must call close() | |
2918 | on sockets on stdio's behalf. | |
2919 | */ | |
0d7a5398 NIS |
2920 | int optval; |
2921 | Sock_size_t optlen = sizeof(int); | |
2922 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *) &optval, &optlen) == 0) { | |
2923 | socksfd = 1; | |
37725cdc | 2924 | invalidate = 1; |
0d7a5398 NIS |
2925 | } |
2926 | #endif | |
9217ff3f NIS |
2927 | if (PerlIOUnix_refcnt_dec(fd) > 0) { |
2928 | /* File descriptor still in use */ | |
0d7a5398 NIS |
2929 | invalidate = 1; |
2930 | socksfd = 0; | |
37725cdc | 2931 | } |
0d7a5398 | 2932 | if (invalidate) { |
37725cdc NIS |
2933 | /* For STD* handles don't close the stdio at all |
2934 | this is because we have shared the FILE * too | |
0d7a5398 NIS |
2935 | */ |
2936 | if (stdio == stdin) { | |
2937 | /* Some stdios are buggy fflush-ing inputs */ | |
2938 | return 0; | |
2939 | } | |
2940 | else if (stdio == stdout || stdio == stderr) { | |
9217ff3f NIS |
2941 | return PerlIO_flush(f); |
2942 | } | |
37725cdc NIS |
2943 | /* Tricky - must fclose(stdio) to free memory but not close(fd) |
2944 | Use Sarathy's trick from maint-5.6 to invalidate the | |
2945 | fileno slot of the FILE * | |
2946 | */ | |
bbfd922f | 2947 | result = PerlIO_flush(f); |
0d7a5398 NIS |
2948 | saveerr = errno; |
2949 | if (!(invalidate = PerlIOStdio_invalidate_fileno(aTHX_ stdio))) { | |
9217ff3f NIS |
2950 | dupfd = PerlLIO_dup(fd); |
2951 | } | |
37725cdc | 2952 | } |
0d7a5398 | 2953 | result = PerlSIO_fclose(stdio); |
37725cdc NIS |
2954 | /* We treat error from stdio as success if we invalidated |
2955 | errno may NOT be expected EBADF | |
e8529473 NIS |
2956 | */ |
2957 | if (invalidate && result != 0) { | |
0d7a5398 NIS |
2958 | errno = saveerr; |
2959 | result = 0; | |
37725cdc | 2960 | } |
0d7a5398 NIS |
2961 | if (socksfd) { |
2962 | /* in SOCKS case let close() determine return value */ | |
2963 | result = close(fd); | |
2964 | } | |
2965 | if (dupfd) { | |
2966 | PerlLIO_dup2(dupfd,fd); | |
8a521f28 | 2967 | PerlLIO_close(dupfd); |
9217ff3f NIS |
2968 | } |
2969 | return result; | |
37725cdc | 2970 | } |
1751d015 NIS |
2971 | } |
2972 | ||
9e353e3b | 2973 | SSize_t |
f62ce20a | 2974 | PerlIOStdio_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
9e353e3b | 2975 | { |
14a5cf38 JH |
2976 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; |
2977 | SSize_t got = 0; | |
4d948241 NIS |
2978 | for (;;) { |
2979 | if (count == 1) { | |
2980 | STDCHAR *buf = (STDCHAR *) vbuf; | |
2981 | /* | |
2982 | * Perl is expecting PerlIO_getc() to fill the buffer Linux's | |
2983 | * stdio does not do that for fread() | |
2984 | */ | |
2985 | int ch = PerlSIO_fgetc(s); | |
2986 | if (ch != EOF) { | |
2987 | *buf = ch; | |
2988 | got = 1; | |
2989 | } | |
14a5cf38 | 2990 | } |
4d948241 NIS |
2991 | else |
2992 | got = PerlSIO_fread(vbuf, 1, count, s); | |
42a7a32f | 2993 | if (got >= 0 || errno != EINTR) |
4d948241 NIS |
2994 | break; |
2995 | PERL_ASYNC_CHECK(); | |
42a7a32f | 2996 | SETERRNO(0,0); /* just in case */ |
14a5cf38 | 2997 | } |
14a5cf38 | 2998 | return got; |
9e353e3b NIS |
2999 | } |
3000 | ||
3001 | SSize_t | |
f62ce20a | 3002 | PerlIOStdio_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 3003 | { |
14a5cf38 | 3004 | SSize_t unread = 0; |
93679785 NIS |
3005 | FILE *s = PerlIOSelf(f, PerlIOStdio)->stdio; |
3006 | ||
313e59c8 | 3007 | #ifdef STDIO_BUFFER_WRITABLE |
9f7cd136 | 3008 | if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { |
93679785 NIS |
3009 | STDCHAR *buf = ((STDCHAR *) vbuf) + count; |
3010 | STDCHAR *base = PerlIO_get_base(f); | |
3011 | SSize_t cnt = PerlIO_get_cnt(f); | |
3012 | STDCHAR *ptr = PerlIO_get_ptr(f); | |
3013 | SSize_t avail = ptr - base; | |
3014 | if (avail > 0) { | |
3015 | if (avail > count) { | |
3016 | avail = count; | |
3017 | } | |
3018 | ptr -= avail; | |
3019 | Move(buf-avail,ptr,avail,STDCHAR); | |
3020 | count -= avail; | |
3021 | unread += avail; | |
3022 | PerlIO_set_ptrcnt(f,ptr,cnt+avail); | |
9f7cd136 NIS |
3023 | if (PerlSIO_feof(s) && unread >= 0) |
3024 | PerlSIO_clearerr(s); | |
3025 | } | |
3026 | } | |
313e59c8 NIS |
3027 | else |
3028 | #endif | |
3029 | if (PerlIO_has_cntptr(f)) { | |
9f7cd136 NIS |
3030 | /* We can get pointer to buffer but not its base |
3031 | Do ungetc() but check chars are ending up in the | |
3032 | buffer | |
3033 | */ | |
3034 | STDCHAR *eptr = (STDCHAR*)PerlSIO_get_ptr(s); | |
3035 | STDCHAR *buf = ((STDCHAR *) vbuf) + count; | |
3036 | while (count > 0) { | |
3037 | int ch = *--buf & 0xFF; | |
3038 | if (ungetc(ch,s) != ch) { | |
3039 | /* ungetc did not work */ | |
3040 | break; | |
3041 | } | |
3042 | if ((STDCHAR*)PerlSIO_get_ptr(s) != --eptr || ((*eptr & 0xFF) != ch)) { | |
3043 | /* Did not change pointer as expected */ | |
3044 | fgetc(s); /* get char back again */ | |
3045 | break; | |
3046 | } | |
3047 | /* It worked ! */ | |
3048 | count--; | |
3049 | unread++; | |
93679785 NIS |
3050 | } |
3051 | } | |
3052 | ||
3053 | if (count > 0) { | |
3054 | unread += PerlIOBase_unread(aTHX_ f, vbuf, count); | |
14a5cf38 JH |
3055 | } |
3056 | return unread; | |
9e353e3b NIS |
3057 | } |
3058 | ||
3059 | SSize_t | |
f62ce20a | 3060 | PerlIOStdio_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
9e353e3b | 3061 | { |
4d948241 NIS |
3062 | SSize_t got; |
3063 | for (;;) { | |
3064 | got = PerlSIO_fwrite(vbuf, 1, count, | |
3065 | PerlIOSelf(f, PerlIOStdio)->stdio); | |
42a7a32f | 3066 | if (got >= 0 || errno != EINTR) |
4d948241 NIS |
3067 | break; |
3068 | PERL_ASYNC_CHECK(); | |
42a7a32f | 3069 | SETERRNO(0,0); /* just in case */ |
4d948241 NIS |
3070 | } |
3071 | return got; | |
9e353e3b NIS |
3072 | } |
3073 | ||
3074 | IV | |
f62ce20a | 3075 | PerlIOStdio_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 3076 | { |
14a5cf38 JH |
3077 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3078 | return PerlSIO_fseek(stdio, offset, whence); | |
9e353e3b NIS |
3079 | } |
3080 | ||
3081 | Off_t | |
f62ce20a | 3082 | PerlIOStdio_tell(pTHX_ PerlIO *f) |
9e353e3b | 3083 | { |
14a5cf38 JH |
3084 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3085 | return PerlSIO_ftell(stdio); | |
9e353e3b NIS |
3086 | } |
3087 | ||
3088 | IV | |
f62ce20a | 3089 | PerlIOStdio_flush(pTHX_ PerlIO *f) |
9e353e3b | 3090 | { |
14a5cf38 JH |
3091 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3092 | if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) { | |
3093 | return PerlSIO_fflush(stdio); | |
3094 | } | |
3095 | else { | |
88b61e10 | 3096 | #if 0 |
14a5cf38 JH |
3097 | /* |
3098 | * FIXME: This discards ungetc() and pre-read stuff which is not | |
71200d45 | 3099 | * right if this is just a "sync" from a layer above Suspect right |
14a5cf38 | 3100 | * design is to do _this_ but not have layer above flush this |
71200d45 | 3101 | * layer read-to-read |
14a5cf38 JH |
3102 | */ |
3103 | /* | |
71200d45 | 3104 | * Not writeable - sync by attempting a seek |
14a5cf38 JH |
3105 | */ |
3106 | int err = errno; | |
3107 | if (PerlSIO_fseek(stdio, (Off_t) 0, SEEK_CUR) != 0) | |
3108 | errno = err; | |
88b61e10 | 3109 | #endif |
14a5cf38 JH |
3110 | } |
3111 | return 0; | |
9e353e3b NIS |
3112 | } |
3113 | ||
3114 | IV | |
f62ce20a | 3115 | PerlIOStdio_eof(pTHX_ PerlIO *f) |
9e353e3b | 3116 | { |
14a5cf38 | 3117 | return PerlSIO_feof(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b NIS |
3118 | } |
3119 | ||
3120 | IV | |
f62ce20a | 3121 | PerlIOStdio_error(pTHX_ PerlIO *f) |
9e353e3b | 3122 | { |
1f376c7c JH |
3123 | IV stdio_error = PerlSIO_ferror(PerlIOSelf(f, PerlIOStdio)->stdio); |
3124 | /* Some stdio implementations do not mind e.g. trying to output | |
3125 | * to a write-only filehandle, or vice versa. Therefore we will | |
3126 | * try both the stdio way and the perlio way. */ | |
3127 | IV iobase_error = PerlIOValid(f) ? | |
3128 | ((PerlIOBase(f)->flags & PERLIO_F_ERROR) != 0) : 0; | |
3129 | return stdio_error || iobase_error; | |
9e353e3b NIS |
3130 | } |
3131 | ||
3132 | void | |
f62ce20a | 3133 | PerlIOStdio_clearerr(pTHX_ PerlIO *f) |
9e353e3b | 3134 | { |
14a5cf38 | 3135 | PerlSIO_clearerr(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b NIS |
3136 | } |
3137 | ||
3138 | void | |
f62ce20a | 3139 | PerlIOStdio_setlinebuf(pTHX_ PerlIO *f) |
9e353e3b NIS |
3140 | { |
3141 | #ifdef HAS_SETLINEBUF | |
14a5cf38 | 3142 | PerlSIO_setlinebuf(PerlIOSelf(f, PerlIOStdio)->stdio); |
9e353e3b | 3143 | #else |
14a5cf38 | 3144 | PerlSIO_setvbuf(PerlIOSelf(f, PerlIOStdio)->stdio, Nullch, _IOLBF, 0); |
9e353e3b NIS |
3145 | #endif |
3146 | } | |
3147 | ||
3148 | #ifdef FILE_base | |
3149 | STDCHAR * | |
f62ce20a | 3150 | PerlIOStdio_get_base(pTHX_ PerlIO *f) |
9e353e3b | 3151 | { |
14a5cf38 | 3152 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
cc00df79 | 3153 | return (STDCHAR*)PerlSIO_get_base(stdio); |
9e353e3b NIS |
3154 | } |
3155 | ||
3156 | Size_t | |
f62ce20a | 3157 | PerlIOStdio_get_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 3158 | { |
14a5cf38 JH |
3159 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3160 | return PerlSIO_get_bufsiz(stdio); | |
9e353e3b NIS |
3161 | } |
3162 | #endif | |
3163 | ||
3164 | #ifdef USE_STDIO_PTR | |
3165 | STDCHAR * | |
f62ce20a | 3166 | PerlIOStdio_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 3167 | { |
14a5cf38 | 3168 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
cc00df79 | 3169 | return (STDCHAR*)PerlSIO_get_ptr(stdio); |
9e353e3b NIS |
3170 | } |
3171 | ||
3172 | SSize_t | |
f62ce20a | 3173 | PerlIOStdio_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 3174 | { |
14a5cf38 JH |
3175 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
3176 | return PerlSIO_get_cnt(stdio); | |
9e353e3b NIS |
3177 | } |
3178 | ||
3179 | void | |
f62ce20a | 3180 | PerlIOStdio_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 3181 | { |
14a5cf38 | 3182 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
14a5cf38 | 3183 | if (ptr != NULL) { |
9e353e3b | 3184 | #ifdef STDIO_PTR_LVALUE |
22569500 | 3185 | PerlSIO_set_ptr(stdio, (void*)ptr); /* LHS STDCHAR* cast non-portable */ |
9e353e3b | 3186 | #ifdef STDIO_PTR_LVAL_SETS_CNT |
14a5cf38 | 3187 | if (PerlSIO_get_cnt(stdio) != (cnt)) { |
14a5cf38 JH |
3188 | assert(PerlSIO_get_cnt(stdio) == (cnt)); |
3189 | } | |
9e353e3b NIS |
3190 | #endif |
3191 | #if (!defined(STDIO_PTR_LVAL_NOCHANGE_CNT)) | |
14a5cf38 | 3192 | /* |
71200d45 | 3193 | * Setting ptr _does_ change cnt - we are done |
14a5cf38 JH |
3194 | */ |
3195 | return; | |
9e353e3b | 3196 | #endif |
22569500 | 3197 | #else /* STDIO_PTR_LVALUE */ |
14a5cf38 | 3198 | PerlProc_abort(); |
22569500 | 3199 | #endif /* STDIO_PTR_LVALUE */ |
14a5cf38 JH |
3200 | } |
3201 | /* | |
71200d45 | 3202 | * Now (or only) set cnt |
14a5cf38 | 3203 | */ |
9e353e3b | 3204 | #ifdef STDIO_CNT_LVALUE |
14a5cf38 | 3205 | PerlSIO_set_cnt(stdio, cnt); |
22569500 | 3206 | #else /* STDIO_CNT_LVALUE */ |
9e353e3b | 3207 | #if (defined(STDIO_PTR_LVALUE) && defined(STDIO_PTR_LVAL_SETS_CNT)) |
14a5cf38 JH |
3208 | PerlSIO_set_ptr(stdio, |
3209 | PerlSIO_get_ptr(stdio) + (PerlSIO_get_cnt(stdio) - | |
3210 | cnt)); | |
22569500 | 3211 | #else /* STDIO_PTR_LVAL_SETS_CNT */ |
14a5cf38 | 3212 | PerlProc_abort(); |
22569500 NIS |
3213 | #endif /* STDIO_PTR_LVAL_SETS_CNT */ |
3214 | #endif /* STDIO_CNT_LVALUE */ | |
9e353e3b NIS |
3215 | } |
3216 | ||
93679785 | 3217 | |
9e353e3b NIS |
3218 | #endif |
3219 | ||
93679785 NIS |
3220 | IV |
3221 | PerlIOStdio_fill(pTHX_ PerlIO *f) | |
3222 | { | |
3223 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; | |
3224 | int c; | |
3225 | /* | |
3226 | * fflush()ing read-only streams can cause trouble on some stdio-s | |
3227 | */ | |
3228 | if ((PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) { | |
3229 | if (PerlSIO_fflush(stdio) != 0) | |
3230 | return EOF; | |
3231 | } | |
3232 | c = PerlSIO_fgetc(stdio); | |
3233 | if (c == EOF) | |
3234 | return EOF; | |
3235 | ||
3236 | #if (defined(STDIO_PTR_LVALUE) && (defined(STDIO_CNT_LVALUE) || defined(STDIO_PTR_LVAL_SETS_CNT))) | |
313e59c8 NIS |
3237 | |
3238 | #ifdef STDIO_BUFFER_WRITABLE | |
9f7cd136 | 3239 | if (PerlIO_fast_gets(f) && PerlIO_has_base(f)) { |
93679785 NIS |
3240 | /* Fake ungetc() to the real buffer in case system's ungetc |
3241 | goes elsewhere | |
3242 | */ | |
3243 | STDCHAR *base = (STDCHAR*)PerlSIO_get_base(stdio); | |
3244 | SSize_t cnt = PerlSIO_get_cnt(stdio); | |
3245 | STDCHAR *ptr = (STDCHAR*)PerlSIO_get_ptr(stdio); | |
3246 | if (ptr == base+1) { | |
3247 | *--ptr = (STDCHAR) c; | |
3248 | PerlIOStdio_set_ptrcnt(aTHX_ f,ptr,cnt+1); | |
3249 | if (PerlSIO_feof(stdio)) | |
3250 | PerlSIO_clearerr(stdio); | |
3251 | return 0; | |
3252 | } | |
3253 | } | |
313e59c8 NIS |
3254 | else |
3255 | #endif | |
3256 | if (PerlIO_has_cntptr(f)) { | |
9f7cd136 NIS |
3257 | STDCHAR ch = c; |
3258 | if (PerlIOStdio_unread(aTHX_ f,&ch,1) == 1) { | |
3259 | return 0; | |
3260 | } | |
3261 | } | |
93679785 NIS |
3262 | #endif |
3263 | ||
3264 | #if defined(VMS) | |
3265 | /* An ungetc()d char is handled separately from the regular | |
3266 | * buffer, so we stuff it in the buffer ourselves. | |
3267 | * Should never get called as should hit code above | |
3268 | */ | |
bad9695d NIS |
3269 | *(--((*stdio)->_ptr)) = (unsigned char) c; |
3270 | (*stdio)->_cnt++; | |
93679785 NIS |
3271 | #else |
3272 | /* If buffer snoop scheme above fails fall back to | |
9f7cd136 | 3273 | using ungetc(). |
93679785 NIS |
3274 | */ |
3275 | if (PerlSIO_ungetc(c, stdio) != c) | |
3276 | return EOF; | |
3277 | #endif | |
3278 | return 0; | |
3279 | } | |
3280 | ||
3281 | ||
3282 | ||
9e353e3b | 3283 | PerlIO_funcs PerlIO_stdio = { |
2dc2558e | 3284 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
3285 | "stdio", |
3286 | sizeof(PerlIOStdio), | |
86e05cf2 | 3287 | PERLIO_K_BUFFERED|PERLIO_K_RAW, |
1fd8f4ce | 3288 | PerlIOStdio_pushed, |
44798d05 | 3289 | PerlIOBase_popped, |
14a5cf38 | 3290 | PerlIOStdio_open, |
86e05cf2 | 3291 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
3292 | NULL, |
3293 | PerlIOStdio_fileno, | |
71200d45 | 3294 | PerlIOStdio_dup, |
14a5cf38 JH |
3295 | PerlIOStdio_read, |
3296 | PerlIOStdio_unread, | |
3297 | PerlIOStdio_write, | |
3298 | PerlIOStdio_seek, | |
3299 | PerlIOStdio_tell, | |
3300 | PerlIOStdio_close, | |
3301 | PerlIOStdio_flush, | |
3302 | PerlIOStdio_fill, | |
3303 | PerlIOStdio_eof, | |
3304 | PerlIOStdio_error, | |
3305 | PerlIOStdio_clearerr, | |
3306 | PerlIOStdio_setlinebuf, | |
9e353e3b | 3307 | #ifdef FILE_base |
14a5cf38 JH |
3308 | PerlIOStdio_get_base, |
3309 | PerlIOStdio_get_bufsiz, | |
9e353e3b | 3310 | #else |
14a5cf38 JH |
3311 | NULL, |
3312 | NULL, | |
9e353e3b NIS |
3313 | #endif |
3314 | #ifdef USE_STDIO_PTR | |
14a5cf38 JH |
3315 | PerlIOStdio_get_ptr, |
3316 | PerlIOStdio_get_cnt, | |
15b61c98 | 3317 | # if defined(HAS_FAST_STDIO) && defined(USE_FAST_STDIO) |
79ed0f43 | 3318 | PerlIOStdio_set_ptrcnt, |
15b61c98 JH |
3319 | # else |
3320 | NULL, | |
3321 | # endif /* HAS_FAST_STDIO && USE_FAST_STDIO */ | |
3322 | #else | |
3323 | NULL, | |
14a5cf38 JH |
3324 | NULL, |
3325 | NULL, | |
15b61c98 | 3326 | #endif /* USE_STDIO_PTR */ |
9e353e3b NIS |
3327 | }; |
3328 | ||
b9d6bf13 JH |
3329 | /* Note that calls to PerlIO_exportFILE() are reversed using |
3330 | * PerlIO_releaseFILE(), not importFILE. */ | |
9e353e3b | 3331 | FILE * |
81428673 | 3332 | PerlIO_exportFILE(PerlIO * f, const char *mode) |
9e353e3b | 3333 | { |
e87a358a | 3334 | dTHX; |
81428673 NIS |
3335 | FILE *stdio = NULL; |
3336 | if (PerlIOValid(f)) { | |
3337 | char buf[8]; | |
3338 | PerlIO_flush(f); | |
3339 | if (!mode || !*mode) { | |
3340 | mode = PerlIO_modestr(f, buf); | |
3341 | } | |
3342 | stdio = PerlSIO_fdopen(PerlIO_fileno(f), mode); | |
3343 | if (stdio) { | |
3344 | PerlIOl *l = *f; | |
9f75cc58 | 3345 | PerlIO *f2; |
81428673 NIS |
3346 | /* De-link any lower layers so new :stdio sticks */ |
3347 | *f = NULL; | |
9f75cc58 RGS |
3348 | if ((f2 = PerlIO_push(aTHX_ f, &PerlIO_stdio, buf, Nullsv))) { |
3349 | PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio); | |
81428673 NIS |
3350 | s->stdio = stdio; |
3351 | /* Link previous lower layers under new one */ | |
3352 | *PerlIONext(f) = l; | |
3353 | } | |
3354 | else { | |
3355 | /* restore layers list */ | |
3356 | *f = l; | |
3357 | } | |
a33cf58c | 3358 | } |
14a5cf38 JH |
3359 | } |
3360 | return stdio; | |
9e353e3b NIS |
3361 | } |
3362 | ||
81428673 | 3363 | |
9e353e3b NIS |
3364 | FILE * |
3365 | PerlIO_findFILE(PerlIO *f) | |
3366 | { | |
14a5cf38 JH |
3367 | PerlIOl *l = *f; |
3368 | while (l) { | |
3369 | if (l->tab == &PerlIO_stdio) { | |
3370 | PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); | |
3371 | return s->stdio; | |
3372 | } | |
3373 | l = *PerlIONext(&l); | |
f7e7eb72 | 3374 | } |
4b069b44 NIS |
3375 | /* Uses fallback "mode" via PerlIO_modestr() in PerlIO_exportFILE */ |
3376 | return PerlIO_exportFILE(f, Nullch); | |
9e353e3b NIS |
3377 | } |
3378 | ||
b9d6bf13 | 3379 | /* Use this to reverse PerlIO_exportFILE calls. */ |
9e353e3b NIS |
3380 | void |
3381 | PerlIO_releaseFILE(PerlIO *p, FILE *f) | |
3382 | { | |
22569500 NIS |
3383 | PerlIOl *l; |
3384 | while ((l = *p)) { | |
3385 | if (l->tab == &PerlIO_stdio) { | |
3386 | PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); | |
3387 | if (s->stdio == f) { | |
3388 | dTHX; | |
3389 | PerlIO_pop(aTHX_ p); | |
3390 | return; | |
3391 | } | |
3392 | } | |
3393 | p = PerlIONext(p); | |
3394 | } | |
3395 | return; | |
9e353e3b NIS |
3396 | } |
3397 | ||
3398 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 3399 | /* |
71200d45 | 3400 | * perlio buffer layer |
14a5cf38 | 3401 | */ |
9e353e3b | 3402 | |
5e2ab84b | 3403 | IV |
2dc2558e | 3404 | PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
5e2ab84b | 3405 | { |
14a5cf38 JH |
3406 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3407 | int fd = PerlIO_fileno(f); | |
14a5cf38 JH |
3408 | if (fd >= 0 && PerlLIO_isatty(fd)) { |
3409 | PerlIOBase(f)->flags |= PERLIO_F_LINEBUF | PERLIO_F_TTY; | |
3410 | } | |
4b069b44 NIS |
3411 | if (*PerlIONext(f)) { |
3412 | Off_t posn = PerlIO_tell(PerlIONext(f)); | |
3413 | if (posn != (Off_t) - 1) { | |
3414 | b->posn = posn; | |
3415 | } | |
14a5cf38 | 3416 | } |
2dc2558e | 3417 | return PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
5e2ab84b NIS |
3418 | } |
3419 | ||
9e353e3b | 3420 | PerlIO * |
14a5cf38 JH |
3421 | PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, |
3422 | IV n, const char *mode, int fd, int imode, int perm, | |
3423 | PerlIO *f, int narg, SV **args) | |
3424 | { | |
04892f78 | 3425 | if (PerlIOValid(f)) { |
14a5cf38 | 3426 | PerlIO *next = PerlIONext(f); |
67363c0d JH |
3427 | PerlIO_funcs *tab = |
3428 | PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIOBase(next)->tab); | |
3429 | if (tab && tab->Open) | |
3430 | next = | |
3431 | (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
3432 | next, narg, args); | |
2dc2558e | 3433 | if (!next || (*PerlIOBase(f)->tab->Pushed) (aTHX_ f, mode, PerlIOArg, self) != 0) { |
14a5cf38 JH |
3434 | return NULL; |
3435 | } | |
3436 | } | |
3437 | else { | |
04892f78 | 3438 | PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm()); |
14a5cf38 JH |
3439 | int init = 0; |
3440 | if (*mode == 'I') { | |
3441 | init = 1; | |
3442 | /* | |
71200d45 | 3443 | * mode++; |
14a5cf38 JH |
3444 | */ |
3445 | } | |
67363c0d JH |
3446 | if (tab && tab->Open) |
3447 | f = (*tab->Open)(aTHX_ tab, layers, n - 1, mode, fd, imode, perm, | |
3448 | f, narg, args); | |
3449 | else | |
3450 | SETERRNO(EINVAL, LIB_INVARG); | |
14a5cf38 | 3451 | if (f) { |
22569500 | 3452 | if (PerlIO_push(aTHX_ f, self, mode, PerlIOArg) == 0) { |
b26b1ab5 NC |
3453 | /* |
3454 | * if push fails during open, open fails. close will pop us. | |
3455 | */ | |
3456 | PerlIO_close (f); | |
3457 | return NULL; | |
3458 | } else { | |
3459 | fd = PerlIO_fileno(f); | |
b26b1ab5 NC |
3460 | if (init && fd == 2) { |
3461 | /* | |
3462 | * Initial stderr is unbuffered | |
3463 | */ | |
3464 | PerlIOBase(f)->flags |= PERLIO_F_UNBUF; | |
3465 | } | |
23b84778 IZ |
3466 | #ifdef PERLIO_USING_CRLF |
3467 | # ifdef PERLIO_IS_BINMODE_FD | |
3468 | if (PERLIO_IS_BINMODE_FD(fd)) | |
5c728af0 | 3469 | PerlIO_binmode(aTHX_ f, '<'/*not used*/, O_BINARY, Nullch); |
23b84778 IZ |
3470 | else |
3471 | # endif | |
3472 | /* | |
3473 | * do something about failing setmode()? --jhi | |
3474 | */ | |
3475 | PerlLIO_setmode(fd, O_BINARY); | |
3476 | #endif | |
14a5cf38 JH |
3477 | } |
3478 | } | |
ee518936 | 3479 | } |
14a5cf38 | 3480 | return f; |
9e353e3b NIS |
3481 | } |
3482 | ||
14a5cf38 JH |
3483 | /* |
3484 | * This "flush" is akin to sfio's sync in that it handles files in either | |
71200d45 | 3485 | * read or write state |
14a5cf38 | 3486 | */ |
9e353e3b | 3487 | IV |
f62ce20a | 3488 | PerlIOBuf_flush(pTHX_ PerlIO *f) |
6f9d8c32 | 3489 | { |
14a5cf38 JH |
3490 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3491 | int code = 0; | |
04892f78 | 3492 | PerlIO *n = PerlIONext(f); |
14a5cf38 JH |
3493 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) { |
3494 | /* | |
71200d45 | 3495 | * write() the buffer |
14a5cf38 JH |
3496 | */ |
3497 | STDCHAR *buf = b->buf; | |
3498 | STDCHAR *p = buf; | |
14a5cf38 JH |
3499 | while (p < b->ptr) { |
3500 | SSize_t count = PerlIO_write(n, p, b->ptr - p); | |
3501 | if (count > 0) { | |
3502 | p += count; | |
3503 | } | |
3504 | else if (count < 0 || PerlIO_error(n)) { | |
3505 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
3506 | code = -1; | |
3507 | break; | |
3508 | } | |
3509 | } | |
3510 | b->posn += (p - buf); | |
3511 | } | |
3512 | else if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
3513 | STDCHAR *buf = PerlIO_get_base(f); | |
3514 | /* | |
71200d45 | 3515 | * Note position change |
14a5cf38 JH |
3516 | */ |
3517 | b->posn += (b->ptr - buf); | |
3518 | if (b->ptr < b->end) { | |
4b069b44 NIS |
3519 | /* We did not consume all of it - try and seek downstream to |
3520 | our logical position | |
14a5cf38 | 3521 | */ |
4b069b44 | 3522 | if (PerlIOValid(n) && PerlIO_seek(n, b->posn, SEEK_SET) == 0) { |
04892f78 NIS |
3523 | /* Reload n as some layers may pop themselves on seek */ |
3524 | b->posn = PerlIO_tell(n = PerlIONext(f)); | |
14a5cf38 | 3525 | } |
ba5c3fe9 | 3526 | else { |
4b069b44 NIS |
3527 | /* Seek failed (e.g. pipe or tty). Do NOT clear buffer or pre-read |
3528 | data is lost for good - so return saying "ok" having undone | |
3529 | the position adjust | |
3530 | */ | |
3531 | b->posn -= (b->ptr - buf); | |
ba5c3fe9 NIS |
3532 | return code; |
3533 | } | |
14a5cf38 JH |
3534 | } |
3535 | } | |
3536 | b->ptr = b->end = b->buf; | |
3537 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
04892f78 | 3538 | /* We check for Valid because of dubious decision to make PerlIO_flush(NULL) flush all */ |
04892f78 | 3539 | if (PerlIOValid(n) && PerlIO_flush(n) != 0) |
14a5cf38 JH |
3540 | code = -1; |
3541 | return code; | |
6f9d8c32 NIS |
3542 | } |
3543 | ||
06da4f11 | 3544 | IV |
f62ce20a | 3545 | PerlIOBuf_fill(pTHX_ PerlIO *f) |
06da4f11 | 3546 | { |
14a5cf38 JH |
3547 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3548 | PerlIO *n = PerlIONext(f); | |
3549 | SSize_t avail; | |
3550 | /* | |
4b069b44 NIS |
3551 | * Down-stream flush is defined not to loose read data so is harmless. |
3552 | * we would not normally be fill'ing if there was data left in anycase. | |
14a5cf38 JH |
3553 | */ |
3554 | if (PerlIO_flush(f) != 0) | |
3555 | return -1; | |
3556 | if (PerlIOBase(f)->flags & PERLIO_F_TTY) | |
f62ce20a | 3557 | PerlIOBase_flush_linebuf(aTHX); |
14a5cf38 JH |
3558 | |
3559 | if (!b->buf) | |
22569500 | 3560 | PerlIO_get_base(f); /* allocate via vtable */ |
14a5cf38 JH |
3561 | |
3562 | b->ptr = b->end = b->buf; | |
4b069b44 NIS |
3563 | |
3564 | if (!PerlIOValid(n)) { | |
3565 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
3566 | return -1; | |
3567 | } | |
3568 | ||
14a5cf38 JH |
3569 | if (PerlIO_fast_gets(n)) { |
3570 | /* | |
04892f78 | 3571 | * Layer below is also buffered. We do _NOT_ want to call its |
14a5cf38 JH |
3572 | * ->Read() because that will loop till it gets what we asked for |
3573 | * which may hang on a pipe etc. Instead take anything it has to | |
71200d45 | 3574 | * hand, or ask it to fill _once_. |
14a5cf38 JH |
3575 | */ |
3576 | avail = PerlIO_get_cnt(n); | |
3577 | if (avail <= 0) { | |
3578 | avail = PerlIO_fill(n); | |
3579 | if (avail == 0) | |
3580 | avail = PerlIO_get_cnt(n); | |
3581 | else { | |
3582 | if (!PerlIO_error(n) && PerlIO_eof(n)) | |
3583 | avail = 0; | |
3584 | } | |
3585 | } | |
3586 | if (avail > 0) { | |
3587 | STDCHAR *ptr = PerlIO_get_ptr(n); | |
3588 | SSize_t cnt = avail; | |
eb160463 | 3589 | if (avail > (SSize_t)b->bufsiz) |
14a5cf38 JH |
3590 | avail = b->bufsiz; |
3591 | Copy(ptr, b->buf, avail, STDCHAR); | |
3592 | PerlIO_set_ptrcnt(n, ptr + avail, cnt - avail); | |
3593 | } | |
3594 | } | |
3595 | else { | |
3596 | avail = PerlIO_read(n, b->ptr, b->bufsiz); | |
3597 | } | |
3598 | if (avail <= 0) { | |
3599 | if (avail == 0) | |
3600 | PerlIOBase(f)->flags |= PERLIO_F_EOF; | |
3601 | else | |
3602 | PerlIOBase(f)->flags |= PERLIO_F_ERROR; | |
3603 | return -1; | |
3604 | } | |
3605 | b->end = b->buf + avail; | |
3606 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
3607 | return 0; | |
06da4f11 NIS |
3608 | } |
3609 | ||
6f9d8c32 | 3610 | SSize_t |
f62ce20a | 3611 | PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
6f9d8c32 | 3612 | { |
14a5cf38 | 3613 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
04892f78 | 3614 | if (PerlIOValid(f)) { |
14a5cf38 JH |
3615 | if (!b->ptr) |
3616 | PerlIO_get_base(f); | |
f62ce20a | 3617 | return PerlIOBase_read(aTHX_ f, vbuf, count); |
14a5cf38 JH |
3618 | } |
3619 | return 0; | |
6f9d8c32 NIS |
3620 | } |
3621 | ||
9e353e3b | 3622 | SSize_t |
f62ce20a | 3623 | PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 3624 | { |
14a5cf38 JH |
3625 | const STDCHAR *buf = (const STDCHAR *) vbuf + count; |
3626 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3627 | SSize_t unread = 0; | |
3628 | SSize_t avail; | |
3629 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
3630 | PerlIO_flush(f); | |
3631 | if (!b->buf) | |
3632 | PerlIO_get_base(f); | |
3633 | if (b->buf) { | |
3634 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
3635 | /* | |
3636 | * Buffer is already a read buffer, we can overwrite any chars | |
71200d45 | 3637 | * which have been read back to buffer start |
14a5cf38 JH |
3638 | */ |
3639 | avail = (b->ptr - b->buf); | |
3640 | } | |
3641 | else { | |
3642 | /* | |
3643 | * Buffer is idle, set it up so whole buffer is available for | |
71200d45 | 3644 | * unread |
14a5cf38 JH |
3645 | */ |
3646 | avail = b->bufsiz; | |
3647 | b->end = b->buf + avail; | |
3648 | b->ptr = b->end; | |
3649 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
3650 | /* | |
71200d45 | 3651 | * Buffer extends _back_ from where we are now |
14a5cf38 JH |
3652 | */ |
3653 | b->posn -= b->bufsiz; | |
3654 | } | |
3655 | if (avail > (SSize_t) count) { | |
3656 | /* | |
71200d45 | 3657 | * If we have space for more than count, just move count |
14a5cf38 JH |
3658 | */ |
3659 | avail = count; | |
3660 | } | |
3661 | if (avail > 0) { | |
3662 | b->ptr -= avail; | |
3663 | buf -= avail; | |
3664 | /* | |
3665 | * In simple stdio-like ungetc() case chars will be already | |
71200d45 | 3666 | * there |
14a5cf38 JH |
3667 | */ |
3668 | if (buf != b->ptr) { | |
3669 | Copy(buf, b->ptr, avail, STDCHAR); | |
3670 | } | |
3671 | count -= avail; | |
3672 | unread += avail; | |
3673 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
3674 | } | |
3675 | } | |
93679785 NIS |
3676 | if (count > 0) { |
3677 | unread += PerlIOBase_unread(aTHX_ f, vbuf, count); | |
3678 | } | |
14a5cf38 | 3679 | return unread; |
760ac839 LW |
3680 | } |
3681 | ||
9e353e3b | 3682 | SSize_t |
f62ce20a | 3683 | PerlIOBuf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
760ac839 | 3684 | { |
14a5cf38 JH |
3685 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3686 | const STDCHAR *buf = (const STDCHAR *) vbuf; | |
3687 | Size_t written = 0; | |
3688 | if (!b->buf) | |
3689 | PerlIO_get_base(f); | |
3690 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) | |
3691 | return 0; | |
0678cb22 NIS |
3692 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { |
3693 | if (PerlIO_flush(f) != 0) { | |
3694 | return 0; | |
3695 | } | |
3696 | } | |
14a5cf38 JH |
3697 | while (count > 0) { |
3698 | SSize_t avail = b->bufsiz - (b->ptr - b->buf); | |
3699 | if ((SSize_t) count < avail) | |
3700 | avail = count; | |
3701 | PerlIOBase(f)->flags |= PERLIO_F_WRBUF; | |
3702 | if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { | |
3703 | while (avail > 0) { | |
3704 | int ch = *buf++; | |
3705 | *(b->ptr)++ = ch; | |
3706 | count--; | |
3707 | avail--; | |
3708 | written++; | |
3709 | if (ch == '\n') { | |
3710 | PerlIO_flush(f); | |
3711 | break; | |
3712 | } | |
3713 | } | |
3714 | } | |
3715 | else { | |
3716 | if (avail) { | |
3717 | Copy(buf, b->ptr, avail, STDCHAR); | |
3718 | count -= avail; | |
3719 | buf += avail; | |
3720 | written += avail; | |
3721 | b->ptr += avail; | |
3722 | } | |
3723 | } | |
3724 | if (b->ptr >= (b->buf + b->bufsiz)) | |
3725 | PerlIO_flush(f); | |
3726 | } | |
3727 | if (PerlIOBase(f)->flags & PERLIO_F_UNBUF) | |
3728 | PerlIO_flush(f); | |
3729 | return written; | |
9e353e3b NIS |
3730 | } |
3731 | ||
3732 | IV | |
f62ce20a | 3733 | PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
9e353e3b | 3734 | { |
14a5cf38 JH |
3735 | IV code; |
3736 | if ((code = PerlIO_flush(f)) == 0) { | |
3737 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3738 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
3739 | code = PerlIO_seek(PerlIONext(f), offset, whence); | |
3740 | if (code == 0) { | |
3741 | b->posn = PerlIO_tell(PerlIONext(f)); | |
3742 | } | |
9e353e3b | 3743 | } |
14a5cf38 | 3744 | return code; |
9e353e3b NIS |
3745 | } |
3746 | ||
3747 | Off_t | |
f62ce20a | 3748 | PerlIOBuf_tell(pTHX_ PerlIO *f) |
9e353e3b | 3749 | { |
14a5cf38 JH |
3750 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3751 | /* | |
71200d45 | 3752 | * b->posn is file position where b->buf was read, or will be written |
14a5cf38 JH |
3753 | */ |
3754 | Off_t posn = b->posn; | |
37725cdc | 3755 | if ((PerlIOBase(f)->flags & PERLIO_F_APPEND) && |
0678cb22 NIS |
3756 | (PerlIOBase(f)->flags & PERLIO_F_WRBUF)) { |
3757 | #if 1 | |
3758 | /* As O_APPEND files are normally shared in some sense it is better | |
3759 | to flush : | |
3760 | */ | |
3761 | PerlIO_flush(f); | |
3762 | #else | |
37725cdc | 3763 | /* when file is NOT shared then this is sufficient */ |
0678cb22 NIS |
3764 | PerlIO_seek(PerlIONext(f),0, SEEK_END); |
3765 | #endif | |
3766 | posn = b->posn = PerlIO_tell(PerlIONext(f)); | |
3767 | } | |
14a5cf38 JH |
3768 | if (b->buf) { |
3769 | /* | |
71200d45 | 3770 | * If buffer is valid adjust position by amount in buffer |
14a5cf38 JH |
3771 | */ |
3772 | posn += (b->ptr - b->buf); | |
3773 | } | |
3774 | return posn; | |
9e353e3b NIS |
3775 | } |
3776 | ||
3777 | IV | |
44798d05 NIS |
3778 | PerlIOBuf_popped(pTHX_ PerlIO *f) |
3779 | { | |
3780 | IV code = PerlIOBase_popped(aTHX_ f); | |
3781 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
3782 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3783 | Safefree(b->buf); | |
3784 | } | |
3785 | b->buf = NULL; | |
3786 | b->ptr = b->end = b->buf; | |
3787 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
3788 | return code; | |
3789 | } | |
3790 | ||
3791 | IV | |
f62ce20a | 3792 | PerlIOBuf_close(pTHX_ PerlIO *f) |
9e353e3b | 3793 | { |
f62ce20a | 3794 | IV code = PerlIOBase_close(aTHX_ f); |
14a5cf38 JH |
3795 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3796 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3a1ee7e8 | 3797 | Safefree(b->buf); |
14a5cf38 JH |
3798 | } |
3799 | b->buf = NULL; | |
3800 | b->ptr = b->end = b->buf; | |
3801 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
3802 | return code; | |
760ac839 LW |
3803 | } |
3804 | ||
9e353e3b | 3805 | STDCHAR * |
f62ce20a | 3806 | PerlIOBuf_get_ptr(pTHX_ PerlIO *f) |
9e353e3b | 3807 | { |
14a5cf38 JH |
3808 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3809 | if (!b->buf) | |
3810 | PerlIO_get_base(f); | |
3811 | return b->ptr; | |
9e353e3b NIS |
3812 | } |
3813 | ||
05d1247b | 3814 | SSize_t |
f62ce20a | 3815 | PerlIOBuf_get_cnt(pTHX_ PerlIO *f) |
9e353e3b | 3816 | { |
14a5cf38 JH |
3817 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3818 | if (!b->buf) | |
3819 | PerlIO_get_base(f); | |
3820 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) | |
3821 | return (b->end - b->ptr); | |
3822 | return 0; | |
9e353e3b NIS |
3823 | } |
3824 | ||
3825 | STDCHAR * | |
f62ce20a | 3826 | PerlIOBuf_get_base(pTHX_ PerlIO *f) |
9e353e3b | 3827 | { |
14a5cf38 JH |
3828 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3829 | if (!b->buf) { | |
3830 | if (!b->bufsiz) | |
3831 | b->bufsiz = 4096; | |
a1ea730d | 3832 | b->buf = |
7fcdafbd | 3833 | Newz('B',b->buf,b->bufsiz, STDCHAR); |
14a5cf38 JH |
3834 | if (!b->buf) { |
3835 | b->buf = (STDCHAR *) & b->oneword; | |
3836 | b->bufsiz = sizeof(b->oneword); | |
3837 | } | |
3838 | b->ptr = b->buf; | |
3839 | b->end = b->ptr; | |
06da4f11 | 3840 | } |
14a5cf38 | 3841 | return b->buf; |
9e353e3b NIS |
3842 | } |
3843 | ||
3844 | Size_t | |
f62ce20a | 3845 | PerlIOBuf_bufsiz(pTHX_ PerlIO *f) |
9e353e3b | 3846 | { |
14a5cf38 JH |
3847 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3848 | if (!b->buf) | |
3849 | PerlIO_get_base(f); | |
3850 | return (b->end - b->buf); | |
9e353e3b NIS |
3851 | } |
3852 | ||
3853 | void | |
f62ce20a | 3854 | PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
9e353e3b | 3855 | { |
14a5cf38 JH |
3856 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3857 | if (!b->buf) | |
3858 | PerlIO_get_base(f); | |
3859 | b->ptr = ptr; | |
3860 | if (PerlIO_get_cnt(f) != cnt || b->ptr < b->buf) { | |
14a5cf38 JH |
3861 | assert(PerlIO_get_cnt(f) == cnt); |
3862 | assert(b->ptr >= b->buf); | |
3863 | } | |
3864 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
760ac839 LW |
3865 | } |
3866 | ||
71200d45 | 3867 | PerlIO * |
ecdeb87c | 3868 | PerlIOBuf_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
71200d45 | 3869 | { |
ecdeb87c | 3870 | return PerlIOBase_dup(aTHX_ f, o, param, flags); |
71200d45 NIS |
3871 | } |
3872 | ||
3873 | ||
3874 | ||
9e353e3b | 3875 | PerlIO_funcs PerlIO_perlio = { |
2dc2558e | 3876 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
3877 | "perlio", |
3878 | sizeof(PerlIOBuf), | |
86e05cf2 | 3879 | PERLIO_K_BUFFERED|PERLIO_K_RAW, |
14a5cf38 | 3880 | PerlIOBuf_pushed, |
44798d05 | 3881 | PerlIOBuf_popped, |
14a5cf38 | 3882 | PerlIOBuf_open, |
86e05cf2 | 3883 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
3884 | NULL, |
3885 | PerlIOBase_fileno, | |
71200d45 | 3886 | PerlIOBuf_dup, |
14a5cf38 JH |
3887 | PerlIOBuf_read, |
3888 | PerlIOBuf_unread, | |
3889 | PerlIOBuf_write, | |
3890 | PerlIOBuf_seek, | |
3891 | PerlIOBuf_tell, | |
3892 | PerlIOBuf_close, | |
3893 | PerlIOBuf_flush, | |
3894 | PerlIOBuf_fill, | |
3895 | PerlIOBase_eof, | |
3896 | PerlIOBase_error, | |
3897 | PerlIOBase_clearerr, | |
3898 | PerlIOBase_setlinebuf, | |
3899 | PerlIOBuf_get_base, | |
3900 | PerlIOBuf_bufsiz, | |
3901 | PerlIOBuf_get_ptr, | |
3902 | PerlIOBuf_get_cnt, | |
3903 | PerlIOBuf_set_ptrcnt, | |
9e353e3b NIS |
3904 | }; |
3905 | ||
66ecd56b | 3906 | /*--------------------------------------------------------------------------------------*/ |
14a5cf38 | 3907 | /* |
71200d45 | 3908 | * Temp layer to hold unread chars when cannot do it any other way |
14a5cf38 | 3909 | */ |
5e2ab84b NIS |
3910 | |
3911 | IV | |
f62ce20a | 3912 | PerlIOPending_fill(pTHX_ PerlIO *f) |
5e2ab84b | 3913 | { |
14a5cf38 | 3914 | /* |
71200d45 | 3915 | * Should never happen |
14a5cf38 JH |
3916 | */ |
3917 | PerlIO_flush(f); | |
3918 | return 0; | |
5e2ab84b NIS |
3919 | } |
3920 | ||
3921 | IV | |
f62ce20a | 3922 | PerlIOPending_close(pTHX_ PerlIO *f) |
5e2ab84b | 3923 | { |
14a5cf38 | 3924 | /* |
71200d45 | 3925 | * A tad tricky - flush pops us, then we close new top |
14a5cf38 JH |
3926 | */ |
3927 | PerlIO_flush(f); | |
3928 | return PerlIO_close(f); | |
5e2ab84b NIS |
3929 | } |
3930 | ||
3931 | IV | |
f62ce20a | 3932 | PerlIOPending_seek(pTHX_ PerlIO *f, Off_t offset, int whence) |
5e2ab84b | 3933 | { |
14a5cf38 | 3934 | /* |
71200d45 | 3935 | * A tad tricky - flush pops us, then we seek new top |
14a5cf38 JH |
3936 | */ |
3937 | PerlIO_flush(f); | |
3938 | return PerlIO_seek(f, offset, whence); | |
5e2ab84b NIS |
3939 | } |
3940 | ||
3941 | ||
3942 | IV | |
f62ce20a | 3943 | PerlIOPending_flush(pTHX_ PerlIO *f) |
5e2ab84b | 3944 | { |
14a5cf38 JH |
3945 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
3946 | if (b->buf && b->buf != (STDCHAR *) & b->oneword) { | |
3a1ee7e8 | 3947 | Safefree(b->buf); |
14a5cf38 JH |
3948 | b->buf = NULL; |
3949 | } | |
3950 | PerlIO_pop(aTHX_ f); | |
3951 | return 0; | |
5e2ab84b NIS |
3952 | } |
3953 | ||
3954 | void | |
f62ce20a | 3955 | PerlIOPending_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
5e2ab84b | 3956 | { |
14a5cf38 JH |
3957 | if (cnt <= 0) { |
3958 | PerlIO_flush(f); | |
3959 | } | |
3960 | else { | |
f62ce20a | 3961 | PerlIOBuf_set_ptrcnt(aTHX_ f, ptr, cnt); |
14a5cf38 | 3962 | } |
5e2ab84b NIS |
3963 | } |
3964 | ||
3965 | IV | |
2dc2558e | 3966 | PerlIOPending_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
5e2ab84b | 3967 | { |
2dc2558e | 3968 | IV code = PerlIOBase_pushed(aTHX_ f, mode, arg, tab); |
14a5cf38 JH |
3969 | PerlIOl *l = PerlIOBase(f); |
3970 | /* | |
71200d45 NIS |
3971 | * Our PerlIO_fast_gets must match what we are pushed on, or sv_gets() |
3972 | * etc. get muddled when it changes mid-string when we auto-pop. | |
14a5cf38 JH |
3973 | */ |
3974 | l->flags = (l->flags & ~(PERLIO_F_FASTGETS | PERLIO_F_UTF8)) | | |
3975 | (PerlIOBase(PerlIONext(f))-> | |
3976 | flags & (PERLIO_F_FASTGETS | PERLIO_F_UTF8)); | |
3977 | return code; | |
5e2ab84b NIS |
3978 | } |
3979 | ||
3980 | SSize_t | |
f62ce20a | 3981 | PerlIOPending_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) |
5e2ab84b | 3982 | { |
14a5cf38 JH |
3983 | SSize_t avail = PerlIO_get_cnt(f); |
3984 | SSize_t got = 0; | |
eb160463 | 3985 | if ((SSize_t)count < avail) |
14a5cf38 JH |
3986 | avail = count; |
3987 | if (avail > 0) | |
f62ce20a | 3988 | got = PerlIOBuf_read(aTHX_ f, vbuf, avail); |
eb160463 | 3989 | if (got >= 0 && got < (SSize_t)count) { |
14a5cf38 JH |
3990 | SSize_t more = |
3991 | PerlIO_read(f, ((STDCHAR *) vbuf) + got, count - got); | |
3992 | if (more >= 0 || got == 0) | |
3993 | got += more; | |
3994 | } | |
3995 | return got; | |
5e2ab84b NIS |
3996 | } |
3997 | ||
5e2ab84b | 3998 | PerlIO_funcs PerlIO_pending = { |
2dc2558e | 3999 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
4000 | "pending", |
4001 | sizeof(PerlIOBuf), | |
86e05cf2 | 4002 | PERLIO_K_BUFFERED|PERLIO_K_RAW, /* not sure about RAW here */ |
14a5cf38 | 4003 | PerlIOPending_pushed, |
44798d05 | 4004 | PerlIOBuf_popped, |
14a5cf38 | 4005 | NULL, |
86e05cf2 | 4006 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
4007 | NULL, |
4008 | PerlIOBase_fileno, | |
71200d45 | 4009 | PerlIOBuf_dup, |
14a5cf38 JH |
4010 | PerlIOPending_read, |
4011 | PerlIOBuf_unread, | |
4012 | PerlIOBuf_write, | |
4013 | PerlIOPending_seek, | |
4014 | PerlIOBuf_tell, | |
4015 | PerlIOPending_close, | |
4016 | PerlIOPending_flush, | |
4017 | PerlIOPending_fill, | |
4018 | PerlIOBase_eof, | |
4019 | PerlIOBase_error, | |
4020 | PerlIOBase_clearerr, | |
4021 | PerlIOBase_setlinebuf, | |
4022 | PerlIOBuf_get_base, | |
4023 | PerlIOBuf_bufsiz, | |
4024 | PerlIOBuf_get_ptr, | |
4025 | PerlIOBuf_get_cnt, | |
4026 | PerlIOPending_set_ptrcnt, | |
5e2ab84b NIS |
4027 | }; |
4028 | ||
4029 | ||
4030 | ||
4031 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 JH |
4032 | /* |
4033 | * crlf - translation On read translate CR,LF to "\n" we do this by | |
4034 | * overriding ptr/cnt entries to hand back a line at a time and keeping a | |
71200d45 | 4035 | * record of which nl we "lied" about. On write translate "\n" to CR,LF |
66ecd56b NIS |
4036 | */ |
4037 | ||
14a5cf38 | 4038 | typedef struct { |
22569500 NIS |
4039 | PerlIOBuf base; /* PerlIOBuf stuff */ |
4040 | STDCHAR *nl; /* Position of crlf we "lied" about in the | |
14a5cf38 | 4041 | * buffer */ |
99efab12 NIS |
4042 | } PerlIOCrlf; |
4043 | ||
f5b9d040 | 4044 | IV |
2dc2558e | 4045 | PerlIOCrlf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
f5b9d040 | 4046 | { |
14a5cf38 JH |
4047 | IV code; |
4048 | PerlIOBase(f)->flags |= PERLIO_F_CRLF; | |
2dc2558e | 4049 | code = PerlIOBuf_pushed(aTHX_ f, mode, arg, tab); |
5e2ab84b | 4050 | #if 0 |
14a5cf38 JH |
4051 | PerlIO_debug("PerlIOCrlf_pushed f=%p %s %s fl=%08" UVxf "\n", |
4052 | f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)", | |
4053 | PerlIOBase(f)->flags); | |
5e2ab84b | 4054 | #endif |
14a5cf38 | 4055 | return code; |
f5b9d040 NIS |
4056 | } |
4057 | ||
4058 | ||
99efab12 | 4059 | SSize_t |
f62ce20a | 4060 | PerlIOCrlf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
99efab12 | 4061 | { |
14a5cf38 JH |
4062 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); |
4063 | if (c->nl) { | |
4064 | *(c->nl) = 0xd; | |
4065 | c->nl = NULL; | |
4066 | } | |
4067 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) | |
f62ce20a | 4068 | return PerlIOBuf_unread(aTHX_ f, vbuf, count); |
14a5cf38 JH |
4069 | else { |
4070 | const STDCHAR *buf = (const STDCHAR *) vbuf + count; | |
4071 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
4072 | SSize_t unread = 0; | |
4073 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
4074 | PerlIO_flush(f); | |
4075 | if (!b->buf) | |
4076 | PerlIO_get_base(f); | |
4077 | if (b->buf) { | |
4078 | if (!(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
4079 | b->end = b->ptr = b->buf + b->bufsiz; | |
4080 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
4081 | b->posn -= b->bufsiz; | |
4082 | } | |
4083 | while (count > 0 && b->ptr > b->buf) { | |
4084 | int ch = *--buf; | |
4085 | if (ch == '\n') { | |
4086 | if (b->ptr - 2 >= b->buf) { | |
4087 | *--(b->ptr) = 0xa; | |
4088 | *--(b->ptr) = 0xd; | |
4089 | unread++; | |
4090 | count--; | |
4091 | } | |
4092 | else { | |
4093 | buf++; | |
4094 | break; | |
4095 | } | |
4096 | } | |
4097 | else { | |
4098 | *--(b->ptr) = ch; | |
4099 | unread++; | |
4100 | count--; | |
4101 | } | |
4102 | } | |
4103 | } | |
4104 | return unread; | |
4105 | } | |
99efab12 NIS |
4106 | } |
4107 | ||
4108 | SSize_t | |
f62ce20a | 4109 | PerlIOCrlf_get_cnt(pTHX_ PerlIO *f) |
99efab12 | 4110 | { |
14a5cf38 JH |
4111 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
4112 | if (!b->buf) | |
4113 | PerlIO_get_base(f); | |
4114 | if (PerlIOBase(f)->flags & PERLIO_F_RDBUF) { | |
4115 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); | |
23b3c6af NIS |
4116 | if ((PerlIOBase(f)->flags & PERLIO_F_CRLF) && (!c->nl || *c->nl == 0xd)) { |
4117 | STDCHAR *nl = (c->nl) ? c->nl : b->ptr; | |
14a5cf38 JH |
4118 | scan: |
4119 | while (nl < b->end && *nl != 0xd) | |
4120 | nl++; | |
4121 | if (nl < b->end && *nl == 0xd) { | |
4122 | test: | |
4123 | if (nl + 1 < b->end) { | |
4124 | if (nl[1] == 0xa) { | |
4125 | *nl = '\n'; | |
4126 | c->nl = nl; | |
4127 | } | |
4128 | else { | |
4129 | /* | |
71200d45 | 4130 | * Not CR,LF but just CR |
14a5cf38 JH |
4131 | */ |
4132 | nl++; | |
4133 | goto scan; | |
4134 | } | |
4135 | } | |
4136 | else { | |
4137 | /* | |
71200d45 | 4138 | * Blast - found CR as last char in buffer |
14a5cf38 | 4139 | */ |
e87a358a | 4140 | |
14a5cf38 JH |
4141 | if (b->ptr < nl) { |
4142 | /* | |
4143 | * They may not care, defer work as long as | |
71200d45 | 4144 | * possible |
14a5cf38 | 4145 | */ |
a0d1d361 | 4146 | c->nl = nl; |
14a5cf38 JH |
4147 | return (nl - b->ptr); |
4148 | } | |
4149 | else { | |
4150 | int code; | |
22569500 | 4151 | b->ptr++; /* say we have read it as far as |
14a5cf38 | 4152 | * flush() is concerned */ |
22569500 | 4153 | b->buf++; /* Leave space in front of buffer */ |
e949e37c NIS |
4154 | /* Note as we have moved buf up flush's |
4155 | posn += ptr-buf | |
4156 | will naturally make posn point at CR | |
4157 | */ | |
22569500 NIS |
4158 | b->bufsiz--; /* Buffer is thus smaller */ |
4159 | code = PerlIO_fill(f); /* Fetch some more */ | |
4160 | b->bufsiz++; /* Restore size for next time */ | |
4161 | b->buf--; /* Point at space */ | |
4162 | b->ptr = nl = b->buf; /* Which is what we hand | |
14a5cf38 | 4163 | * off */ |
22569500 | 4164 | *nl = 0xd; /* Fill in the CR */ |
14a5cf38 | 4165 | if (code == 0) |
22569500 | 4166 | goto test; /* fill() call worked */ |
14a5cf38 | 4167 | /* |
71200d45 | 4168 | * CR at EOF - just fall through |
14a5cf38 | 4169 | */ |
a0d1d361 | 4170 | /* Should we clear EOF though ??? */ |
14a5cf38 JH |
4171 | } |
4172 | } | |
4173 | } | |
4174 | } | |
4175 | return (((c->nl) ? (c->nl + 1) : b->end) - b->ptr); | |
4176 | } | |
4177 | return 0; | |
99efab12 NIS |
4178 | } |
4179 | ||
4180 | void | |
f62ce20a | 4181 | PerlIOCrlf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt) |
14a5cf38 JH |
4182 | { |
4183 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
4184 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); | |
14a5cf38 JH |
4185 | if (!b->buf) |
4186 | PerlIO_get_base(f); | |
4187 | if (!ptr) { | |
a0d1d361 | 4188 | if (c->nl) { |
14a5cf38 | 4189 | ptr = c->nl + 1; |
22569500 | 4190 | if (ptr == b->end && *c->nl == 0xd) { |
a0d1d361 | 4191 | /* Defered CR at end of buffer case - we lied about count */ |
22569500 NIS |
4192 | ptr--; |
4193 | } | |
4194 | } | |
14a5cf38 JH |
4195 | else { |
4196 | ptr = b->end; | |
14a5cf38 JH |
4197 | } |
4198 | ptr -= cnt; | |
4199 | } | |
4200 | else { | |
3b4bd3fd | 4201 | #if 0 |
14a5cf38 | 4202 | /* |
71200d45 | 4203 | * Test code - delete when it works ... |
14a5cf38 | 4204 | */ |
3b4bd3fd | 4205 | IV flags = PerlIOBase(f)->flags; |
ba7abf9d | 4206 | STDCHAR *chk = (c->nl) ? (c->nl+1) : b->end; |
22569500 | 4207 | if (ptr+cnt == c->nl && c->nl+1 == b->end && *c->nl == 0xd) { |
a0d1d361 NIS |
4208 | /* Defered CR at end of buffer case - we lied about count */ |
4209 | chk--; | |
22569500 | 4210 | } |
14a5cf38 JH |
4211 | chk -= cnt; |
4212 | ||
a0d1d361 | 4213 | if (ptr != chk ) { |
99ef548b | 4214 | Perl_croak(aTHX_ "ptr wrong %p != %p fl=%08" UVxf |
14a5cf38 JH |
4215 | " nl=%p e=%p for %d", ptr, chk, flags, c->nl, |
4216 | b->end, cnt); | |
4217 | } | |
99ef548b | 4218 | #endif |
14a5cf38 JH |
4219 | } |
4220 | if (c->nl) { | |
4221 | if (ptr > c->nl) { | |
4222 | /* | |
71200d45 | 4223 | * They have taken what we lied about |
14a5cf38 JH |
4224 | */ |
4225 | *(c->nl) = 0xd; | |
4226 | c->nl = NULL; | |
4227 | ptr++; | |
4228 | } | |
4229 | } | |
4230 | b->ptr = ptr; | |
4231 | PerlIOBase(f)->flags |= PERLIO_F_RDBUF; | |
99efab12 NIS |
4232 | } |
4233 | ||
4234 | SSize_t | |
f62ce20a | 4235 | PerlIOCrlf_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
99efab12 | 4236 | { |
14a5cf38 | 4237 | if (!(PerlIOBase(f)->flags & PERLIO_F_CRLF)) |
f62ce20a | 4238 | return PerlIOBuf_write(aTHX_ f, vbuf, count); |
14a5cf38 JH |
4239 | else { |
4240 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
4241 | const STDCHAR *buf = (const STDCHAR *) vbuf; | |
4242 | const STDCHAR *ebuf = buf + count; | |
4243 | if (!b->buf) | |
4244 | PerlIO_get_base(f); | |
4245 | if (!(PerlIOBase(f)->flags & PERLIO_F_CANWRITE)) | |
4246 | return 0; | |
4247 | while (buf < ebuf) { | |
4248 | STDCHAR *eptr = b->buf + b->bufsiz; | |
4249 | PerlIOBase(f)->flags |= PERLIO_F_WRBUF; | |
4250 | while (buf < ebuf && b->ptr < eptr) { | |
4251 | if (*buf == '\n') { | |
4252 | if ((b->ptr + 2) > eptr) { | |
4253 | /* | |
71200d45 | 4254 | * Not room for both |
14a5cf38 JH |
4255 | */ |
4256 | PerlIO_flush(f); | |
4257 | break; | |
4258 | } | |
4259 | else { | |
22569500 NIS |
4260 | *(b->ptr)++ = 0xd; /* CR */ |
4261 | *(b->ptr)++ = 0xa; /* LF */ | |
14a5cf38 JH |
4262 | buf++; |
4263 | if (PerlIOBase(f)->flags & PERLIO_F_LINEBUF) { | |
4264 | PerlIO_flush(f); | |
4265 | break; | |
4266 | } | |
4267 | } | |
4268 | } | |
4269 | else { | |
4270 | int ch = *buf++; | |
4271 | *(b->ptr)++ = ch; | |
4272 | } | |
4273 | if (b->ptr >= eptr) { | |
4274 | PerlIO_flush(f); | |
4275 | break; | |
4276 | } | |
4277 | } | |
4278 | } | |
4279 | if (PerlIOBase(f)->flags & PERLIO_F_UNBUF) | |
4280 | PerlIO_flush(f); | |
4281 | return (buf - (STDCHAR *) vbuf); | |
4282 | } | |
99efab12 NIS |
4283 | } |
4284 | ||
4285 | IV | |
f62ce20a | 4286 | PerlIOCrlf_flush(pTHX_ PerlIO *f) |
99efab12 | 4287 | { |
14a5cf38 JH |
4288 | PerlIOCrlf *c = PerlIOSelf(f, PerlIOCrlf); |
4289 | if (c->nl) { | |
4290 | *(c->nl) = 0xd; | |
4291 | c->nl = NULL; | |
4292 | } | |
f62ce20a | 4293 | return PerlIOBuf_flush(aTHX_ f); |
99efab12 NIS |
4294 | } |
4295 | ||
86e05cf2 NIS |
4296 | IV |
4297 | PerlIOCrlf_binmode(pTHX_ PerlIO *f) | |
4298 | { | |
4299 | if ((PerlIOBase(f)->flags & PERLIO_F_CRLF)) { | |
4300 | /* In text mode - flush any pending stuff and flip it */ | |
4301 | PerlIOBase(f)->flags &= ~PERLIO_F_CRLF; | |
4302 | #ifndef PERLIO_USING_CRLF | |
4303 | /* CRLF is unusual case - if this is just the :crlf layer pop it */ | |
4304 | if (PerlIOBase(f)->tab == &PerlIO_crlf) { | |
4305 | PerlIO_pop(aTHX_ f); | |
4306 | } | |
4307 | #endif | |
4308 | } | |
4309 | return 0; | |
4310 | } | |
4311 | ||
66ecd56b | 4312 | PerlIO_funcs PerlIO_crlf = { |
2dc2558e | 4313 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
4314 | "crlf", |
4315 | sizeof(PerlIOCrlf), | |
86e05cf2 | 4316 | PERLIO_K_BUFFERED | PERLIO_K_CANCRLF | PERLIO_K_RAW, |
14a5cf38 | 4317 | PerlIOCrlf_pushed, |
44798d05 | 4318 | PerlIOBuf_popped, /* popped */ |
14a5cf38 | 4319 | PerlIOBuf_open, |
86e05cf2 | 4320 | PerlIOCrlf_binmode, /* binmode */ |
14a5cf38 JH |
4321 | NULL, |
4322 | PerlIOBase_fileno, | |
71200d45 | 4323 | PerlIOBuf_dup, |
22569500 | 4324 | PerlIOBuf_read, /* generic read works with ptr/cnt lies |
14a5cf38 | 4325 | * ... */ |
22569500 NIS |
4326 | PerlIOCrlf_unread, /* Put CR,LF in buffer for each '\n' */ |
4327 | PerlIOCrlf_write, /* Put CR,LF in buffer for each '\n' */ | |
14a5cf38 JH |
4328 | PerlIOBuf_seek, |
4329 | PerlIOBuf_tell, | |
4330 | PerlIOBuf_close, | |
4331 | PerlIOCrlf_flush, | |
4332 | PerlIOBuf_fill, | |
4333 | PerlIOBase_eof, | |
4334 | PerlIOBase_error, | |
4335 | PerlIOBase_clearerr, | |
4336 | PerlIOBase_setlinebuf, | |
4337 | PerlIOBuf_get_base, | |
4338 | PerlIOBuf_bufsiz, | |
4339 | PerlIOBuf_get_ptr, | |
4340 | PerlIOCrlf_get_cnt, | |
4341 | PerlIOCrlf_set_ptrcnt, | |
66ecd56b NIS |
4342 | }; |
4343 | ||
06da4f11 NIS |
4344 | #ifdef HAS_MMAP |
4345 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 | 4346 | /* |
71200d45 | 4347 | * mmap as "buffer" layer |
14a5cf38 | 4348 | */ |
06da4f11 | 4349 | |
14a5cf38 | 4350 | typedef struct { |
22569500 NIS |
4351 | PerlIOBuf base; /* PerlIOBuf stuff */ |
4352 | Mmap_t mptr; /* Mapped address */ | |
4353 | Size_t len; /* mapped length */ | |
4354 | STDCHAR *bbuf; /* malloced buffer if map fails */ | |
06da4f11 NIS |
4355 | } PerlIOMmap; |
4356 | ||
c3d7c7c9 NIS |
4357 | static size_t page_size = 0; |
4358 | ||
06da4f11 | 4359 | IV |
f62ce20a | 4360 | PerlIOMmap_map(pTHX_ PerlIO *f) |
06da4f11 | 4361 | { |
14a5cf38 JH |
4362 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4363 | IV flags = PerlIOBase(f)->flags; | |
4364 | IV code = 0; | |
4365 | if (m->len) | |
4366 | abort(); | |
4367 | if (flags & PERLIO_F_CANREAD) { | |
4368 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); | |
4369 | int fd = PerlIO_fileno(f); | |
10eefe7f CB |
4370 | Stat_t st; |
4371 | code = Fstat(fd, &st); | |
14a5cf38 JH |
4372 | if (code == 0 && S_ISREG(st.st_mode)) { |
4373 | SSize_t len = st.st_size - b->posn; | |
4374 | if (len > 0) { | |
4375 | Off_t posn; | |
4376 | if (!page_size) { | |
68d873c6 | 4377 | #if defined(HAS_SYSCONF) && (defined(_SC_PAGESIZE) || defined(_SC_PAGE_SIZE)) |
14a5cf38 | 4378 | { |
93189314 | 4379 | SETERRNO(0, SS_NORMAL); |
68d873c6 | 4380 | # ifdef _SC_PAGESIZE |
14a5cf38 | 4381 | page_size = sysconf(_SC_PAGESIZE); |
68d873c6 | 4382 | # else |
14a5cf38 | 4383 | page_size = sysconf(_SC_PAGE_SIZE); |
14aaf8e8 | 4384 | # endif |
14a5cf38 JH |
4385 | if ((long) page_size < 0) { |
4386 | if (errno) { | |
4387 | SV *error = ERRSV; | |
4388 | char *msg; | |
4389 | STRLEN n_a; | |
4390 | (void) SvUPGRADE(error, SVt_PV); | |
4391 | msg = SvPVx(error, n_a); | |
4392 | Perl_croak(aTHX_ "panic: sysconf: %s", | |
4393 | msg); | |
4394 | } | |
4395 | else | |
4396 | Perl_croak(aTHX_ | |
4397 | "panic: sysconf: pagesize unknown"); | |
4398 | } | |
4399 | } | |
68d873c6 JH |
4400 | #else |
4401 | # ifdef HAS_GETPAGESIZE | |
14a5cf38 | 4402 | page_size = getpagesize(); |
68d873c6 JH |
4403 | # else |
4404 | # if defined(I_SYS_PARAM) && defined(PAGESIZE) | |
22569500 | 4405 | page_size = PAGESIZE; /* compiletime, bad */ |
68d873c6 JH |
4406 | # endif |
4407 | # endif | |
4408 | #endif | |
14a5cf38 JH |
4409 | if ((IV) page_size <= 0) |
4410 | Perl_croak(aTHX_ "panic: bad pagesize %" IVdf, | |
4411 | (IV) page_size); | |
4412 | } | |
4413 | if (b->posn < 0) { | |
4414 | /* | |
4415 | * This is a hack - should never happen - open should | |
71200d45 | 4416 | * have set it ! |
14a5cf38 JH |
4417 | */ |
4418 | b->posn = PerlIO_tell(PerlIONext(f)); | |
4419 | } | |
4420 | posn = (b->posn / page_size) * page_size; | |
4421 | len = st.st_size - posn; | |
4422 | m->mptr = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, posn); | |
4423 | if (m->mptr && m->mptr != (Mmap_t) - 1) { | |
a5262162 | 4424 | #if 0 && defined(HAS_MADVISE) && defined(MADV_SEQUENTIAL) |
14a5cf38 | 4425 | madvise(m->mptr, len, MADV_SEQUENTIAL); |
06da4f11 | 4426 | #endif |
a5262162 | 4427 | #if 0 && defined(HAS_MADVISE) && defined(MADV_WILLNEED) |
14a5cf38 | 4428 | madvise(m->mptr, len, MADV_WILLNEED); |
a5262162 | 4429 | #endif |
14a5cf38 JH |
4430 | PerlIOBase(f)->flags = |
4431 | (flags & ~PERLIO_F_EOF) | PERLIO_F_RDBUF; | |
4432 | b->end = ((STDCHAR *) m->mptr) + len; | |
4433 | b->buf = ((STDCHAR *) m->mptr) + (b->posn - posn); | |
4434 | b->ptr = b->buf; | |
4435 | m->len = len; | |
4436 | } | |
4437 | else { | |
4438 | b->buf = NULL; | |
4439 | } | |
4440 | } | |
4441 | else { | |
4442 | PerlIOBase(f)->flags = | |
4443 | flags | PERLIO_F_EOF | PERLIO_F_RDBUF; | |
4444 | b->buf = NULL; | |
4445 | b->ptr = b->end = b->ptr; | |
4446 | code = -1; | |
4447 | } | |
4448 | } | |
4449 | } | |
4450 | return code; | |
06da4f11 NIS |
4451 | } |
4452 | ||
4453 | IV | |
e87a358a | 4454 | PerlIOMmap_unmap(pTHX_ PerlIO *f) |
06da4f11 | 4455 | { |
14a5cf38 JH |
4456 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4457 | PerlIOBuf *b = &m->base; | |
4458 | IV code = 0; | |
4459 | if (m->len) { | |
4460 | if (b->buf) { | |
4461 | code = munmap(m->mptr, m->len); | |
4462 | b->buf = NULL; | |
4463 | m->len = 0; | |
4464 | m->mptr = NULL; | |
4465 | if (PerlIO_seek(PerlIONext(f), b->posn, SEEK_SET) != 0) | |
4466 | code = -1; | |
4467 | } | |
4468 | b->ptr = b->end = b->buf; | |
4469 | PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); | |
4470 | } | |
4471 | return code; | |
06da4f11 NIS |
4472 | } |
4473 | ||
4474 | STDCHAR * | |
f62ce20a | 4475 | PerlIOMmap_get_base(pTHX_ PerlIO *f) |
06da4f11 | 4476 | { |
14a5cf38 JH |
4477 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4478 | PerlIOBuf *b = &m->base; | |
4479 | if (b->buf && (PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
4480 | /* | |
71200d45 | 4481 | * Already have a readbuffer in progress |
14a5cf38 JH |
4482 | */ |
4483 | return b->buf; | |
4484 | } | |
4485 | if (b->buf) { | |
4486 | /* | |
71200d45 | 4487 | * We have a write buffer or flushed PerlIOBuf read buffer |
14a5cf38 | 4488 | */ |
22569500 NIS |
4489 | m->bbuf = b->buf; /* save it in case we need it again */ |
4490 | b->buf = NULL; /* Clear to trigger below */ | |
14a5cf38 JH |
4491 | } |
4492 | if (!b->buf) { | |
22569500 | 4493 | PerlIOMmap_map(aTHX_ f); /* Try and map it */ |
14a5cf38 JH |
4494 | if (!b->buf) { |
4495 | /* | |
71200d45 | 4496 | * Map did not work - recover PerlIOBuf buffer if we have one |
14a5cf38 JH |
4497 | */ |
4498 | b->buf = m->bbuf; | |
4499 | } | |
4500 | } | |
4501 | b->ptr = b->end = b->buf; | |
4502 | if (b->buf) | |
4503 | return b->buf; | |
f62ce20a | 4504 | return PerlIOBuf_get_base(aTHX_ f); |
06da4f11 NIS |
4505 | } |
4506 | ||
4507 | SSize_t | |
f62ce20a | 4508 | PerlIOMmap_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
06da4f11 | 4509 | { |
14a5cf38 JH |
4510 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4511 | PerlIOBuf *b = &m->base; | |
4512 | if (PerlIOBase(f)->flags & PERLIO_F_WRBUF) | |
4513 | PerlIO_flush(f); | |
4514 | if (b->ptr && (b->ptr - count) >= b->buf | |
4515 | && memEQ(b->ptr - count, vbuf, count)) { | |
4516 | b->ptr -= count; | |
4517 | PerlIOBase(f)->flags &= ~PERLIO_F_EOF; | |
4518 | return count; | |
4519 | } | |
4520 | if (m->len) { | |
4521 | /* | |
71200d45 | 4522 | * Loose the unwritable mapped buffer |
14a5cf38 JH |
4523 | */ |
4524 | PerlIO_flush(f); | |
4525 | /* | |
71200d45 | 4526 | * If flush took the "buffer" see if we have one from before |
14a5cf38 JH |
4527 | */ |
4528 | if (!b->buf && m->bbuf) | |
4529 | b->buf = m->bbuf; | |
4530 | if (!b->buf) { | |
f62ce20a | 4531 | PerlIOBuf_get_base(aTHX_ f); |
14a5cf38 JH |
4532 | m->bbuf = b->buf; |
4533 | } | |
4534 | } | |
f62ce20a | 4535 | return PerlIOBuf_unread(aTHX_ f, vbuf, count); |
06da4f11 NIS |
4536 | } |
4537 | ||
4538 | SSize_t | |
f62ce20a | 4539 | PerlIOMmap_write(pTHX_ PerlIO *f, const void *vbuf, Size_t count) |
06da4f11 | 4540 | { |
14a5cf38 JH |
4541 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4542 | PerlIOBuf *b = &m->base; | |
4543 | if (!b->buf || !(PerlIOBase(f)->flags & PERLIO_F_WRBUF)) { | |
4544 | /* | |
71200d45 | 4545 | * No, or wrong sort of, buffer |
14a5cf38 JH |
4546 | */ |
4547 | if (m->len) { | |
e87a358a | 4548 | if (PerlIOMmap_unmap(aTHX_ f) != 0) |
14a5cf38 JH |
4549 | return 0; |
4550 | } | |
4551 | /* | |
71200d45 | 4552 | * If unmap took the "buffer" see if we have one from before |
14a5cf38 JH |
4553 | */ |
4554 | if (!b->buf && m->bbuf) | |
4555 | b->buf = m->bbuf; | |
4556 | if (!b->buf) { | |
f62ce20a | 4557 | PerlIOBuf_get_base(aTHX_ f); |
14a5cf38 JH |
4558 | m->bbuf = b->buf; |
4559 | } | |
06da4f11 | 4560 | } |
f62ce20a | 4561 | return PerlIOBuf_write(aTHX_ f, vbuf, count); |
06da4f11 NIS |
4562 | } |
4563 | ||
4564 | IV | |
f62ce20a | 4565 | PerlIOMmap_flush(pTHX_ PerlIO *f) |
06da4f11 | 4566 | { |
14a5cf38 JH |
4567 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4568 | PerlIOBuf *b = &m->base; | |
f62ce20a | 4569 | IV code = PerlIOBuf_flush(aTHX_ f); |
14a5cf38 | 4570 | /* |
71200d45 | 4571 | * Now we are "synced" at PerlIOBuf level |
14a5cf38 JH |
4572 | */ |
4573 | if (b->buf) { | |
4574 | if (m->len) { | |
4575 | /* | |
71200d45 | 4576 | * Unmap the buffer |
14a5cf38 | 4577 | */ |
e87a358a | 4578 | if (PerlIOMmap_unmap(aTHX_ f) != 0) |
14a5cf38 JH |
4579 | code = -1; |
4580 | } | |
4581 | else { | |
4582 | /* | |
4583 | * We seem to have a PerlIOBuf buffer which was not mapped | |
71200d45 | 4584 | * remember it in case we need one later |
14a5cf38 JH |
4585 | */ |
4586 | m->bbuf = b->buf; | |
4587 | } | |
4588 | } | |
4589 | return code; | |
06da4f11 NIS |
4590 | } |
4591 | ||
4592 | IV | |
f62ce20a | 4593 | PerlIOMmap_fill(pTHX_ PerlIO *f) |
06da4f11 | 4594 | { |
14a5cf38 JH |
4595 | PerlIOBuf *b = PerlIOSelf(f, PerlIOBuf); |
4596 | IV code = PerlIO_flush(f); | |
4597 | if (code == 0 && !b->buf) { | |
f62ce20a | 4598 | code = PerlIOMmap_map(aTHX_ f); |
14a5cf38 JH |
4599 | } |
4600 | if (code == 0 && !(PerlIOBase(f)->flags & PERLIO_F_RDBUF)) { | |
f62ce20a | 4601 | code = PerlIOBuf_fill(aTHX_ f); |
14a5cf38 JH |
4602 | } |
4603 | return code; | |
06da4f11 NIS |
4604 | } |
4605 | ||
4606 | IV | |
f62ce20a | 4607 | PerlIOMmap_close(pTHX_ PerlIO *f) |
06da4f11 | 4608 | { |
14a5cf38 JH |
4609 | PerlIOMmap *m = PerlIOSelf(f, PerlIOMmap); |
4610 | PerlIOBuf *b = &m->base; | |
4611 | IV code = PerlIO_flush(f); | |
4612 | if (m->bbuf) { | |
4613 | b->buf = m->bbuf; | |
4614 | m->bbuf = NULL; | |
4615 | b->ptr = b->end = b->buf; | |
4616 | } | |
f62ce20a | 4617 | if (PerlIOBuf_close(aTHX_ f) != 0) |
14a5cf38 JH |
4618 | code = -1; |
4619 | return code; | |
06da4f11 NIS |
4620 | } |
4621 | ||
71200d45 | 4622 | PerlIO * |
ecdeb87c | 4623 | PerlIOMmap_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags) |
71200d45 | 4624 | { |
ecdeb87c | 4625 | return PerlIOBase_dup(aTHX_ f, o, param, flags); |
71200d45 NIS |
4626 | } |
4627 | ||
06da4f11 NIS |
4628 | |
4629 | PerlIO_funcs PerlIO_mmap = { | |
2dc2558e | 4630 | sizeof(PerlIO_funcs), |
14a5cf38 JH |
4631 | "mmap", |
4632 | sizeof(PerlIOMmap), | |
86e05cf2 | 4633 | PERLIO_K_BUFFERED|PERLIO_K_RAW, |
14a5cf38 | 4634 | PerlIOBuf_pushed, |
44798d05 | 4635 | PerlIOBuf_popped, |
14a5cf38 | 4636 | PerlIOBuf_open, |
86e05cf2 | 4637 | PerlIOBase_binmode, /* binmode */ |
14a5cf38 JH |
4638 | NULL, |
4639 | PerlIOBase_fileno, | |
71200d45 | 4640 | PerlIOMmap_dup, |
14a5cf38 JH |
4641 | PerlIOBuf_read, |
4642 | PerlIOMmap_unread, | |
4643 | PerlIOMmap_write, | |
4644 | PerlIOBuf_seek, | |
4645 | PerlIOBuf_tell, | |
4646 | PerlIOBuf_close, | |
4647 | PerlIOMmap_flush, | |
4648 | PerlIOMmap_fill, | |
4649 | PerlIOBase_eof, | |
4650 | PerlIOBase_error, | |
4651 | PerlIOBase_clearerr, | |
4652 | PerlIOBase_setlinebuf, | |
4653 | PerlIOMmap_get_base, | |
4654 | PerlIOBuf_bufsiz, | |
4655 | PerlIOBuf_get_ptr, | |
4656 | PerlIOBuf_get_cnt, | |
4657 | PerlIOBuf_set_ptrcnt, | |
06da4f11 NIS |
4658 | }; |
4659 | ||
22569500 | 4660 | #endif /* HAS_MMAP */ |
06da4f11 | 4661 | |
9e353e3b | 4662 | PerlIO * |
e87a358a | 4663 | Perl_PerlIO_stdin(pTHX) |
9e353e3b | 4664 | { |
a1ea730d | 4665 | if (!PL_perlio) { |
14a5cf38 JH |
4666 | PerlIO_stdstreams(aTHX); |
4667 | } | |
a1ea730d | 4668 | return &PL_perlio[1]; |
9e353e3b NIS |
4669 | } |
4670 | ||
9e353e3b | 4671 | PerlIO * |
e87a358a | 4672 | Perl_PerlIO_stdout(pTHX) |
9e353e3b | 4673 | { |
a1ea730d | 4674 | if (!PL_perlio) { |
14a5cf38 JH |
4675 | PerlIO_stdstreams(aTHX); |
4676 | } | |
a1ea730d | 4677 | return &PL_perlio[2]; |
9e353e3b NIS |
4678 | } |
4679 | ||
9e353e3b | 4680 | PerlIO * |
e87a358a | 4681 | Perl_PerlIO_stderr(pTHX) |
9e353e3b | 4682 | { |
a1ea730d | 4683 | if (!PL_perlio) { |
14a5cf38 JH |
4684 | PerlIO_stdstreams(aTHX); |
4685 | } | |
a1ea730d | 4686 | return &PL_perlio[3]; |
9e353e3b NIS |
4687 | } |
4688 | ||
4689 | /*--------------------------------------------------------------------------------------*/ | |
4690 | ||
9e353e3b NIS |
4691 | char * |
4692 | PerlIO_getname(PerlIO *f, char *buf) | |
4693 | { | |
14a5cf38 JH |
4694 | dTHX; |
4695 | char *name = NULL; | |
a15cef0c | 4696 | #ifdef VMS |
14a5cf38 JH |
4697 | FILE *stdio = PerlIOSelf(f, PerlIOStdio)->stdio; |
4698 | if (stdio) | |
4699 | name = fgetname(stdio, buf); | |
a15cef0c | 4700 | #else |
14a5cf38 | 4701 | Perl_croak(aTHX_ "Don't know how to get file name"); |
a15cef0c | 4702 | #endif |
14a5cf38 | 4703 | return name; |
9e353e3b NIS |
4704 | } |
4705 | ||
4706 | ||
4707 | /*--------------------------------------------------------------------------------------*/ | |
14a5cf38 JH |
4708 | /* |
4709 | * Functions which can be called on any kind of PerlIO implemented in | |
71200d45 | 4710 | * terms of above |
14a5cf38 | 4711 | */ |
9e353e3b | 4712 | |
e87a358a NIS |
4713 | #undef PerlIO_fdopen |
4714 | PerlIO * | |
4715 | PerlIO_fdopen(int fd, const char *mode) | |
4716 | { | |
4717 | dTHX; | |
4718 | return PerlIO_openn(aTHX_ Nullch, mode, fd, 0, 0, NULL, 0, NULL); | |
4719 | } | |
4720 | ||
4721 | #undef PerlIO_open | |
4722 | PerlIO * | |
4723 | PerlIO_open(const char *path, const char *mode) | |
4724 | { | |
4725 | dTHX; | |
4726 | SV *name = sv_2mortal(newSVpvn(path, strlen(path))); | |
4727 | return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, NULL, 1, &name); | |
4728 | } | |
4729 | ||
4730 | #undef Perlio_reopen | |
4731 | PerlIO * | |
4732 | PerlIO_reopen(const char *path, const char *mode, PerlIO *f) | |
4733 | { | |
4734 | dTHX; | |
4735 | SV *name = sv_2mortal(newSVpvn(path, strlen(path))); | |
4736 | return PerlIO_openn(aTHX_ Nullch, mode, -1, 0, 0, f, 1, &name); | |
4737 | } | |
4738 | ||
9e353e3b | 4739 | #undef PerlIO_getc |
6f9d8c32 | 4740 | int |
9e353e3b | 4741 | PerlIO_getc(PerlIO *f) |
760ac839 | 4742 | { |
e87a358a | 4743 | dTHX; |
14a5cf38 JH |
4744 | STDCHAR buf[1]; |
4745 | SSize_t count = PerlIO_read(f, buf, 1); | |
4746 | if (count == 1) { | |
4747 | return (unsigned char) buf[0]; | |
4748 | } | |
4749 | return EOF; | |
313ca112 NIS |
4750 | } |
4751 | ||
4752 | #undef PerlIO_ungetc | |
4753 | int | |
4754 | PerlIO_ungetc(PerlIO *f, int ch) | |
4755 | { | |
e87a358a | 4756 | dTHX; |
14a5cf38 JH |
4757 | if (ch != EOF) { |
4758 | STDCHAR buf = ch; | |
4759 | if (PerlIO_unread(f, &buf, 1) == 1) | |
4760 | return ch; | |
4761 | } | |
4762 | return EOF; | |
760ac839 LW |
4763 | } |
4764 | ||
9e353e3b NIS |
4765 | #undef PerlIO_putc |
4766 | int | |
4767 | PerlIO_putc(PerlIO *f, int ch) | |
760ac839 | 4768 | { |
e87a358a | 4769 | dTHX; |
14a5cf38 JH |
4770 | STDCHAR buf = ch; |
4771 | return PerlIO_write(f, &buf, 1); | |
760ac839 LW |
4772 | } |
4773 | ||
9e353e3b | 4774 | #undef PerlIO_puts |
760ac839 | 4775 | int |
9e353e3b | 4776 | PerlIO_puts(PerlIO *f, const char *s) |
760ac839 | 4777 | { |
e87a358a | 4778 | dTHX; |
14a5cf38 JH |
4779 | STRLEN len = strlen(s); |
4780 | return PerlIO_write(f, s, len); | |
760ac839 LW |
4781 | } |
4782 | ||
4783 | #undef PerlIO_rewind | |
4784 | void | |
c78749f2 | 4785 | PerlIO_rewind(PerlIO *f) |
760ac839 | 4786 | { |
e87a358a | 4787 | dTHX; |
14a5cf38 JH |
4788 | PerlIO_seek(f, (Off_t) 0, SEEK_SET); |
4789 | PerlIO_clearerr(f); | |
6f9d8c32 NIS |
4790 | } |
4791 | ||
4792 | #undef PerlIO_vprintf | |
4793 | int | |
4794 | PerlIO_vprintf(PerlIO *f, const char *fmt, va_list ap) | |
4795 | { | |
14a5cf38 JH |
4796 | dTHX; |
4797 | SV *sv = newSVpvn("", 0); | |
4798 | char *s; | |
4799 | STRLEN len; | |
4800 | SSize_t wrote; | |
2cc61e15 | 4801 | #ifdef NEED_VA_COPY |
14a5cf38 JH |
4802 | va_list apc; |
4803 | Perl_va_copy(ap, apc); | |
4804 | sv_vcatpvf(sv, fmt, &apc); | |
2cc61e15 | 4805 | #else |
14a5cf38 | 4806 | sv_vcatpvf(sv, fmt, &ap); |
2cc61e15 | 4807 | #endif |
14a5cf38 JH |
4808 | s = SvPV(sv, len); |
4809 | wrote = PerlIO_write(f, s, len); | |
4810 | SvREFCNT_dec(sv); | |
4811 | return wrote; | |
760ac839 LW |
4812 | } |
4813 | ||
4814 | #undef PerlIO_printf | |
6f9d8c32 | 4815 | int |
14a5cf38 | 4816 | PerlIO_printf(PerlIO *f, const char *fmt, ...) |
760ac839 | 4817 | { |
14a5cf38 JH |
4818 | va_list ap; |
4819 | int result; | |
4820 | va_start(ap, fmt); | |
4821 | result = PerlIO_vprintf(f, fmt, ap); | |
4822 | va_end(ap); | |
4823 | return result; | |
760ac839 LW |
4824 | } |
4825 | ||
4826 | #undef PerlIO_stdoutf | |
6f9d8c32 | 4827 | int |
14a5cf38 | 4828 | PerlIO_stdoutf(const char *fmt, ...) |
760ac839 | 4829 | { |
e87a358a | 4830 | dTHX; |
14a5cf38 JH |
4831 | va_list ap; |
4832 | int result; | |
4833 | va_start(ap, fmt); | |
4834 | result = PerlIO_vprintf(PerlIO_stdout(), fmt, ap); | |
4835 | va_end(ap); | |
4836 | return result; | |
760ac839 LW |
4837 | } |
4838 | ||
4839 | #undef PerlIO_tmpfile | |
4840 | PerlIO * | |
c78749f2 | 4841 | PerlIO_tmpfile(void) |
760ac839 | 4842 | { |
2941a2e1 JH |
4843 | dTHX; |
4844 | PerlIO *f = NULL; | |
4845 | int fd = -1; | |
4846 | #ifdef WIN32 | |
4847 | fd = win32_tmpfd(); | |
4848 | if (fd >= 0) | |
4849 | f = PerlIO_fdopen(fd, "w+b"); | |
4850 | #else /* WIN32 */ | |
4851 | # ifdef HAS_MKSTEMP | |
4852 | SV *sv = newSVpv("/tmp/PerlIO_XXXXXX", 0); | |
4853 | ||
4854 | /* | |
4855 | * I have no idea how portable mkstemp() is ... NI-S | |
4856 | */ | |
4857 | fd = mkstemp(SvPVX(sv)); | |
4858 | if (fd >= 0) { | |
4859 | f = PerlIO_fdopen(fd, "w+"); | |
4860 | if (f) | |
4861 | PerlIOBase(f)->flags |= PERLIO_F_TEMP; | |
4862 | PerlLIO_unlink(SvPVX(sv)); | |
4863 | SvREFCNT_dec(sv); | |
4864 | } | |
4865 | # else /* !HAS_MKSTEMP, fallback to stdio tmpfile(). */ | |
4866 | FILE *stdio = PerlSIO_tmpfile(); | |
4867 | ||
4868 | if (stdio) { | |
4869 | if ((f = PerlIO_push(aTHX_(PerlIO_allocate(aTHX)), | |
4870 | &PerlIO_stdio, "w+", Nullsv))) { | |
4871 | PerlIOStdio *s = PerlIOSelf(f, PerlIOStdio); | |
4872 | ||
4873 | if (s) | |
4874 | s->stdio = stdio; | |
4875 | } | |
4876 | } | |
4877 | # endif /* else HAS_MKSTEMP */ | |
4878 | #endif /* else WIN32 */ | |
4879 | return f; | |
760ac839 LW |
4880 | } |
4881 | ||
6f9d8c32 NIS |
4882 | #undef HAS_FSETPOS |
4883 | #undef HAS_FGETPOS | |
4884 | ||
22569500 NIS |
4885 | #endif /* USE_SFIO */ |
4886 | #endif /* PERLIO_IS_STDIO */ | |
760ac839 | 4887 | |
9e353e3b | 4888 | /*======================================================================================*/ |
14a5cf38 | 4889 | /* |
71200d45 NIS |
4890 | * Now some functions in terms of above which may be needed even if we are |
4891 | * not in true PerlIO mode | |
9e353e3b NIS |
4892 | */ |
4893 | ||
760ac839 LW |
4894 | #ifndef HAS_FSETPOS |
4895 | #undef PerlIO_setpos | |
4896 | int | |
766a733e | 4897 | PerlIO_setpos(PerlIO *f, SV *pos) |
760ac839 | 4898 | { |
14a5cf38 JH |
4899 | dTHX; |
4900 | if (SvOK(pos)) { | |
4901 | STRLEN len; | |
4902 | Off_t *posn = (Off_t *) SvPV(pos, len); | |
4903 | if (f && len == sizeof(Off_t)) | |
4904 | return PerlIO_seek(f, *posn, SEEK_SET); | |
4905 | } | |
93189314 | 4906 | SETERRNO(EINVAL, SS_IVCHAN); |
14a5cf38 | 4907 | return -1; |
760ac839 | 4908 | } |
c411622e | 4909 | #else |
c411622e | 4910 | #undef PerlIO_setpos |
4911 | int | |
766a733e | 4912 | PerlIO_setpos(PerlIO *f, SV *pos) |
c411622e | 4913 | { |
14a5cf38 JH |
4914 | dTHX; |
4915 | if (SvOK(pos)) { | |
4916 | STRLEN len; | |
4917 | Fpos_t *fpos = (Fpos_t *) SvPV(pos, len); | |
4918 | if (f && len == sizeof(Fpos_t)) { | |
2d4389e4 | 4919 | #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64) |
14a5cf38 | 4920 | return fsetpos64(f, fpos); |
d9b3e12d | 4921 | #else |
14a5cf38 | 4922 | return fsetpos(f, fpos); |
d9b3e12d | 4923 | #endif |
14a5cf38 | 4924 | } |
766a733e | 4925 | } |
93189314 | 4926 | SETERRNO(EINVAL, SS_IVCHAN); |
14a5cf38 | 4927 | return -1; |
c411622e | 4928 | } |
4929 | #endif | |
760ac839 LW |
4930 | |
4931 | #ifndef HAS_FGETPOS | |
4932 | #undef PerlIO_getpos | |
4933 | int | |
766a733e | 4934 | PerlIO_getpos(PerlIO *f, SV *pos) |
760ac839 | 4935 | { |
14a5cf38 JH |
4936 | dTHX; |
4937 | Off_t posn = PerlIO_tell(f); | |
4938 | sv_setpvn(pos, (char *) &posn, sizeof(posn)); | |
4939 | return (posn == (Off_t) - 1) ? -1 : 0; | |
760ac839 | 4940 | } |
c411622e | 4941 | #else |
c411622e | 4942 | #undef PerlIO_getpos |
4943 | int | |
766a733e | 4944 | PerlIO_getpos(PerlIO *f, SV *pos) |
c411622e | 4945 | { |
14a5cf38 JH |
4946 | dTHX; |
4947 | Fpos_t fpos; | |
4948 | int code; | |
2d4389e4 | 4949 | #if defined(USE_64_BIT_STDIO) && defined(USE_FSETPOS64) |
14a5cf38 | 4950 | code = fgetpos64(f, &fpos); |
d9b3e12d | 4951 | #else |
14a5cf38 | 4952 | code = fgetpos(f, &fpos); |
d9b3e12d | 4953 | #endif |
14a5cf38 JH |
4954 | sv_setpvn(pos, (char *) &fpos, sizeof(fpos)); |
4955 | return code; | |
c411622e | 4956 | } |
4957 | #endif | |
760ac839 LW |
4958 | |
4959 | #if (defined(PERLIO_IS_STDIO) || !defined(USE_SFIO)) && !defined(HAS_VPRINTF) | |
4960 | ||
4961 | int | |
c78749f2 | 4962 | vprintf(char *pat, char *args) |
662a7e3f CS |
4963 | { |
4964 | _doprnt(pat, args, stdout); | |
22569500 | 4965 | return 0; /* wrong, but perl doesn't use the return |
14a5cf38 | 4966 | * value */ |
662a7e3f CS |
4967 | } |
4968 | ||
4969 | int | |
c78749f2 | 4970 | vfprintf(FILE *fd, char *pat, char *args) |
760ac839 LW |
4971 | { |
4972 | _doprnt(pat, args, fd); | |
22569500 | 4973 | return 0; /* wrong, but perl doesn't use the return |
14a5cf38 | 4974 | * value */ |
760ac839 LW |
4975 | } |
4976 | ||
4977 | #endif | |
4978 | ||
4979 | #ifndef PerlIO_vsprintf | |
6f9d8c32 | 4980 | int |
8ac85365 | 4981 | PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap) |
760ac839 | 4982 | { |
14a5cf38 JH |
4983 | int val = vsprintf(s, fmt, ap); |
4984 | if (n >= 0) { | |
4985 | if (strlen(s) >= (STRLEN) n) { | |
4986 | dTHX; | |
4987 | (void) PerlIO_puts(Perl_error_log, | |
4988 | "panic: sprintf overflow - memory corrupted!\n"); | |
4989 | my_exit(1); | |
4990 | } | |
760ac839 | 4991 | } |
14a5cf38 | 4992 | return val; |
760ac839 LW |
4993 | } |
4994 | #endif | |
4995 | ||
4996 | #ifndef PerlIO_sprintf | |
6f9d8c32 | 4997 | int |
14a5cf38 | 4998 | PerlIO_sprintf(char *s, int n, const char *fmt, ...) |
760ac839 | 4999 | { |
14a5cf38 JH |
5000 | va_list ap; |
5001 | int result; | |
5002 | va_start(ap, fmt); | |
5003 | result = PerlIO_vsprintf(s, n, fmt, ap); | |
5004 | va_end(ap); | |
5005 | return result; | |
760ac839 LW |
5006 | } |
5007 | #endif | |
245468eb JH |
5008 | |
5009 | ||
5010 | ||
5011 | ||
5012 | ||
5013 | ||
5014 | ||
5015 |