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