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