This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
e6906430 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
2c351e65 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
a687059c 5 *
d48672a2
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
8d063cd8 9 */
a0d0e21e
LW
10
11/*
12 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
13 * not content." --Gandalf
14 */
8d063cd8 15
40d34c0d
SB
16/* This file contains assorted utility routines.
17 * Which is a polite way of saying any stuff that people couldn't think of
18 * a better place for. Amongst other things, it includes the warning and
19 * dieing stuff, plus wrappers for malloc code.
20 */
21
8d063cd8 22#include "EXTERN.h"
864dbfa3 23#define PERL_IN_UTIL_C
8d063cd8 24#include "perl.h"
62b28dd9 25
64ca3a65 26#ifndef PERL_MICRO
a687059c 27#include <signal.h>
36477c24
PP
28#ifndef SIG_ERR
29# define SIG_ERR ((Sighandler_t) -1)
30#endif
64ca3a65 31#endif
36477c24 32
5783f60e
OS
33#ifdef __Lynx__
34/* Missing protos on LynxOS */
35int putenv(char *);
36#endif
37
ff68c719
PP
38#ifdef I_SYS_WAIT
39# include <sys/wait.h>
40#endif
41
868439a2
JH
42#ifdef HAS_SELECT
43# ifdef I_SYS_SELECT
44# include <sys/select.h>
45# endif
46#endif
47
8d063cd8 48#define FLUSH
8d063cd8 49
16cebae2
GS
50#if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
51# define FD_CLOEXEC 1 /* NeXT needs this */
52#endif
53
a687059c
LW
54/* NOTE: Do not call the next three routines directly. Use the macros
55 * in handy.h, so that we can easily redefine everything to do tracking of
56 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 57 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c
LW
58 */
59
aae6d3c0
NC
60static char *
61S_write_no_mem(pTHX)
62{
63 /* Can't use PerlIO to write as it allocates memory */
64 PerlLIO_write(PerlIO_fileno(Perl_error_log),
65 PL_no_mem, strlen(PL_no_mem));
66 my_exit(1);
8c89da26 67 NORETURN_FUNCTION_END;
aae6d3c0
NC
68}
69
26fa51c3
AMS
70/* paranoid version of system's malloc() */
71
bd4080b3 72Malloc_t
4f63d024 73Perl_safesysmalloc(MEM_SIZE size)
8d063cd8 74{
54aff467 75 dTHX;
bd4080b3 76 Malloc_t ptr;
55497cff 77#ifdef HAS_64K_LIMIT
62b28dd9 78 if (size > 0xffff) {
bf49b057 79 PerlIO_printf(Perl_error_log,
16cebae2 80 "Allocation too large: %lx\n", size) FLUSH;
54aff467 81 my_exit(1);
62b28dd9 82 }
55497cff 83#endif /* HAS_64K_LIMIT */
fe397c97
JD
84#ifdef PERL_TRACK_MEMPOOL
85 size += sTHX;
86#endif
34de22dd
LW
87#ifdef DEBUGGING
88 if ((long)size < 0)
4f63d024 89 Perl_croak_nocontext("panic: malloc");
34de22dd 90#endif
12ae5dfc 91 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
da927450 92 PERL_ALLOC_CHECK(ptr);
97835f67 93 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
fe397c97
JD
94 if (ptr != NULL) {
95#ifdef PERL_TRACK_MEMPOOL
96 *(tTHX*)ptr = aTHX;
97 ptr = (Malloc_t)((char*)ptr+sTHX);
98#endif
8d063cd8 99 return ptr;
fe397c97 100}
3280af22 101 else if (PL_nomemok)
0e2d6244 102 return NULL;
8d063cd8 103 else {
8c89da26 104 return write_no_mem();
8d063cd8
LW
105 }
106 /*NOTREACHED*/
107}
108
f2517201 109/* paranoid version of system's realloc() */
8d063cd8 110
bd4080b3 111Malloc_t
4f63d024 112Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 113{
54aff467 114 dTHX;
bd4080b3 115 Malloc_t ptr;
9a34ef1d 116#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO)
6ad3d225 117 Malloc_t PerlMem_realloc();
ecfc5424 118#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 119
a1d180c4 120#ifdef HAS_64K_LIMIT
5f05dabc 121 if (size > 0xffff) {
bf49b057 122 PerlIO_printf(Perl_error_log,
5f05dabc 123 "Reallocation too large: %lx\n", size) FLUSH;
54aff467 124 my_exit(1);
5f05dabc 125 }
55497cff 126#endif /* HAS_64K_LIMIT */
7614df0c 127 if (!size) {
f2517201 128 safesysfree(where);
7614df0c
JD
129 return NULL;
130 }
131
378cc40b 132 if (!where)
f2517201 133 return safesysmalloc(size);
fe397c97
JD
134#ifdef PERL_TRACK_MEMPOOL
135 where = (Malloc_t)((char*)where-sTHX);
136 size += sTHX;
137 if (*(tTHX*)where != aTHX) {
fe397c97
JD
138 Perl_croak_nocontext("panic: realloc from wrong pool");
139 }
140#endif
34de22dd
LW
141#ifdef DEBUGGING
142 if ((long)size < 0)
4f63d024 143 Perl_croak_nocontext("panic: realloc");
34de22dd 144#endif
12ae5dfc 145 ptr = (Malloc_t)PerlMem_realloc(where,size);
da927450 146 PERL_ALLOC_CHECK(ptr);
a1d180c4 147
97835f67
JH
148 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
149 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
79072805 150
fe397c97
JD
151 if (ptr != NULL) {
152#ifdef PERL_TRACK_MEMPOOL
153 ptr = (Malloc_t)((char*)ptr+sTHX);
154#endif
8d063cd8 155 return ptr;
fe397c97 156 }
3280af22 157 else if (PL_nomemok)
0e2d6244 158 return NULL;
8d063cd8 159 else {
8c89da26 160 return write_no_mem();
8d063cd8
LW
161 }
162 /*NOTREACHED*/
163}
164
f2517201 165/* safe version of system's free() */
8d063cd8 166
54310121 167Free_t
4f63d024 168Perl_safesysfree(Malloc_t where)
8d063cd8 169{
fe397c97 170#if defined(PERL_IMPLICIT_SYS) || defined(PERL_TRACK_MEMPOOL)
54aff467 171 dTHX;
155aba94 172#endif
97835f67 173 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
378cc40b 174 if (where) {
fe397c97
JD
175#ifdef PERL_TRACK_MEMPOOL
176 where = (Malloc_t)((char*)where-sTHX);
177 if (*(tTHX*)where != aTHX) {
fe397c97
JD
178 Perl_croak_nocontext("panic: free from wrong pool");
179 }
180#endif
6ad3d225 181 PerlMem_free(where);
378cc40b 182 }
8d063cd8
LW
183}
184
f2517201 185/* safe version of system's calloc() */
1050c9ca 186
bd4080b3 187Malloc_t
4f63d024 188Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 189{
54aff467 190 dTHX;
bd4080b3 191 Malloc_t ptr;
1050c9ca 192
55497cff 193#ifdef HAS_64K_LIMIT
5f05dabc 194 if (size * count > 0xffff) {
bf49b057 195 PerlIO_printf(Perl_error_log,
5f05dabc 196 "Allocation too large: %lx\n", size * count) FLUSH;
54aff467 197 my_exit(1);
5f05dabc 198 }
55497cff 199#endif /* HAS_64K_LIMIT */
1050c9ca
PP
200#ifdef DEBUGGING
201 if ((long)size < 0 || (long)count < 0)
4f63d024 202 Perl_croak_nocontext("panic: calloc");
1050c9ca 203#endif
0b7c1c42 204 size *= count;
fe397c97
JD
205#ifdef PERL_TRACK_MEMPOOL
206 size += sTHX;
207#endif
12ae5dfc 208 ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
da927450 209 PERL_ALLOC_CHECK(ptr);
97835f67 210 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)size));
0e2d6244 211 if (ptr != NULL) {
1050c9ca 212 memset((void*)ptr, 0, size);
fe397c97
JD
213#ifdef PERL_TRACK_MEMPOOL
214 *(tTHX*)ptr = aTHX;
215 ptr = (Malloc_t)((char*)ptr+sTHX);
216#endif
1050c9ca
PP
217 return ptr;
218 }
3280af22 219 else if (PL_nomemok)
0e2d6244 220 return NULL;
8c89da26 221 return write_no_mem();
1050c9ca
PP
222}
223
cae6d0e5
GS
224/* These must be defined when not using Perl's malloc for binary
225 * compatibility */
226
227#ifndef MYMALLOC
228
229Malloc_t Perl_malloc (MEM_SIZE nbytes)
230{
231 dTHXs;
077a72a9 232 return (Malloc_t)PerlMem_malloc(nbytes);
cae6d0e5
GS
233}
234
235Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
236{
237 dTHXs;
077a72a9 238 return (Malloc_t)PerlMem_calloc(elements, size);
cae6d0e5
GS
239}
240
241Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
242{
243 dTHXs;
077a72a9 244 return (Malloc_t)PerlMem_realloc(where, nbytes);
cae6d0e5
GS
245}
246
247Free_t Perl_mfree (Malloc_t where)
248{
249 dTHXs;
250 PerlMem_free(where);
251}
252
253#endif
254
8d063cd8
LW
255/* copy a string up to some (non-backslashed) delimiter, if any */
256
257char *
864dbfa3 258Perl_delimcpy(pTHX_ register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
8d063cd8 259{
fc36a67e
PP
260 register I32 tolen;
261 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b
LW
262 if (*from == '\\') {
263 if (from[1] == delim)
264 from++;
fc36a67e
PP
265 else {
266 if (to < toend)
267 *to++ = *from;
268 tolen++;
269 from++;
270 }
378cc40b 271 }
bedebaa5 272 else if (*from == delim)
8d063cd8 273 break;
fc36a67e
PP
274 if (to < toend)
275 *to++ = *from;
8d063cd8 276 }
bedebaa5
CS
277 if (to < toend)
278 *to = '\0';
fc36a67e 279 *retlen = tolen;
e2b56717 280 return (char *)from;
8d063cd8
LW
281}
282
283/* return ptr to little string in big string, NULL if not found */
378cc40b 284/* This routine was donated by Corey Satten. */
8d063cd8
LW
285
286char *
864dbfa3 287Perl_instr(pTHX_ register const char *big, register const char *little)
378cc40b 288{
79072805 289 register I32 first;
378cc40b 290
a687059c 291 if (!little)
08105a92 292 return (char*)big;
a687059c 293 first = *little++;
378cc40b 294 if (!first)
08105a92 295 return (char*)big;
378cc40b 296 while (*big) {
4996ee04 297 register const char *s, *x;
378cc40b
LW
298 if (*big++ != first)
299 continue;
300 for (x=big,s=little; *s; /**/ ) {
301 if (!*x)
0e2d6244 302 return NULL;
0507e8a3 303 if (*s != *x)
378cc40b 304 break;
0507e8a3
AL
305 else {
306 s++;
307 x++;
378cc40b
LW
308 }
309 }
310 if (!*s)
08105a92 311 return (char*)(big-1);
378cc40b 312 }
0e2d6244 313 return NULL;
378cc40b 314}
8d063cd8 315
a687059c
LW
316/* same as instr but allow embedded nulls */
317
318char *
0507e8a3 319Perl_ninstr(pTHX_ const char *big, const char *bigend, const char *little, const char *lend)
8d063cd8 320{
0507e8a3
AL
321 if (little >= lend)
322 return (char*)big;
323 {
324 char first = *little++;
325 const char *s, *x;
326 bigend -= lend - little;
327 OUTER:
328 while (big <= bigend) {
329 if (*big++ != first)
330 goto OUTER;
331 for (x=big,s=little; s < lend; x++,s++) {
332 if (*s != *x)
333 goto OUTER;
334 }
335 return (char*)(big-1);
336 }
378cc40b 337 }
0e2d6244 338 return NULL;
a687059c
LW
339}
340
341/* reverse of the above--find last substring */
342
343char *
864dbfa3 344Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend)
a687059c 345{
08105a92 346 register const char *bigbeg;
c05e0e2f 347 register const I32 first = *little;
6d29369a 348 register const char * const littleend = lend;
a687059c 349
0507e8a3 350 if (little >= littleend)
08105a92 351 return (char*)bigend;
a687059c
LW
352 bigbeg = big;
353 big = bigend - (littleend - little++);
354 while (big >= bigbeg) {
4996ee04 355 register const char *s, *x;
a687059c
LW
356 if (*big-- != first)
357 continue;
358 for (x=big+2,s=little; s < littleend; /**/ ) {
0507e8a3 359 if (*s != *x)
a687059c 360 break;
0507e8a3
AL
361 else {
362 x++;
363 s++;
a687059c
LW
364 }
365 }
366 if (s >= littleend)
08105a92 367 return (char*)(big+1);
378cc40b 368 }
0e2d6244 369 return NULL;
378cc40b 370}
a687059c 371
cf93c79d
IZ
372#define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/
373
374/* As a space optimization, we do not compile tables for strings of length
375 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are
376 special-cased in fbm_instr().
377
378 If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
379
954c1994 380/*
ccfc67b7
JH
381=head1 Miscellaneous Functions
382
954c1994
GS
383=for apidoc fbm_compile
384
385Analyses the string in order to make fast searches on it using fbm_instr()
386-- the Boyer-Moore algorithm.
387
388=cut
389*/
390
378cc40b 391void
7506f9c3 392Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
378cc40b 393{
3bad88ff 394 register const U8 *s;
79072805 395 register U32 i;
0b71040e 396 STRLEN len;
79072805
LW
397 I32 rarest = 0;
398 U32 frequency = 256;
399
d34f9d2e 400 if (flags & FBMcf_TAIL) {
c6d79d47 401 MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
d7559646 402 sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */
d34f9d2e
JH
403 if (mg && mg->mg_len >= 0)
404 mg->mg_len++;
405 }
8ec8dcb0 406 s = (U8*)SvPV_force_mutable(sv, len);
07f14f54 407 (void)SvUPGRADE(sv, SVt_PVBM);
d1be9408 408 if (len == 0) /* TAIL might be on a zero-length string. */
cf93c79d 409 return;
02128f11 410 if (len > 2) {
8ec8dcb0 411 const unsigned char *sb;
4996ee04 412 const U8 mlen = (len>255) ? 255 : (U8)len;
c6d79d47 413 register U8 *table;
cf93c79d 414
7506f9c3 415 Sv_Grow(sv, len + 256 + FBM_TABLE_OFFSET);
8ec8dcb0 416 table = (unsigned char*)(SvPVX_mutable(sv) + len + FBM_TABLE_OFFSET);
7506f9c3
GS
417 s = table - 1 - FBM_TABLE_OFFSET; /* last char */
418 memset((void*)table, mlen, 256);
419 table[-1] = (U8)flags;
02128f11 420 i = 0;
7506f9c3 421 sb = s - mlen + 1; /* first char (maybe) */
cf93c79d
IZ
422 while (s >= sb) {
423 if (table[*s] == mlen)
7506f9c3 424 table[*s] = (U8)i;
cf93c79d
IZ
425 s--, i++;
426 }
378cc40b 427 }
0e2d6244 428 sv_magic(sv, NULL, PERL_MAGIC_bm, NULL, 0); /* deep magic */
79072805 429 SvVALID_on(sv);
378cc40b 430
8ec8dcb0 431 s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */
bbce6d69 432 for (i = 0; i < len; i++) {
22c35a8c 433 if (PL_freq[s[i]] < frequency) {
bbce6d69 434 rarest = i;
22c35a8c 435 frequency = PL_freq[s[i]];
378cc40b
LW
436 }
437 }
79072805 438 BmRARE(sv) = s[rarest];
eb160463 439 BmPREVIOUS(sv) = (U16)rarest;
cf93c79d
IZ
440 BmUSEFUL(sv) = 100; /* Initial value */
441 if (flags & FBMcf_TAIL)
442 SvTAIL_on(sv);
7506f9c3
GS
443 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",
444 BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
445}
446
cf93c79d
IZ
447/* If SvTAIL(littlestr), it has a fake '\n' at end. */
448/* If SvTAIL is actually due to \Z or \z, this gives false positives
449 if multiline */
450
954c1994
GS
451/*
452=for apidoc fbm_instr
453
454Returns the location of the SV in the string delimited by C<str> and
0e2d6244 455C<strend>. It returns C<NULL> if the string can't be found. The C<sv>
954c1994
GS
456does not have to be fbm_compiled, but the search will not be as fast
457then.
458
459=cut
460*/
461
378cc40b 462char *
864dbfa3 463Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
378cc40b 464{
a687059c 465 register unsigned char *s;
cf93c79d 466 STRLEN l;
8ec8dcb0
NC
467 register const unsigned char *little
468 = (const unsigned char *)SvPV_const(littlestr,l);
cf93c79d 469 register STRLEN littlelen = l;
c05e0e2f 470 register const I32 multiline = flags & FBMrf_MULTILINE;
cf93c79d 471
eb160463 472 if ((STRLEN)(bigend - big) < littlelen) {
a1d180c4 473 if ( SvTAIL(littlestr)
eb160463 474 && ((STRLEN)(bigend - big) == littlelen - 1)
a1d180c4 475 && (littlelen == 1
12ae5dfc 476 || (*big == *little &&
fe20fd30 477 memEQ((char *)big, (char *)little, littlelen - 1))))
cf93c79d 478 return (char*)big;
0e2d6244 479 return NULL;
cf93c79d 480 }
378cc40b 481
cf93c79d 482 if (littlelen <= 2) { /* Special-cased */
cf93c79d
IZ
483
484 if (littlelen == 1) {
485 if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
486 /* Know that bigend != big. */
487 if (bigend[-1] == '\n')
488 return (char *)(bigend - 1);
489 return (char *) bigend;
490 }
491 s = big;
492 while (s < bigend) {
493 if (*s == *little)
494 return (char *)s;
495 s++;
496 }
497 if (SvTAIL(littlestr))
498 return (char *) bigend;
0e2d6244 499 return NULL;
cf93c79d
IZ
500 }
501 if (!littlelen)
502 return (char*)big; /* Cannot be SvTAIL! */
503
504 /* littlelen is 2 */
505 if (SvTAIL(littlestr) && !multiline) {
506 if (bigend[-1] == '\n' && bigend[-2] == *little)
507 return (char*)bigend - 2;
508 if (bigend[-1] == *little)
509 return (char*)bigend - 1;
0e2d6244 510 return NULL;
cf93c79d
IZ
511 }
512 {
513 /* This should be better than FBM if c1 == c2, and almost
514 as good otherwise: maybe better since we do less indirection.
515 And we save a lot of memory by caching no table. */
4996ee04
AL
516 const unsigned char c1 = little[0];
517 const unsigned char c2 = little[1];
cf93c79d
IZ
518
519 s = big + 1;
520 bigend--;
521 if (c1 != c2) {
522 while (s <= bigend) {
523 if (s[0] == c2) {
524 if (s[-1] == c1)
525 return (char*)s - 1;
526 s += 2;
527 continue;
3fe6f2dc 528 }
cf93c79d
IZ
529 next_chars:
530 if (s[0] == c1) {
531 if (s == bigend)
532 goto check_1char_anchor;
533 if (s[1] == c2)
534 return (char*)s;
535 else {
536 s++;
537 goto next_chars;
538 }
539 }
540 else
541 s += 2;
542 }
543 goto check_1char_anchor;
544 }
545 /* Now c1 == c2 */
546 while (s <= bigend) {
547 if (s[0] == c1) {
548 if (s[-1] == c1)
549 return (char*)s - 1;
550 if (s == bigend)
551 goto check_1char_anchor;
552 if (s[1] == c1)
553 return (char*)s;
554 s += 3;
02128f11 555 }
c277df42 556 else
cf93c79d 557 s += 2;
c277df42 558 }
c277df42 559 }
cf93c79d
IZ
560 check_1char_anchor: /* One char and anchor! */
561 if (SvTAIL(littlestr) && (*bigend == *little))
562 return (char *)bigend; /* bigend is already decremented. */
0e2d6244 563 return NULL;
d48672a2 564 }
cf93c79d 565 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
bbce6d69 566 s = bigend - littlelen;
a1d180c4 567 if (s >= big && bigend[-1] == '\n' && *s == *little
cf93c79d
IZ
568 /* Automatically of length > 2 */
569 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
7506f9c3 570 {
bbce6d69 571 return (char*)s; /* how sweet it is */
7506f9c3
GS
572 }
573 if (s[1] == *little
574 && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
575 {
cf93c79d 576 return (char*)s + 1; /* how sweet it is */
7506f9c3 577 }
0e2d6244 578 return NULL;
02128f11 579 }
cf93c79d 580 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
1a9219e7 581 char * const b = ninstr((char*)big,(char*)bigend,
cf93c79d
IZ
582 (char*)little, (char*)little + littlelen);
583
584 if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */
585 /* Chop \n from littlestr: */
586 s = bigend - littlelen + 1;
7506f9c3
GS
587 if (*s == *little
588 && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
589 {
3fe6f2dc 590 return (char*)s;
7506f9c3 591 }
0e2d6244 592 return NULL;
a687059c 593 }
cf93c79d 594 return b;
a687059c 595 }
a1d180c4 596
cf93c79d 597 { /* Do actual FBM. */
1a9219e7 598 register const unsigned char * const table = little + littlelen + FBM_TABLE_OFFSET;
3bad88ff 599 register const unsigned char *oldlittle;
cf93c79d 600
eb160463 601 if (littlelen > (STRLEN)(bigend - big))
0e2d6244 602 return NULL;
cf93c79d
IZ
603 --littlelen; /* Last char found by table lookup */
604
605 s = big + littlelen;
606 little += littlelen; /* last char */
607 oldlittle = little;
608 if (s < bigend) {
609 register I32 tmp;
610
611 top2:
7506f9c3 612 if ((tmp = table[*s])) {
cf93c79d 613 if ((s += tmp) < bigend)
62b28dd9 614 goto top2;
cf93c79d
IZ
615 goto check_end;
616 }
617 else { /* less expensive than calling strncmp() */
4996ee04 618 register unsigned char * const olds = s;
cf93c79d
IZ
619
620 tmp = littlelen;
621
622 while (tmp--) {
623 if (*--s == *--little)
624 continue;
cf93c79d
IZ
625 s = olds + 1; /* here we pay the price for failure */
626 little = oldlittle;
627 if (s < bigend) /* fake up continue to outer loop */
628 goto top2;
629 goto check_end;
630 }
631 return (char *)s;
a687059c 632 }
378cc40b 633 }
cf93c79d
IZ
634 check_end:
635 if ( s == bigend && (table[-1] & FBMcf_TAIL)
12ae5dfc
JH
636 && memEQ((char *)(bigend - littlelen),
637 (char *)(oldlittle - littlelen), littlelen) )
cf93c79d 638 return (char*)bigend - littlelen;
0e2d6244 639 return NULL;
378cc40b 640 }
378cc40b
LW
641}
642
c277df42
IZ
643/* start_shift, end_shift are positive quantities which give offsets
644 of ends of some substring of bigstr.
5332c881 645 If "last" we want the last occurrence.
c277df42 646 old_posp is the way of communication between consequent calls if
a1d180c4 647 the next call needs to find the .
c277df42 648 The initial *old_posp should be -1.
cf93c79d
IZ
649
650 Note that we take into account SvTAIL, so one can get extra
651 optimizations if _ALL flag is set.
c277df42
IZ
652 */
653
cf93c79d 654/* If SvTAIL is actually due to \Z or \z, this gives false positives
26fa51c3 655 if PL_multiline. In fact if !PL_multiline the authoritative answer
cf93c79d
IZ
656 is not supported yet. */
657
378cc40b 658char *
864dbfa3 659Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 660{
3bad88ff 661 register const unsigned char *big;
79072805
LW
662 register I32 pos;
663 register I32 previous;
664 register I32 first;
3bad88ff 665 register const unsigned char *little;
c277df42 666 register I32 stop_pos;
3bad88ff 667 register const unsigned char *littleend;
c277df42 668 I32 found = 0;
378cc40b 669
c277df42 670 if (*old_posp == -1
3280af22 671 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
cf93c79d
IZ
672 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) {
673 cant_find:
a1d180c4 674 if ( BmRARE(littlestr) == '\n'
cf93c79d 675 && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
71a0dd65 676 little = (const unsigned char *)(SvPVX_const(littlestr));
cf93c79d
IZ
677 littleend = little + SvCUR(littlestr);
678 first = *little++;
679 goto check_tail;
680 }
0e2d6244 681 return NULL;
cf93c79d
IZ
682 }
683
71a0dd65 684 little = (const unsigned char *)(SvPVX_const(littlestr));
79072805 685 littleend = little + SvCUR(littlestr);
378cc40b 686 first = *little++;
c277df42 687 /* The value of pos we can start at: */
79072805 688 previous = BmPREVIOUS(littlestr);
71a0dd65 689 big = (const unsigned char *)(SvPVX_const(bigstr));
c277df42
IZ
690 /* The value of pos we can stop at: */
691 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
cf93c79d 692 if (previous + start_shift > stop_pos) {
0fe87f7c
HS
693/*
694 stop_pos does not include SvTAIL in the count, so this check is incorrect
695 (I think) - see [ID 20010618.006] and t/op/study.t. HVDS 2001/06/19
696*/
697#if 0
cf93c79d
IZ
698 if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */
699 goto check_tail;
0fe87f7c 700#endif
0e2d6244 701 return NULL;
cf93c79d 702 }
c277df42 703 while (pos < previous + start_shift) {
3280af22 704 if (!(pos += PL_screamnext[pos]))
cf93c79d 705 goto cant_find;
378cc40b 706 }
de3bb511 707 big -= previous;
bbce6d69 708 do {
3bad88ff 709 register const unsigned char *s, *x;
ef64f398 710 if (pos >= stop_pos) break;
bbce6d69
PP
711 if (big[pos] != first)
712 continue;
713 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69
PP
714 if (*s++ != *x++) {
715 s--;
716 break;
378cc40b 717 }
bbce6d69 718 }
c277df42
IZ
719 if (s == littleend) {
720 *old_posp = pos;
721 if (!last) return (char *)(big+pos);
722 found = 1;
723 }
3280af22 724 } while ( pos += PL_screamnext[pos] );
a1d180c4 725 if (last && found)
cf93c79d 726 return (char *)(big+(*old_posp));
cf93c79d
IZ
727 check_tail:
728 if (!SvTAIL(littlestr) || (end_shift > 0))
0e2d6244 729 return NULL;
cf93c79d 730 /* Ignore the trailing "\n". This code is not microoptimized */
71a0dd65 731 big = (const unsigned char *)(SvPVX_const(bigstr) + SvCUR(bigstr));
cf93c79d
IZ
732 stop_pos = littleend - little; /* Actual littlestr len */
733 if (stop_pos == 0)
734 return (char*)big;
735 big -= stop_pos;
736 if (*big == first
12ae5dfc
JH
737 && ((stop_pos == 1) ||
738 memEQ((char *)(big + 1), (char *)little, stop_pos - 1)))
cf93c79d 739 return (char*)big;
0e2d6244 740 return NULL;
8d063cd8
LW
741}
742
79072805 743I32
864dbfa3 744Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len)
79072805 745{
c05e0e2f
AL
746 register const U8 *a = (const U8 *)s1;
747 register const U8 *b = (const U8 *)s2;
79072805 748 while (len--) {
22c35a8c 749 if (*a != *b && *a != PL_fold[*b])
bbce6d69
PP
750 return 1;
751 a++,b++;
752 }
753 return 0;
754}
755
756I32
864dbfa3 757Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len)
bbce6d69 758{
c05e0e2f
AL
759 register const U8 *a = (const U8 *)s1;
760 register const U8 *b = (const U8 *)s2;
bbce6d69 761 while (len--) {
22c35a8c 762 if (*a != *b && *a != PL_fold_locale[*b])
bbce6d69
PP
763 return 1;
764 a++,b++;
79072805
LW
765 }
766 return 0;
767}
768
8d063cd8
LW
769/* copy a string to a safe spot */
770
954c1994 771/*
ccfc67b7
JH
772=head1 Memory Management
773
954c1994
GS
774=for apidoc savepv
775
61a925ed
AMS
776Perl's version of C<strdup()>. Returns a pointer to a newly allocated
777string which is a duplicate of C<pv>. The size of the string is
778determined by C<strlen()>. The memory allocated for the new string can
779be freed with the C<Safefree()> function.
954c1994
GS
780
781=cut
782*/
783
8d063cd8 784char *
efdfce31 785Perl_savepv(pTHX_ const char *pv)
8d063cd8 786{
735fe74b 787 if (!pv)
0e2d6244 788 return NULL;
4996ee04
AL
789 else {
790 char *newaddr;
791 const STRLEN pvlen = strlen(pv)+1;
cd7a8267 792 Newx(newaddr,pvlen,char);
4c58c75a 793 return memcpy(newaddr,pv,pvlen);
4996ee04 794 }
735fe74b 795
8d063cd8
LW
796}
797
a687059c
LW
798/* same thing but with a known length */
799
954c1994
GS
800/*
801=for apidoc savepvn
802
61a925ed
AMS
803Perl's version of what C<strndup()> would be if it existed. Returns a
804pointer to a newly allocated string which is a duplicate of the first
805C<len> bytes from C<pv>. The memory allocated for the new string can be
806freed with the C<Safefree()> function.
954c1994
GS
807
808=cut
809*/
810
a687059c 811char *
efdfce31 812Perl_savepvn(pTHX_ const char *pv, register I32 len)
a687059c
LW
813{
814 register char *newaddr;
815
cd7a8267 816 Newx(newaddr,len+1,char);
92110913 817 /* Give a meaning to NULL pointer mainly for the use in sv_magic() */
efdfce31 818 if (pv) {
735fe74b
NC
819 /* might not be null terminated */
820 newaddr[len] = '\0';
206b424e 821 return (char *) CopyD(pv,newaddr,len,char);
92110913
NIS
822 }
823 else {
206b424e 824 return (char *) ZeroD(newaddr,len+1,char);
92110913 825 }
a687059c
LW
826}
827
05ec9bb3
NIS
828/*
829=for apidoc savesharedpv
830
61a925ed
AMS
831A version of C<savepv()> which allocates the duplicate string in memory
832which is shared between threads.
05ec9bb3
NIS
833
834=cut
835*/
836char *
efdfce31 837Perl_savesharedpv(pTHX_ const char *pv)
05ec9bb3 838{
735fe74b 839 register char *newaddr;
4c58c75a 840 STRLEN pvlen;
735fe74b 841 if (!pv)
0e2d6244 842 return NULL;
735fe74b 843
4c58c75a
NC
844 pvlen = strlen(pv)+1;
845 newaddr = (char*)PerlMemShared_malloc(pvlen);
735fe74b 846 if (!newaddr) {
8c89da26 847 return write_no_mem();
05ec9bb3 848 }
4c58c75a 849 return memcpy(newaddr,pv,pvlen);
05ec9bb3
NIS
850}
851
04851bb3
NC
852/*
853=for apidoc savesvpv
854
16ac37f7 855A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from
04851bb3
NC
856the passed in SV using C<SvPV()>
857
858=cut
859*/
860
861char *
862Perl_savesvpv(pTHX_ SV *sv)
863{
864 STRLEN len;
6d29369a 865 const char * const pv = SvPV_const(sv, len);
04851bb3
NC
866 register char *newaddr;
867
31ab2e0d 868 ++len;
cd7a8267 869 Newx(newaddr,len,char);
206b424e 870 return (char *) CopyD(pv,newaddr,len,char);
04851bb3 871}
05ec9bb3
NIS
872
873
cea2e8a9 874/* the SV for Perl_form() and mess() is not kept in an arena */
fc36a67e 875
76e3520e 876STATIC SV *
cea2e8a9 877S_mess_alloc(pTHX)
fc36a67e
PP
878{
879 SV *sv;
880 XPVMG *any;
881
e72dc28c 882 if (!PL_dirty)
d7559646 883 return sv_2mortal(newSVpvs(""));
e72dc28c 884
0372dbb6
GS
885 if (PL_mess_sv)
886 return PL_mess_sv;
887
fc36a67e 888 /* Create as PVMG now, to avoid any upgrading later */
cd7a8267
JC
889 Newx(sv, 1, SV);
890 Newxz(any, 1, XPVMG);
fc36a67e
PP
891 SvFLAGS(sv) = SVt_PVMG;
892 SvANY(sv) = (void*)any;
893 SvREFCNT(sv) = 1 << 30; /* practically infinite */
e72dc28c 894 PL_mess_sv = sv;
fc36a67e
PP
895 return sv;
896}
897
c5be433b 898#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
899char *
900Perl_form_nocontext(const char* pat, ...)
901{
902 dTHX;
c5be433b 903 char *retval;
cea2e8a9
GS
904 va_list args;
905 va_start(args, pat);
c5be433b 906 retval = vform(pat, &args);
cea2e8a9 907 va_end(args);
c5be433b 908 return retval;
cea2e8a9 909}
c5be433b 910#endif /* PERL_IMPLICIT_CONTEXT */
cea2e8a9 911
7c9e965c 912/*
ccfc67b7 913=head1 Miscellaneous Functions
7c9e965c
JP
914=for apidoc form
915
916Takes a sprintf-style format pattern and conventional
917(non-SV) arguments and returns the formatted string.
918
919 (char *) Perl_form(pTHX_ const char* pat, ...)
920
921can be used any place a string (char *) is required:
922
923 char * s = Perl_form("%d.%d",major,minor);
924
925Uses a single private buffer so if you want to format several strings you
926must explicitly copy the earlier strings away (and free the copies when you
927are done).
928
929=cut
930*/
931
8990e307 932char *
864dbfa3 933Perl_form(pTHX_ const char* pat, ...)
8990e307 934{
c5be433b 935 char *retval;
46fc3d4c 936 va_list args;
46fc3d4c 937 va_start(args, pat);
c5be433b 938 retval = vform(pat, &args);
46fc3d4c 939 va_end(args);
c5be433b
GS
940 return retval;
941}
942
943char *
944Perl_vform(pTHX_ const char *pat, va_list *args)
945{
339a2a6a 946 SV * const sv = mess_alloc();
0e2d6244 947 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
e72dc28c 948 return SvPVX(sv);
46fc3d4c 949}
a687059c 950
5a844595
GS
951#if defined(PERL_IMPLICIT_CONTEXT)
952SV *
953Perl_mess_nocontext(const char *pat, ...)
954{
955 dTHX;
956 SV *retval;
957 va_list args;
958 va_start(args, pat);
959 retval = vmess(pat, &args);
960 va_end(args);
961 return retval;
962}
963#endif /* PERL_IMPLICIT_CONTEXT */
964
06bf62c7 965SV *
5a844595
GS
966Perl_mess(pTHX_ const char *pat, ...)
967{
968 SV *retval;
969 va_list args;
970 va_start(args, pat);
971 retval = vmess(pat, &args);
972 va_end(args);
973 return retval;
974}
975
ae7d165c 976STATIC COP*
4f1e9d25 977S_closest_cop(pTHX_ COP *cop, const OP *o)
ae7d165c
PJ
978{
979 /* Look for PL_op starting from o. cop is the last COP we've seen. */
980
260c88e8
AL
981 if (!o || o == PL_op)
982 return cop;
ae7d165c
PJ
983
984 if (o->op_flags & OPf_KIDS) {
985 OP *kid;
260c88e8 986 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
ae7d165c
PJ
987 COP *new_cop;
988
989 /* If the OP_NEXTSTATE has been optimised away we can still use it
990 * the get the file and line number. */
991
992 if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
993 cop = (COP *)kid;
994
995 /* Keep searching, and return when we've found something. */
996
997 new_cop = closest_cop(cop, kid);
260c88e8
AL
998 if (new_cop)
999 return new_cop;
ae7d165c
PJ
1000 }
1001 }
1002
1003 /* Nothing found. */
1004
4f1e9d25 1005 return Null(COP *);
ae7d165c
PJ
1006}
1007
5a844595
GS
1008SV *
1009Perl_vmess(pTHX_ const char *pat, va_list *args)
46fc3d4c 1010{
1a9219e7 1011 SV * const sv = mess_alloc();
fe20fd30 1012 static const char dgd[] = " during global destruction.\n";
46fc3d4c 1013
fc36a67e 1014 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1015 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
ae7d165c
PJ
1016
1017 /*
1018 * Try and find the file and line for PL_op. This will usually be
1019 * PL_curcop, but it might be a cop that has been optimised away. We
1020 * can try to find such a cop by searching through the optree starting
1021 * from the sibling of PL_curcop.
1022 */
1023
c05e0e2f 1024 const COP *cop = closest_cop(PL_curcop, PL_curcop->op_sibling);
ae7d165c
PJ
1025 if (!cop) cop = PL_curcop;
1026
1027 if (CopLINE(cop))
ed094faf 1028 Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf,
5b7ea690 1029 OutCopFILE(cop), (IV)CopLINE(cop));
2035c5e8 1030 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
c05e0e2f 1031 const bool line_mode = (RsSIMPLE(PL_rs) &&
5e7e76a3 1032 SvCUR(PL_rs) == 1 && *SvPVX_const(PL_rs) == '\n');
57def98f 1033 Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf,
edc2eac3
JH
1034 PL_last_in_gv == PL_argvgv ?
1035 "" : GvNAME(PL_last_in_gv),
1036 line_mode ? "line" : "chunk",
1037 (IV)IoLINES(GvIOp(PL_last_in_gv)));
a687059c 1038 }
4d1ff10f 1039#ifdef USE_5005THREADS
e8e6f333
GS
1040 if (thr->tid)
1041 Perl_sv_catpvf(aTHX_ sv, " thread %ld", thr->tid);
9efbc0eb 1042#endif
515f54a1 1043 sv_catpv(sv, PL_dirty ? dgd : ".\n");
a687059c 1044 }
06bf62c7 1045 return sv;
a687059c
LW
1046}
1047
1324178b
JH
1048void
1049Perl_write_to_stderr(pTHX_ const char* message, int msglen)
1050{
1051 IO *io;
1052 MAGIC *mg;
1053
1054 if (PL_stderrgv && SvREFCNT(PL_stderrgv)
1055 && (io = GvIO(PL_stderrgv))
1056 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
1057 {
1058 dSP;
1059 ENTER;
1060 SAVETMPS;
1061
1062 save_re_context();
1063 SAVESPTR(PL_stderrgv);
0e2d6244 1064 PL_stderrgv = NULL;
1324178b
JH
1065
1066 PUSHSTACKi(PERLSI_MAGIC);
1067
1068 PUSHMARK(SP);
1069 EXTEND(SP,2);
1070 PUSHs(SvTIED_obj((SV*)io, mg));
1071 PUSHs(sv_2mortal(newSVpvn(message, msglen)));
1072 PUTBACK;
1073 call_method("PRINT", G_SCALAR);
1074
1075 POPSTACK;
1076 FREETMPS;
1077 LEAVE;
1078 }
1079 else {
1080#ifdef USE_SFIO
1081 /* SFIO can really mess with your errno */
0188be2e 1082 const int e = errno;
1324178b 1083#endif
0188be2e 1084 PerlIO * const serr = Perl_error_log;
1324178b
JH
1085
1086 PERL_WRITE_MSG_TO_CONSOLE(serr, message, msglen);
1087 (void)PerlIO_flush(serr);
1088#ifdef USE_SFIO
1089 errno = e;
1090#endif
1091 }
1092}
1093
145ab0cc 1094/* Common code used by vcroak, vdie, vwarn and vwarner */
65daad90 1095
145ab0cc
NC
1096STATIC bool
1097S_vdie_common(pTHX_ const char *message, STRLEN msglen, I32 utf8, bool warn)
65daad90
NC
1098{
1099 HV *stash;
1100 GV *gv;
1101 CV *cv;
145ab0cc
NC
1102 SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1103 /* sv_2cv might call Perl_croak() or Perl_warner() */
1104 SV * const oldhook = *hook;
1105
1106 assert(oldhook);
65daad90 1107
65daad90 1108 ENTER;
145ab0cc
NC
1109 SAVESPTR(*hook);
1110 *hook = NULL;
1111 cv = sv_2cv(oldhook, &stash, &gv, 0);
65daad90
NC
1112 LEAVE;
1113 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1114 dSP;
1115 SV *msg;
1738f5c4 1116
65daad90
NC
1117 ENTER;
1118 save_re_context();
145ab0cc
NC
1119 if (warn) {
1120 SAVESPTR(*hook);
1121 *hook = NULL;
1122 }
1123 if (warn || message) {
65daad90
NC
1124 msg = newSVpvn(message, msglen);
1125 SvFLAGS(msg) |= utf8;
1126 SvREADONLY_on(msg);
1127 SAVEFREESV(msg);
1128 }
1129 else {
1130 msg = ERRSV;
1738f5c4 1131 }
65daad90 1132
145ab0cc 1133 PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
65daad90
NC
1134 PUSHMARK(SP);
1135 XPUSHs(msg);
1136 PUTBACK;
1137 call_sv((SV*)cv, G_DISCARD);
1138 POPSTACK;
1139 LEAVE;
145ab0cc 1140 return TRUE;
36477c24 1141 }
145ab0cc 1142 return FALSE;
65daad90
NC
1143}
1144
767c9e37
AT
1145/* Whilst this should really be STATIC, it was not in 5.8.7, hence something
1146 may have linked against it. */
1147char *
1148S_vdie_croak_common(pTHX_ const char* pat, va_list* args, STRLEN* msglen,
1149 I32* utf8)
1150{
71a0dd65 1151 const char *message;
767c9e37
AT
1152
1153 if (pat) {
c6d79d47 1154 SV * const msv = vmess(pat, args);
767c9e37
AT
1155 if (PL_errors && SvCUR(PL_errors)) {
1156 sv_catsv(PL_errors, msv);
71a0dd65 1157 message = SvPV_const(PL_errors, *msglen);
767c9e37
AT
1158 SvCUR_set(PL_errors, 0);
1159 }
1160 else
71a0dd65 1161 message = SvPV_const(msv,*msglen);
767c9e37
AT
1162 *utf8 = SvUTF8(msv);
1163 }
1164 else {
0e2d6244 1165 message = NULL;
767c9e37
AT
1166 }
1167
1168 DEBUG_S(PerlIO_printf(Perl_debug_log,
1169 "%p: die/croak: message = %s\ndiehook = %p\n",
1170 thr, message, PL_diehook));
1171 if (PL_diehook) {
145ab0cc 1172 S_vdie_common(aTHX_ message, *msglen, *utf8, FALSE);
767c9e37 1173 }
ed6a78eb
NC
1174 /* Cast because we're not changing function prototypes in maint, and this
1175 function isn't actually static. */
1176 return (char *) message;
767c9e37
AT
1177}
1178
65daad90
NC
1179OP *
1180Perl_vdie(pTHX_ const char* pat, va_list *args)
1181{
e2b56717 1182 const char *message;
c05e0e2f 1183 const int was_in_eval = PL_in_eval;
65daad90
NC
1184 STRLEN msglen;
1185 I32 utf8 = 0;
1186
1187 DEBUG_S(PerlIO_printf(Perl_debug_log,
1188 "%p: die: curstack = %p, mainstack = %p\n",
1189 thr, PL_curstack, PL_mainstack));
1190
1191 message = S_vdie_croak_common(aTHX_ pat, args, &msglen, &utf8);
36477c24 1192
ed6a78eb 1193 PL_restartop = die_where((char *)message, msglen);
4baa34cc 1194 SvFLAGS(ERRSV) |= utf8;
bf49b057 1195 DEBUG_S(PerlIO_printf(Perl_debug_log,
7c06b590 1196 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
533c011a 1197 thr, PL_restartop, was_in_eval, PL_top_env));
3280af22 1198 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
6224f72b 1199 JMPENV_JUMP(3);
3280af22 1200 return PL_restartop;
36477c24
PP
1201}
1202
c5be433b 1203#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
1204OP *
1205Perl_die_nocontext(const char* pat, ...)
a687059c 1206{
cea2e8a9
GS
1207 dTHX;
1208 OP *o;
a687059c 1209 va_list args;
cea2e8a9 1210 va_start(args, pat);
c5be433b 1211 o = vdie(pat, &args);
cea2e8a9
GS
1212 va_end(args);
1213 return o;
1214}
c5be433b 1215#endif /* PERL_IMPLICIT_CONTEXT */
cea2e8a9
GS
1216
1217OP *
1218Perl_die(pTHX_ const char* pat, ...)
1219{
1220 OP *o;
1221 va_list args;
1222 va_start(args, pat);
c5be433b 1223 o = vdie(pat, &args);
cea2e8a9
GS
1224 va_end(args);
1225 return o;
1226}
1227
c5be433b
GS
1228void
1229Perl_vcroak(pTHX_ const char* pat, va_list *args)
cea2e8a9 1230{
e2b56717 1231 const char *message;
06bf62c7 1232 STRLEN msglen;
4baa34cc 1233 I32 utf8 = 0;
a687059c 1234
65daad90 1235 message = S_vdie_croak_common(aTHX_ pat, args, &msglen, &utf8);
5a844595 1236
3280af22 1237 if (PL_in_eval) {
ed6a78eb 1238 PL_restartop = die_where((char *) message, msglen);
4baa34cc 1239 SvFLAGS(ERRSV) |= utf8;
6224f72b 1240 JMPENV_JUMP(3);
a0d0e21e 1241 }
84414e3e 1242 else if (!message)
71a0dd65 1243 message = SvPVx_const(ERRSV, msglen);
84414e3e 1244
1324178b 1245 write_to_stderr(message, msglen);
f86702cc 1246 my_failure_exit();
a687059c
LW
1247}
1248
c5be433b 1249#if defined(PERL_IMPLICIT_CONTEXT)
8990e307 1250void
cea2e8a9 1251Perl_croak_nocontext(const char *pat, ...)
a687059c 1252{
cea2e8a9 1253 dTHX;
a687059c 1254 va_list args;
cea2e8a9 1255 va_start(args, pat);
c5be433b 1256 vcroak(pat, &args);
cea2e8a9
GS
1257 /* NOTREACHED */
1258 va_end(args);
1259}
1260#endif /* PERL_IMPLICIT_CONTEXT */
1261
954c1994 1262/*
ccfc67b7
JH
1263=head1 Warning and Dieing
1264
954c1994
GS
1265=for apidoc croak
1266
9983fa3c 1267This is the XSUB-writer's interface to Perl's C<die> function.
be0b3d4b
NC
1268Normally call this function the same way you call the C C<printf>
1269function. Calling C<croak> returns control directly to Perl,
1270sidestepping the normal C order of execution. See C<warn>.
9983fa3c
GS
1271
1272If you want to throw an exception object, assign the object to
0e2d6244 1273C<$@> and then pass C<NULL> to croak():
9983fa3c
GS
1274
1275 errsv = get_sv("@", TRUE);
1276 sv_setsv(errsv, exception_object);
0e2d6244 1277 croak(NULL);
954c1994
GS
1278
1279=cut
1280*/
1281
cea2e8a9
GS
1282void
1283Perl_croak(pTHX_ const char *pat, ...)
1284{
1285 va_list args;
1286 va_start(args, pat);
c5be433b 1287 vcroak(pat, &args);
cea2e8a9
GS
1288 /* NOTREACHED */
1289 va_end(args);
1290}
1291
c5be433b
GS
1292void
1293Perl_vwarn(pTHX_ const char* pat, va_list *args)
cea2e8a9 1294{
06bf62c7 1295 STRLEN msglen;
0188be2e
AL
1296 SV * const msv = vmess(pat, args);
1297 const I32 utf8 = SvUTF8(msv);
1298 const char * const message = SvPV_const(msv, msglen);
a687059c 1299
3280af22 1300 if (PL_warnhook) {
145ab0cc 1301 if (vdie_common(message, msglen, utf8, TRUE))
20cec16a 1302 return;
748a9306 1303 }
87582a92 1304
1324178b 1305 write_to_stderr(message, msglen);
a687059c 1306}
8d063cd8 1307
c5be433b 1308#if defined(PERL_IMPLICIT_CONTEXT)
cea2e8a9
GS
1309void
1310Perl_warn_nocontext(const char *pat, ...)
1311{
1312 dTHX;
1313 va_list args;
1314 va_start(args, pat);
c5be433b 1315 vwarn(pat, &args);
cea2e8a9
GS
1316 va_end(args);
1317}
1318#endif /* PERL_IMPLICIT_CONTEXT */
1319
954c1994
GS
1320/*
1321=for apidoc warn
1322
be0b3d4b
NC
1323This is the XSUB-writer's interface to Perl's C<warn> function. Call this
1324function the same way you call the C C<printf> function. See C<croak>.
954c1994
GS
1325
1326=cut
1327*/
1328
cea2e8a9
GS
1329void
1330Perl_warn(pTHX_ const char *pat, ...)
1331{
1332 va_list args;
1333 va_start(args, pat);
c5be433b 1334 vwarn(pat, &args);
cea2e8a9
GS
1335 va_end(args);
1336}
1337
c5be433b
GS
1338#if defined(PERL_IMPLICIT_CONTEXT)
1339void
1340Perl_warner_nocontext(U32 err, const char *pat, ...)
1341{
fe20fd30 1342 dTHX;
c5be433b
GS
1343 va_list args;
1344 va_start(args, pat);
1345 vwarner(err, pat, &args);
1346 va_end(args);
1347}
1348#endif /* PERL_IMPLICIT_CONTEXT */
1349
599cee73 1350void
864dbfa3 1351Perl_warner(pTHX_ U32 err, const char* pat,...)
599cee73
PM
1352{
1353 va_list args;
c5be433b
GS
1354 va_start(args, pat);
1355 vwarner(err, pat, &args);
1356 va_end(args);
1357}
1358
1359void
1360Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args)
1361{
599cee73 1362 if (ckDEAD(err)) {
8c18bf38 1363 SV * const msv = vmess(pat, args);
65daad90 1364 STRLEN msglen;
6d29369a 1365 const char * const message = SvPV_const(msv, msglen);
8c18bf38 1366 const I32 utf8 = SvUTF8(msv);
65daad90 1367
4d1ff10f 1368#ifdef USE_5005THREADS
5b7ea690 1369 DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s", PTR2UV(thr), message));
4d1ff10f 1370#endif /* USE_5005THREADS */
5b7ea690 1371 if (PL_diehook) {
65daad90 1372 assert(message);
145ab0cc 1373 S_vdie_common(aTHX_ message, msglen, utf8, FALSE);
5b7ea690
JH
1374 }
1375 if (PL_in_eval) {
ed6a78eb 1376 PL_restartop = die_where((char *) message, msglen);
4baa34cc 1377 SvFLAGS(ERRSV) |= utf8;
5b7ea690
JH
1378 JMPENV_JUMP(3);
1379 }
1324178b 1380 write_to_stderr(message, msglen);
5b7ea690 1381 my_failure_exit();
599cee73
PM
1382 }
1383 else {
65daad90 1384 Perl_vwarn(aTHX_ pat, args);
599cee73
PM
1385 }
1386}
1387
f5e9f069
NC
1388/* implements the ckWARN? macros */
1389
1390bool
1391Perl_ckwarn(pTHX_ U32 w)
1392{
1393 return
1394 (
1395 isLEXWARN_on
1396 && PL_curcop->cop_warnings != pWARN_NONE
1397 && (
1398 PL_curcop->cop_warnings == pWARN_ALL
1399 || isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))
1400 || (unpackWARN2(w) &&
1401 isWARN_on(PL_curcop->cop_warnings, unpackWARN2(w)))
1402 || (unpackWARN3(w) &&
1403 isWARN_on(PL_curcop->cop_warnings, unpackWARN3(w)))
1404 || (unpackWARN4(w) &&
1405 isWARN_on(PL_curcop->cop_warnings, unpackWARN4(w)))
1406 )
1407 )
1408 ||
1409 (
1410 isLEXWARN_off && PL_dowarn & G_WARN_ON
1411 )
1412 ;
1413}
1414
1415/* implements the ckWARN?_d macro */
1416
1417bool
1418Perl_ckwarn_d(pTHX_ U32 w)
1419{
1420 return
1421 isLEXWARN_off
1422 || PL_curcop->cop_warnings == pWARN_ALL
1423 || (
1424 PL_curcop->cop_warnings != pWARN_NONE
1425 && (
1426 isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))
1427 || (unpackWARN2(w) &&
1428 isWARN_on(PL_curcop->cop_warnings, unpackWARN2(w)))
1429 || (unpackWARN3(w) &&
1430 isWARN_on(PL_curcop->cop_warnings, unpackWARN3(w)))
1431 || (unpackWARN4(w) &&
1432 isWARN_on(PL_curcop->cop_warnings, unpackWARN4(w)))
1433 )
1434 )
1435 ;
1436}
1437
1438
1439
e6587932
DM
1440/* since we've already done strlen() for both nam and val
1441 * we can use that info to make things faster than
1442 * sprintf(s, "%s=%s", nam, val)
1443 */
1444#define my_setenv_format(s, nam, nlen, val, vlen) \
1445 Copy(nam, s, nlen, char); \
1446 *(s+nlen) = '='; \
1447 Copy(val, s+(nlen+1), vlen, char); \
1448 *(s+(nlen+1+vlen)) = '\0'
1449
13b6e58c 1450#ifdef USE_ENVIRON_ARRAY
eccd403f 1451 /* VMS' my_setenv() is in vms.c */
2986a63f 1452#if !defined(WIN32) && !defined(NETWARE)
8d063cd8 1453void
864dbfa3 1454Perl_my_setenv(pTHX_ char *nam, char *val)
8d063cd8 1455{
4efc5df6
GS
1456#ifdef USE_ITHREADS
1457 /* only parent thread can modify process environment */
1458 if (PL_curinterp == aTHX)
1459#endif
1460 {
f2517201 1461#ifndef PERL_USE_SAFE_PUTENV
620ebc51 1462 if (!PL_use_safe_putenv) {
f2517201 1463 /* most putenv()s leak, so we manipulate environ directly */
79072805 1464 register I32 i=setenv_getix(nam); /* where does it go? */
e6587932 1465 int nlen, vlen;
8d063cd8 1466
3280af22 1467 if (environ == PL_origenviron) { /* need we copy environment? */
79072805
LW
1468 I32 j;
1469 I32 max;
fe14fcc3
LW
1470 char **tmpenv;
1471
1472 for (max = i; environ[max]; max++) ;
f2517201
GS
1473 tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
1474 for (j=0; j<max; j++) { /* copy environment */
c05e0e2f 1475 const int len = strlen(environ[j]);
5b7ea690
JH
1476 tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
1477 Copy(environ[j], tmpenv[j], len+1, char);
f2517201 1478 }
0e2d6244 1479 tmpenv[max] = NULL;
fe14fcc3
LW
1480 environ = tmpenv; /* tell exec where it is now */
1481 }
a687059c 1482 if (!val) {
f2517201 1483 safesysfree(environ[i]);
a687059c
LW
1484 while (environ[i]) {
1485 environ[i] = environ[i+1];
1486 i++;
1487 }
1488 return;
1489 }
8d063cd8 1490 if (!environ[i]) { /* does not exist yet */
f2517201 1491 environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
0e2d6244 1492 environ[i+1] = NULL; /* make sure it's null terminated */
8d063cd8 1493 }
fe14fcc3 1494 else
f2517201 1495 safesysfree(environ[i]);
e6587932
DM
1496 nlen = strlen(nam);
1497 vlen = strlen(val);
f2517201 1498
e6587932
DM
1499 environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
1500 /* all that work just for this */
1501 my_setenv_format(environ[i], nam, nlen, val, vlen);
620ebc51
MS
1502 } else {
1503# endif
fe20fd30 1504# if defined(__CYGWIN__) || defined(EPOC) || defined(SYMBIAN)
64596349
AB
1505# if defined(HAS_UNSETENV)
1506 if (val == NULL) {
1507 (void)unsetenv(nam);
1508 } else {
1509 (void)setenv(nam, val, 1);
1510 }
1511# else /* ! HAS_UNSETENV */
1512 (void)setenv(nam, val, 1);
1513# endif /* HAS_UNSETENV */
47dafe4d 1514# else
64596349
AB
1515# if defined(HAS_UNSETENV)
1516 if (val == NULL) {
1517 (void)unsetenv(nam);
1518 } else {
1a9219e7
AL
1519 const int nlen = strlen(nam);
1520 const int vlen = strlen(val);
1521 char * const new_env =
64596349
AB
1522 (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
1523 my_setenv_format(new_env, nam, nlen, val, vlen);
1524 (void)putenv(new_env);
1525 }
1526# else /* ! HAS_UNSETENV */
1527 char *new_env;
1a9219e7
AL
1528 const int nlen = strlen(nam);
1529 int vlen;
64596349
AB
1530 if (!val) {
1531 val = "";
1532 }
1533 vlen = strlen(val);
1534 new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
1535 /* all that work just for this */
1536 my_setenv_format(new_env, nam, nlen, val, vlen);
1537 (void)putenv(new_env);
1538# endif /* HAS_UNSETENV */
47dafe4d 1539# endif /* __CYGWIN__ */
620ebc51
MS
1540#ifndef PERL_USE_SAFE_PUTENV
1541 }
1542#endif
4efc5df6 1543 }
8d063cd8
LW
1544}
1545
2986a63f 1546#else /* WIN32 || NETWARE */
68dc0745
PP
1547
1548void
fa3e6490 1549Perl_my_setenv(pTHX_ char *nam, char *val)
68dc0745 1550{
ac5c734f 1551 register char *envstr;
c05e0e2f
AL
1552 const int nlen = strlen(nam);
1553 int vlen;
e6587932 1554
ac5c734f
GS
1555 if (!val) {
1556 val = "";
1557 }
e6587932 1558 vlen = strlen(val);
cd7a8267 1559 Newx(envstr, nlen+vlen+2, char);
e6587932 1560 my_setenv_format(envstr, nam, nlen, val, vlen);
ac5c734f
GS
1561 (void)PerlEnv_putenv(envstr);
1562 Safefree(envstr);
3e3baf6d
TB
1563}
1564
2986a63f 1565#endif /* WIN32 || NETWARE */
3e3baf6d 1566
75a5c1c6 1567#ifndef PERL_MICRO
3e3baf6d 1568I32
864dbfa3 1569Perl_setenv_getix(pTHX_ char *nam)
3e3baf6d 1570{
0188be2e 1571 register I32 i;
3bad88ff 1572 register const I32 len = strlen(nam);
3e3baf6d
TB
1573
1574 for (i = 0; environ[i]; i++) {
1575 if (
1576#ifdef WIN32
1577 strnicmp(environ[i],nam,len) == 0
1578#else
1579 strnEQ(environ[i],nam,len)
1580#endif
1581 && environ[i][len] == '=')
1582 break; /* strnEQ must come first to avoid */
1583 } /* potential SEGV's */
1584 return i;
68dc0745 1585}
75a5c1c6 1586#endif /* !PERL_MICRO */
68dc0745 1587
ed79a026 1588#endif /* !VMS && !EPOC*/
378cc40b 1589
16d20bd9 1590#ifdef UNLINK_ALL_VERSIONS
79072805 1591I32
864dbfa3 1592Perl_unlnk(pTHX_ char *f) /* unlink all versions of a file */
378cc40b 1593{
79072805 1594 I32 i;
378cc40b 1595
6ad3d225 1596 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
378cc40b
LW
1597 return i ? 0 : -1;
1598}
1599#endif
1600
7a3f2258 1601/* this is a drop-in replacement for bcopy() */
2253333f 1602#if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY))
378cc40b 1603char *
7a3f2258 1604Perl_my_bcopy(register const char *from,register char *to,register I32 len)
378cc40b 1605{
339a2a6a 1606 char * const retval = to;
378cc40b 1607
7c0587c8
LW
1608 if (from - to >= 0) {
1609 while (len--)
1610 *to++ = *from++;
1611 }
1612 else {
1613 to += len;
1614 from += len;
1615 while (len--)
faf8582f 1616 *(--to) = *(--from);
7c0587c8 1617 }
378cc40b
LW
1618 return retval;
1619}
ffed7fef 1620#endif
378cc40b 1621
7a3f2258 1622/* this is a drop-in replacement for memset() */
fc36a67e
PP
1623#ifndef HAS_MEMSET
1624void *
7a3f2258 1625Perl_my_memset(register char *loc, register I32 ch, register I32 len)
fc36a67e 1626{
339a2a6a 1627 char * const retval = loc;
fc36a67e
PP
1628
1629 while (len--)
1630 *loc++ = ch;
1631 return retval;
1632}
1633#endif
1634
7a3f2258 1635/* this is a drop-in replacement for bzero() */
7c0587c8 1636#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1637char *
7a3f2258 1638Perl_my_bzero(register char *loc, register I32 len)
378cc40b 1639{
339a2a6a 1640 char * const retval = loc;
378cc40b
LW
1641
1642 while (len--)
1643 *loc++ = 0;
1644 return retval;
1645}
1646#endif
7c0587c8 1647
7a3f2258 1648/* this is a drop-in replacement for memcmp() */
36477c24 1649#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1650I32
7a3f2258 1651Perl_my_memcmp(const char *s1, const char *s2, register I32 len)
7c0587c8 1652{
c05e0e2f
AL
1653 register const U8 *a = (const U8 *)s1;
1654 register const U8 *b = (const U8 *)s2;
79072805 1655 register I32 tmp;
7c0587c8
LW
1656
1657 while (len--) {
fe20fd30 1658 if ((tmp = *a++ - *b++))
7c0587c8
LW
1659 return tmp;
1660 }
1661 return 0;
1662}
36477c24 1663#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1664
fe14fcc3 1665#ifndef HAS_VPRINTF
a687059c 1666
85e6fe83 1667#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1668char *
1669#else
1670int
1671#endif
08105a92 1672vsprintf(char *dest, const char *pat, char *args)
a687059c
LW
1673{
1674 FILE fakebuf;
1675
1676 fakebuf._ptr = dest;
1677 fakebuf._cnt = 32767;
35c8bce7
LW
1678#ifndef _IOSTRG
1679#define _IOSTRG 0
1680#endif
a687059c
LW
1681 fakebuf._flag = _IOWRT|_IOSTRG;
1682 _doprnt(pat, args, &fakebuf); /* what a kludge */
1683 (void)putc('\0', &fakebuf);
85e6fe83 1684#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1685 return(dest);
1686#else
1687 return 0; /* perl doesn't use return value */
1688#endif
1689}
1690
fe14fcc3 1691#endif /* HAS_VPRINTF */
a687059c
LW
1692
1693#ifdef MYSWAP
ffed7fef 1694#if BYTEORDER != 0x4321
a687059c 1695short
864dbfa3 1696Perl_my_swap(pTHX_ short s)
a687059c
LW
1697{
1698#if (BYTEORDER & 1) == 0
1699 short result;
1700
1701 result = ((s & 255) << 8) + ((s >> 8) & 255);
1702 return result;
1703#else
1704 return s;
1705#endif
1706}
1707
1708long
864dbfa3 1709Perl_my_htonl(pTHX_ long l)
a687059c
LW
1710{
1711 union {
1712 long result;
ffed7fef 1713 char c[sizeof(long)];
a687059c
LW
1714 } u;
1715
ffed7fef 1716#if BYTEORDER == 0x1234
a687059c
LW
1717 u.c[0] = (l >> 24) & 255;
1718 u.c[1] = (l >> 16) & 255;
1719 u.c[2] = (l >> 8) & 255;
1720 u.c[3] = l & 255;
1721 return u.result;
1722#else
ffed7fef 1723#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
cea2e8a9 1724 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
a687059c 1725#else
79072805
LW
1726 register I32 o;
1727 register I32 s;
a687059c 1728
ffed7fef
LW
1729 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1730 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1731 }
1732 return u.result;
1733#endif
1734#endif
1735}
1736
1737long
864dbfa3 1738Perl_my_ntohl(pTHX_ long l)
a687059c
LW
1739{
1740 union {
1741 long l;
ffed7fef 1742 char c[sizeof(long)];
a687059c
LW
1743 } u;
1744
ffed7fef 1745#if BYTEORDER == 0x1234
a687059c
LW
1746 u.c[0] = (l >> 24) & 255;
1747 u.c[1] = (l >> 16) & 255;
1748 u.c[2] = (l >> 8) & 255;
1749 u.c[3] = l & 255;
1750 return u.l;
1751#else
ffed7fef 1752#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
cea2e8a9 1753 Perl_croak(aTHX_ "Unknown BYTEORDER\n");
a687059c 1754#else
79072805
LW
1755 register I32 o;
1756 register I32 s;
a687059c
LW
1757
1758 u.l = l;
1759 l = 0;
ffed7fef
LW
1760 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1761 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1762 }
1763 return l;
1764#endif
1765#endif
1766}
1767
ffed7fef 1768#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1769#endif /* MYSWAP */
1770
1771/*
1772 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1773 * If these functions are defined,
1774 * the BYTEORDER is neither 0x1234 nor 0x4321.
1775 * However, this is not assumed.
1776 * -DWS
1777 */
1778
cc3ea0aa 1779#define HTOLE(name,type) \
988174c1 1780 type \
ba106d47 1781 name (register type n) \
988174c1
LW
1782 { \
1783 union { \
1784 type value; \
1785 char c[sizeof(type)]; \
1786 } u; \
79072805 1787 register I32 i; \
cc3ea0aa
NC
1788 register I32 s = 0; \
1789 for (i = 0; i < sizeof(u.c); i++, s += 8) { \
988174c1
LW
1790 u.c[i] = (n >> s) & 0xFF; \
1791 } \
1792 return u.value; \
1793 }
1794
cc3ea0aa 1795#define LETOH(name,type) \
988174c1 1796 type \
ba106d47 1797 name (register type n) \
988174c1
LW
1798 { \
1799 union { \
1800 type value; \
1801 char c[sizeof(type)]; \
1802 } u; \
79072805 1803 register I32 i; \
cc3ea0aa 1804 register I32 s = 0; \
988174c1
LW
1805 u.value = n; \
1806 n = 0; \
cc3ea0aa
NC
1807 for (i = 0; i < sizeof(u.c); i++, s += 8) { \
1808 n |= ((type)(u.c[i] & 0xFF)) << s; \
988174c1
LW
1809 } \
1810 return n; \
1811 }
1812
cc3ea0aa
NC
1813/*
1814 * Big-endian byte order functions.
1815 */
1816
1817#define HTOBE(name,type) \
1818 type \
1819 name (register type n) \
1820 { \
1821 union { \
1822 type value; \
1823 char c[sizeof(type)]; \
1824 } u; \
1825 register I32 i; \
1826 register I32 s = 8*(sizeof(u.c)-1); \
1827 for (i = 0; i < sizeof(u.c); i++, s -= 8) { \
1828 u.c[i] = (n >> s) & 0xFF; \
1829 } \
1830 return u.value; \
1831 }
1832
1833#define BETOH(name,type) \
1834 type \
1835 name (register type n) \
1836 { \
1837 union { \
1838 type value; \
1839 char c[sizeof(type)]; \
1840 } u; \
1841 register I32 i; \
1842 register I32 s = 8*(sizeof(u.c)-1); \
1843 u.value = n; \
1844 n = 0; \
1845 for (i = 0; i < sizeof(u.c); i++, s -= 8) { \
1846 n |= ((type)(u.c[i] & 0xFF)) << s; \
1847 } \
1848 return n; \
1849 }
1850
1851/*
1852 * If we just can't do it...
1853 */
1854
1855#define NOT_AVAIL(name,type) \
1856 type \
1857 name (register type n) \
1858 { \
1859 Perl_croak_nocontext(#name "() not available"); \
1860 return n; /* not reached */ \
1861 }
1862
1863
988174c1 1864#if defined(HAS_HTOVS) && !defined(htovs)
cc3ea0aa 1865HTOLE(htovs,short)
988174c1
LW
1866#endif
1867#if defined(HAS_HTOVL) && !defined(htovl)
cc3ea0aa 1868HTOLE(htovl,long)
988174c1
LW
1869#endif
1870#if defined(HAS_VTOHS) && !defined(vtohs)
cc3ea0aa 1871LETOH(vtohs,short)
988174c1
LW
1872#endif
1873#if defined(HAS_VTOHL) && !defined(vtohl)
cc3ea0aa
NC
1874LETOH(vtohl,long)
1875#endif
1876
1877#ifdef PERL_NEED_MY_HTOLE16
1878# if U16SIZE == 2
1879HTOLE(Perl_my_htole16,U16)
1880# else
1881NOT_AVAIL(Perl_my_htole16,U16)
1882# endif
1883#endif
1884#ifdef PERL_NEED_MY_LETOH16
1885# if U16SIZE == 2
1886LETOH(Perl_my_letoh16,U16)
1887# else
1888NOT_AVAIL(Perl_my_letoh16,U16)
1889# endif
1890#endif
1891#ifdef PERL_NEED_MY_HTOBE16
1892# if U16SIZE == 2
1893HTOBE(Perl_my_htobe16,U16)
1894# else
1895NOT_AVAIL(Perl_my_htobe16,U16)
1896# endif
1897#endif
1898#ifdef PERL_NEED_MY_BETOH16
1899# if U16SIZE == 2
1900BETOH(Perl_my_betoh16,U16)
1901# else
1902NOT_AVAIL(Perl_my_betoh16,U16)
1903# endif
1904#endif
1905
1906#ifdef PERL_NEED_MY_HTOLE32
1907# if U32SIZE == 4
1908HTOLE(Perl_my_htole32,U32)
1909# else
1910NOT_AVAIL(Perl_my_htole32,U32)
1911# endif
1912#endif
1913#ifdef PERL_NEED_MY_LETOH32
1914# if U32SIZE == 4
1915LETOH(Perl_my_letoh32,U32)
1916# else
1917NOT_AVAIL(Perl_my_letoh32,U32)
1918# endif
1919#endif
1920#ifdef PERL_NEED_MY_HTOBE32
1921# if U32SIZE == 4
1922HTOBE(Perl_my_htobe32,U32)
1923# else
1924NOT_AVAIL(Perl_my_htobe32,U32)
1925# endif
1926#endif
1927#ifdef PERL_NEED_MY_BETOH32
1928# if U32SIZE == 4
1929BETOH(Perl_my_betoh32,U32)
1930# else
1931NOT_AVAIL(Perl_my_betoh32,U32)
1932# endif
1933#endif
1934
1935#ifdef PERL_NEED_MY_HTOLE64
1936# if U64SIZE == 8
1937HTOLE(Perl_my_htole64,U64)
1938# else
1939NOT_AVAIL(Perl_my_htole64,U64)
1940# endif
1941#endif
1942#ifdef PERL_NEED_MY_LETOH64
1943# if U64SIZE == 8
1944LETOH(Perl_my_letoh64,U64)
1945# else
1946NOT_AVAIL(Perl_my_letoh64,U64)
1947# endif
1948#endif
1949#ifdef PERL_NEED_MY_HTOBE64
1950# if U64SIZE == 8
1951HTOBE(Perl_my_htobe64,U64)
1952# else
1953NOT_AVAIL(Perl_my_htobe64,U64)
1954# endif
1955#endif
1956#ifdef PERL_NEED_MY_BETOH64
1957# if U64SIZE == 8
1958BETOH(Perl_my_betoh64,U64)
1959# else
1960NOT_AVAIL(Perl_my_betoh64,U64)
1961# endif
988174c1 1962#endif
a687059c 1963
cc3ea0aa
NC
1964#ifdef PERL_NEED_MY_HTOLES
1965HTOLE(Perl_my_htoles,short)
1966#endif
1967#ifdef PERL_NEED_MY_LETOHS
1968LETOH(Perl_my_letohs,short)
1969#endif
1970#ifdef PERL_NEED_MY_HTOBES
1971HTOBE(Perl_my_htobes,short)
1972#endif
1973#ifdef PERL_NEED_MY_BETOHS
1974BETOH(Perl_my_betohs,short)
1975#endif
1976
1977#ifdef PERL_NEED_MY_HTOLEI
1978HTOLE(Perl_my_htolei,int)
1979#endif
1980#ifdef PERL_NEED_MY_LETOHI
1981LETOH(Perl_my_letohi,int)
1982#endif
1983#ifdef PERL_NEED_MY_HTOBEI
1984HTOBE(Perl_my_htobei,int)
1985#endif
1986#ifdef PERL_NEED_MY_BETOHI
1987BETOH(Perl_my_betohi,int)
1988#endif
1989
1990#ifdef PERL_NEED_MY_HTOLEL
1991HTOLE(Perl_my_htolel,long)
1992#endif
1993#ifdef PERL_NEED_MY_LETOHL
1994LETOH(Perl_my_letohl,long)
1995#endif
1996#ifdef PERL_NEED_MY_HTOBEL
1997HTOBE(Perl_my_htobel,long)
1998#endif
1999#ifdef PERL_NEED_MY_BETOHL
2000BETOH(Perl_my_betohl,long)
2001#endif
2002
2003void
2004Perl_my_swabn(void *ptr, int n)
2005{
2006 register char *s = (char *)ptr;
2007 register char *e = s + (n-1);
2008 register char tc;
2009
2010 for (n /= 2; n > 0; s++, e--, n--) {
2011 tc = *s;
2012 *s = *e;
2013 *e = tc;
2014 }
2015}
2016
4a7d1889
NIS
2017PerlIO *
2018Perl_my_popen_list(pTHX_ char *mode, int n, SV **args)
2019{
2986a63f 2020#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) && !defined(NETWARE)
1f852d0d
NIS
2021 int p[2];
2022 register I32 This, that;
2023 register Pid_t pid;
2024 SV *sv;
2025 I32 did_pipes = 0;
2026 int pp[2];
2027
2028 PERL_FLUSHALL_FOR_CHILD;
2029 This = (*mode == 'w');
2030 that = !This;
2031 if (PL_tainting) {
2032 taint_env();
2033 taint_proper("Insecure %s%s", "EXEC");
2034 }
2035 if (PerlProc_pipe(p) < 0)
0e2d6244 2036 return NULL;
1f852d0d
NIS
2037 /* Try for another pipe pair for error return */
2038 if (PerlProc_pipe(pp) >= 0)
2039 did_pipes = 1;
52e18b1f 2040 while ((pid = PerlProc_fork()) < 0) {
1f852d0d
NIS
2041 if (errno != EAGAIN) {
2042 PerlLIO_close(p[This]);
4e6dfe71 2043 PerlLIO_close(p[that]);
1f852d0d
NIS
2044 if (did_pipes) {
2045 PerlLIO_close(pp[0]);
2046 PerlLIO_close(pp[1]);
2047 }
0e2d6244 2048 return NULL;
1f852d0d
NIS
2049 }
2050 sleep(5);
2051 }
2052 if (pid == 0) {
2053 /* Child */
1f852d0d
NIS
2054#undef THIS
2055#undef THAT
2056#define THIS that
2057#define THAT This
1f852d0d
NIS
2058 /* Close parent's end of error status pipe (if any) */
2059 if (did_pipes) {
2060 PerlLIO_close(pp[0]);
2061#if defined(HAS_FCNTL) && defined(F_SETFD)
2062 /* Close error pipe automatically if exec works */
2063 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2064#endif
2065 }
2066 /* Now dup our end of _the_ pipe to right position */
2067 if (p[THIS] != (*mode == 'r')) {
2068 PerlLIO_dup2(p[THIS], *mode == 'r');
2069 PerlLIO_close(p[THIS]);
4e6dfe71
GS
2070 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2071 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
1f852d0d 2072 }
4e6dfe71
GS
2073 else
2074 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
1f852d0d
NIS
2075#if !defined(HAS_FCNTL) || !defined(F_SETFD)
2076 /* No automatic close - do it by hand */
b7953727
JH
2077# ifndef NOFILE
2078# define NOFILE 20
2079# endif
a080fe3d
NIS
2080 {
2081 int fd;
2082
2083 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
5b7ea690 2084 if (fd != pp[1])
a080fe3d
NIS
2085 PerlLIO_close(fd);
2086 }
1f852d0d
NIS
2087 }
2088#endif
0e2d6244 2089 do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
1f852d0d
NIS
2090 PerlProc__exit(1);
2091#undef THIS
2092#undef THAT
2093 }
2094 /* Parent */
52e18b1f 2095 do_execfree(); /* free any memory malloced by child on fork */
1f852d0d
NIS
2096 if (did_pipes)
2097 PerlLIO_close(pp[1]);
2098 /* Keep the lower of the two fd numbers */
2099 if (p[that] < p[This]) {
2100 PerlLIO_dup2(p[This], p[that]);
2101 PerlLIO_close(p[This]);
2102 p[This] = p[that];
2103 }
4e6dfe71
GS
2104 else
2105 PerlLIO_close(p[that]); /* close child's end of pipe */
2106
1f852d0d
NIS
2107 LOCK_FDPID_MUTEX;
2108 sv = *av_fetch(PL_fdpid,p[This],TRUE);
2109 UNLOCK_FDPID_MUTEX;
2110 (void)SvUPGRADE(sv,SVt_IV);
0da6cfda 2111 SvIV_set(sv, pid);
1f852d0d
NIS
2112 PL_forkprocess = pid;
2113 /* If we managed to get status pipe check for exec fail */
2114 if (did_pipes && pid > 0) {
2115 int errkid;
2116 int n = 0, n1;
2117
2118 while (n < sizeof(int)) {
2119 n1 = PerlLIO_read(pp[0],
2120 (void*)(((char*)&errkid)+n),
2121 (sizeof(int)) - n);
2122 if (n1 <= 0)
2123 break;
2124 n += n1;
2125 }
2126 PerlLIO_close(pp[0]);
2127 did_pipes = 0;
2128 if (n) { /* Error */
2129 int pid2, status;
8c51524e 2130 PerlLIO_close(p[This]);
1f852d0d
NIS
2131 if (n != sizeof(int))
2132 Perl_croak(aTHX_ "panic: kid popen errno read");
2133 do {
2134 pid2 = wait4pid(pid, &status, 0);
2135 } while (pid2 == -1 && errno == EINTR);
2136 errno = errkid; /* Propagate errno from kid */
0e2d6244 2137 return NULL;
1f852d0d
NIS
2138 }
2139 }
2140 if (did_pipes)
2141 PerlLIO_close(pp[0]);
2142 return PerlIO_fdopen(p[This], mode);
2143#else
4a7d1889
NIS
2144 Perl_croak(aTHX_ "List form of piped open not implemented");
2145 return (PerlIO *) NULL;
1f852d0d 2146#endif
4a7d1889
NIS
2147}
2148
5f05dabc 2149 /* VMS' my_popen() is in VMS.c, same with OS/2. */
cd39f2b6 2150#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 2151PerlIO *
864dbfa3 2152Perl_my_popen(pTHX_ char *cmd, char *mode)
a687059c
LW
2153{
2154 int p[2];
8ac85365 2155 register I32 This, that;
d8a83dd3 2156 register Pid_t pid;
79072805 2157 SV *sv;
34939ee9 2158 const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
e446cec8
IZ
2159 I32 did_pipes = 0;
2160 int pp[2];
a687059c 2161
45bc9206 2162 PERL_FLUSHALL_FOR_CHILD;
ddcf38b7
IZ
2163#ifdef OS2
2164 if (doexec) {
23da6c43 2165 return my_syspopen(aTHX_ cmd,mode);
ddcf38b7 2166 }
a1d180c4 2167#endif
8ac85365
NIS
2168 This = (*mode == 'w');
2169 that = !This;
3280af22 2170 if (doexec && PL_tainting) {
bbce6d69
PP
2171 taint_env();
2172 taint_proper("Insecure %s%s", "EXEC");
d48672a2 2173 }
c2267164 2174 if (PerlProc_pipe(p) < 0)
0e2d6244 2175 return NULL;
e446cec8
IZ
2176 if (doexec && PerlProc_pipe(pp) >= 0)
2177 did_pipes = 1;
52e18b1f 2178 while ((pid = PerlProc_fork()) < 0) {
a687059c 2179 if (errno != EAGAIN) {
6ad3d225 2180 PerlLIO_close(p[This]);
b5ac89c3 2181 PerlLIO_close(p[that]);
e446cec8
IZ
2182 if (did_pipes) {
2183 PerlLIO_close(pp[0]);
2184 PerlLIO_close(pp[1]);
2185 }
a687059c 2186 if (!doexec)
cea2e8a9 2187 Perl_croak(aTHX_ "Can't fork");
0e2d6244 2188 return NULL;
a687059c
LW
2189 }
2190 sleep(5);
2191 }
2192 if (pid == 0) {
79072805
LW
2193 GV* tmpgv;
2194
30ac6d9b
GS
2195#undef THIS
2196#undef THAT
a687059c 2197#define THIS that
8ac85365 2198#define THAT This
e446cec8
IZ
2199 if (did_pipes) {
2200 PerlLIO_close(pp[0]);
2201#if defined(HAS_FCNTL) && defined(F_SETFD)
2202 fcntl(pp[1], F_SETFD, FD_CLOEXEC);
2203#endif
2204 }
a687059c 2205 if (p[THIS] != (*mode == 'r')) {
6ad3d225
GS
2206 PerlLIO_dup2(p[THIS], *mode == 'r');
2207 PerlLIO_close(p[THIS]);
b5ac89c3
NIS
2208 if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */
2209 PerlLIO_close(p[THAT]);
a687059c 2210 }
b5ac89c3
NIS
2211 else
2212 PerlLIO_close(p[THAT]);
4435c477 2213#ifndef OS2
a687059c 2214 if (doexec) {
a0d0e21e 2215#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
2216#ifndef NOFILE
2217#define NOFILE 20
2218#endif
a080fe3d 2219 {
5b7ea690 2220 int fd;
a080fe3d
NIS
2221
2222 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2223 if (fd != pp[1])
5b7ea690 2224 PerlLIO_close(fd);
a080fe3d 2225 }
ae986130 2226#endif
a080fe3d
NIS
2227 /* may or may not use the shell */
2228 do_exec3(cmd, pp[1], did_pipes);
6ad3d225 2229 PerlProc__exit(1);
a687059c 2230 }
4435c477 2231#endif /* defined OS2 */
b977d03a 2232 if ((tmpgv = gv_fetchpvs("$", GV_ADD|GV_NOTQUAL, SVt_PV))) {
5b7ea690 2233 SvREADONLY_off(GvSV(tmpgv));
7766f137 2234 sv_setiv(GvSV(tmpgv), PerlProc_getpid());
5b7ea690
JH
2235 SvREADONLY_on(GvSV(tmpgv));
2236 }
2237#ifdef THREADS_HAVE_PIDS
2238 PL_ppid = (IV)getppid();
2239#endif
3280af22
NIS
2240 PL_forkprocess = 0;
2241 hv_clear(PL_pidstatus); /* we have no children */
0e2d6244 2242 return NULL;
a687059c
LW
2243#undef THIS
2244#undef THAT
2245 }
b5ac89c3 2246 do_execfree(); /* free any memory malloced by child on vfork */
e446cec8
IZ
2247 if (did_pipes)
2248 PerlLIO_close(pp[1]);
8ac85365 2249 if (p[that] < p[This]) {
6ad3d225
GS
2250 PerlLIO_dup2(p[This], p[that]);
2251 PerlLIO_close(p[This]);
8ac85365 2252 p[This] = p[that];
62b28dd9 2253 }
b5ac89c3
NIS
2254 else
2255 PerlLIO_close(p[that]);
2256
4755096e 2257 LOCK_FDPID_MUTEX;
3280af22 2258 sv = *av_fetch(PL_fdpid,p[This],TRUE);
4755096e 2259 UNLOCK_FDPID_MUTEX;
a0d0e21e 2260 (void)SvUPGRADE(sv,SVt_IV);
0da6cfda 2261 SvIV_set(sv, pid);
3280af22 2262 PL_forkprocess = pid;
e446cec8
IZ
2263 if (did_pipes && pid > 0) {
2264 int errkid;
2265 int n = 0, n1;
2266
2267 while (n < sizeof(int)) {
2268 n1 = PerlLIO_read(pp[0],
2269 (void*)(((char*)&errkid)+n),
2270 (sizeof(int)) - n);
2271 if (n1 <= 0)
2272 break;
2273 n += n1;
2274 }
2f96c702
IZ
2275 PerlLIO_close(pp[0]);
2276 did_pipes = 0;
e446cec8 2277 if (n) { /* Error */
faa466a7 2278 int pid2, status;
8c51524e 2279 PerlLIO_close(p[This]);
e446cec8 2280 if (n != sizeof(int))
cea2e8a9 2281 Perl_croak(aTHX_ "panic: kid popen errno read");
faa466a7
RG
2282 do {
2283 pid2 = wait4pid(pid, &status, 0);
2284 } while (pid2 == -1 && errno == EINTR);
e446cec8 2285 errno = errkid; /* Propagate errno from kid */
0e2d6244 2286 return NULL;
e446cec8
IZ
2287 }
2288 }
2289 if (did_pipes)
2290 PerlLIO_close(pp[0]);
8ac85365 2291 return PerlIO_fdopen(p[This], mode);
a687059c 2292}
7c0587c8 2293#else
85ca448a 2294#if defined(atarist) || defined(EPOC)
7c0587c8 2295FILE *popen();
760ac839 2296PerlIO *
864dbfa3 2297Perl_my_popen(pTHX_ char *cmd, char *mode)
7c0587c8 2298{
45bc9206 2299 PERL_FLUSHALL_FOR_CHILD;
a1d180c4
NIS
2300 /* Call system's popen() to get a FILE *, then import it.
2301 used 0 for 2nd parameter to PerlIO_importFILE;
2302 apparently not used
2303 */
2304 return PerlIO_importFILE(popen(cmd, mode), 0);
7c0587c8 2305}
2b96b0a5
JH
2306#else
2307#if defined(DJGPP)
2308FILE *djgpp_popen();
2309PerlIO *
2310Perl_my_popen(pTHX_ char *cmd, char *mode)
2311{
2312 PERL_FLUSHALL_FOR_CHILD;
2313 /* Call system's popen() to get a FILE *, then import it.
2314 used 0 for 2nd parameter to PerlIO_importFILE;
2315 apparently not used
2316 */
2317 return PerlIO_importFILE(djgpp_popen(cmd, mode), 0);
2318}
2319#endif
7c0587c8
LW
2320#endif
2321
2322#endif /* !DOSISH */
a687059c 2323
52e18b1f
GS
2324/* this is called in parent before the fork() */
2325void
2326Perl_atfork_lock(void)
2327{
4d1ff10f 2328#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
52e18b1f
GS
2329 /* locks must be held in locking order (if any) */
2330# ifdef MYMALLOC
2331 MUTEX_LOCK(&PL_malloc_mutex);
2332# endif
2333 OP_REFCNT_LOCK;
2334#endif
2335}
2336
2337/* this is called in both parent and child after the fork() */
2338void
2339Perl_atfork_unlock(void)
2340{
4d1ff10f 2341#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
52e18b1f
GS
2342 /* locks must be released in same order as in atfork_lock() */
2343# ifdef MYMALLOC
2344 MUTEX_UNLOCK(&PL_malloc_mutex);
2345# endif
2346 OP_REFCNT_UNLOCK;
2347#endif
2348}
2349
2350Pid_t
2351Perl_my_fork(void)
2352{
2353#if defined(HAS_FORK)
2354 Pid_t pid;
4d1ff10f 2355#if (defined(USE_5005THREADS) || defined(USE_ITHREADS)) && !defined(HAS_PTHREAD_ATFORK)
52e18b1f
GS
2356 atfork_lock();
2357 pid = fork();
2358 atfork_unlock();
2359#else
2360 /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2361 * handlers elsewhere in the code */
2362 pid = fork();
2363#endif
2364 return pid;
2365#else
2366 /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2367 Perl_croak_nocontext("fork() not available");
b961a566 2368 return 0;
52e18b1f
GS
2369#endif /* HAS_FORK */
2370}
2371
748a9306 2372#ifdef DUMP_FDS
35ff7856 2373void
864dbfa3 2374Perl_dump_fds(pTHX_ char *s)
ae986130
LW
2375{
2376 int fd;
c623ac67 2377 Stat_t tmpstatbuf;
ae986130 2378
bf49b057 2379 PerlIO_printf(Perl_debug_log,"%s", s);
ae986130 2380 for (fd = 0; fd < 32; fd++) {
6ad3d225 2381 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
bf49b057 2382 PerlIO_printf(Perl_debug_log," %d",fd);
ae986130 2383 }
bf49b057 2384 PerlIO_printf(Perl_debug_log,"\n");
fe20fd30 2385 return;
ae986130 2386}
35ff7856 2387#endif /* DUMP_FDS */
ae986130 2388
fe14fcc3 2389#ifndef HAS_DUP2
fec02dd3 2390int
ba106d47 2391dup2(int oldfd, int newfd)
a687059c 2392{
a0d0e21e 2393#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
2394 if (oldfd == newfd)
2395 return oldfd;
6ad3d225 2396 PerlLIO_close(newfd);
fec02dd3 2397 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 2398#else
fc36a67e
PP
2399#define DUP2_MAX_FDS 256
2400 int fdtmp[DUP2_MAX_FDS];
79072805 2401 I32 fdx = 0;
ae986130
LW
2402 int fd;
2403
fe14fcc3 2404 if (oldfd == newfd)
fec02dd3 2405 return oldfd;
6ad3d225 2406 PerlLIO_close(newfd);
fc36a67e 2407 /* good enough for low fd's... */
6ad3d225 2408 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
fc36a67e 2409 if (fdx >= DUP2_MAX_FDS) {
6ad3d225 2410 PerlLIO_close(fd);
fc36a67e
PP
2411 fd = -1;
2412 break;
2413 }
ae986130 2414 fdtmp[fdx++] = fd;
fc36a67e 2415 }
ae986130 2416 while (fdx > 0)
6ad3d225 2417 PerlLIO_close(fdtmp[--fdx]);
fec02dd3 2418 return fd;
62b28dd9 2419#endif
a687059c
LW
2420}
2421#endif
2422
64ca3a65 2423#ifndef PERL_MICRO
ff68c719
PP
2424#ifdef HAS_SIGACTION
2425
1a8b74ef
CN
2426#ifdef MACOS_TRADITIONAL
2427/* We don't want restart behavior on MacOS */
2428#undef SA_RESTART
2429#endif
2430
ff68c719 2431Sighandler_t
864dbfa3 2432Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
ff68c719
PP
2433{
2434 struct sigaction act, oact;
2435
a10b1e10
JH
2436#ifdef USE_ITHREADS
2437 /* only "parent" interpreter can diddle signals */
2438 if (PL_curinterp != aTHX)
17afd9a0 2439 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2440#endif
2441
17afd9a0 2442 act.sa_handler = (void(*)(int))handler;
ff68c719
PP
2443 sigemptyset(&act.sa_mask);
2444 act.sa_flags = 0;
2445#ifdef SA_RESTART
5835a535
JH
2446 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2447 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
0a8e0eff 2448#endif
714e8223 2449#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
17afd9a0 2450 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
85264bed
CS
2451 act.sa_flags |= SA_NOCLDWAIT;
2452#endif
ff68c719 2453 if (sigaction(signo, &act, &oact) == -1)
17afd9a0 2454 return (Sighandler_t) SIG_ERR;
ff68c719 2455 else
17afd9a0 2456 return (Sighandler_t) oact.sa_handler;
ff68c719
PP
2457}
2458
2459Sighandler_t
864dbfa3 2460Perl_rsignal_state(pTHX_ int signo)
ff68c719
PP
2461{
2462 struct sigaction oact;
2463
2464 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
17afd9a0 2465 return (Sighandler_t) SIG_ERR;
ff68c719 2466 else
17afd9a0 2467 return (Sighandler_t) oact.sa_handler;
ff68c719
PP
2468}
2469
2470int
864dbfa3 2471Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719
PP
2472{
2473 struct sigaction act;
2474
a10b1e10
JH
2475#ifdef USE_ITHREADS
2476 /* only "parent" interpreter can diddle signals */
2477 if (PL_curinterp != aTHX)
2478 return -1;
2479#endif
2480
17afd9a0 2481 act.sa_handler = (void(*)(int))handler;
ff68c719
PP
2482 sigemptyset(&act.sa_mask);
2483 act.sa_flags = 0;
2484#ifdef SA_RESTART
5835a535
JH
2485 if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2486 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
0a8e0eff 2487#endif
7865c879 2488#if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
17afd9a0 2489 if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
85264bed
CS
2490 act.sa_flags |= SA_NOCLDWAIT;
2491#endif
ff68c719
PP
2492 return sigaction(signo, &act, save);
2493}
2494
2495int
864dbfa3 2496Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
ff68c719 2497{
a10b1e10
JH
2498#ifdef USE_ITHREADS
2499 /* only "parent" interpreter can diddle signals */
2500 if (PL_curinterp != aTHX)
2501 return -1;
2502#endif
2503
ff68c719
PP
2504 return sigaction(signo, save, (struct sigaction *)NULL);
2505}
2506
2507#else /* !HAS_SIGACTION */
2508
2509Sighandler_t
864dbfa3 2510Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
ff68c719 2511{
39f1703b 2512#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2513 /* only "parent" interpreter can diddle signals */
2514 if (PL_curinterp != aTHX)
17afd9a0 2515 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2516#endif
2517
6ad3d225 2518 return PerlProc_signal(signo, handler);
ff68c719
PP
2519}
2520
903d57dc
NC
2521static int PL_sig_trapped; /* XXX signals are process-wide anyway, so we
2522 ignore the implications of this for threading */
2523
260c88e8 2524static Signal_t
4e35701f 2525sig_trap(int signo)
ff68c719 2526{
fe20fd30 2527 PL_sig_trapped++;
ff68c719
PP
2528}
2529
2530Sighandler_t
864dbfa3 2531Perl_rsignal_state(pTHX_ int signo)
ff68c719
PP
2532{
2533 Sighandler_t oldsig;
2534
39f1703b 2535#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2536 /* only "parent" interpreter can diddle signals */
2537 if (PL_curinterp != aTHX)
17afd9a0 2538 return (Sighandler_t) SIG_ERR;
a10b1e10
JH
2539#endif
2540
fe20fd30 2541 PL_sig_trapped = 0;
6ad3d225
GS
2542 oldsig = PerlProc_signal(signo, sig_trap);
2543 PerlProc_signal(signo, oldsig);
fe20fd30 2544 if (PL_sig_trapped)
5b7ea690 2545 PerlProc_kill(PerlProc_getpid(), signo);
ff68c719
PP
2546 return oldsig;
2547}
2548
2549int
864dbfa3 2550Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2551{
39f1703b 2552#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2553 /* only "parent" interpreter can diddle signals */
2554 if (PL_curinterp != aTHX)
2555 return -1;
2556#endif
6ad3d225 2557 *save = PerlProc_signal(signo, handler);
17afd9a0 2558 return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
ff68c719
PP
2559}
2560
2561int
864dbfa3 2562Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
ff68c719 2563{
39f1703b 2564#if defined(USE_ITHREADS) && !defined(WIN32)
a10b1e10
JH
2565 /* only "parent" interpreter can diddle signals */
2566 if (PL_curinterp != aTHX)
2567 return -1;
2568#endif
17afd9a0 2569 return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
ff68c719
PP
2570}
2571
2572#endif /* !HAS_SIGACTION */
64ca3a65 2573#endif /* !PERL_MICRO */
ff68c719 2574
5f05dabc 2575 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
cd39f2b6 2576#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
79072805 2577I32
864dbfa3 2578Perl_my_pclose(pTHX_ PerlIO *ptr)
a687059c 2579{
ff68c719 2580 Sigsave_t hstat, istat, qstat;
a687059c 2581 int status;
a0d0e21e 2582 SV **svp;
d8a83dd3
JH
2583 Pid_t pid;
2584 Pid_t pid2;
03136e13 2585 bool close_failed;
b7953727 2586 int saved_errno = 0;
22fae026
TM
2587#ifdef WIN32
2588 int saved_win32_errno;
2589#endif
a687059c 2590
4755096e 2591 LOCK_FDPID_MUTEX;
3280af22 2592 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
4755096e 2593 UNLOCK_FDPID_MUTEX;
25d92023 2594 pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
a0d0e21e 2595 SvREFCNT_dec(*svp);
3280af22 2596 *svp = &PL_sv_undef;
ddcf38b7
IZ
2597#ifdef OS2
2598 if (pid == -1) { /* Opened by popen. */
2599 return my_syspclose(ptr);
2600 }
a1d180c4 2601#endif
03136e13
CS
2602 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2603 saved_errno = errno;
22fae026
TM
2604#ifdef WIN32
2605 saved_win32_errno = GetLastError();
2606#endif
03136e13 2607 }
7c0587c8 2608#ifdef UTS
6ad3d225 2609 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
7c0587c8 2610#endif
64ca3a65 2611#ifndef PERL_MICRO
17afd9a0
NC
2612 rsignal_save(SIGHUP, (Sighandler_t) SIG_IGN, &hstat);
2613 rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &istat);
2614 rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qstat);
64ca3a65 2615#endif
748a9306 2616 do {
1d3434b8
GS
2617 pid2 = wait4pid(pid, &status, 0);
2618 } while (pid2 == -1 && errno == EINTR);
64ca3a65 2619#ifndef PERL_MICRO
ff68c719
PP
2620 rsignal_restore(SIGHUP, &hstat);
2621 rsignal_restore(SIGINT, &istat);
2622 rsignal_restore(SIGQUIT, &qstat);
64ca3a65 2623#endif
03136e13 2624 if (close_failed) {
eb8cd4b1 2625 SETERRNO(saved_errno, 0);
03136e13
CS
2626 return -1;
2627 }
1d3434b8 2628 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
20188a90 2629}
4633a7c4
LW
2630#endif /* !DOSISH */
2631
2986a63f 2632#if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(MACOS_TRADITIONAL)
79072805 2633I32
d8a83dd3 2634Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
20188a90 2635{
fe20fd30 2636 I32 result = 0;
b7953727
JH
2637 if (!pid)
2638 return -1;
2639#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2640 {
8e0c135f 2641 char spid[TYPE_CHARS(IV)];
eb160463 2642
5b7ea690 2643 if (pid > 0) {
37724321 2644 const I32 len = my_sprintf(spid, "%"IVdf, (IV)pid);
1a9219e7 2645 SV * const * const svp = hv_fetch(PL_pidstatus,spid,len,FALSE);
37724321 2646
5b7ea690
JH
2647 if (svp && *svp != &PL_sv_undef) {
2648 *statusp = SvIVX(*svp);
2649 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
2650 return pid;
2651 }
2652 }
2653 else {
2654 HE *entry;
2655
2656 hv_iterinit(PL_pidstatus);
2657 if ((entry = hv_iternext(PL_pidstatus))) {
1a9219e7 2658 SV * const sv = hv_iterval(PL_pidstatus,entry);
37724321 2659 I32 len;
fe20fd30 2660
5b7ea690 2661 pid = atoi(hv_iterkey(entry,(I32*)statusp));
5b7ea690 2662 *statusp = SvIVX(sv);
37724321 2663 len = my_sprintf(spid, "%"IVdf, (IV)pid);
87c3f7b6
TP
2664 /* The hash iterator is currently on this entry, so simply
2665 calling hv_delete would trigger the lazy delete, which on
2666 aggregate does more work, beacuse next call to hv_iterinit()
2667 would spot the flag, and have to call the delete routine,
2668 while in the meantime any new entries can't re-use that
2669 memory. */
2670 hv_iterinit(PL_pidstatus);
37724321 2671 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
5b7ea690
JH
2672 return pid;
2673 }
20188a90
LW
2674 }
2675 }
68a29c53 2676#endif
79072805 2677#ifdef HAS_WAITPID
367f3c24
IZ
2678# ifdef HAS_WAITPID_RUNTIME
2679 if (!HAS_WAITPID_RUNTIME)
2680 goto hard_way;
2681# endif
cddd4526 2682 result = PerlProc_waitpid(pid,statusp,flags);
dfcfdb64 2683 goto finish;
367f3c24
IZ
2684#endif
2685#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
0e2d6244 2686 result = wait4((pid==-1)?0:pid,statusp,flags,NULL);
dfcfdb64 2687 goto finish;
367f3c24
IZ
2688#endif
2689#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
fe20fd30 2690#if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
367f3c24 2691 hard_way:
fe20fd30 2692#endif
a0d0e21e 2693 {
a0d0e21e 2694 if (flags)
cea2e8a9 2695 Perl_croak(aTHX_ "Can't do waitpid with flags");
a0d0e21e 2696 else {
76e3520e 2697 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
a0d0e21e
LW
2698 pidgone(result,*statusp);
2699 if (result < 0)
2700 *statusp = -1;
2701 }
a687059c
LW
2702 }
2703#endif
fe20fd30 2704#if defined(HAS_WAITPID) || defined(HAS_WAIT4)
dfcfdb64 2705 finish:
fe20fd30 2706#endif
cddd4526
NIS
2707 if (result < 0 && errno == EINTR) {
2708 PERL_ASYNC_CHECK();
2709 }
2710 return result;
a687059c 2711}
2986a63f 2712#endif /* !DOSISH || OS2 || WIN32 || NETWARE */
a687059c 2713
7c0587c8 2714void
d8a83dd3 2715Perl_pidgone(pTHX_ Pid_t pid, int status)
a687059c 2716{
79072805 2717 register SV *sv;
8e0c135f 2718 char spid[TYPE_CHARS(IV)];
37724321 2719 const size_t len = my_sprintf(spid, "%"IVdf, (IV)pid);
a687059c 2720
37724321 2721 sv = *hv_fetch(PL_pidstatus,spid,len,TRUE);
a0d0e21e 2722 (void)SvUPGRADE(sv,SVt_IV);
0da6cfda 2723 SvIV_set(sv, status);
20188a90 2724 return;
a687059c
LW
2725}
2726
85ca448a 2727#if defined(atarist) || defined(OS2) || defined(EPOC)
7c0587c8 2728int pclose();
ddcf38b7
IZ
2729#ifdef HAS_FORK
2730int /* Cannot prototype with I32
2731 in os2ish.h. */
ba106d47 2732my_syspclose(PerlIO *ptr)
ddcf38b7 2733#else
79072805 2734I32
864dbfa3 2735Perl_my_pclose(pTHX_ PerlIO *ptr)
a1d180c4 2736#endif
a687059c 2737{
760ac839 2738 /* Needs work for PerlIO ! */
1a9219e7 2739 FILE * const f = PerlIO_findFILE(ptr);
6d29369a 2740 const I32 result = pclose(f);
2b96b0a5
JH
2741 PerlIO_releaseFILE(ptr,f);
2742 return result;
2743}
2744#endif
2745
933fea7f 2746#if defined(DJGPP)
2b96b0a5
JH
2747int djgpp_pclose();
2748I32
2749Perl_my_pclose(pTHX_ PerlIO *ptr)
2750{
2751 /* Needs work for PerlIO ! */
1a9219e7 2752 FILE * const f = PerlIO_findFILE(ptr);
2b96b0a5 2753 I32 result = djgpp_pclose(f);
933fea7f 2754 result = (result << 8) & 0xff00;
760ac839
LW
2755 PerlIO_releaseFILE(ptr,f);
2756 return result;
a687059c 2757}
7c0587c8 2758#endif
9f68db38
LW
2759
2760void
864dbfa3 2761Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count)
9f68db38 2762{
79072805 2763 register I32 todo;
1a9219e7 2764 register const char * const frombase = from;
9f68db38
LW
2765
2766 if (len == 1) {
08105a92 2767 register const char c = *from;
9f68db38 2768 while (count-- > 0)
5926133d 2769 *to++ = c;
9f68db38
LW
2770 return;
2771 }
2772 while (count-- > 0) {
2773 for (todo = len; todo > 0; todo--) {
2774 *to++ = *from++;
2775 }
2776 from = frombase;
2777 }
2778}
0f85fab0 2779
fe14fcc3 2780#ifndef HAS_RENAME
79072805 2781I32
864dbfa3 2782Perl_same_dirent(pTHX_ char *a, char *b)
62b28dd9 2783{
93a17b20
LW
2784 char *fa = strrchr(a,'/');
2785 char *fb = strrchr(b,'/');
c623ac67
GS
2786 Stat_t tmpstatbuf1;
2787 Stat_t tmpstatbuf2;
1a9219e7 2788 SV * const tmpsv = sv_newmortal();
62b28dd9
LW
2789
2790 if (fa)
2791 fa++;
2792 else
2793 fa = a;
2794 if (fb)
2795 fb++;
2796 else
2797 fb = b;
2798 if (strNE(a,b))
2799 return FALSE;
2800 if (fa == a)
2a8de9e2 2801 sv_setpvn(tmpsv, ".", 1);
62b28dd9 2802 else
46fc3d4c 2803 sv_setpvn(tmpsv, a, fa - a);
5e7e76a3 2804 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2805 return FALSE;
2806 if (fb == b)
2a8de9e2 2807 sv_setpvn(tmpsv, ".", 1);
62b28dd9 2808 else
46fc3d4c 2809 sv_setpvn(tmpsv, b, fb - b);
5e7e76a3 2810 if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2811 return FALSE;
2812 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2813 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2814}
fe14fcc3
LW
2815#endif /* !HAS_RENAME */
2816
491527d0 2817char*
9467e164
NC
2818Perl_find_script(pTHX_ char *scriptname, bool dosearch, char **search_ext,
2819 I32 flags)
491527d0 2820{
0e2d6244
SS
2821 const char *xfound = NULL;
2822 char *xfailed = NULL;
0f31cffe 2823 char tmpbuf[MAXPATHLEN];
491527d0 2824 register char *s;
5f74f29c 2825 I32 len = 0;
491527d0
GS
2826 int retval;
2827#if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2828# define SEARCH_EXTS ".bat", ".cmd", NULL
2829# define MAX_EXT_LEN 4
2830#endif
2831#ifdef OS2
2832# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2833# define MAX_EXT_LEN 4
2834#endif
2835#ifdef VMS
2836# define SEARCH_EXTS ".pl", ".com", NULL
2837# define MAX_EXT_LEN 4
2838#endif
2839 /* additional extensions to try in each dir if scriptname not found */
2840#ifdef SEARCH_EXTS
7508116b 2841 static const char *const exts[] = { SEARCH_EXTS };
e83d91ab
SH
2842 const char *const *const ext =
2843 search_ext ? (const char *const *const)search_ext : exts;
491527d0 2844 int extidx = 0, i = 0;
0e2d6244 2845 const char *curext = NULL;
491527d0 2846#else
0188be2e 2847 PERL_UNUSED_ARG(search_ext);
491527d0
GS
2848# define MAX_EXT_LEN 0
2849#endif
2850
2851 /*
2852 * If dosearch is true and if scriptname does not contain path
2853 * delimiters, search the PATH for scriptname.
2854 *
2855 * If SEARCH_EXTS is also defined, will look for each
2856 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2857 * while searching the PATH.
2858 *
2859 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2860 * proceeds as follows:
2861 * If DOSISH or VMSISH:
2862 * + look for ./scriptname{,.foo,.bar}
2863 * + search the PATH for scriptname{,.foo,.bar}
2864 *
2865 * If !DOSISH:
2866 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2867 * this will not look in '.' if it's not in the PATH)
2868 */
84486fc6 2869 tmpbuf[0] = '\0';
491527d0
GS
2870
2871#ifdef VMS
2872# ifdef ALWAYS_DEFTYPES
2873 len = strlen(scriptname);
2874 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
1a9219e7 2875 int idx = 0, deftypes = 1;
491527d0
GS
2876 bool seen_dot = 1;
2877
1a9219e7 2878 const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
491527d0
GS
2879# else
2880 if (dosearch) {
1a9219e7 2881 int idx = 0, deftypes = 1;
491527d0
GS
2882 bool seen_dot = 1;
2883
1a9219e7 2884 const int hasdir = (strpbrk(scriptname,":[</") != NULL);
491527d0
GS
2885# endif
2886 /* The first time through, just add SEARCH_EXTS to whatever we
2887 * already have, so we can check for default file types. */
2888 while (deftypes ||
84486fc6 2889 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
491527d0
GS
2890 {
2891 if (deftypes) {
2892 deftypes = 0;
84486fc6 2893 *tmpbuf = '\0';
491527d0 2894 }
84486fc6
GS
2895 if ((strlen(tmpbuf) + strlen(scriptname)
2896 + MAX_EXT_LEN) >= sizeof tmpbuf)
491527d0 2897 continue; /* don't search dir with too-long name */
84486fc6 2898 strcat(tmpbuf, scriptname);
491527d0
GS
2899#else /* !VMS */
2900
2901#ifdef DOSISH
2902 if (strEQ(scriptname, "-"))
2903 dosearch = 0;
2904 if (dosearch) { /* Look in '.' first. */
aa651147 2905 char *cur = scriptname;
491527d0
GS
2906#ifdef SEARCH_EXTS
2907 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2908 while (ext[i])
2909 if (strEQ(ext[i++],curext)) {
2910 extidx = -1; /* already has an ext */
2911 break;
2912 }
2913 do {
2914#endif
2915 DEBUG_p(PerlIO_printf(Perl_debug_log,
2916 "Looking for %s\n",cur));
017f25f1
IZ
2917 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2918 && !S_ISDIR(PL_statbuf.st_mode)) {
491527d0
GS
2919 dosearch = 0;
2920 scriptname = cur;
2921#ifdef SEARCH_EXTS
2922 break;
2923#endif
2924 }
2925#ifdef SEARCH_EXTS
2926 if (cur == scriptname) {
2927 len = strlen(scriptname);
84486fc6 2928 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
491527d0 2929 break;
4c58c75a 2930 /* FIXME? Convert to memcpy */
84486fc6 2931 cur = strcpy(tmpbuf, scriptname);
491527d0
GS
2932 }
2933 } while (extidx >= 0 && ext[extidx] /* try an extension? */
84486fc6 2934 && strcpy(tmpbuf+len, ext[extidx++]));
491527d0
GS
2935#endif
2936 }
2937#endif
2938
cd39f2b6
JH
2939#ifdef MACOS_TRADITIONAL
2940 if (dosearch && !strchr(scriptname, ':') &&
2941 (s = PerlEnv_getenv("Commands")))
2942#else
491527d0
GS
2943 if (dosearch && !strchr(scriptname, '/')
2944#ifdef DOSISH
2945 && !strchr(scriptname, '\\')
2946#endif
cd39f2b6
JH
2947 && (s = PerlEnv_getenv("PATH")))
2948#endif
2949 {
491527d0
GS
2950 bool seen_dot = 0;
2951
3280af22
NIS
2952 PL_bufend = s + strlen(s);
2953 while (s < PL_bufend) {
cd39f2b6
JH
2954#ifdef MACOS_TRADITIONAL
2955 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
2956 ',',
2957 &len);
2958#else
491527d0
GS
2959#if defined(atarist) || defined(DOSISH)
2960 for (len = 0; *s
2961# ifdef atarist
2962 && *s != ','
2963# endif
2964 && *s != ';'; len++, s++) {
84486fc6
GS
2965 if (len < sizeof tmpbuf)
2966 tmpbuf[len] = *s;
491527d0 2967 }
84486fc6
GS
2968 if (len < sizeof tmpbuf)
2969 tmpbuf[len] = '\0';
491527d0 2970#else /* ! (atarist || DOSISH) */
3280af22 2971 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
491527d0
GS
2972 ':',
2973 &len);
2974#endif /* ! (atarist || DOSISH) */
cd39f2b6 2975#endif /* MACOS_TRADITIONAL */
3280af22 2976 if (s < PL_bufend)
491527d0 2977 s++;
84486fc6 2978 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
491527d0 2979 continue; /* don't search dir with too-long name */
cd39f2b6
JH
2980#ifdef MACOS_TRADITIONAL
2981 if (len && tmpbuf[len - 1] != ':')
2982 tmpbuf[len++] = ':';
2983#else
491527d0 2984 if (len
4c58c75a 2985# if defined(atarist) || defined(__MINT__) || defined(DOSISH)
84486fc6
GS
2986 && tmpbuf[len - 1] != '/'
2987 && tmpbuf[len - 1] != '\\'
4c58c75a 2988# endif
491527d0 2989 )
84486fc6
GS
2990 tmpbuf[len++] = '/';
2991 if (len == 2 && tmpbuf[0] == '.')
491527d0 2992 seen_dot = 1;
cd39f2b6 2993#endif
b0f2d060
JH
2994#ifdef HAS_STRLCAT
2995 (void)strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
2996#else
4c58c75a
NC
2997 /* FIXME? Convert to memcpy by storing previous strlen(scriptname)
2998 */
84486fc6 2999 (void)strcpy(tmpbuf + len, scriptname);
b0f2d060 3000#endif /* #ifdef HAS_STRLCAT */
491527d0
GS
3001#endif /* !VMS */
3002
3003#ifdef SEARCH_EXTS
84486fc6 3004 len = strlen(tmpbuf);
491527d0
GS
3005 if (extidx > 0) /* reset after previous loop */
3006 extidx = 0;
3007 do {
3008#endif
84486fc6 3009 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3280af22 3010 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
017f25f1
IZ
3011 if (S_ISDIR(PL_statbuf.st_mode)) {
3012 retval = -1;
3013 }
491527d0
GS
3014#ifdef SEARCH_EXTS
3015 } while ( retval < 0 /* not there */
3016 && extidx>=0 && ext[extidx] /* try an extension? */
84486fc6 3017 && strcpy(tmpbuf+len, ext[extidx++])
491527d0
GS
3018 );
3019#endif
3020 if (retval < 0)
3021 continue;
3280af22
NIS
3022 if (S_ISREG(PL_statbuf.st_mode)
3023 && cando(S_IRUSR,TRUE,&PL_statbuf)
73811745 3024#if !defined(DOSISH) && !defined(MACOS_TRADITIONAL)
3280af22 3025 && cando(S_IXUSR,TRUE,&PL_statbuf)
491527d0
GS
3026#endif
3027 )
3028 {
5b7ea690 3029 xfound = tmpbuf; /* bingo! */
491527d0
GS
3030 break;
3031 }
3032 if (!xfailed)
84486fc6 3033 xfailed = savepv(tmpbuf);
491527d0
GS
3034 }
3035#ifndef DOSISH
017f25f1 3036 if (!xfound && !seen_dot && !xfailed &&
a1d180c4 3037 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
017f25f1 3038 || S_ISDIR(PL_statbuf.st_mode)))
491527d0
GS
3039#endif
3040 seen_dot = 1; /* Disable message. */
9ccb31f9
GS
3041 if (!xfound) {
3042 if (flags & 1) { /* do or die? */
5b7ea690 3043 Perl_croak(aTHX_ "Can't %s %s%s%s",
9ccb31f9
GS
3044 (xfailed ? "execute" : "find"),
3045 (xfailed ? xfailed : scriptname),
3046 (xfailed ? "" : " on PATH"),
3047 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3048 }
0e2d6244 3049 scriptname = NULL;
9ccb31f9 3050 }
4c58c75a 3051 Safefree(xfailed);
ed6a78eb
NC
3052 /* Cast because we're not changing function prototypes in maint. */
3053 scriptname = (char *) xfound;
491527d0 3054 }
0e2d6244 3055 return (scriptname ? savepv(scriptname) : NULL);
491527d0
GS
3056}
3057
ba869deb
GS
3058#ifndef PERL_GET_CONTEXT_DEFINED
3059
3060void *
3061Perl_get_context(void)
3062{
4d1ff10f 3063#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
ba869deb
GS
3064# ifdef OLD_PTHREADS_API
3065 pthread_addr_t t;
3066 if (pthread_getspecific(PL_thr_key, &t))
3067 Perl_croak_nocontext("panic: pthread_getspecific");
3068 return (void*)t;
3069# else
bce813aa 3070# ifdef I_MACH_CTHREADS
8b8b35ab 3071 return (void*)cthread_data(cthread_self());
bce813aa 3072# else
8b8b35ab
JH
3073 return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
3074# endif
c44d3fdb 3075# endif
ba869deb
GS
3076#else
3077 return (void*)NULL;
3078#endif
3079}
3080
3081void
3082Perl_set_context(void *t)
3083{
4d1ff10f 3084#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
c44d3fdb
GS
3085# ifdef I_MACH_CTHREADS
3086 cthread_set_data(cthread_self(), t);
3087# else
ba869deb
GS
3088 if (pthread_setspecific(PL_thr_key, t))
3089 Perl_croak_nocontext("panic: pthread_setspecific");
c44d3fdb 3090# endif
481da01c 3091#else
4f1e9d25 3092 PERL_UNUSED_ARG(t);
ba869deb
GS
3093#endif
3094}
3095
3096#endif /* !PERL_GET_CONTEXT_DEFINED */
491527d0 3097
4d1ff10f 3098#ifdef USE_5005THREADS
ba869deb 3099
12ca11f6
MB
3100#ifdef FAKE_THREADS
3101/* Very simplistic scheduler for now */
3102void
3103schedule(void)
3104{
c7848ba1 3105 thr = thr->i.next_run;
12ca11f6
MB
3106}
3107
3108void
864dbfa3 3109Perl_cond_init(pTHX_ perl_cond *cp)
12ca11f6
MB
3110{
3111 *cp = 0;
3112}
3113
3114void
864dbfa3 3115Perl_cond_signal(pTHX_ perl_cond *cp)
12ca11f6 3116{
51dd5992 3117 perl_os_thread t;
12ca11f6 3118 perl_cond cond = *cp;
a1d180c4 3119
12ca11f6
MB
3120 if (!cond)
3121 return;
3122 t = cond->thread;
3123 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
3124 t->i.next_run = thr->i.next_run;
3125 thr->i.next_run->i.prev_run = t;
3126 t->i.prev_run = thr;
3127 thr->i.next_run = t;
3128 thr->i.wait_queue = 0;
12ca11f6
MB
3129 /* Remove from the wait queue */
3130 *cp = cond->next;
3131 Safefree(cond);
3132}
3133
3134void
864dbfa3 3135Perl_cond_broadcast(pTHX_ perl_cond *cp)
12ca11f6 3136{
51dd5992 3137 perl_os_thread t;
12ca11f6 3138 perl_cond cond, cond_next;
a1d180c4 3139
12ca11f6
MB
3140 for (cond = *cp; cond; cond = cond_next) {
3141 t = cond->thread;
3142 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
3143 t->i.next_run = thr->i.next_run;
3144 thr->i.next_run->i.prev_run = t;
3145 t->i.prev_run = thr;
3146 thr->i.next_run = t;
3147 thr->i.wait_queue = 0;
12ca11f6
MB
3148 /* Remove from the wait queue */
3149 cond_next = cond->next;
3150 Safefree(cond);
3151 }
3152 *cp = 0;
3153}
3154
3155void
864dbfa3 3156Perl_cond_wait(pTHX_ perl_cond *cp)
12ca11f6
MB
3157{
3158 perl_cond cond;
3159
c7848ba1 3160 if (thr->i.next_run == thr)
cea2e8a9 3161 Perl_croak(aTHX_ "panic: perl_cond_wait called by last runnable thread");
a1d180c4 3162
0f15f207 3163 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
3164 cond->thread = thr;
3165 cond->next = *cp;
3166 *cp = cond;
c7848ba1 3167 thr->i.wait_queue = cond;
12ca11f6 3168 /* Remove ourselves from runnable queue */
c7848ba1
MB
3169 thr->i.next_run->i.prev_run = thr->i.prev_run;
3170 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
3171}
3172#endif /* FAKE_THREADS */
3173
f93b4edd 3174MAGIC *
864dbfa3 3175Perl_condpair_magic(pTHX_ SV *sv)
f93b4edd
MB
3176{
3177 MAGIC *mg;
a1d180c4 3178
3e209e71 3179 (void)SvUPGRADE(sv, SVt_PVMG);
14befaf4 3180 mg = mg_find(sv, PERL_MAGIC_mutex);
f93b4edd
MB
3181 if (!mg) {
3182 condpair_t *cp;
3183
3184 New(53, cp, 1, condpair_t);
3185 MUTEX_INIT(&cp->mutex);
3186 COND_INIT(&cp->owner_cond);
3187 COND_INIT(&cp->cond);
3188 cp->owner = 0;
1feb2720 3189 LOCK_CRED_MUTEX; /* XXX need separate mutex? */
14befaf4 3190 mg = mg_find(sv, PERL_MAGIC_mutex);
f93b4edd
MB
3191 if (mg) {
3192 /* someone else beat us to initialising it */
1feb2720 3193 UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */
f93b4edd
MB
3194 MUTEX_DESTROY(&cp->mutex);
3195 COND_DESTROY(&cp->owner_cond);
3196 COND_DESTROY(&cp->cond);
3197 Safefree(cp);
3198 }
3199 else {
14befaf4 3200 sv_magic(sv, Nullsv, PERL_MAGIC_mutex, 0, 0);
f93b4edd
MB
3201 mg = SvMAGIC(sv);
3202 mg->mg_ptr = (char *)cp;
565764a8 3203 mg->mg_len = sizeof(cp);
1feb2720 3204 UNLOCK_CRED_MUTEX; /* XXX need separate mutex? */
bf49b057 3205 DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log,
a674cc95 3206 "%p: condpair_magic %p\n", thr, sv)));
f93b4edd
MB
3207 }
3208 }
3209 return mg;
3210}
a863c7d1 3211
3d35f11b 3212SV *
4755096e 3213Perl_sv_lock(pTHX_ SV *osv)
3d35f11b
GS
3214{
3215 MAGIC *mg;
3216 SV *sv = osv;
3217
631cfb58 3218 LOCK_SV_LOCK_MUTEX;
3d35f11b
GS
3219 if (SvROK(sv)) {
3220 sv = SvRV(sv);
3d35f11b
GS
3221 }
3222
3223 mg = condpair_magic(sv);
3224 MUTEX_LOCK(MgMUTEXP(mg));
3225 if (MgOWNER(mg) == thr)
3226 MUTEX_UNLOCK(MgMUTEXP(mg));
4755096e 3227 else {
3d35f11b
GS
3228 while (MgOWNER(mg))
3229 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
3230 MgOWNER(mg) = thr;
4755096e
GS
3231 DEBUG_S(PerlIO_printf(Perl_debug_log,
3232 "0x%"UVxf": Perl_lock lock 0x%"UVxf"\n",
a674cc95 3233 PTR2UV(thr), PTR2UV(sv)));
3d35f11b
GS
3234 MUTEX_UNLOCK(MgMUTEXP(mg));
3235 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
3236 }
631cfb58 3237 UNLOCK_SV_LOCK_MUTEX;
4755096e 3238 return sv;
3d35f11b
GS
3239}
3240
a863c7d1 3241/*
199100c8
MB
3242 * Make a new perl thread structure using t as a prototype. Some of the
3243 * fields for the new thread are copied from the prototype thread, t,
3244 * so t should not be running in perl at the time this function is
3245 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
3246 * thread calling new_struct_thread) clearly satisfies this constraint.
a863c7d1 3247 */
52e1cb5e 3248struct perl_thread *
864dbfa3 3249Perl_new_struct_thread(pTHX_ struct perl_thread *t)
a863c7d1 3250{
c5be433b 3251#if !defined(PERL_IMPLICIT_CONTEXT)
52e1cb5e 3252 struct perl_thread *thr;
cea2e8a9 3253#endif
a863c7d1 3254 SV *sv;
199100c8
MB
3255 SV **svp;
3256 I32 i;
3257
79cb57f6 3258 sv = newSVpvn("", 0);
52e1cb5e
JH
3259 SvGROW(sv, sizeof(struct perl_thread) + 1);
3260 SvCUR_set(sv, sizeof(struct perl_thread));
199100c8 3261 thr = (Thread) SvPVX(sv);
949ced2d 3262#ifdef DEBUGGING
9965345d 3263 Poison(thr, 1, struct perl_thread);
533c011a
NIS
3264 PL_markstack = 0;
3265 PL_scopestack = 0;
3266 PL_savestack = 0;
3267 PL_retstack = 0;
3268 PL_dirty = 0;
3269 PL_localizing = 0;
949ced2d 3270 Zero(&PL_hv_fetch_ent_mh, 1, HE);
d0e9ca0c
HS
3271 PL_efloatbuf = (char*)NULL;
3272 PL_efloatsize = 0;
949ced2d
GS
3273#else
3274 Zero(thr, 1, struct perl_thread);
3275#endif
199100c8
MB
3276
3277 thr->oursv = sv;
cea2e8a9 3278 init_stacks();
a863c7d1 3279
533c011a 3280 PL_curcop = &PL_compiling;
c5be433b 3281 thr->interp = t->interp;
199100c8 3282 thr->cvcache = newHV();
54b9620d 3283 thr->threadsv = newAV();
a863c7d1 3284 thr->specific = newAV();
79cb57f6 3285 thr->errsv = newSVpvn("", 0);
a863c7d1 3286 thr->flags = THRf_R_JOINABLE;
8dcd6f7b 3287 thr->thr_done = 0;
a863c7d1 3288 MUTEX_INIT(&thr->mutex);
199100c8 3289
5c831c24 3290 JMPENV_BOOTSTRAP;
533c011a 3291
6dc8a9e4 3292 PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR|EVAL_INREQUIRE) */
533c011a
NIS
3293 PL_restartop = 0;
3294
133cdda0 3295 PL_statname = newSV(0);
5a844595 3296 PL_errors = newSVpvn("", 0);
b099ddc0 3297 PL_maxscream = -1;
0b94c7bb
GS
3298 PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp);
3299 PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags);
3300 PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start);
3301 PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string);
3302 PL_regfree = MEMBER_TO_FPTR(Perl_pregfree);
b099ddc0
GS
3303 PL_regindent = 0;
3304 PL_reginterp_cnt = 0;
3305 PL_lastscream = Nullsv;
3306 PL_screamfirst = 0;
3307 PL_screamnext = 0;
3308 PL_reg_start_tmp = 0;
3309 PL_reg_start_tmpl = 0;
14ed4b74 3310 PL_reg_poscache = Nullch;
b099ddc0 3311
a2efc822
SC
3312 PL_peepp = MEMBER_TO_FPTR(Perl_peep);
3313
b099ddc0
GS
3314 /* parent thread's data needs to be locked while we make copy */
3315 MUTEX_LOCK(&t->mutex);
3316
14dd3ad8 3317#ifdef PERL_FLEXIBLE_EXCEPTIONS
312caa8e 3318 PL_protect = t->Tprotect;
14dd3ad8 3319#endif
312caa8e 3320
b099ddc0
GS
3321 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
3322 PL_defstash = t->Tdefstash; /* XXX maybe these should */
3323 PL_curstash = t->Tcurstash; /* always be set to main? */
3324
6b88bc9c 3325 PL_tainted = t->Ttainted;
5b7ea690 3326 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
8bfdd7d9 3327 PL_rs = newSVsv(t->Trs);
84fee439 3328 PL_last_in_gv = Nullgv;
7d3de3d5 3329 PL_ofs_sv = t->Tofs_sv ? SvREFCNT_inc(PL_ofs_sv) : Nullsv;
84fee439
NIS
3330 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
3331 PL_chopset = t->Tchopset;
84fee439
NIS
3332 PL_bodytarget = newSVsv(t->Tbodytarget);
3333 PL_toptarget = newSVsv(t->Ttoptarget);
5c831c24
GS
3334 if (t->Tformtarget == t->Ttoptarget)
3335 PL_formtarget = PL_toptarget;
3336 else
3337 PL_formtarget = PL_bodytarget;
e5345b23
JH
3338 PL_watchaddr = 0; /* XXX */
3339 PL_watchok = 0; /* XXX */
3340 PL_comppad = 0;
3341 PL_curpad = 0;
533c011a 3342
54b9620d
MB
3343 /* Initialise all per-thread SVs that the template thread used */
3344 svp = AvARRAY(t->threadsv);
93965878 3345 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
533c011a 3346 if (*svp && *svp != &PL_sv_undef) {
199100c8 3347 SV *sv = newSVsv(*svp);
54b9620d 3348 av_store(thr->threadsv, i, sv);
14befaf4 3349 sv_magic(sv, 0, PERL_MAGIC_sv, &PL_threadsv_names[i], 1);
bf49b057 3350 DEBUG_S(PerlIO_printf(Perl_debug_log,
f1dbda3d
JH
3351 "new_struct_thread: copied threadsv %"IVdf" %p->%p\n",
3352 (IV)i, t, thr));
199100c8 3353 }
a1d180c4 3354 }
940cb80d 3355 thr->threadsvp = AvARRAY(thr->threadsv);
199100c8 3356
533c011a
NIS
3357 MUTEX_LOCK(&PL_threads_mutex);
3358 PL_nthreads++;
3359 thr->tid = ++PL_threadnum;
199100c8
MB
3360 thr->next = t->next;
3361 thr->prev = t;
3362 t->next = thr;
3363 thr->next->prev = thr;
533c011a 3364 MUTEX_UNLOCK(&PL_threads_mutex);
a863c7d1 3365
b099ddc0
GS
3366 /* done copying parent's state */
3367 MUTEX_UNLOCK(&t->mutex);
3368
a863c7d1 3369#ifdef HAVE_THREAD_INTERN
4f63d024 3370 Perl_init_thread_intern(thr);
a863c7d1 3371#endif /* HAVE_THREAD_INTERN */
a863c7d1
MB
3372 return thr;
3373}
4d1ff10f 3374#endif /* USE_5005THREADS */
760ac839 3375
fe20fd30 3376#if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
22239a37 3377struct perl_vars *
864dbfa3 3378Perl_GetVars(pTHX)
22239a37 3379{
533c011a 3380 return &PL_Vars;
22239a37 3381}
31fb1209
NIS
3382#endif
3383
3384char **
864dbfa3 3385Perl_get_op_names(pTHX)
31fb1209 3386{
fe20fd30 3387 return (char **)PL_op_name;
31fb1209
NIS
3388}
3389
3390char **
864dbfa3 3391Perl_get_op_descs(pTHX)
31fb1209 3392{
fe20fd30 3393 return (char **)PL_op_desc;
31fb1209 3394}
9e6b2b00
GS
3395
3396char *
864dbfa3 3397Perl_get_no_modify(pTHX)
9e6b2b00 3398{
ed6a78eb
NC
3399 /* Cast because we're not changing function prototypes in maint. */
3400 return (char *) PL_no_modify;
9e6b2b00
GS
3401}
3402
3403U32 *
864dbfa3 3404Perl_get_opargs(pTHX)
9e6b2b00 3405{
fe20fd30 3406 return (U32 *)PL_opargs;
9e6b2b00 3407}
51aa15f3 3408
0cb96387
GS
3409PPADDR_t*
3410Perl_get_ppaddr(pTHX)
3411{
12ae5dfc 3412 return (PPADDR_t*)PL_ppaddr;
0cb96387
GS
3413}
3414
a6c40364
GS
3415#ifndef HAS_GETENV_LEN
3416char *
bf4acbe4 3417Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
a6c40364 3418{
4f1e9d25 3419 char * const env_trans = PerlEnv_getenv(env_elem);
a6c40364
GS
3420 if (env_trans)
3421 *len = strlen(env_trans);
3422 return env_trans;
f675dbe5
CB
3423}
3424#endif
3425
dc9e4912
GS
3426
3427MGVTBL*
864dbfa3 3428Perl_get_vtbl(pTHX_ int vtbl_id)
dc9e4912 3429{
6d29369a 3430 const MGVTBL* result;
dc9e4912
GS
3431
3432 switch(vtbl_id) {
3433 case want_vtbl_sv:
3434 result = &PL_vtbl_sv;
3435 break;
3436 case want_vtbl_env:
3437 result = &PL_vtbl_env;
3438 break;
3439 case want_vtbl_envelem:
3440 result = &PL_vtbl_envelem;
3441 break;
3442 case want_vtbl_sig:
3443 result = &PL_vtbl_sig;
3444 break;
3445 case want_vtbl_sigelem:
3446 result = &PL_vtbl_sigelem;
3447 break;
3448 case want_vtbl_pack:
3449 result = &PL_vtbl_pack;
3450 break;
3451 case want_vtbl_packelem:
3452 result = &PL_vtbl_packelem;
3453 break;
3454 case want_vtbl_dbline:
3455 result = &PL_vtbl_dbline;
3456 break;
3457 case want_vtbl_isa:
3458 result = &PL_vtbl_isa;
3459 break;
3460 case want_vtbl_isaelem:
3461 result = &PL_vtbl_isaelem;
3462 break;
3463 case want_vtbl_arylen:
3464 result = &PL_vtbl_arylen;
3465 break;
3466 case want_vtbl_glob:
3467 result = &PL_vtbl_glob;
3468 break;
3469 case want_vtbl_mglob:
3470 result = &PL_vtbl_mglob;
3471 break;
3472 case want_vtbl_nkeys:
3473 result = &PL_vtbl_nkeys;
3474 break;
3475 case want_vtbl_taint:
3476 result = &PL_vtbl_taint;
3477 break;
3478 case want_vtbl_substr:
3479 result = &PL_vtbl_substr;
3480 break;
3481 case want_vtbl_vec:
3482 result = &PL_vtbl_vec;
3483 break;
3484 case want_vtbl_pos:
3485 result = &PL_vtbl_pos;
3486 break;
3487 case want_vtbl_bm:
3488 result = &PL_vtbl_bm;
3489 break;
3490 case want_vtbl_fm:
3491 result = &PL_vtbl_fm;
3492 break;
3493 case want_vtbl_uvar:
3494 result = &PL_vtbl_uvar;
3495 break;
4d1ff10f 3496#ifdef USE_5005THREADS
dc9e4912
GS
3497 case want_vtbl_mutex:
3498 result = &PL_vtbl_mutex;
3499 break;
3500#endif
3501 case want_vtbl_defelem:
3502 result = &PL_vtbl_defelem;
3503 break;
3504 case want_vtbl_regexp:
3505 result = &PL_vtbl_regexp;
3506 break;
3507 case want_vtbl_regdata:
3508 result = &PL_vtbl_regdata;
3509 break;
3510 case want_vtbl_regdatum:
3511 result = &PL_vtbl_regdatum;
3512 break;
3c90161d 3513#ifdef USE_LOCALE_COLLATE
dc9e4912
GS
3514 case want_vtbl_collxfrm:
3515 result = &PL_vtbl_collxfrm;
3516 break;
3c90161d 3517#endif
dc9e4912
GS
3518 case want_vtbl_amagic:
3519 result = &PL_vtbl_amagic;
3520 break;
3521 case want_vtbl_amagicelem:
3522 result = &PL_vtbl_amagicelem;
3523 break;
810b8aa5
GS
3524 case want_vtbl_backref:
3525 result = &PL_vtbl_backref;
3526 break;
323eb6b5
JH
3527 case want_vtbl_utf8:
3528 result = &PL_vtbl_utf8;
3529 break;
6d29369a
AL
3530 default:
3531 result = Null(MGVTBL*);
3532 break;
dc9e4912 3533 }
fe20fd30 3534 return (MGVTBL*)result;
dc9e4912
GS
3535}
3536
767df6a1 3537I32
864dbfa3 3538Perl_my_fflush_all(pTHX)
767df6a1 3539{
4f4e7967 3540#if defined(USE_PERLIO) || defined(FFLUSH_NULL) || defined(USE_SFIO)
ce720889 3541 return PerlIO_flush(NULL);
767df6a1 3542#else
8fbdfb7c 3543# if defined(HAS__FWALK)
f13a2bc0 3544 extern int fflush(FILE *);
74cac757
JH
3545 /* undocumented, unprototyped, but very useful BSDism */
3546 extern void _fwalk(int (*)(FILE *));
8fbdfb7c 3547 _fwalk(&fflush);
74cac757 3548 return 0;
8fa7f367 3549# else
8fbdfb7c 3550# if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
8fa7f367 3551 long open_max = -1;
8fbdfb7c 3552# ifdef PERL_FFLUSH_ALL_FOPEN_MAX
d2201af2 3553 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
8fbdfb7c 3554# else
8fa7f367 3555# if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
767df6a1 3556 open_max = sysconf(_SC_OPEN_MAX);
8fa7f367
JH
3557# else
3558# ifdef FOPEN_MAX
74cac757 3559 open_max = FOPEN_MAX;
8fa7f367
JH
3560# else
3561# ifdef OPEN_MAX
74cac757 3562 open_max = OPEN_MAX;
8fa7f367
JH
3563# else
3564# ifdef _NFILE
d2201af2 3565 open_max = _NFILE;
8fa7f367
JH
3566# endif
3567# endif
74cac757 3568# endif
767df6a1
JH
3569# endif
3570# endif
767df6a1
JH
3571 if (open_max > 0) {
3572 long i;
3573 for (i = 0; i < open_max; i++)
d2201af2
AD
3574 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3575 STDIO_STREAM_ARRAY[i]._file < open_max &&
3576 STDIO_STREAM_ARRAY[i]._flag)
3577 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
767df6a1
JH
3578 return 0;
3579 }
8fbdfb7c 3580# endif
5b7ea690 3581 SETERRNO(EBADF,RMS_IFI);
767df6a1 3582 return EOF;
74cac757 3583# endif
767df6a1
JH
3584#endif
3585}
097ee67d 3586
69282e91 3587void
bc37a18f
RG
3588Perl_report_evil_fh(pTHX_ GV *gv, IO *io, I32 op)
3589{
e4a519ba 3590 const char * const func =
66fc2fa5
JH
3591 op == OP_READLINE ? "readline" : /* "<HANDLE>" not nice */
3592 op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */
bc37a18f 3593 PL_op_desc[op];
e4a519ba
AL
3594 const char * const pars = OP_IS_FILETEST(op) ? "" : "()";
3595 const char * const type = OP_IS_SOCKET(op)
5b7ea690
JH