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