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