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