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