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