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