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