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