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