This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove full stop in the 'try' feature heading
[perl5.git] / util.c
1 /*    util.c
2  *
3  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4  *    2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5  *
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.
8  *
9  */
10
11 /*
12  * 'Very useful, no doubt, that was to Saruman; yet it seems that he was
13  *  not content.'                                    --Gandalf to Pippin
14  *
15  *     [p.598 of _The Lord of the Rings_, III/xi: "The Palantír"]
16  */
17
18 /* This file contains assorted utility routines.
19  * Which is a polite way of saying any stuff that people couldn't think of
20  * a better place for. Amongst other things, it includes the warning and
21  * dieing stuff, plus wrappers for malloc code.
22  */
23
24 #include "EXTERN.h"
25 #define PERL_IN_UTIL_C
26 #include "perl.h"
27 #include "reentr.h"
28
29 #if defined(USE_PERLIO)
30 #include "perliol.h" /* For PerlIOUnix_refcnt */
31 #endif
32
33 #ifndef PERL_MICRO
34 #include <signal.h>
35 #ifndef SIG_ERR
36 # define SIG_ERR ((Sighandler_t) -1)
37 #endif
38 #endif
39
40 #include <math.h>
41 #include <stdlib.h>
42
43 #ifdef __Lynx__
44 /* Missing protos on LynxOS */
45 int putenv(char *);
46 #endif
47
48 #ifdef __amigaos__
49 # include "amigaos4/amigaio.h"
50 #endif
51
52 #ifdef HAS_SELECT
53 # ifdef I_SYS_SELECT
54 #  include <sys/select.h>
55 # endif
56 #endif
57
58 #ifdef USE_C_BACKTRACE
59 #  ifdef I_BFD
60 #    define USE_BFD
61 #    ifdef PERL_DARWIN
62 #      undef USE_BFD /* BFD is useless in OS X. */
63 #    endif
64 #    ifdef USE_BFD
65 #      include <bfd.h>
66 #    endif
67 #  endif
68 #  ifdef I_DLFCN
69 #    include <dlfcn.h>
70 #  endif
71 #  ifdef I_EXECINFO
72 #    include <execinfo.h>
73 #  endif
74 #endif
75
76 #ifdef PERL_DEBUG_READONLY_COW
77 # include <sys/mman.h>
78 #endif
79
80 #define FLUSH
81
82 /* NOTE:  Do not call the next three routines directly.  Use the macros
83  * in handy.h, so that we can easily redefine everything to do tracking of
84  * allocated hunks back to the original New to track down any memory leaks.
85  * XXX This advice seems to be widely ignored :-(   --AD  August 1996.
86  */
87
88 #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL)
89 #  define ALWAYS_NEED_THX
90 #endif
91
92 #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW)
93 static void
94 S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header)
95 {
96     if (header->readonly
97      && mprotect(header, header->size, PROT_READ|PROT_WRITE))
98         Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
99                          header, header->size, errno);
100 }
101
102 static void
103 S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header)
104 {
105     if (header->readonly
106      && mprotect(header, header->size, PROT_READ))
107         Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
108                          header, header->size, errno);
109 }
110 # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo)
111 # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo)
112 #else
113 # define maybe_protect_rw(foo) NOOP
114 # define maybe_protect_ro(foo) NOOP
115 #endif
116
117 #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW)
118  /* Use memory_debug_header */
119 # define USE_MDH
120 # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \
121    || defined(PERL_DEBUG_READONLY_COW)
122 #  define MDH_HAS_SIZE
123 # endif
124 #endif
125
126 /*
127 =for apidoc_section $memory
128 =for apidoc safesysmalloc
129 Paranoid version of system's malloc()
130
131 =cut
132 */
133
134 Malloc_t
135 Perl_safesysmalloc(MEM_SIZE size)
136 {
137 #ifdef ALWAYS_NEED_THX
138     dTHX;
139 #endif
140     Malloc_t ptr;
141     dSAVEDERRNO;
142
143 #ifdef USE_MDH
144     if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
145         goto out_of_memory;
146     size += PERL_MEMORY_DEBUG_HEADER_SIZE;
147 #endif
148 #ifdef DEBUGGING
149     if ((SSize_t)size < 0)
150         Perl_croak_nocontext("panic: malloc, size=%" UVuf, (UV) size);
151 #endif
152     if (!size) size = 1;        /* malloc(0) is NASTY on our system */
153     SAVE_ERRNO;
154 #ifdef PERL_DEBUG_READONLY_COW
155     if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
156                     MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
157         perror("mmap failed");
158         abort();
159     }
160 #else
161     ptr = (Malloc_t)PerlMem_malloc(size);
162 #endif
163     PERL_ALLOC_CHECK(ptr);
164     if (ptr != NULL) {
165 #ifdef USE_MDH
166         struct perl_memory_debug_header *const header
167             = (struct perl_memory_debug_header *)ptr;
168 #endif
169
170 #ifdef PERL_POISON
171         PoisonNew(((char *)ptr), size, char);
172 #endif
173
174 #ifdef PERL_TRACK_MEMPOOL
175         header->interpreter = aTHX;
176         /* Link us into the list.  */
177         header->prev = &PL_memory_debug_header;
178         header->next = PL_memory_debug_header.next;
179         PL_memory_debug_header.next = header;
180         maybe_protect_rw(header->next);
181         header->next->prev = header;
182         maybe_protect_ro(header->next);
183 #  ifdef PERL_DEBUG_READONLY_COW
184         header->readonly = 0;
185 #  endif
186 #endif
187 #ifdef MDH_HAS_SIZE
188         header->size = size;
189 #endif
190         ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
191         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
192
193         /* malloc() can modify errno() even on success, but since someone
194            writing perl code doesn't have any control over when perl calls
195            malloc() we need to hide that.
196         */
197         RESTORE_ERRNO;
198     }
199     else {
200 #ifdef USE_MDH
201       out_of_memory:
202 #endif
203         {
204 #ifndef ALWAYS_NEED_THX
205             dTHX;
206 #endif
207             if (PL_nomemok)
208                 ptr =  NULL;
209             else
210                 croak_no_mem();
211         }
212     }
213     return ptr;
214 }
215
216 /*
217 =for apidoc safesysrealloc
218 Paranoid version of system's realloc()
219
220 =cut
221 */
222
223 Malloc_t
224 Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
225 {
226 #ifdef ALWAYS_NEED_THX
227     dTHX;
228 #endif
229     Malloc_t ptr;
230 #ifdef PERL_DEBUG_READONLY_COW
231     const MEM_SIZE oldsize = where
232         ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size
233         : 0;
234 #endif
235
236     if (!size) {
237         safesysfree(where);
238         ptr = NULL;
239     }
240     else if (!where) {
241         ptr = safesysmalloc(size);
242     }
243     else {
244         dSAVE_ERRNO;
245 #ifdef USE_MDH
246         where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
247         if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size)
248             goto out_of_memory;
249         size += PERL_MEMORY_DEBUG_HEADER_SIZE;
250         {
251             struct perl_memory_debug_header *const header
252                 = (struct perl_memory_debug_header *)where;
253
254 # ifdef PERL_TRACK_MEMPOOL
255             if (header->interpreter != aTHX) {
256                 Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p",
257                                      header->interpreter, aTHX);
258             }
259             assert(header->next->prev == header);
260             assert(header->prev->next == header);
261 #  ifdef PERL_POISON
262             if (header->size > size) {
263                 const MEM_SIZE freed_up = header->size - size;
264                 char *start_of_freed = ((char *)where) + size;
265                 PoisonFree(start_of_freed, freed_up, char);
266             }
267 #  endif
268 # endif
269 # ifdef MDH_HAS_SIZE
270             header->size = size;
271 # endif
272         }
273 #endif
274 #ifdef DEBUGGING
275         if ((SSize_t)size < 0)
276             Perl_croak_nocontext("panic: realloc, size=%" UVuf, (UV)size);
277 #endif
278 #ifdef PERL_DEBUG_READONLY_COW
279         if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE,
280                         MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
281             perror("mmap failed");
282             abort();
283         }
284         Copy(where,ptr,oldsize < size ? oldsize : size,char);
285         if (munmap(where, oldsize)) {
286             perror("munmap failed");
287             abort();
288         }
289 #else
290         ptr = (Malloc_t)PerlMem_realloc(where,size);
291 #endif
292         PERL_ALLOC_CHECK(ptr);
293
294     /* MUST do this fixup first, before doing ANYTHING else, as anything else
295        might allocate memory/free/move memory, and until we do the fixup, it
296        may well be chasing (and writing to) free memory.  */
297         if (ptr != NULL) {
298 #ifdef PERL_TRACK_MEMPOOL
299             struct perl_memory_debug_header *const header
300                 = (struct perl_memory_debug_header *)ptr;
301
302 #  ifdef PERL_POISON
303             if (header->size < size) {
304                 const MEM_SIZE fresh = size - header->size;
305                 char *start_of_fresh = ((char *)ptr) + size;
306                 PoisonNew(start_of_fresh, fresh, char);
307             }
308 #  endif
309
310             maybe_protect_rw(header->next);
311             header->next->prev = header;
312             maybe_protect_ro(header->next);
313             maybe_protect_rw(header->prev);
314             header->prev->next = header;
315             maybe_protect_ro(header->prev);
316 #endif
317             ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
318
319             /* realloc() can modify errno() even on success, but since someone
320                writing perl code doesn't have any control over when perl calls
321                realloc() we need to hide that.
322             */
323             RESTORE_ERRNO;
324         }
325
326     /* In particular, must do that fixup above before logging anything via
327      *printf(), as it can reallocate memory, which can cause SEGVs.  */
328
329         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++));
330         DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size));
331
332         if (ptr == NULL) {
333 #ifdef USE_MDH
334           out_of_memory:
335 #endif
336             {
337 #ifndef ALWAYS_NEED_THX
338                 dTHX;
339 #endif
340                 if (PL_nomemok)
341                     ptr = NULL;
342                 else
343                     croak_no_mem();
344             }
345         }
346     }
347     return ptr;
348 }
349
350 /*
351 =for apidoc safesysfree
352 Safe version of system's free()
353
354 =cut
355 */
356
357 Free_t
358 Perl_safesysfree(Malloc_t where)
359 {
360 #ifdef ALWAYS_NEED_THX
361     dTHX;
362 #endif
363     DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) free\n",PTR2UV(where),(long)PL_an++));
364     if (where) {
365 #ifdef USE_MDH
366         Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE);
367         {
368             struct perl_memory_debug_header *const header
369                 = (struct perl_memory_debug_header *)where_intrn;
370
371 # ifdef MDH_HAS_SIZE
372             const MEM_SIZE size = header->size;
373 # endif
374 # ifdef PERL_TRACK_MEMPOOL
375             if (header->interpreter != aTHX) {
376                 Perl_croak_nocontext("panic: free from wrong pool, %p!=%p",
377                                      header->interpreter, aTHX);
378             }
379             if (!header->prev) {
380                 Perl_croak_nocontext("panic: duplicate free");
381             }
382             if (!(header->next))
383                 Perl_croak_nocontext("panic: bad free, header->next==NULL");
384             if (header->next->prev != header || header->prev->next != header) {
385                 Perl_croak_nocontext("panic: bad free, ->next->prev=%p, "
386                                      "header=%p, ->prev->next=%p",
387                                      header->next->prev, header,
388                                      header->prev->next);
389             }
390             /* Unlink us from the chain.  */
391             maybe_protect_rw(header->next);
392             header->next->prev = header->prev;
393             maybe_protect_ro(header->next);
394             maybe_protect_rw(header->prev);
395             header->prev->next = header->next;
396             maybe_protect_ro(header->prev);
397             maybe_protect_rw(header);
398 #  ifdef PERL_POISON
399             PoisonNew(where_intrn, size, char);
400 #  endif
401             /* Trigger the duplicate free warning.  */
402             header->next = NULL;
403 # endif
404 # ifdef PERL_DEBUG_READONLY_COW
405             if (munmap(where_intrn, size)) {
406                 perror("munmap failed");
407                 abort();
408             }   
409 # endif
410         }
411 #else
412         Malloc_t where_intrn = where;
413 #endif /* USE_MDH */
414 #ifndef PERL_DEBUG_READONLY_COW
415         PerlMem_free(where_intrn);
416 #endif
417     }
418 }
419
420 /*
421 =for apidoc safesyscalloc
422 Safe version of system's calloc()
423
424 =cut
425 */
426
427 Malloc_t
428 Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
429 {
430 #ifdef ALWAYS_NEED_THX
431     dTHX;
432 #endif
433     Malloc_t ptr;
434 #if defined(USE_MDH) || defined(DEBUGGING)
435     MEM_SIZE total_size = 0;
436 #endif
437
438     /* Even though calloc() for zero bytes is strange, be robust. */
439     if (size && (count <= MEM_SIZE_MAX / size)) {
440 #if defined(USE_MDH) || defined(DEBUGGING)
441         total_size = size * count;
442 #endif
443     }
444     else
445         croak_memory_wrap();
446 #ifdef USE_MDH
447     if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size)
448         total_size += PERL_MEMORY_DEBUG_HEADER_SIZE;
449     else
450         croak_memory_wrap();
451 #endif
452 #ifdef DEBUGGING
453     if ((SSize_t)size < 0 || (SSize_t)count < 0)
454         Perl_croak_nocontext("panic: calloc, size=%" UVuf ", count=%" UVuf,
455                              (UV)size, (UV)count);
456 #endif
457 #ifdef PERL_DEBUG_READONLY_COW
458     if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE,
459                     MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
460         perror("mmap failed");
461         abort();
462     }
463 #elif defined(PERL_TRACK_MEMPOOL)
464     /* Have to use malloc() because we've added some space for our tracking
465        header.  */
466     /* malloc(0) is non-portable. */
467     ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1);
468 #else
469     /* Use calloc() because it might save a memset() if the memory is fresh
470        and clean from the OS.  */
471     if (count && size)
472         ptr = (Malloc_t)PerlMem_calloc(count, size);
473     else /* calloc(0) is non-portable. */
474         ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1);
475 #endif
476     PERL_ALLOC_CHECK(ptr);
477     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) calloc %zu x %zu = %zu bytes\n",PTR2UV(ptr),(long)PL_an++, count, size, total_size));
478     if (ptr != NULL) {
479 #ifdef USE_MDH
480         {
481             struct perl_memory_debug_header *const header
482                 = (struct perl_memory_debug_header *)ptr;
483
484 #  ifndef PERL_DEBUG_READONLY_COW
485             memset((void*)ptr, 0, total_size);
486 #  endif
487 #  ifdef PERL_TRACK_MEMPOOL
488             header->interpreter = aTHX;
489             /* Link us into the list.  */
490             header->prev = &PL_memory_debug_header;
491             header->next = PL_memory_debug_header.next;
492             PL_memory_debug_header.next = header;
493             maybe_protect_rw(header->next);
494             header->next->prev = header;
495             maybe_protect_ro(header->next);
496 #    ifdef PERL_DEBUG_READONLY_COW
497             header->readonly = 0;
498 #    endif
499 #  endif
500 #  ifdef MDH_HAS_SIZE
501             header->size = total_size;
502 #  endif
503             ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE);
504         }
505 #endif
506         return ptr;
507     }
508     else {
509 #ifndef ALWAYS_NEED_THX
510         dTHX;
511 #endif
512         if (PL_nomemok)
513             return NULL;
514         croak_no_mem();
515     }
516 }
517
518 /* These must be defined when not using Perl's malloc for binary
519  * compatibility */
520
521 #ifndef MYMALLOC
522
523 Malloc_t Perl_malloc (MEM_SIZE nbytes)
524 {
525 #ifdef PERL_IMPLICIT_SYS
526     dTHX;
527 #endif
528     return (Malloc_t)PerlMem_malloc(nbytes);
529 }
530
531 Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size)
532 {
533 #ifdef PERL_IMPLICIT_SYS
534     dTHX;
535 #endif
536     return (Malloc_t)PerlMem_calloc(elements, size);
537 }
538
539 Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes)
540 {
541 #ifdef PERL_IMPLICIT_SYS
542     dTHX;
543 #endif
544     return (Malloc_t)PerlMem_realloc(where, nbytes);
545 }
546
547 Free_t   Perl_mfree (Malloc_t where)
548 {
549 #ifdef PERL_IMPLICIT_SYS
550     dTHX;
551 #endif
552     PerlMem_free(where);
553 }
554
555 #endif
556
557 /* This is the value stored in *retlen in the two delimcpy routines below when
558  * there wasn't enough room in the destination to store everything it was asked
559  * to.  The value is deliberately very large so that hopefully if code uses it
560  * unquestioningly to access memory, it will likely segfault.  And it is small
561  * enough that if the caller does some arithmetic on it before accessing, it
562  * won't overflow into a small legal number. */
563 #define DELIMCPY_OUT_OF_BOUNDS_RET  I32_MAX
564
565 /*
566 =for apidoc_section $string
567 =for apidoc delimcpy_no_escape
568
569 Copy a source buffer to a destination buffer, stopping at (but not including)
570 the first occurrence in the source of the delimiter byte, C<delim>.  The source
571 is the bytes between S<C<from> and C<from_end> - 1>.  Similarly, the dest is
572 C<to> up to C<to_end>.
573
574 The number of bytes copied is written to C<*retlen>.
575
576 Returns the position of C<delim> in the C<from> buffer, but if there is no
577 such occurrence before C<from_end>, then C<from_end> is returned, and the entire
578 buffer S<C<from> .. C<from_end> - 1> is copied.
579
580 If there is room in the destination available after the copy, an extra
581 terminating safety C<NUL> byte is appended (not included in the returned
582 length).
583
584 The error case is if the destination buffer is not large enough to accommodate
585 everything that should be copied.  In this situation, a value larger than
586 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
587 fits will be written to the destination.  Not having room for the safety C<NUL>
588 is not considered an error.
589
590 =cut
591 */
592 char *
593 Perl_delimcpy_no_escape(char *to, const char *to_end,
594                         const char *from, const char *from_end,
595                         const int delim, I32 *retlen)
596 {
597     const char * delim_pos;
598     Ptrdiff_t from_len = from_end - from;
599     Ptrdiff_t to_len = to_end - to;
600     SSize_t copy_len;
601
602     PERL_ARGS_ASSERT_DELIMCPY_NO_ESCAPE;
603
604     assert(from_len >= 0);
605     assert(to_len >= 0);
606
607     /* Look for the first delimiter in the source */
608     delim_pos = (const char *) memchr(from, delim, from_len);
609
610     /* Copy up to where the delimiter was found, or the entire buffer if not
611      * found */
612     copy_len = (delim_pos) ? delim_pos - from : from_len;
613
614     /* If not enough room, copy as much as can fit, and set error return */
615     if (copy_len > to_len) {
616         Copy(from, to, to_len, char);
617         *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
618     }
619     else {
620         Copy(from, to, copy_len, char);
621
622         /* If there is extra space available, add a trailing NUL */
623         if (copy_len < to_len) {
624             to[copy_len] = '\0';
625         }
626
627         *retlen = copy_len;
628     }
629
630     return (char *) from + copy_len;
631 }
632
633 /*
634 =for apidoc delimcpy
635
636 Copy a source buffer to a destination buffer, stopping at (but not including)
637 the first occurrence in the source of an unescaped (defined below) delimiter
638 byte, C<delim>.  The source is the bytes between S<C<from> and C<from_end> -
639 1>.  Similarly, the dest is C<to> up to C<to_end>.
640
641 The number of bytes copied is written to C<*retlen>.
642
643 Returns the position of the first uncopied C<delim> in the C<from> buffer, but
644 if there is no such occurrence before C<from_end>, then C<from_end> is returned,
645 and the entire buffer S<C<from> .. C<from_end> - 1> is copied.
646
647 If there is room in the destination available after the copy, an extra
648 terminating safety C<NUL> byte is appended (not included in the returned
649 length).
650
651 The error case is if the destination buffer is not large enough to accommodate
652 everything that should be copied.  In this situation, a value larger than
653 S<C<to_end> - C<to>> is written to C<*retlen>, and as much of the source as
654 fits will be written to the destination.  Not having room for the safety C<NUL>
655 is not considered an error.
656
657 In the following examples, let C<x> be the delimiter, and C<0> represent a C<NUL>
658 byte (B<NOT> the digit C<0>).  Then we would have
659
660   Source     Destination
661  abcxdef        abc0
662
663 provided the destination buffer is at least 4 bytes long.
664
665 An escaped delimiter is one which is immediately preceded by a single
666 backslash.  Escaped delimiters are copied, and the copy continues past the
667 delimiter; the backslash is not copied:
668
669   Source       Destination
670  abc\xdef       abcxdef0
671
672 (provided the destination buffer is at least 8 bytes long).
673
674 It's actually somewhat more complicated than that. A sequence of any odd number
675 of backslashes escapes the following delimiter, and the copy continues with
676 exactly one of the backslashes stripped.
677
678      Source         Destination
679      abc\xdef          abcxdef0
680    abc\\\xdef        abc\\xdef0
681  abc\\\\\xdef      abc\\\\xdef0
682
683 (as always, if the destination is large enough)
684
685 An even number of preceding backslashes does not escape the delimiter, so that
686 the copy stops just before it, and includes all the backslashes (no stripping;
687 zero is considered even):
688
689       Source         Destination
690       abcxdef          abc0
691     abc\\xdef          abc\\0
692   abc\\\\xdef          abc\\\\0
693
694 =cut
695 */
696
697 char *
698 Perl_delimcpy(char *to, const char *to_end,
699               const char *from, const char *from_end,
700               const int delim, I32 *retlen)
701 {
702     const char * const orig_to = to;
703     Ptrdiff_t copy_len = 0;
704     bool stopped_early = FALSE;     /* Ran out of room to copy to */
705
706     PERL_ARGS_ASSERT_DELIMCPY;
707     assert(from_end >= from);
708     assert(to_end >= to);
709
710     /* Don't use the loop for the trivial case of the first character being the
711      * delimiter; otherwise would have to worry inside the loop about backing
712      * up before the start of 'from' */
713     if (LIKELY(from_end > from && *from != delim)) {
714         while ((copy_len = from_end - from) > 0) {
715             const char * backslash_pos;
716             const char * delim_pos;
717
718             /* Look for the next delimiter in the remaining portion of the
719              * source. A loop invariant is that we already know that the copy
720              * should include *from; this comes from the conditional before the
721              * loop, and how we set things up at the end of each iteration */
722             delim_pos = (const char *) memchr(from + 1, delim, copy_len - 1);
723
724             /* If didn't find it, done looking; set up so copies all of the
725              * source */
726             if (! delim_pos) {
727                 copy_len = from_end - from;
728                 break;
729             }
730
731             /* Look for a backslash immediately before the delimiter */
732             backslash_pos = delim_pos - 1;
733
734             /* If the delimiter is not escaped, this ends the copy */
735             if (*backslash_pos != '\\') {
736                 copy_len = delim_pos - from;
737                 break;
738             }
739
740             /* Here there is a backslash just before the delimiter, but it
741              * could be the final backslash in a sequence of them.  Backup to
742              * find the first one in it. */
743             do {
744                 backslash_pos--;
745             }
746             while (backslash_pos >= from && *backslash_pos == '\\');
747
748             /* If the number of backslashes is even, they just escape one
749              * another, leaving the delimiter unescaped, and stopping the copy.
750              * */
751             if (! ((delim_pos - (backslash_pos + 1)) & 1)) {
752                 copy_len = delim_pos - from;  /* even, copy up to delimiter */
753                 break;
754             }
755
756             /* Here is odd, so the delimiter is escaped.  We will try to copy
757              * all but the final backslash in the sequence */
758             copy_len = delim_pos - 1 - from;
759
760             /* Do the copy, but not beyond the end of the destination */
761             if (copy_len >= to_end - to) {
762                 Copy(from, to, to_end - to, char);
763                 stopped_early = TRUE;
764                 to = (char *) to_end;
765             }
766             else {
767                 Copy(from, to, copy_len, char);
768                 to += copy_len;
769             }
770
771             /* Set up so next iteration will include the delimiter */
772             from = delim_pos;
773         }
774     }
775
776     /* Here, have found the final segment to copy.  Copy that, but not beyond
777      * the size of the destination.  If not enough room, copy as much as can
778      * fit, and set error return */
779     if (stopped_early || copy_len > to_end - to) {
780         Copy(from, to, to_end - to, char);
781         *retlen = DELIMCPY_OUT_OF_BOUNDS_RET;
782     }
783     else {
784         Copy(from, to, copy_len, char);
785
786         to += copy_len;
787
788         /* If there is extra space available, add a trailing NUL */
789         if (to < to_end) {
790             *to = '\0';
791         }
792
793         *retlen = to - orig_to;
794     }
795
796     return (char *) from + copy_len;
797 }
798
799 /*
800 =for apidoc ninstr
801
802 Find the first (leftmost) occurrence of a sequence of bytes within another
803 sequence.  This is the Perl version of C<strstr()>, extended to handle
804 arbitrary sequences, potentially containing embedded C<NUL> characters (C<NUL>
805 is what the initial C<n> in the function name stands for; some systems have an
806 equivalent, C<memmem()>, but with a somewhat different API).
807
808 Another way of thinking about this function is finding a needle in a haystack.
809 C<big> points to the first byte in the haystack.  C<big_end> points to one byte
810 beyond the final byte in the haystack.  C<little> points to the first byte in
811 the needle.  C<little_end> points to one byte beyond the final byte in the
812 needle.  All the parameters must be non-C<NULL>.
813
814 The function returns C<NULL> if there is no occurrence of C<little> within
815 C<big>.  If C<little> is the empty string, C<big> is returned.
816
817 Because this function operates at the byte level, and because of the inherent
818 characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the
819 needle and the haystack are strings with the same UTF-8ness, but not if the
820 UTF-8ness differs.
821
822 =cut
823
824 */
825
826 char *
827 Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend)
828 {
829     PERL_ARGS_ASSERT_NINSTR;
830
831 #ifdef HAS_MEMMEM
832     return ninstr(big, bigend, little, lend);
833 #else
834
835     if (little >= lend) {
836         return (char*) big;
837     }
838     else {
839         const U8 first = *little;
840         Size_t lsize;
841
842         /* No match can start closer to the end of the haystack than the length
843          * of the needle. */
844         bigend -= lend - little;
845         little++;       /* Look for 'first', then the remainder is in here */
846         lsize = lend - little;
847
848         while (big <= bigend) {
849             big = (char *) memchr((U8 *) big, first, bigend - big + 1);
850             if (big == NULL || big > bigend) {
851                 return NULL;
852             }
853
854             if (memEQ(big + 1, little, lsize)) {
855                 return (char*) big;
856             }
857             big++;
858         }
859     }
860
861     return NULL;
862
863 #endif
864
865 }
866
867 /*
868 =for apidoc rninstr
869
870 Like C<L</ninstr>>, but instead finds the final (rightmost) occurrence of a
871 sequence of bytes within another sequence, returning C<NULL> if there is no
872 such occurrence.
873
874 =cut
875
876 */
877
878 char *
879 Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend)
880 {
881     const Ptrdiff_t little_len = lend - little;
882     const Ptrdiff_t big_len = bigend - big;
883
884     PERL_ARGS_ASSERT_RNINSTR;
885
886     /* A non-existent needle trivially matches the rightmost possible position
887      * in the haystack */
888     if (UNLIKELY(little_len <= 0)) {
889         return (char*)bigend;
890     }
891
892     /* If the needle is larger than the haystack, the needle can't possibly fit
893      * inside the haystack. */
894     if (UNLIKELY(little_len > big_len)) {
895         return NULL;
896     }
897
898     /* Special case length 1 needles.  It's trivial if we have memrchr();
899      * and otherwise we just do a per-byte search backwards.
900      *
901      * XXX When we don't have memrchr, we could use something like
902      * S_find_next_masked( or S_find_span_end() to do per-word searches */
903     if (little_len == 1) {
904         const char final = *little;
905
906 #ifdef HAS_MEMRCHR
907
908         return (char *) memrchr(big, final, big_len);
909 #else
910         const char * cur = bigend - 1;
911
912         do {
913             if (*cur == final) {
914                 return (char *) cur;
915             }
916         } while (--cur >= big);
917
918         return NULL;
919 #endif
920
921     }
922     else {  /* Below, the needle is longer than a single byte */
923
924         /* We search backwards in the haystack for the final character of the
925          * needle.  Each time one is found, we see if the characters just
926          * before it in the haystack match the rest of the needle. */
927         const char final = *(lend - 1);
928
929         /* What matches consists of 'little_len'-1 characters, then the final
930          * one */
931         const Size_t prefix_len = little_len - 1;
932
933         /* If the final character in the needle is any closer than this to the
934          * left edge, there wouldn't be enough room for all of it to fit in the
935          * haystack */
936         const char * const left_fence = big + prefix_len;
937
938         /* Start at the right edge */
939         char * cur = (char *) bigend;
940
941         /* memrchr() makes the search easy (and fast); otherwise, look
942          * backwards byte-by-byte. */
943         do {
944
945 #ifdef HAS_MEMRCHR
946
947             cur = (char *) memrchr(left_fence, final, cur - left_fence);
948             if (cur == NULL) {
949                 return NULL;
950             }
951 #else
952             do {
953                 cur--;
954                 if (cur < left_fence) {
955                     return NULL;
956                 }
957             }
958             while (*cur != final);
959 #endif
960
961             /* Here, we know that *cur is 'final'; see if the preceding bytes
962              * of the needle also match the corresponding haystack bytes */
963             if memEQ(cur - prefix_len, little, prefix_len) {
964                 return cur - prefix_len;
965             }
966         } while (cur > left_fence);
967
968         return NULL;
969     }
970 }
971
972 /* As a space optimization, we do not compile tables for strings of length
973    0 and 1, and for strings of length 2 unless FBMcf_TAIL.  These are
974    special-cased in fbm_instr().
975
976    If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
977
978 /*
979
980 =for apidoc fbm_compile
981
982 Analyzes the string in order to make fast searches on it using C<fbm_instr()>
983 -- the Boyer-Moore algorithm.
984
985 =cut
986 */
987
988 void
989 Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
990 {
991     const U8 *s;
992     STRLEN i;
993     STRLEN len;
994     MAGIC *mg;
995
996     PERL_ARGS_ASSERT_FBM_COMPILE;
997
998     if (isGV_with_GP(sv) || SvROK(sv))
999         return;
1000
1001     if (SvVALID(sv))
1002         return;
1003
1004     if (flags & FBMcf_TAIL) {
1005         MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
1006         sv_catpvs(sv, "\n");            /* Taken into account in fbm_instr() */
1007         if (mg && mg->mg_len >= 0)
1008             mg->mg_len++;
1009     }
1010     if (!SvPOK(sv) || SvNIOKp(sv))
1011         s = (U8*)SvPV_force_mutable(sv, len);
1012     else s = (U8 *)SvPV_mutable(sv, len);
1013     if (len == 0)               /* TAIL might be on a zero-length string. */
1014         return;
1015     SvUPGRADE(sv, SVt_PVMG);
1016     SvIOK_off(sv);
1017     SvNOK_off(sv);
1018
1019     /* add PERL_MAGIC_bm magic holding the FBM lookup table */
1020
1021     assert(!mg_find(sv, PERL_MAGIC_bm));
1022     mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0);
1023     assert(mg);
1024
1025     if (len > 2) {
1026         /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use
1027            the BM table.  */
1028         const U8 mlen = (len>255) ? 255 : (U8)len;
1029         const unsigned char *const sb = s + len - mlen; /* first char (maybe) */
1030         U8 *table;
1031
1032         Newx(table, 256, U8);
1033         memset((void*)table, mlen, 256);
1034         mg->mg_ptr = (char *)table;
1035         mg->mg_len = 256;
1036
1037         s += len - 1; /* last char */
1038         i = 0;
1039         while (s >= sb) {
1040             if (table[*s] == mlen)
1041                 table[*s] = (U8)i;
1042             s--, i++;
1043         }
1044     }
1045
1046     BmUSEFUL(sv) = 100;                 /* Initial value */
1047     ((XPVNV*)SvANY(sv))->xnv_u.xnv_bm_tail = cBOOL(flags & FBMcf_TAIL);
1048 }
1049
1050
1051 /*
1052 =for apidoc fbm_instr
1053
1054 Returns the location of the SV in the string delimited by C<big> and
1055 C<bigend> (C<bigend>) is the char following the last char).
1056 It returns C<NULL> if the string can't be found.  The C<sv>
1057 does not have to be C<fbm_compiled>, but the search will not be as fast
1058 then.
1059
1060 =cut
1061
1062 If SvTAIL(littlestr) is true, a fake "\n" was appended to the string
1063 during FBM compilation due to FBMcf_TAIL in flags. It indicates that
1064 the littlestr must be anchored to the end of bigstr (or to any \n if
1065 FBMrf_MULTILINE).
1066
1067 E.g. The regex compiler would compile /abc/ to a littlestr of "abc",
1068 while /abc$/ compiles to "abc\n" with SvTAIL() true.
1069
1070 A littlestr of "abc", !SvTAIL matches as /abc/;
1071 a littlestr of "ab\n", SvTAIL matches as:
1072    without FBMrf_MULTILINE: /ab\n?\z/
1073    with    FBMrf_MULTILINE: /ab\n/ || /ab\z/;
1074
1075 (According to Ilya from 1999; I don't know if this is still true, DAPM 2015):
1076   "If SvTAIL is actually due to \Z or \z, this gives false positives
1077   if multiline".
1078 */
1079
1080
1081 char *
1082 Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags)
1083 {
1084     unsigned char *s;
1085     STRLEN l;
1086     const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l);
1087     STRLEN littlelen = l;
1088     const I32 multiline = flags & FBMrf_MULTILINE;
1089     bool valid = SvVALID(littlestr);
1090     bool tail = valid ? cBOOL(SvTAIL(littlestr)) : FALSE;
1091
1092     PERL_ARGS_ASSERT_FBM_INSTR;
1093
1094     assert(bigend >= big);
1095
1096     if ((STRLEN)(bigend - big) < littlelen) {
1097         if (     tail
1098              && ((STRLEN)(bigend - big) == littlelen - 1)
1099              && (littlelen == 1
1100                  || (*big == *little &&
1101                      memEQ((char *)big, (char *)little, littlelen - 1))))
1102             return (char*)big;
1103         return NULL;
1104     }
1105
1106     switch (littlelen) { /* Special cases for 0, 1 and 2  */
1107     case 0:
1108         return (char*)big;              /* Cannot be SvTAIL! */
1109
1110     case 1:
1111             if (tail && !multiline) /* Anchor only! */
1112                 /* [-1] is safe because we know that bigend != big.  */
1113                 return (char *) (bigend - (bigend[-1] == '\n'));
1114
1115             s = (unsigned char *)memchr((void*)big, *little, bigend-big);
1116             if (s)
1117                 return (char *)s;
1118             if (tail)
1119                 return (char *) bigend;
1120             return NULL;
1121
1122     case 2:
1123         if (tail && !multiline) {
1124             /* a littlestr with SvTAIL must be of the form "X\n" (where X
1125              * is a single char). It is anchored, and can only match
1126              * "....X\n"  or  "....X" */
1127             if (bigend[-2] == *little && bigend[-1] == '\n')
1128                 return (char*)bigend - 2;
1129             if (bigend[-1] == *little)
1130                 return (char*)bigend - 1;
1131             return NULL;
1132         }
1133
1134         {
1135             /* memchr() is likely to be very fast, possibly using whatever
1136              * hardware support is available, such as checking a whole
1137              * cache line in one instruction.
1138              * So for a 2 char pattern, calling memchr() is likely to be
1139              * faster than running FBM, or rolling our own. The previous
1140              * version of this code was roll-your-own which typically
1141              * only needed to read every 2nd char, which was good back in
1142              * the day, but no longer.
1143              */
1144             unsigned char c1 = little[0];
1145             unsigned char c2 = little[1];
1146
1147             /* *** for all this case, bigend points to the last char,
1148              * not the trailing \0: this makes the conditions slightly
1149              * simpler */
1150             bigend--;
1151             s = big;
1152             if (c1 != c2) {
1153                 while (s < bigend) {
1154                     /* do a quick test for c1 before calling memchr();
1155                      * this avoids the expensive fn call overhead when
1156                      * there are lots of c1's */
1157                     if (LIKELY(*s != c1)) {
1158                         s++;
1159                         s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1160                         if (!s)
1161                             break;
1162                     }
1163                     if (s[1] == c2)
1164                         return (char*)s;
1165
1166                     /* failed; try searching for c2 this time; that way
1167                      * we don't go pathologically slow when the string
1168                      * consists mostly of c1's or vice versa.
1169                      */
1170                     s += 2;
1171                     if (s > bigend)
1172                         break;
1173                     s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1);
1174                     if (!s)
1175                         break;
1176                     if (s[-1] == c1)
1177                         return (char*)s - 1;
1178                 }
1179             }
1180             else {
1181                 /* c1, c2 the same */
1182                 while (s < bigend) {
1183                     if (s[0] == c1) {
1184                       got_1char:
1185                         if (s[1] == c1)
1186                             return (char*)s;
1187                         s += 2;
1188                     }
1189                     else {
1190                         s++;
1191                         s = (unsigned char *)memchr((void*)s, c1, bigend - s);
1192                         if (!s || s >= bigend)
1193                             break;
1194                         goto got_1char;
1195                     }
1196                 }
1197             }
1198
1199             /* failed to find 2 chars; try anchored match at end without
1200              * the \n */
1201             if (tail && bigend[0] == little[0])
1202                 return (char *)bigend;
1203             return NULL;
1204         }
1205
1206     default:
1207         break; /* Only lengths 0 1 and 2 have special-case code.  */
1208     }
1209
1210     if (tail && !multiline) {   /* tail anchored? */
1211         s = bigend - littlelen;
1212         if (s >= big && bigend[-1] == '\n' && *s == *little
1213             /* Automatically of length > 2 */
1214             && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
1215         {
1216             return (char*)s;            /* how sweet it is */
1217         }
1218         if (s[1] == *little
1219             && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
1220         {
1221             return (char*)s + 1;        /* how sweet it is */
1222         }
1223         return NULL;
1224     }
1225
1226     if (!valid) {
1227         /* not compiled; use Perl_ninstr() instead */
1228         char * const b = ninstr((char*)big,(char*)bigend,
1229                          (char*)little, (char*)little + littlelen);
1230
1231         assert(!tail); /* valid => FBM; tail only set on SvVALID SVs */
1232         return b;
1233     }
1234
1235     /* Do actual FBM.  */
1236     if (littlelen > (STRLEN)(bigend - big))
1237         return NULL;
1238
1239     {
1240         const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm);
1241         const unsigned char *oldlittle;
1242
1243         assert(mg);
1244
1245         --littlelen;                    /* Last char found by table lookup */
1246
1247         s = big + littlelen;
1248         little += littlelen;            /* last char */
1249         oldlittle = little;
1250         if (s < bigend) {
1251             const unsigned char * const table = (const unsigned char *) mg->mg_ptr;
1252             const unsigned char lastc = *little;
1253             I32 tmp;
1254
1255           top2:
1256             if ((tmp = table[*s])) {
1257                 /* *s != lastc; earliest position it could match now is
1258                  * tmp slots further on */
1259                 if ((s += tmp) >= bigend)
1260                     goto check_end;
1261                 if (LIKELY(*s != lastc)) {
1262                     s++;
1263                     s = (unsigned char *)memchr((void*)s, lastc, bigend - s);
1264                     if (!s) {
1265                         s = bigend;
1266                         goto check_end;
1267                     }
1268                     goto top2;
1269                 }
1270             }
1271
1272
1273             /* hand-rolled strncmp(): less expensive than calling the
1274              * real function (maybe???) */
1275             {
1276                 unsigned char * const olds = s;
1277
1278                 tmp = littlelen;
1279
1280                 while (tmp--) {
1281                     if (*--s == *--little)
1282                         continue;
1283                     s = olds + 1;       /* here we pay the price for failure */
1284                     little = oldlittle;
1285                     if (s < bigend)     /* fake up continue to outer loop */
1286                         goto top2;
1287                     goto check_end;
1288                 }
1289                 return (char *)s;
1290             }
1291         }
1292       check_end:
1293         if ( s == bigend
1294              && tail
1295              && memEQ((char *)(bigend - littlelen),
1296                       (char *)(oldlittle - littlelen), littlelen) )
1297             return (char*)bigend - littlelen;
1298         return NULL;
1299     }
1300 }
1301
1302 const char *
1303 Perl_cntrl_to_mnemonic(const U8 c)
1304 {
1305     /* Returns the mnemonic string that represents character 'c', if one
1306      * exists; NULL otherwise.  The only ones that exist for the purposes of
1307      * this routine are a few control characters */
1308
1309     switch (c) {
1310         case '\a':       return "\\a";
1311         case '\b':       return "\\b";
1312         case ESC_NATIVE: return "\\e";
1313         case '\f':       return "\\f";
1314         case '\n':       return "\\n";
1315         case '\r':       return "\\r";
1316         case '\t':       return "\\t";
1317     }
1318
1319     return NULL;
1320 }
1321
1322 /*
1323 =for apidoc savesharedpv
1324
1325 A version of C<savepv()> which allocates the duplicate string in memory
1326 which is shared between threads.
1327
1328 =cut
1329 */
1330 char *
1331 Perl_savesharedpv(pTHX_ const char *pv)
1332 {
1333     char *newaddr;
1334     STRLEN pvlen;
1335
1336     PERL_UNUSED_CONTEXT;
1337
1338     if (!pv)
1339         return NULL;
1340
1341     pvlen = strlen(pv)+1;
1342     newaddr = (char*)PerlMemShared_malloc(pvlen);
1343     if (!newaddr) {
1344         croak_no_mem();
1345     }
1346     return (char*)memcpy(newaddr, pv, pvlen);
1347 }
1348
1349 /*
1350 =for apidoc savesharedpvn
1351
1352 A version of C<savepvn()> which allocates the duplicate string in memory
1353 which is shared between threads.  (With the specific difference that a C<NULL>
1354 pointer is not acceptable)
1355
1356 =cut
1357 */
1358 char *
1359 Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len)
1360 {
1361     char *const newaddr = (char*)PerlMemShared_malloc(len + 1);
1362
1363     PERL_UNUSED_CONTEXT;
1364     /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */
1365
1366     if (!newaddr) {
1367         croak_no_mem();
1368     }
1369     newaddr[len] = '\0';
1370     return (char*)memcpy(newaddr, pv, len);
1371 }
1372
1373 /* the SV for Perl_form() and mess() is not kept in an arena */
1374
1375 STATIC SV *
1376 S_mess_alloc(pTHX)
1377 {
1378     SV *sv;
1379     XPVMG *any;
1380
1381     if (PL_phase != PERL_PHASE_DESTRUCT)
1382         return newSVpvs_flags("", SVs_TEMP);
1383
1384     if (PL_mess_sv)
1385         return PL_mess_sv;
1386
1387     /* Create as PVMG now, to avoid any upgrading later */
1388     Newx(sv, 1, SV);
1389     Newxz(any, 1, XPVMG);
1390     SvFLAGS(sv) = SVt_PVMG;
1391     SvANY(sv) = (void*)any;
1392     SvPV_set(sv, NULL);
1393     SvREFCNT(sv) = 1 << 30; /* practically infinite */
1394     PL_mess_sv = sv;
1395     return sv;
1396 }
1397
1398 #if defined(MULTIPLICITY)
1399 char *
1400 Perl_form_nocontext(const char* pat, ...)
1401 {
1402     dTHX;
1403     char *retval;
1404     va_list args;
1405     PERL_ARGS_ASSERT_FORM_NOCONTEXT;
1406     va_start(args, pat);
1407     retval = vform(pat, &args);
1408     va_end(args);
1409     return retval;
1410 }
1411 #endif /* MULTIPLICITY */
1412
1413 /*
1414 =for apidoc_section $display
1415 =for apidoc form
1416 =for apidoc_item form_nocontext
1417
1418 These take a sprintf-style format pattern and conventional
1419 (non-SV) arguments and return the formatted string.
1420
1421     (char *) Perl_form(pTHX_ const char* pat, ...)
1422
1423 can be used any place a string (char *) is required:
1424
1425     char * s = Perl_form("%d.%d",major,minor);
1426
1427 They use a single (per-thread) private buffer so if you want to format several
1428 strings you must explicitly copy the earlier strings away (and free the copies
1429 when you are done).
1430
1431 The two forms differ only in that C<form_nocontext> does not take a thread
1432 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1433 already have the thread context.
1434
1435 =for apidoc vform
1436 Like C<L</form>> but but the arguments are an encapsulated argument list.
1437
1438 =cut
1439 */
1440
1441 char *
1442 Perl_form(pTHX_ const char* pat, ...)
1443 {
1444     char *retval;
1445     va_list args;
1446     PERL_ARGS_ASSERT_FORM;
1447     va_start(args, pat);
1448     retval = vform(pat, &args);
1449     va_end(args);
1450     return retval;
1451 }
1452
1453 char *
1454 Perl_vform(pTHX_ const char *pat, va_list *args)
1455 {
1456     SV * const sv = mess_alloc();
1457     PERL_ARGS_ASSERT_VFORM;
1458     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1459     return SvPVX(sv);
1460 }
1461
1462 /*
1463 =for apidoc mess
1464 =for apidoc_item mess_nocontext
1465
1466 These take a sprintf-style format pattern and argument list, which are used to
1467 generate a string message.  If the message does not end with a newline, then it
1468 will be extended with some indication of the current location in the code, as
1469 described for C<L</mess_sv>>.
1470
1471 Normally, the resulting message is returned in a new mortal SV.
1472 But during global destruction a single SV may be shared between uses of
1473 this function.
1474
1475 The two forms differ only in that C<mess_nocontext> does not take a thread
1476 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1477 already have the thread context.
1478
1479 =cut
1480 */
1481
1482 #if defined(MULTIPLICITY)
1483 SV *
1484 Perl_mess_nocontext(const char *pat, ...)
1485 {
1486     dTHX;
1487     SV *retval;
1488     va_list args;
1489     PERL_ARGS_ASSERT_MESS_NOCONTEXT;
1490     va_start(args, pat);
1491     retval = vmess(pat, &args);
1492     va_end(args);
1493     return retval;
1494 }
1495 #endif /* MULTIPLICITY */
1496
1497 SV *
1498 Perl_mess(pTHX_ const char *pat, ...)
1499 {
1500     SV *retval;
1501     va_list args;
1502     PERL_ARGS_ASSERT_MESS;
1503     va_start(args, pat);
1504     retval = vmess(pat, &args);
1505     va_end(args);
1506     return retval;
1507 }
1508
1509 const COP*
1510 Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop,
1511                        bool opnext)
1512 {
1513     /* Look for curop starting from o.  cop is the last COP we've seen. */
1514     /* opnext means that curop is actually the ->op_next of the op we are
1515        seeking. */
1516
1517     PERL_ARGS_ASSERT_CLOSEST_COP;
1518
1519     if (!o || !curop || (
1520         opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop
1521     ))
1522         return cop;
1523
1524     if (o->op_flags & OPf_KIDS) {
1525         const OP *kid;
1526         for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
1527             const COP *new_cop;
1528
1529             /* If the OP_NEXTSTATE has been optimised away we can still use it
1530              * the get the file and line number. */
1531
1532             if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
1533                 cop = (const COP *)kid;
1534
1535             /* Keep searching, and return when we've found something. */
1536
1537             new_cop = closest_cop(cop, kid, curop, opnext);
1538             if (new_cop)
1539                 return new_cop;
1540         }
1541     }
1542
1543     /* Nothing found. */
1544
1545     return NULL;
1546 }
1547
1548 /*
1549 =for apidoc mess_sv
1550
1551 Expands a message, intended for the user, to include an indication of
1552 the current location in the code, if the message does not already appear
1553 to be complete.
1554
1555 C<basemsg> is the initial message or object.  If it is a reference, it
1556 will be used as-is and will be the result of this function.  Otherwise it
1557 is used as a string, and if it already ends with a newline, it is taken
1558 to be complete, and the result of this function will be the same string.
1559 If the message does not end with a newline, then a segment such as C<at
1560 foo.pl line 37> will be appended, and possibly other clauses indicating
1561 the current state of execution.  The resulting message will end with a
1562 dot and a newline.
1563
1564 Normally, the resulting message is returned in a new mortal SV.
1565 During global destruction a single SV may be shared between uses of this
1566 function.  If C<consume> is true, then the function is permitted (but not
1567 required) to modify and return C<basemsg> instead of allocating a new SV.
1568
1569 =cut
1570 */
1571
1572 SV *
1573 Perl_mess_sv(pTHX_ SV *basemsg, bool consume)
1574 {
1575     SV *sv;
1576
1577 #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR)
1578     {
1579         char *ws;
1580         UV wi;
1581         /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */
1582         if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR"))
1583             && grok_atoUV(ws, &wi, NULL)
1584             && wi <= PERL_INT_MAX
1585         ) {
1586             Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1);
1587         }
1588     }
1589 #endif
1590
1591     PERL_ARGS_ASSERT_MESS_SV;
1592
1593     if (SvROK(basemsg)) {
1594         if (consume) {
1595             sv = basemsg;
1596         }
1597         else {
1598             sv = mess_alloc();
1599             sv_setsv(sv, basemsg);
1600         }
1601         return sv;
1602     }
1603
1604     if (SvPOK(basemsg) && consume) {
1605         sv = basemsg;
1606     }
1607     else {
1608         sv = mess_alloc();
1609         sv_copypv(sv, basemsg);
1610     }
1611
1612     if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
1613         /*
1614          * Try and find the file and line for PL_op.  This will usually be
1615          * PL_curcop, but it might be a cop that has been optimised away.  We
1616          * can try to find such a cop by searching through the optree starting
1617          * from the sibling of PL_curcop.
1618          */
1619
1620         if (PL_curcop) {
1621             const COP *cop =
1622                 closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE);
1623             if (!cop)
1624                 cop = PL_curcop;
1625
1626             if (CopLINE(cop))
1627                 Perl_sv_catpvf(aTHX_ sv, " at %s line %" LINE_Tf,
1628                                 OutCopFILE(cop), CopLINE(cop));
1629         }
1630
1631         /* Seems that GvIO() can be untrustworthy during global destruction. */
1632         if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO)
1633                 && IoLINES(GvIOp(PL_last_in_gv)))
1634         {
1635             STRLEN l;
1636             const bool line_mode = (RsSIMPLE(PL_rs) &&
1637                                    *SvPV_const(PL_rs,l) == '\n' && l == 1);
1638             Perl_sv_catpvf(aTHX_ sv, ", <%" SVf "> %s %" IVdf,
1639                            SVfARG(PL_last_in_gv == PL_argvgv
1640                                  ? &PL_sv_no
1641                                  : newSVhek_mortal(GvNAME_HEK(PL_last_in_gv))),
1642                            line_mode ? "line" : "chunk",
1643                            (IV)IoLINES(GvIOp(PL_last_in_gv)));
1644         }
1645         if (PL_phase == PERL_PHASE_DESTRUCT)
1646             sv_catpvs(sv, " during global destruction");
1647         sv_catpvs(sv, ".\n");
1648     }
1649     return sv;
1650 }
1651
1652 /*
1653 =for apidoc vmess
1654
1655 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1656 argument list, respectively.  These are used to generate a string message.  If
1657 the
1658 message does not end with a newline, then it will be extended with
1659 some indication of the current location in the code, as described for
1660 L</mess_sv>.
1661
1662 Normally, the resulting message is returned in a new mortal SV.
1663 During global destruction a single SV may be shared between uses of
1664 this function.
1665
1666 =cut
1667 */
1668
1669 SV *
1670 Perl_vmess(pTHX_ const char *pat, va_list *args)
1671 {
1672     SV * const sv = mess_alloc();
1673
1674     PERL_ARGS_ASSERT_VMESS;
1675
1676     sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
1677     return mess_sv(sv, 1);
1678 }
1679
1680 void
1681 Perl_write_to_stderr(pTHX_ SV* msv)
1682 {
1683     IO *io;
1684     MAGIC *mg;
1685
1686     PERL_ARGS_ASSERT_WRITE_TO_STDERR;
1687
1688     if (PL_stderrgv && SvREFCNT(PL_stderrgv) 
1689         && (io = GvIO(PL_stderrgv))
1690         && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) 
1691         Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT),
1692                             G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv);
1693     else {
1694         PerlIO * const serr = Perl_error_log;
1695
1696         do_print(msv, serr);
1697         (void)PerlIO_flush(serr);
1698     }
1699 }
1700
1701 /*
1702 =for apidoc_section $warning
1703 */
1704
1705 /* Common code used in dieing and warning */
1706
1707 STATIC SV *
1708 S_with_queued_errors(pTHX_ SV *ex)
1709 {
1710     PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS;
1711     if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) {
1712         sv_catsv(PL_errors, ex);
1713         ex = sv_mortalcopy(PL_errors);
1714         SvCUR_set(PL_errors, 0);
1715     }
1716     return ex;
1717 }
1718
1719 STATIC bool
1720 S_invoke_exception_hook(pTHX_ SV *ex, bool warn)
1721 {
1722     HV *stash;
1723     GV *gv;
1724     CV *cv;
1725     SV **const hook = warn ? &PL_warnhook : &PL_diehook;
1726     /* sv_2cv might call Perl_croak() or Perl_warner() */
1727     SV * const oldhook = *hook;
1728
1729     if (!oldhook || oldhook == PERL_WARNHOOK_FATAL)
1730         return FALSE;
1731
1732     ENTER;
1733     SAVESPTR(*hook);
1734     *hook = NULL;
1735     cv = sv_2cv(oldhook, &stash, &gv, 0);
1736     LEAVE;
1737     if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1738         dSP;
1739         SV *exarg;
1740
1741         ENTER;
1742         save_re_context();
1743         if (warn) {
1744             SAVESPTR(*hook);
1745             *hook = NULL;
1746         }
1747         exarg = newSVsv(ex);
1748         SvREADONLY_on(exarg);
1749         SAVEFREESV(exarg);
1750
1751         PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK);
1752         PUSHMARK(SP);
1753         XPUSHs(exarg);
1754         PUTBACK;
1755         call_sv(MUTABLE_SV(cv), G_DISCARD);
1756         POPSTACK;
1757         LEAVE;
1758         return TRUE;
1759     }
1760     return FALSE;
1761 }
1762
1763 /*
1764 =for apidoc die_sv
1765
1766 This behaves the same as L</croak_sv>, except for the return type.
1767 It should be used only where the C<OP *> return type is required.
1768 The function never actually returns.
1769
1770 =cut
1771 */
1772
1773 /* silence __declspec(noreturn) warnings */
1774 MSVC_DIAG_IGNORE(4646 4645)
1775 OP *
1776 Perl_die_sv(pTHX_ SV *baseex)
1777 {
1778     PERL_ARGS_ASSERT_DIE_SV;
1779     croak_sv(baseex);
1780     /* NOTREACHED */
1781     NORETURN_FUNCTION_END;
1782 }
1783 MSVC_DIAG_RESTORE
1784
1785 /*
1786 =for apidoc      die
1787 =for apidoc_item die_nocontext
1788
1789 These behave the same as L</croak>, except for the return type.
1790 They should be used only where the C<OP *> return type is required.
1791 They never actually return.
1792
1793 The two forms differ only in that C<die_nocontext> does not take a thread
1794 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
1795 already have the thread context.
1796
1797 =cut
1798 */
1799
1800 #if defined(MULTIPLICITY)
1801
1802 /* silence __declspec(noreturn) warnings */
1803 MSVC_DIAG_IGNORE(4646 4645)
1804 OP *
1805 Perl_die_nocontext(const char* pat, ...)
1806 {
1807     dTHX;
1808     va_list args;
1809     va_start(args, pat);
1810     vcroak(pat, &args);
1811     NOT_REACHED; /* NOTREACHED */
1812     va_end(args);
1813     NORETURN_FUNCTION_END;
1814 }
1815 MSVC_DIAG_RESTORE
1816
1817 #endif /* MULTIPLICITY */
1818
1819 /* silence __declspec(noreturn) warnings */
1820 MSVC_DIAG_IGNORE(4646 4645)
1821 OP *
1822 Perl_die(pTHX_ const char* pat, ...)
1823 {
1824     va_list args;
1825     va_start(args, pat);
1826     vcroak(pat, &args);
1827     NOT_REACHED; /* NOTREACHED */
1828     va_end(args);
1829     NORETURN_FUNCTION_END;
1830 }
1831 MSVC_DIAG_RESTORE
1832
1833 /*
1834 =for apidoc croak_sv
1835
1836 This is an XS interface to Perl's C<die> function.
1837
1838 C<baseex> is the error message or object.  If it is a reference, it
1839 will be used as-is.  Otherwise it is used as a string, and if it does
1840 not end with a newline then it will be extended with some indication of
1841 the current location in the code, as described for L</mess_sv>.
1842
1843 The error message or object will be used as an exception, by default
1844 returning control to the nearest enclosing C<eval>, but subject to
1845 modification by a C<$SIG{__DIE__}> handler.  In any case, the C<croak_sv>
1846 function never returns normally.
1847
1848 To die with a simple string message, the L</croak> function may be
1849 more convenient.
1850
1851 =cut
1852 */
1853
1854 void
1855 Perl_croak_sv(pTHX_ SV *baseex)
1856 {
1857     SV *ex = with_queued_errors(mess_sv(baseex, 0));
1858     PERL_ARGS_ASSERT_CROAK_SV;
1859     invoke_exception_hook(ex, FALSE);
1860     die_unwind(ex);
1861 }
1862
1863 /*
1864 =for apidoc vcroak
1865
1866 This is an XS interface to Perl's C<die> function.
1867
1868 C<pat> and C<args> are a sprintf-style format pattern and encapsulated
1869 argument list.  These are used to generate a string message.  If the
1870 message does not end with a newline, then it will be extended with
1871 some indication of the current location in the code, as described for
1872 L</mess_sv>.
1873
1874 The error message will be used as an exception, by default
1875 returning control to the nearest enclosing C<eval>, but subject to
1876 modification by a C<$SIG{__DIE__}> handler.  In any case, the C<croak>
1877 function never returns normally.
1878
1879 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1880 (C<$@>) will be used as an error message or object instead of building an
1881 error message from arguments.  If you want to throw a non-string object,
1882 or build an error message in an SV yourself, it is preferable to use
1883 the L</croak_sv> function, which does not involve clobbering C<ERRSV>.
1884
1885 =cut
1886 */
1887
1888 void
1889 Perl_vcroak(pTHX_ const char* pat, va_list *args)
1890 {
1891     SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0));
1892     invoke_exception_hook(ex, FALSE);
1893     die_unwind(ex);
1894 }
1895
1896 /*
1897 =for apidoc croak
1898 =for apidoc_item croak_nocontext
1899
1900 These are XS interfaces to Perl's C<die> function.
1901
1902 They take a sprintf-style format pattern and argument list, which are used to
1903 generate a string message.  If the message does not end with a newline, then it
1904 will be extended with some indication of the current location in the code, as
1905 described for C<L</mess_sv>>.
1906
1907 The error message will be used as an exception, by default
1908 returning control to the nearest enclosing C<eval>, but subject to
1909 modification by a C<$SIG{__DIE__}> handler.  In any case, these croak
1910 functions never return normally.
1911
1912 For historical reasons, if C<pat> is null then the contents of C<ERRSV>
1913 (C<$@>) will be used as an error message or object instead of building an
1914 error message from arguments.  If you want to throw a non-string object,
1915 or build an error message in an SV yourself, it is preferable to use
1916 the C<L</croak_sv>> function, which does not involve clobbering C<ERRSV>.
1917
1918 The two forms differ only in that C<croak_nocontext> does not take a thread
1919 context (C<aTHX>) parameter.  It is usually preferred as it takes up fewer
1920 bytes of code than plain C<Perl_croak>, and time is rarely a critical resource
1921 when you are about to throw an exception.
1922
1923 =cut
1924 */
1925
1926 #if defined(MULTIPLICITY)
1927 void
1928 Perl_croak_nocontext(const char *pat, ...)
1929 {
1930     dTHX;
1931     va_list args;
1932     va_start(args, pat);
1933     vcroak(pat, &args);
1934     NOT_REACHED; /* NOTREACHED */
1935     va_end(args);
1936 }
1937 #endif /* MULTIPLICITY */
1938
1939 void
1940 Perl_croak(pTHX_ const char *pat, ...)
1941 {
1942     va_list args;
1943     va_start(args, pat);
1944     vcroak(pat, &args);
1945     NOT_REACHED; /* NOTREACHED */
1946     va_end(args);
1947 }
1948
1949 /*
1950 =for apidoc croak_no_modify
1951
1952 This encapsulates a common reason for dying, generating terser object code than
1953 using the generic C<Perl_croak>.  It is exactly equivalent to
1954 C<Perl_croak(aTHX_ "%s", PL_no_modify)> (which expands to something like
1955 "Modification of a read-only value attempted").
1956
1957 Less code used on exception code paths reduces CPU cache pressure.
1958
1959 =cut
1960 */
1961
1962 void
1963 Perl_croak_no_modify(void)
1964 {
1965     Perl_croak_nocontext( "%s", PL_no_modify);
1966 }
1967
1968 /* does not return, used in util.c perlio.c and win32.c
1969    This is typically called when malloc returns NULL.
1970 */
1971 void
1972 Perl_croak_no_mem(void)
1973 {
1974     dTHX;
1975
1976     int fd = PerlIO_fileno(Perl_error_log);
1977     if (fd < 0)
1978         SETERRNO(EBADF,RMS_IFI);
1979     else {
1980         /* Can't use PerlIO to write as it allocates memory */
1981         PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1));
1982     }
1983     my_exit(1);
1984 }
1985
1986 /* does not return, used only in POPSTACK */
1987 void
1988 Perl_croak_popstack(void)
1989 {
1990     dTHX;
1991     PerlIO_printf(Perl_error_log, "panic: POPSTACK\n");
1992     my_exit(1);
1993 }
1994
1995 /*
1996 =for apidoc warn_sv
1997
1998 This is an XS interface to Perl's C<warn> function.
1999
2000 C<baseex> is the error message or object.  If it is a reference, it
2001 will be used as-is.  Otherwise it is used as a string, and if it does
2002 not end with a newline then it will be extended with some indication of
2003 the current location in the code, as described for L</mess_sv>.
2004
2005 The error message or object will by default be written to standard error,
2006 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2007
2008 To warn with a simple string message, the L</warn> function may be
2009 more convenient.
2010
2011 =cut
2012 */
2013
2014 void
2015 Perl_warn_sv(pTHX_ SV *baseex)
2016 {
2017     SV *ex = mess_sv(baseex, 0);
2018     PERL_ARGS_ASSERT_WARN_SV;
2019     if (!invoke_exception_hook(ex, TRUE))
2020         write_to_stderr(ex);
2021 }
2022
2023 /*
2024 =for apidoc vwarn
2025
2026 This is an XS interface to Perl's C<warn> function.
2027
2028 This is like C<L</warn>>, but C<args> are an encapsulated
2029 argument list.
2030
2031 Unlike with L</vcroak>, C<pat> is not permitted to be null.
2032
2033 =cut
2034 */
2035
2036 void
2037 Perl_vwarn(pTHX_ const char* pat, va_list *args)
2038 {
2039     SV *ex = vmess(pat, args);
2040     PERL_ARGS_ASSERT_VWARN;
2041     if (!invoke_exception_hook(ex, TRUE))
2042         write_to_stderr(ex);
2043 }
2044
2045 /*
2046 =for apidoc warn
2047 =for apidoc_item warn_nocontext
2048
2049 These are XS interfaces to Perl's C<warn> function.
2050
2051 They take a sprintf-style format pattern and argument list, which  are used to
2052 generate a string message.  If the message does not end with a newline, then it
2053 will be extended with some indication of the current location in the code, as
2054 described for C<L</mess_sv>>.
2055
2056 The error message or object will by default be written to standard error,
2057 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2058
2059 Unlike with C<L</croak>>, C<pat> is not permitted to be null.
2060
2061 The two forms differ only in that C<warn_nocontext> does not take a thread
2062 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2063 already have the thread context.
2064
2065 =cut
2066 */
2067
2068 #if defined(MULTIPLICITY)
2069 void
2070 Perl_warn_nocontext(const char *pat, ...)
2071 {
2072     dTHX;
2073     va_list args;
2074     PERL_ARGS_ASSERT_WARN_NOCONTEXT;
2075     va_start(args, pat);
2076     vwarn(pat, &args);
2077     va_end(args);
2078 }
2079 #endif /* MULTIPLICITY */
2080
2081 void
2082 Perl_warn(pTHX_ const char *pat, ...)
2083 {
2084     va_list args;
2085     PERL_ARGS_ASSERT_WARN;
2086     va_start(args, pat);
2087     vwarn(pat, &args);
2088     va_end(args);
2089 }
2090
2091 /*
2092 =for apidoc warner
2093 =for apidoc_item warner_nocontext
2094
2095 These output a warning of the specified category (or categories) given by
2096 C<err>, using the sprintf-style format pattern C<pat>, and argument list.
2097
2098 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2099 C<packWARN4> macros populated with the appropriate number of warning
2100 categories.  If any of the warning categories they specify is fatal, a fatal
2101 exception is thrown.
2102
2103 In any event a message is generated by the pattern and arguments.  If the
2104 message does not end with a newline, then it will be extended with some
2105 indication of the current location in the code, as described for L</mess_sv>.
2106
2107 The error message or object will by default be written to standard error,
2108 but this is subject to modification by a C<$SIG{__WARN__}> handler.
2109
2110 C<pat> is not permitted to be null.
2111
2112 The two forms differ only in that C<warner_nocontext> does not take a thread
2113 context (C<aTHX>) parameter, so is used in situations where the caller doesn't
2114 already have the thread context.
2115
2116 These functions differ from the similarly named C<L</warn>> functions, in that
2117 the latter are for XS code to unconditionally display a warning, whereas these
2118 are for code that may be compiling a perl program, and does extra checking to
2119 see if the warning should be fatal.
2120
2121 =for apidoc ck_warner
2122 =for apidoc_item ck_warner_d
2123 If none of the warning categories given by C<err> are enabled, do nothing;
2124 otherwise call C<L</warner>>  or C<L</warner_nocontext>> with the passed-in
2125 parameters;.
2126
2127 C<err> must be one of the C<L</packWARN>>, C<packWARN2>, C<packWARN3>,
2128 C<packWARN4> macros populated with the appropriate number of warning
2129 categories.
2130
2131 The two forms differ only in that C<ck_warner_d> should be used if warnings for
2132 any of the categories are by default enabled.
2133
2134 =for apidoc vwarner
2135 This is like C<L</warner>>, but C<args> are an encapsulated argument list.
2136
2137 =cut
2138 */
2139
2140 #if defined(MULTIPLICITY)
2141 void
2142 Perl_warner_nocontext(U32 err, const char *pat, ...)
2143 {
2144     dTHX; 
2145     va_list args;
2146     PERL_ARGS_ASSERT_WARNER_NOCONTEXT;
2147     va_start(args, pat);
2148     vwarner(err, pat, &args);
2149     va_end(args);
2150 }
2151 #endif /* MULTIPLICITY */
2152
2153 void
2154 Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...)
2155 {
2156     PERL_ARGS_ASSERT_CK_WARNER_D;
2157
2158     if (Perl_ckwarn_d(aTHX_ err)) {
2159         va_list args;
2160         va_start(args, pat);
2161         vwarner(err, pat, &args);
2162         va_end(args);
2163     }
2164 }
2165
2166 void
2167 Perl_ck_warner(pTHX_ U32 err, const char* pat, ...)
2168 {
2169     PERL_ARGS_ASSERT_CK_WARNER;
2170
2171     if (Perl_ckwarn(aTHX_ err)) {
2172         va_list args;
2173         va_start(args, pat);
2174         vwarner(err, pat, &args);
2175         va_end(args);
2176     }
2177 }
2178
2179 void
2180 Perl_warner(pTHX_ U32  err, const char* pat,...)
2181 {
2182     va_list args;
2183     PERL_ARGS_ASSERT_WARNER;
2184     va_start(args, pat);
2185     vwarner(err, pat, &args);
2186     va_end(args);
2187 }
2188
2189 void
2190 Perl_vwarner(pTHX_ U32  err, const char* pat, va_list* args)
2191 {
2192     PERL_ARGS_ASSERT_VWARNER;
2193     if (
2194         (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) &&
2195         !(PL_in_eval & EVAL_KEEPERR)
2196     ) {
2197         SV * const msv = vmess(pat, args);
2198
2199         if (PL_parser && PL_parser->error_count) {
2200             qerror(msv);
2201         }
2202         else {
2203             invoke_exception_hook(msv, FALSE);
2204             die_unwind(msv);
2205         }
2206     }
2207     else {
2208         Perl_vwarn(aTHX_ pat, args);
2209     }
2210 }
2211
2212 /* implements the ckWARN? macros */
2213
2214 bool
2215 Perl_ckwarn(pTHX_ U32 w)
2216 {
2217     /* If lexical warnings have not been set, use $^W.  */
2218     if (isLEXWARN_off)
2219         return PL_dowarn & G_WARN_ON;
2220
2221     return ckwarn_common(w);
2222 }
2223
2224 /* implements the ckWARN?_d macro */
2225
2226 bool
2227 Perl_ckwarn_d(pTHX_ U32 w)
2228 {
2229     /* If lexical warnings have not been set then default classes warn.  */
2230     if (isLEXWARN_off)
2231         return TRUE;
2232
2233     return ckwarn_common(w);
2234 }
2235
2236 static bool
2237 S_ckwarn_common(pTHX_ U32 w)
2238 {
2239     if (PL_curcop->cop_warnings == pWARN_ALL)
2240         return TRUE;
2241
2242     if (PL_curcop->cop_warnings == pWARN_NONE)
2243         return FALSE;
2244
2245     /* Check the assumption that at least the first slot is non-zero.  */
2246     assert(unpackWARN1(w));
2247
2248     /* Check the assumption that it is valid to stop as soon as a zero slot is
2249        seen.  */
2250     if (!unpackWARN2(w)) {
2251         assert(!unpackWARN3(w));
2252         assert(!unpackWARN4(w));
2253     } else if (!unpackWARN3(w)) {
2254         assert(!unpackWARN4(w));
2255     }
2256         
2257     /* Right, dealt with all the special cases, which are implemented as non-
2258        pointers, so there is a pointer to a real warnings mask.  */
2259     do {
2260         if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w)))
2261             return TRUE;
2262     } while (w >>= WARNshift);
2263
2264     return FALSE;
2265 }
2266
2267 char *
2268 Perl_new_warnings_bitfield(pTHX_ char *buffer, const char *const bits,
2269                            STRLEN size) {
2270     const MEM_SIZE len_wanted = (size > WARNsize ? size : WARNsize);
2271     PERL_UNUSED_CONTEXT;
2272     PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD;
2273
2274     /* pass in null as the source string as we will do the
2275      * copy ourselves. */
2276     buffer = rcpv_new(NULL, len_wanted, RCPVf_NO_COPY);
2277     Copy(bits, buffer, size, char);
2278     if (size < WARNsize)
2279         Zero(buffer + size, WARNsize - size, char);
2280     return buffer;
2281 }
2282
2283 /* since we've already done strlen() for both nam and val
2284  * we can use that info to make things faster than
2285  * sprintf(s, "%s=%s", nam, val)
2286  */
2287 #define my_setenv_format(s, nam, nlen, val, vlen) \
2288    Copy(nam, s, nlen, char); \
2289    *(s+nlen) = '='; \
2290    Copy(val, s+(nlen+1), vlen, char); \
2291    *(s+(nlen+1+vlen)) = '\0'
2292
2293
2294
2295 #if defined(USE_ENVIRON_ARRAY) || defined(WIN32)
2296 /* NB: VMS' my_setenv() is in vms.c */
2297
2298 /* small wrapper for use by Perl_my_setenv that mallocs, or reallocs if
2299  * 'current' is non-null, with up to three sizes that are added together.
2300  * It handles integer overflow.
2301  */
2302 #  ifndef HAS_SETENV
2303 static char *
2304 S_env_alloc(void *current, Size_t l1, Size_t l2, Size_t l3, Size_t size)
2305 {
2306     void *p;
2307     Size_t sl, l = l1 + l2;
2308
2309     if (l < l2)
2310         goto panic;
2311     l += l3;
2312     if (l < l3)
2313         goto panic;
2314     sl = l * size;
2315     if (sl < l)
2316         goto panic;
2317
2318     p = current
2319             ? safesysrealloc(current, sl)
2320             : safesysmalloc(sl);
2321     if (p)
2322         return (char*)p;
2323
2324   panic:
2325     croak_memory_wrap();
2326 }
2327 #  endif
2328
2329 /*
2330 =for apidoc_section $utility
2331 =for apidoc my_setenv
2332
2333 A wrapper for the C library L<setenv(3)>.  Don't use the latter, as the perl
2334 version has desirable safeguards
2335
2336 =cut
2337 */
2338
2339 void
2340 Perl_my_setenv(pTHX_ const char *nam, const char *val)
2341 {
2342 #  if defined(USE_ITHREADS) && !defined(WIN32)
2343     /* only parent thread can modify process environment, so no need to use a
2344      * mutex */
2345     if (PL_curinterp != aTHX)
2346         return;
2347 #  endif
2348
2349 #  if defined(HAS_SETENV) && defined(HAS_UNSETENV)
2350         if (val == NULL) {
2351             unsetenv(nam);
2352         } else {
2353             setenv(nam, val, 1);
2354         }
2355
2356 #  elif defined(HAS_UNSETENV)
2357
2358         if (val == NULL) {
2359             if (environ) /* old glibc can crash with null environ */
2360                 unsetenv(nam);
2361         } else {
2362             const Size_t nlen = strlen(nam);
2363             const Size_t vlen = strlen(val);
2364             char * const new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2365             my_setenv_format(new_env, nam, nlen, val, vlen);
2366             putenv(new_env);
2367         }
2368
2369 #  else /* ! HAS_UNSETENV */
2370
2371         const Size_t nlen = strlen(nam);
2372         if (!val) {
2373            val = "";
2374         }
2375         Size_t vlen = strlen(val);
2376         char *new_env = S_env_alloc(NULL, nlen, vlen, 2, 1);
2377         /* all that work just for this */
2378         my_setenv_format(new_env, nam, nlen, val, vlen);
2379 #    ifndef WIN32
2380         putenv(new_env);
2381 #    else
2382         PerlEnv_putenv(new_env);
2383         safesysfree(new_env);
2384 #    endif
2385
2386 #  endif /* HAS_SETENV */
2387 }
2388
2389 #endif /* USE_ENVIRON_ARRAY || WIN32 */
2390
2391 #ifdef UNLINK_ALL_VERSIONS
2392 I32
2393 Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */
2394 {
2395     I32 retries = 0;
2396
2397     PERL_ARGS_ASSERT_UNLNK;
2398
2399     while (PerlLIO_unlink(f) >= 0)
2400         retries++;
2401     return retries ? 0 : -1;
2402 }
2403 #endif
2404
2405 #if defined(OEMVS)
2406   #if (__CHARSET_LIB == 1)
2407   static int chgfdccsid(int fd, unsigned short ccsid) 
2408   {
2409     attrib_t attr;
2410     memset(&attr, 0, sizeof(attr));
2411     attr.att_filetagchg = 1;
2412     attr.att_filetag.ft_ccsid = ccsid;
2413     if (ccsid != FT_BINARY) {
2414       attr.att_filetag.ft_txtflag = 1;
2415     }
2416     return __fchattr(fd, &attr, sizeof(attr));
2417   }
2418   #endif
2419 #endif
2420
2421 /*
2422 =for apidoc my_popen_list
2423
2424 Implementing function on some systems for PerlProc_popen_list()
2425
2426 =cut
2427 */
2428
2429 PerlIO *
2430 Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
2431 {
2432 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2433     int p[2];
2434     I32 This, that;
2435     Pid_t pid;
2436     SV *sv;
2437     I32 did_pipes = 0;
2438     int pp[2];
2439
2440     PERL_ARGS_ASSERT_MY_POPEN_LIST;
2441
2442     PERL_FLUSHALL_FOR_CHILD;
2443     This = (*mode == 'w');
2444     that = !This;
2445     if (TAINTING_get) {
2446         taint_env();
2447         taint_proper("Insecure %s%s", "EXEC");
2448     }
2449     if (PerlProc_pipe_cloexec(p) < 0)
2450         return NULL;
2451     /* Try for another pipe pair for error return */
2452     if (PerlProc_pipe_cloexec(pp) >= 0)
2453         did_pipes = 1;
2454     while ((pid = PerlProc_fork()) < 0) {
2455         if (errno != EAGAIN) {
2456             PerlLIO_close(p[This]);
2457             PerlLIO_close(p[that]);
2458             if (did_pipes) {
2459                 PerlLIO_close(pp[0]);
2460                 PerlLIO_close(pp[1]);
2461             }
2462             return NULL;
2463         }
2464         Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2465         sleep(5);
2466     }
2467     if (pid == 0) {
2468         /* Child */
2469 #undef THIS
2470 #undef THAT
2471 #define THIS that
2472 #define THAT This
2473         /* Close parent's end of error status pipe (if any) */
2474         if (did_pipes)
2475             PerlLIO_close(pp[0]);
2476 #if defined(OEMVS)
2477   #if (__CHARSET_LIB == 1)
2478         chgfdccsid(p[THIS], 819);
2479         chgfdccsid(p[THAT], 819);
2480   #endif
2481 #endif
2482         /* Now dup our end of _the_ pipe to right position */
2483         if (p[THIS] != (*mode == 'r')) {
2484             PerlLIO_dup2(p[THIS], *mode == 'r');
2485             PerlLIO_close(p[THIS]);
2486             if (p[THAT] != (*mode == 'r'))      /* if dup2() didn't close it */
2487                 PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */
2488         }
2489         else {
2490             setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2491             PerlLIO_close(p[THAT]);     /* close parent's end of _the_ pipe */
2492         }
2493 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2494         /* No automatic close - do it by hand */
2495 #  ifndef NOFILE
2496 #  define NOFILE 20
2497 #  endif
2498         {
2499             int fd;
2500
2501             for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) {
2502                 if (fd != pp[1])
2503                     PerlLIO_close(fd);
2504             }
2505         }
2506 #endif
2507         do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes);
2508         PerlProc__exit(1);
2509 #undef THIS
2510 #undef THAT
2511     }
2512     /* Parent */
2513     if (did_pipes)
2514         PerlLIO_close(pp[1]);
2515     /* Keep the lower of the two fd numbers */
2516     if (p[that] < p[This]) {
2517         PerlLIO_dup2_cloexec(p[This], p[that]);
2518         PerlLIO_close(p[This]);
2519         p[This] = p[that];
2520     }
2521     else
2522         PerlLIO_close(p[that]);         /* close child's end of pipe */
2523
2524     sv = *av_fetch(PL_fdpid,p[This],TRUE);
2525     SvUPGRADE(sv,SVt_IV);
2526     SvIV_set(sv, pid);
2527     PL_forkprocess = pid;
2528     /* If we managed to get status pipe check for exec fail */
2529     if (did_pipes && pid > 0) {
2530         int errkid;
2531         unsigned read_total = 0;
2532
2533         while (read_total < sizeof(int)) {
2534             const SSize_t n1 = PerlLIO_read(pp[0],
2535                               (void*)(((char*)&errkid)+read_total),
2536                               (sizeof(int)) - read_total);
2537             if (n1 <= 0)
2538                 break;
2539             read_total += n1;
2540         }
2541         PerlLIO_close(pp[0]);
2542         did_pipes = 0;
2543         if (read_total) {                       /* Error */
2544             int pid2, status;
2545             PerlLIO_close(p[This]);
2546             if (read_total != sizeof(int))
2547                 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", read_total);
2548             do {
2549                 pid2 = wait4pid(pid, &status, 0);
2550             } while (pid2 == -1 && errno == EINTR);
2551             errno = errkid;             /* Propagate errno from kid */
2552             return NULL;
2553         }
2554     }
2555     if (did_pipes)
2556          PerlLIO_close(pp[0]);
2557 #if defined(OEMVS)
2558   #if (__CHARSET_LIB == 1)
2559     PerlIO* io = PerlIO_fdopen(p[This], mode);
2560     if (io) {
2561       chgfdccsid(p[This], 819);
2562     }
2563     return io;
2564   #else
2565     return PerlIO_fdopen(p[This], mode);
2566   #endif
2567 #else
2568     return PerlIO_fdopen(p[This], mode);
2569 #endif
2570
2571 #else
2572 #  if defined(OS2)      /* Same, without fork()ing and all extra overhead... */
2573     return my_syspopen4(aTHX_ NULL, mode, n, args);
2574 #  elif defined(WIN32)
2575     return win32_popenlist(mode, n, args);
2576 #  else
2577     Perl_croak(aTHX_ "List form of piped open not implemented");
2578     return (PerlIO *) NULL;
2579 #  endif
2580 #endif
2581 }
2582
2583     /* VMS' my_popen() is in VMS.c, same with OS/2 and AmigaOS 4. */
2584 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
2585
2586 /*
2587 =for apidoc_section $io
2588 =for apidoc my_popen
2589
2590 A wrapper for the C library L<popen(3)>.  Don't use the latter, as the Perl
2591 version knows things that interact with the rest of the perl interpreter.
2592
2593 =cut
2594 */
2595
2596 PerlIO *
2597 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2598 {
2599     int p[2];
2600     I32 This, that;
2601     Pid_t pid;
2602     SV *sv;
2603     const I32 doexec = !(*cmd == '-' && cmd[1] == '\0');
2604     I32 did_pipes = 0;
2605     int pp[2];
2606
2607     PERL_ARGS_ASSERT_MY_POPEN;
2608
2609     PERL_FLUSHALL_FOR_CHILD;
2610 #ifdef OS2
2611     if (doexec) {
2612         return my_syspopen(aTHX_ cmd,mode);
2613     }
2614 #endif
2615     This = (*mode == 'w');
2616     that = !This;
2617     if (doexec && TAINTING_get) {
2618         taint_env();
2619         taint_proper("Insecure %s%s", "EXEC");
2620     }
2621     if (PerlProc_pipe_cloexec(p) < 0)
2622         return NULL;
2623     if (doexec && PerlProc_pipe_cloexec(pp) >= 0)
2624         did_pipes = 1;
2625     while ((pid = PerlProc_fork()) < 0) {
2626         if (errno != EAGAIN) {
2627             PerlLIO_close(p[This]);
2628             PerlLIO_close(p[that]);
2629             if (did_pipes) {
2630                 PerlLIO_close(pp[0]);
2631                 PerlLIO_close(pp[1]);
2632             }
2633             if (!doexec)
2634                 Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno));
2635             return NULL;
2636         }
2637         Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds");
2638         sleep(5);
2639     }
2640     if (pid == 0) {
2641
2642 #undef THIS
2643 #undef THAT
2644 #define THIS that
2645 #define THAT This
2646         if (did_pipes)
2647             PerlLIO_close(pp[0]);
2648 #if defined(OEMVS)
2649   #if (__CHARSET_LIB == 1)
2650         chgfdccsid(p[THIS], 819);
2651         chgfdccsid(p[THAT], 819);
2652   #endif
2653 #endif
2654         if (p[THIS] != (*mode == 'r')) {
2655             PerlLIO_dup2(p[THIS], *mode == 'r');
2656             PerlLIO_close(p[THIS]);
2657             if (p[THAT] != (*mode == 'r'))      /* if dup2() didn't close it */
2658                 PerlLIO_close(p[THAT]);
2659         }
2660         else {
2661             setfd_cloexec_or_inhexec_by_sysfdness(p[THIS]);
2662             PerlLIO_close(p[THAT]);
2663         }
2664 #ifndef OS2
2665         if (doexec) {
2666 #if !defined(HAS_FCNTL) || !defined(F_SETFD)
2667 #ifndef NOFILE
2668 #define NOFILE 20
2669 #endif
2670             {
2671                 int fd;
2672
2673                 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
2674                     if (fd != pp[1])
2675                         PerlLIO_close(fd);
2676             }
2677 #endif
2678             /* may or may not use the shell */
2679             do_exec3(cmd, pp[1], did_pipes);
2680             PerlProc__exit(1);
2681         }
2682 #endif  /* defined OS2 */
2683
2684 #ifdef PERLIO_USING_CRLF
2685    /* Since we circumvent IO layers when we manipulate low-level
2686       filedescriptors directly, need to manually switch to the
2687       default, binary, low-level mode; see PerlIOBuf_open(). */
2688    PerlLIO_setmode((*mode == 'r'), O_BINARY);
2689 #endif 
2690         PL_forkprocess = 0;
2691 #ifdef PERL_USES_PL_PIDSTATUS
2692         hv_clear(PL_pidstatus); /* we have no children */
2693 #endif
2694         return NULL;
2695 #undef THIS
2696 #undef THAT
2697     }
2698     if (did_pipes)
2699         PerlLIO_close(pp[1]);
2700     if (p[that] < p[This]) {
2701         PerlLIO_dup2_cloexec(p[This], p[that]);
2702         PerlLIO_close(p[This]);
2703         p[This] = p[that];
2704     }
2705     else
2706         PerlLIO_close(p[that]);
2707
2708     sv = *av_fetch(PL_fdpid,p[This],TRUE);
2709     SvUPGRADE(sv,SVt_IV);
2710     SvIV_set(sv, pid);
2711     PL_forkprocess = pid;
2712     if (did_pipes && pid > 0) {
2713         int errkid;
2714         unsigned n = 0;
2715
2716         while (n < sizeof(int)) {
2717             const SSize_t n1 = PerlLIO_read(pp[0],
2718                               (void*)(((char*)&errkid)+n),
2719                               (sizeof(int)) - n);
2720             if (n1 <= 0)
2721                 break;
2722             n += n1;
2723         }
2724         PerlLIO_close(pp[0]);
2725         did_pipes = 0;
2726         if (n) {                        /* Error */
2727             int pid2, status;
2728             PerlLIO_close(p[This]);
2729             if (n != sizeof(int))
2730                 Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n);
2731             do {
2732                 pid2 = wait4pid(pid, &status, 0);
2733             } while (pid2 == -1 && errno == EINTR);
2734             errno = errkid;             /* Propagate errno from kid */
2735             return NULL;
2736         }
2737     }
2738     if (did_pipes)
2739          PerlLIO_close(pp[0]);
2740 #if defined(OEMVS)
2741   #if (__CHARSET_LIB == 1)
2742     PerlIO* io = PerlIO_fdopen(p[This], mode);
2743     if (io) {
2744       chgfdccsid(p[This], 819);
2745     }
2746     return io;
2747   #else
2748     return PerlIO_fdopen(p[This], mode);
2749   #endif
2750 #else
2751     return PerlIO_fdopen(p[This], mode);
2752 #endif
2753 }
2754 #elif defined(__LIBCATAMOUNT__)
2755 PerlIO *
2756 Perl_my_popen(pTHX_ const char *cmd, const char *mode)
2757 {
2758     return NULL;
2759 }
2760
2761 #endif /* !DOSISH */
2762
2763 /* this is called in parent before the fork() */
2764 void
2765 Perl_atfork_lock(void)
2766 #if defined(USE_ITHREADS)
2767 #  ifdef USE_PERLIO
2768   PERL_TSA_ACQUIRE(PL_perlio_mutex)
2769 #  endif
2770 #  ifdef MYMALLOC
2771   PERL_TSA_ACQUIRE(PL_malloc_mutex)
2772 #  endif
2773   PERL_TSA_ACQUIRE(PL_op_mutex)
2774 #endif
2775 {
2776 #if defined(USE_ITHREADS)
2777     /* locks must be held in locking order (if any) */
2778 #  ifdef USE_PERLIO
2779     MUTEX_LOCK(&PL_perlio_mutex);
2780 #  endif
2781 #  ifdef MYMALLOC
2782     MUTEX_LOCK(&PL_malloc_mutex);
2783 #  endif
2784     OP_REFCNT_LOCK;
2785 #endif
2786 }
2787
2788 /* this is called in both parent and child after the fork() */
2789 void
2790 Perl_atfork_unlock(void)
2791 #if defined(USE_ITHREADS)
2792 #  ifdef USE_PERLIO
2793   PERL_TSA_RELEASE(PL_perlio_mutex)
2794 #  endif
2795 #  ifdef MYMALLOC
2796   PERL_TSA_RELEASE(PL_malloc_mutex)
2797 #  endif
2798   PERL_TSA_RELEASE(PL_op_mutex)
2799 #endif
2800 {
2801 #if defined(USE_ITHREADS)
2802     /* locks must be released in same order as in atfork_lock() */
2803 #  ifdef USE_PERLIO
2804     MUTEX_UNLOCK(&PL_perlio_mutex);
2805 #  endif
2806 #  ifdef MYMALLOC
2807     MUTEX_UNLOCK(&PL_malloc_mutex);
2808 #  endif
2809     OP_REFCNT_UNLOCK;
2810 #endif
2811 }
2812
2813 /*
2814 =for apidoc_section $concurrency
2815 =for apidoc my_fork
2816
2817 This is for the use of C<PerlProc_fork> as a wrapper for the C library
2818 L<fork(2)> on some platforms to hide some platform quirks.  It should not be
2819 used except through C<PerlProc_fork>.
2820
2821 =cut
2822 */
2823
2824
2825 Pid_t
2826 Perl_my_fork(void)
2827 {
2828 #if defined(HAS_FORK)
2829     Pid_t pid;
2830 #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
2831     atfork_lock();
2832     pid = fork();
2833     atfork_unlock();
2834 #else
2835     /* atfork_lock() and atfork_unlock() are installed as pthread_atfork()
2836      * handlers elsewhere in the code */
2837     pid = fork();
2838 #endif
2839     return pid;
2840 #elif defined(__amigaos4__)
2841     return amigaos_fork();
2842 #else
2843     /* this "canna happen" since nothing should be calling here if !HAS_FORK */
2844     Perl_croak_nocontext("fork() not available");
2845     return 0;
2846 #endif /* HAS_FORK */
2847 }
2848
2849 #ifndef HAS_DUP2
2850 int
2851 dup2(int oldfd, int newfd)
2852 {
2853 #if defined(HAS_FCNTL) && defined(F_DUPFD)
2854     if (oldfd == newfd)
2855         return oldfd;
2856     PerlLIO_close(newfd);
2857     return fcntl(oldfd, F_DUPFD, newfd);
2858 #else
2859 #define DUP2_MAX_FDS 256
2860     int fdtmp[DUP2_MAX_FDS];
2861     I32 fdx = 0;
2862     int fd;
2863
2864     if (oldfd == newfd)
2865         return oldfd;
2866     PerlLIO_close(newfd);
2867     /* good enough for low fd's... */
2868     while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
2869         if (fdx >= DUP2_MAX_FDS) {
2870             PerlLIO_close(fd);
2871             fd = -1;
2872             break;
2873         }
2874         fdtmp[fdx++] = fd;
2875     }
2876     while (fdx > 0)
2877         PerlLIO_close(fdtmp[--fdx]);
2878     return fd;
2879 #endif
2880 }
2881 #endif
2882
2883 #ifndef PERL_MICRO
2884 #ifdef HAS_SIGACTION
2885
2886 /*
2887 =for apidoc_section $signals
2888 =for apidoc rsignal
2889
2890 A wrapper for the C library functions L<sigaction(2)> or L<signal(2)>.
2891 Use this instead of those libc functions, as the Perl version gives the
2892 safest available implementation, and knows things that interact with the
2893 rest of the perl interpreter.
2894
2895 =cut
2896 */
2897
2898 Sighandler_t
2899 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2900 {
2901     struct sigaction act, oact;
2902
2903 #ifdef USE_ITHREADS
2904     /* only "parent" interpreter can diddle signals */
2905     if (PL_curinterp != aTHX)
2906         return (Sighandler_t) SIG_ERR;
2907 #endif
2908
2909     act.sa_handler = handler;
2910     sigemptyset(&act.sa_mask);
2911     act.sa_flags = 0;
2912 #ifdef SA_RESTART
2913     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2914         act.sa_flags |= SA_RESTART;     /* SVR4, 4.3+BSD */
2915 #endif
2916 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
2917     if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
2918         act.sa_flags |= SA_NOCLDWAIT;
2919 #endif
2920     if (sigaction(signo, &act, &oact) == -1)
2921         return (Sighandler_t) SIG_ERR;
2922     else
2923         return (Sighandler_t) oact.sa_handler;
2924 }
2925
2926 /*
2927 =for apidoc_section $signals
2928 =for apidoc rsignal_state
2929
2930 Returns a the current signal handler for signal C<signo>.
2931 See L</C<rsignal>>.
2932
2933 =cut
2934 */
2935
2936 Sighandler_t
2937 Perl_rsignal_state(pTHX_ int signo)
2938 {
2939     struct sigaction oact;
2940     PERL_UNUSED_CONTEXT;
2941
2942     if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2943         return (Sighandler_t) SIG_ERR;
2944     else
2945         return (Sighandler_t) oact.sa_handler;
2946 }
2947
2948 int
2949 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
2950 {
2951     struct sigaction act;
2952
2953     PERL_ARGS_ASSERT_RSIGNAL_SAVE;
2954
2955 #ifdef USE_ITHREADS
2956     /* only "parent" interpreter can diddle signals */
2957     if (PL_curinterp != aTHX)
2958         return -1;
2959 #endif
2960
2961     act.sa_handler = handler;
2962     sigemptyset(&act.sa_mask);
2963     act.sa_flags = 0;
2964 #ifdef SA_RESTART
2965     if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)
2966         act.sa_flags |= SA_RESTART;     /* SVR4, 4.3+BSD */
2967 #endif
2968 #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */
2969     if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN)
2970         act.sa_flags |= SA_NOCLDWAIT;
2971 #endif
2972     return sigaction(signo, &act, save);
2973 }
2974
2975 int
2976 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
2977 {
2978     PERL_UNUSED_CONTEXT;
2979 #ifdef USE_ITHREADS
2980     /* only "parent" interpreter can diddle signals */
2981     if (PL_curinterp != aTHX)
2982         return -1;
2983 #endif
2984
2985     return sigaction(signo, save, (struct sigaction *)NULL);
2986 }
2987
2988 #else /* !HAS_SIGACTION */
2989
2990 Sighandler_t
2991 Perl_rsignal(pTHX_ int signo, Sighandler_t handler)
2992 {
2993 #if defined(USE_ITHREADS) && !defined(WIN32)
2994     /* only "parent" interpreter can diddle signals */
2995     if (PL_curinterp != aTHX)
2996         return (Sighandler_t) SIG_ERR;
2997 #endif
2998
2999     return PerlProc_signal(signo, handler);
3000 }
3001
3002 static Signal_t
3003 sig_trap(int signo)
3004 {
3005     PL_sig_trapped++;
3006 }
3007
3008 Sighandler_t
3009 Perl_rsignal_state(pTHX_ int signo)
3010 {
3011     Sighandler_t oldsig;
3012
3013 #if defined(USE_ITHREADS) && !defined(WIN32)
3014     /* only "parent" interpreter can diddle signals */
3015     if (PL_curinterp != aTHX)
3016         return (Sighandler_t) SIG_ERR;
3017 #endif
3018
3019     PL_sig_trapped = 0;
3020     oldsig = PerlProc_signal(signo, sig_trap);
3021     PerlProc_signal(signo, oldsig);
3022     if (PL_sig_trapped)
3023         PerlProc_kill(PerlProc_getpid(), signo);
3024     return oldsig;
3025 }
3026
3027 int
3028 Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save)
3029 {
3030 #if defined(USE_ITHREADS) && !defined(WIN32)
3031     /* only "parent" interpreter can diddle signals */
3032     if (PL_curinterp != aTHX)
3033         return -1;
3034 #endif
3035     *save = PerlProc_signal(signo, handler);
3036     return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0;
3037 }
3038
3039 int
3040 Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save)
3041 {
3042 #if defined(USE_ITHREADS) && !defined(WIN32)
3043     /* only "parent" interpreter can diddle signals */
3044     if (PL_curinterp != aTHX)
3045         return -1;
3046 #endif
3047     return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0;
3048 }
3049
3050 #endif /* !HAS_SIGACTION */
3051 #endif /* !PERL_MICRO */
3052
3053     /* VMS' my_pclose() is in VMS.c */
3054
3055 /*
3056 =for apidoc_section $io
3057 =for apidoc my_pclose
3058
3059 A wrapper for the C library L<pclose(3)>.  Don't use the latter, as the Perl
3060 version knows things that interact with the rest of the perl interpreter.
3061
3062 =cut
3063 */
3064
3065 #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
3066 I32
3067 Perl_my_pclose(pTHX_ PerlIO *ptr)
3068 {
3069     int status;
3070     SV **svp;
3071     Pid_t pid;
3072     Pid_t pid2 = 0;
3073     bool close_failed;
3074     dSAVEDERRNO;
3075     const int fd = PerlIO_fileno(ptr);
3076     bool should_wait;
3077
3078     svp = av_fetch(PL_fdpid, fd, FALSE);
3079     if (svp) {
3080         pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1;
3081         SvREFCNT_dec(*svp);
3082         *svp = NULL;
3083     } else {
3084         pid = -1;
3085     }
3086
3087 #if defined(USE_PERLIO)
3088     /* Find out whether the refcount is low enough for us to wait for the
3089        child proc without blocking. */
3090     should_wait = PerlIOUnix_refcnt(fd) == 1 && pid > 0;
3091 #else
3092     should_wait = pid > 0;
3093 #endif
3094
3095 #ifdef OS2
3096     if (pid == -2) {                    /* Opened by popen. */
3097         return my_syspclose(ptr);
3098     }
3099 #endif
3100     close_failed = (PerlIO_close(ptr) == EOF);
3101     SAVE_ERRNO;
3102     if (should_wait) do {
3103         pid2 = wait4pid(pid, &status, 0);
3104     } while (pid2 == -1 && errno == EINTR);
3105     if (close_failed) {
3106         RESTORE_ERRNO;
3107         return -1;
3108     }
3109     return(
3110       should_wait
3111        ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)
3112        : 0
3113     );
3114 }
3115 #elif defined(__LIBCATAMOUNT__)
3116 I32
3117 Perl_my_pclose(pTHX_ PerlIO *ptr)
3118 {
3119     return -1;
3120 }
3121 #endif /* !DOSISH */
3122
3123 #if  (!defined(DOSISH) || defined(OS2) || defined(WIN32)) && !defined(__LIBCATAMOUNT__)
3124 I32
3125 Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags)
3126 {
3127     I32 result = 0;
3128     PERL_ARGS_ASSERT_WAIT4PID;
3129 #ifdef PERL_USES_PL_PIDSTATUS
3130     if (!pid) {
3131         /* PERL_USES_PL_PIDSTATUS is only defined when neither
3132            waitpid() nor wait4() is available, or on OS/2, which
3133            doesn't appear to support waiting for a progress group
3134            member, so we can only treat a 0 pid as an unknown child.
3135         */
3136         errno = ECHILD;
3137         return -1;
3138     }
3139     {
3140         if (pid > 0) {
3141             /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the
3142                pid, rather than a string form.  */
3143             SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE);
3144             if (svp && *svp != &PL_sv_undef) {
3145                 *statusp = SvIVX(*svp);
3146                 (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),
3147                                 G_DISCARD);
3148                 return pid;
3149             }
3150         }
3151         else {
3152             HE *entry;
3153
3154             hv_iterinit(PL_pidstatus);
3155             if ((entry = hv_iternext(PL_pidstatus))) {
3156                 SV * const sv = hv_iterval(PL_pidstatus,entry);
3157                 I32 len;
3158                 const char * const spid = hv_iterkey(entry,&len);
3159
3160                 assert (len == sizeof(Pid_t));
3161                 memcpy((char *)&pid, spid, len);
3162                 *statusp = SvIVX(sv);
3163                 /* The hash iterator is currently on this entry, so simply
3164                    calling hv_delete would trigger the lazy delete, which on
3165                    aggregate does more work, because next call to hv_iterinit()
3166                    would spot the flag, and have to call the delete routine,
3167                    while in the meantime any new entries can't re-use that
3168                    memory.  */
3169                 hv_iterinit(PL_pidstatus);
3170                 (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD);
3171                 return pid;
3172             }
3173         }
3174     }
3175 #endif
3176 #ifdef HAS_WAITPID
3177 #  ifdef HAS_WAITPID_RUNTIME
3178     if (!HAS_WAITPID_RUNTIME)
3179         goto hard_way;
3180 #  endif
3181     result = PerlProc_waitpid(pid,statusp,flags);
3182     goto finish;
3183 #endif
3184 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
3185     result = wait4(pid,statusp,flags,NULL);
3186     goto finish;
3187 #endif
3188 #ifdef PERL_USES_PL_PIDSTATUS
3189 #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME)
3190   hard_way:
3191 #endif
3192     {
3193         if (flags)
3194             Perl_croak(aTHX_ "Can't do waitpid with flags");
3195         else {
3196             while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
3197                 pidgone(result,*statusp);
3198             if (result < 0)
3199                 *statusp = -1;
3200         }
3201     }
3202 #endif
3203 #if defined(HAS_WAITPID) || defined(HAS_WAIT4)
3204   finish:
3205 #endif
3206     if (result < 0 && errno == EINTR) {
3207         PERL_ASYNC_CHECK();
3208         errno = EINTR; /* reset in case a signal handler changed $! */
3209     }
3210     return result;
3211 }
3212 #endif /* !DOSISH || OS2 || WIN32 */
3213
3214 #ifdef PERL_USES_PL_PIDSTATUS
3215 void
3216 S_pidgone(pTHX_ Pid_t pid, int status)
3217 {
3218     SV *sv;
3219
3220     sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE);
3221     SvUPGRADE(sv,SVt_IV);
3222     SvIV_set(sv, status);
3223     return;
3224 }
3225 #endif
3226
3227 #if defined(OS2)
3228 int pclose();
3229 #ifdef HAS_FORK
3230 int                                     /* Cannot prototype with I32
3231                                            in os2ish.h. */
3232 my_syspclose(PerlIO *ptr)
3233 #else
3234 I32
3235 Perl_my_pclose(pTHX_ PerlIO *ptr)
3236 #endif
3237 {
3238     /* Needs work for PerlIO ! */
3239     FILE * const f = PerlIO_findFILE(ptr);
3240     const I32 result = pclose(f);
3241     PerlIO_releaseFILE(ptr,f);
3242     return result;
3243 }
3244 #endif
3245
3246 /*
3247 =for apidoc repeatcpy
3248
3249 Make C<count> copies of the C<len> bytes beginning at C<from>, placing them
3250 into memory beginning at C<to>, which must be big enough to accommodate them
3251 all.
3252
3253 =cut
3254 */
3255
3256 #define PERL_REPEATCPY_LINEAR 4
3257 void
3258 Perl_repeatcpy(char *to, const char *from, I32 len, IV count)
3259 {
3260     PERL_ARGS_ASSERT_REPEATCPY;
3261
3262     assert(len >= 0);
3263
3264     if (count < 0)
3265         croak_memory_wrap();
3266
3267     if (len == 1)
3268         memset(to, *from, count);
3269     else if (count) {
3270         char *p = to;
3271         IV items, linear, half;
3272
3273         linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR;
3274         for (items = 0; items < linear; ++items) {
3275             const char *q = from;
3276             IV todo;
3277             for (todo = len; todo > 0; todo--)
3278                 *p++ = *q++;
3279         }
3280
3281         half = count / 2;
3282         while (items <= half) {
3283             IV size = items * len;
3284             memcpy(p, to, size);
3285             p     += size;
3286             items *= 2;
3287         }
3288
3289         if (count > items)
3290             memcpy(p, to, (count - items) * len);
3291     }
3292 }
3293
3294 #ifndef HAS_RENAME
3295 I32
3296 Perl_same_dirent(pTHX_ const char *a, const char *b)
3297 {
3298     char *fa = strrchr(a,'/');
3299     char *fb = strrchr(b,'/');
3300     Stat_t tmpstatbuf1;
3301     Stat_t tmpstatbuf2;
3302     SV * const tmpsv = sv_newmortal();
3303
3304     PERL_ARGS_ASSERT_SAME_DIRENT;
3305
3306     if (fa)
3307         fa++;
3308     else
3309         fa = a;
3310     if (fb)
3311         fb++;
3312     else
3313         fb = b;
3314     if (strNE(a,b))
3315         return FALSE;
3316     if (fa == a)
3317         sv_setpvs(tmpsv, ".");
3318     else
3319         sv_setpvn(tmpsv, a, fa - a);
3320     if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
3321         return FALSE;
3322     if (fb == b)
3323         sv_setpvs(tmpsv, ".");
3324     else
3325         sv_setpvn(tmpsv, b, fb - b);
3326     if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
3327         return FALSE;
3328     return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
3329            tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
3330 }
3331 #endif /* !HAS_RENAME */
3332
3333 char*
3334 Perl_find_script(pTHX_ const char *scriptname, bool dosearch,
3335                  const char *const *const search_ext, I32 flags)
3336 {
3337     const char *xfound = NULL;
3338     char *xfailed = NULL;
3339     char tmpbuf[MAXPATHLEN];
3340     char *s;
3341     I32 len = 0;
3342     int retval;
3343     char *bufend;
3344 #if defined(DOSISH) && !defined(OS2)
3345 #  define SEARCH_EXTS ".bat", ".cmd", NULL
3346 #  define MAX_EXT_LEN 4
3347 #endif
3348 #ifdef OS2
3349 #  define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
3350 #  define MAX_EXT_LEN 4
3351 #endif
3352 #ifdef VMS
3353 #  define SEARCH_EXTS ".pl", ".com", NULL
3354 #  define MAX_EXT_LEN 4
3355 #endif
3356     /* additional extensions to try in each dir if scriptname not found */
3357 #ifdef SEARCH_EXTS
3358     static const char *const exts[] = { SEARCH_EXTS };
3359     const char *const *const ext = search_ext ? search_ext : exts;
3360     int extidx = 0, i = 0;
3361     const char *curext = NULL;
3362 #else
3363     PERL_UNUSED_ARG(search_ext);
3364 #  define MAX_EXT_LEN 0
3365 #endif
3366
3367     PERL_ARGS_ASSERT_FIND_SCRIPT;
3368
3369     /*
3370      * If dosearch is true and if scriptname does not contain path
3371      * delimiters, search the PATH for scriptname.
3372      *
3373      * If SEARCH_EXTS is also defined, will look for each
3374      * scriptname{SEARCH_EXTS} whenever scriptname is not found
3375      * while searching the PATH.
3376      *
3377      * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
3378      * proceeds as follows:
3379      *   If DOSISH or VMSISH:
3380      *     + look for ./scriptname{,.foo,.bar}
3381      *     + search the PATH for scriptname{,.foo,.bar}
3382      *
3383      *   If !DOSISH:
3384      *     + look *only* in the PATH for scriptname{,.foo,.bar} (note
3385      *       this will not look in '.' if it's not in the PATH)
3386      */
3387     tmpbuf[0] = '\0';
3388
3389 #ifdef VMS
3390 #  ifdef ALWAYS_DEFTYPES
3391     len = strlen(scriptname);
3392     if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
3393         int idx = 0, deftypes = 1;
3394         bool seen_dot = 1;
3395
3396         const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL);
3397 #  else
3398     if (dosearch) {
3399         int idx = 0, deftypes = 1;
3400         bool seen_dot = 1;
3401
3402         const int hasdir = (strpbrk(scriptname,":[</") != NULL);
3403 #  endif
3404         /* The first time through, just add SEARCH_EXTS to whatever we
3405          * already have, so we can check for default file types. */
3406         while (deftypes ||
3407                (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
3408         {
3409             Stat_t statbuf;
3410             if (deftypes) {
3411                 deftypes = 0;
3412                 *tmpbuf = '\0';
3413             }
3414             if ((strlen(tmpbuf) + strlen(scriptname)
3415                  + MAX_EXT_LEN) >= sizeof tmpbuf)
3416                 continue;       /* don't search dir with too-long name */
3417             my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
3418 #else  /* !VMS */
3419
3420 #ifdef DOSISH
3421     if (strEQ(scriptname, "-"))
3422         dosearch = 0;
3423     if (dosearch) {             /* Look in '.' first. */
3424         const char *cur = scriptname;
3425 #ifdef SEARCH_EXTS
3426         if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
3427             while (ext[i])
3428                 if (strEQ(ext[i++],curext)) {
3429                     extidx = -1;                /* already has an ext */
3430                     break;
3431                 }
3432         do {
3433 #endif
3434             DEBUG_p(PerlIO_printf(Perl_debug_log,
3435                                   "Looking for %s\n",cur));
3436             {
3437                 Stat_t statbuf;
3438                 if (PerlLIO_stat(cur,&statbuf) >= 0
3439                     && !S_ISDIR(statbuf.st_mode)) {
3440                     dosearch = 0;
3441                     scriptname = cur;
3442 #ifdef SEARCH_EXTS
3443                     break;
3444 #endif
3445                 }
3446             }
3447 #ifdef SEARCH_EXTS
3448             if (cur == scriptname) {
3449                 len = strlen(scriptname);
3450                 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
3451                     break;
3452                 my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
3453                 cur = tmpbuf;
3454             }
3455         } while (extidx >= 0 && ext[extidx]     /* try an extension? */
3456                  && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len));
3457 #endif
3458     }
3459 #endif
3460
3461     if (dosearch && !strchr(scriptname, '/')
3462 #ifdef DOSISH
3463                  && !strchr(scriptname, '\\')
3464 #endif
3465                  && (s = PerlEnv_getenv("PATH")))
3466     {
3467         bool seen_dot = 0;
3468
3469         bufend = s + strlen(s);
3470         while (s < bufend) {
3471             Stat_t statbuf;
3472 #  ifdef DOSISH
3473             for (len = 0; *s
3474                     && *s != ';'; len++, s++) {
3475                 if (len < sizeof tmpbuf)
3476                     tmpbuf[len] = *s;
3477             }
3478             if (len < sizeof tmpbuf)
3479                 tmpbuf[len] = '\0';
3480 #  else
3481             s = delimcpy_no_escape(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend,
3482                                    ':', &len);
3483 #  endif
3484             if (s < bufend)
3485                 s++;
3486             if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
3487                 continue;       /* don't search dir with too-long name */
3488             if (len
3489 #  ifdef DOSISH
3490                 && tmpbuf[len - 1] != '/'
3491                 && tmpbuf[len - 1] != '\\'
3492 #  endif
3493                )
3494                 tmpbuf[len++] = '/';
3495             if (len == 2 && tmpbuf[0] == '.')
3496                 seen_dot = 1;
3497             (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
3498 #endif  /* !VMS */
3499
3500 #ifdef SEARCH_EXTS
3501             len = strlen(tmpbuf);
3502             if (extidx > 0)     /* reset after previous loop */
3503                 extidx = 0;
3504             do {
3505 #endif
3506                 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3507                 retval = PerlLIO_stat(tmpbuf,&statbuf);
3508                 if (S_ISDIR(statbuf.st_mode)) {
3509                     retval = -1;
3510                 }
3511 #ifdef SEARCH_EXTS
3512             } while (  retval < 0               /* not there */
3513                     && extidx>=0 && ext[extidx] /* try an extension? */
3514                     && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)
3515                 );
3516 #endif
3517             if (retval < 0)
3518                 continue;
3519             if (S_ISREG(statbuf.st_mode)
3520                 && cando(S_IRUSR,TRUE,&statbuf)
3521 #if !defined(DOSISH)
3522                 && cando(S_IXUSR,TRUE,&statbuf)
3523 #endif
3524                 )
3525             {
3526                 xfound = tmpbuf;                /* bingo! */
3527                 break;
3528             }
3529             if (!xfailed)
3530                 xfailed = savepv(tmpbuf);
3531         }
3532 #ifndef DOSISH
3533         {
3534             Stat_t statbuf;
3535             if (!xfound && !seen_dot && !xfailed &&
3536                 (PerlLIO_stat(scriptname,&statbuf) < 0
3537                  || S_ISDIR(statbuf.st_mode)))
3538 #endif
3539                 seen_dot = 1;                   /* Disable message. */
3540 #ifndef DOSISH
3541         }
3542 #endif
3543         if (!xfound) {
3544             if (flags & 1) {                    /* do or die? */
3545                 /* diag_listed_as: Can't execute %s */
3546                 Perl_croak(aTHX_ "Can't %s %s%s%s",
3547                       (xfailed ? "execute" : "find"),
3548                       (xfailed ? xfailed : scriptname),
3549                       (xfailed ? "" : " on PATH"),
3550                       (xfailed || seen_dot) ? "" : ", '.' not in PATH");
3551             }
3552             scriptname = NULL;
3553         }
3554         Safefree(xfailed);
3555         scriptname = xfound;
3556     }
3557     return (scriptname ? savepv(scriptname) : NULL);
3558 }
3559
3560 #ifndef PERL_GET_CONTEXT_DEFINED
3561
3562 /*
3563 =for apidoc_section $embedding
3564 =for apidoc get_context
3565
3566 Implements L<perlapi/C<PERL_GET_CONTEXT>>, which you should use instead.
3567
3568 =cut
3569 */
3570
3571 void *
3572 Perl_get_context(void)
3573 {
3574 #if defined(USE_ITHREADS)
3575 #  ifdef OLD_PTHREADS_API
3576     pthread_addr_t t;
3577     int error = pthread_getspecific(PL_thr_key, &t);
3578     if (error)
3579         Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error);
3580     return (void*)t;
3581 #  elif defined(I_MACH_CTHREADS)
3582     return (void*)cthread_data(cthread_self());
3583 #  else
3584     return (void*)PTHREAD_GETSPECIFIC(PL_thr_key);
3585 #  endif
3586 #else
3587     return (void*)NULL;
3588 #endif
3589 }
3590
3591 /*
3592 =for apidoc_section $embedding
3593 =for apidoc set_context
3594
3595 Implements L<perlapi/C<PERL_SET_CONTEXT>>, which you should use instead.
3596
3597 =cut
3598 */
3599
3600 void
3601 Perl_set_context(void *t)
3602 {
3603     PERL_ARGS_ASSERT_SET_CONTEXT;
3604 #if defined(USE_ITHREADS)
3605 #  ifdef PERL_USE_THREAD_LOCAL
3606     PL_current_context = t;
3607 #  endif
3608 #  ifdef I_MACH_CTHREADS
3609     cthread_set_data(cthread_self(), t);
3610 #  else
3611     /* We set thread-specific value always, as C++ code has to read it with
3612      * pthreads, because the declaration syntax for thread local storage for C11
3613      * is incompatible with C++, meaning that we can't expose the thread local
3614      * variable to C++ code. */
3615     {
3616         const int error = pthread_setspecific(PL_thr_key, t);
3617         if (error)
3618             Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error);
3619     }
3620 #  endif
3621
3622     PERL_SET_NON_tTHX_CONTEXT(t);
3623
3624 #else
3625     PERL_UNUSED_ARG(t);
3626 #endif
3627 }
3628
3629 #endif /* !PERL_GET_CONTEXT_DEFINED */
3630
3631 /*
3632 =for apidoc get_op_names
3633
3634 Return a pointer to the array of all the names of the various OPs
3635 Given an opcode from the enum in F<opcodes.h>, C<PL_op_name[opcode]> returns a
3636 pointer to a C language string giving its name.
3637
3638 =cut
3639 */
3640
3641 char **
3642 Perl_get_op_names(pTHX)
3643 {
3644     PERL_UNUSED_CONTEXT;
3645     return (char **)PL_op_name;
3646 }
3647
3648 /*
3649 =for apidoc get_op_descs
3650
3651 Return a pointer to the array of all the descriptions of the various OPs
3652 Given an opcode from the enum in F<opcodes.h>, C<PL_op_desc[opcode]> returns a
3653 pointer to a C language string giving its description.
3654
3655 =cut
3656 */
3657
3658 char **
3659 Perl_get_op_descs(pTHX)
3660 {
3661     PERL_UNUSED_CONTEXT;
3662     return (char **)PL_op_desc;
3663 }
3664
3665 const char *
3666 Perl_get_no_modify(pTHX)
3667 {
3668     PERL_UNUSED_CONTEXT;
3669     return PL_no_modify;
3670 }
3671
3672 U32 *
3673 Perl_get_opargs(pTHX)
3674 {
3675     PERL_UNUSED_CONTEXT;
3676     return (U32 *)PL_opargs;
3677 }
3678
3679 PPADDR_t*
3680 Perl_get_ppaddr(pTHX)
3681 {
3682     PERL_UNUSED_CONTEXT;
3683     return (PPADDR_t*)PL_ppaddr;
3684 }
3685
3686 #ifndef HAS_GETENV_LEN
3687 char *
3688 Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len)
3689 {
3690     char * const env_trans = PerlEnv_getenv(env_elem);
3691     PERL_UNUSED_CONTEXT;
3692     PERL_ARGS_ASSERT_GETENV_LEN;
3693     if (env_trans)
3694         *len = strlen(env_trans);
3695     return env_trans;
3696 }
3697 #endif
3698
3699
3700 MGVTBL*
3701 Perl_get_vtbl(pTHX_ int vtbl_id)
3702 {
3703     PERL_UNUSED_CONTEXT;
3704
3705     return (vtbl_id < 0 || vtbl_id >= magic_vtable_max)
3706         ? NULL : (MGVTBL*)PL_magic_vtables + vtbl_id;
3707 }
3708
3709 /*
3710 =for apidoc_section $io
3711 =for apidoc my_fflush_all
3712
3713 Implements C<PERL_FLUSHALL_FOR_CHILD> on some platforms.
3714
3715 =cut
3716  */
3717
3718 I32
3719 Perl_my_fflush_all(pTHX)
3720 {
3721 #if defined(USE_PERLIO) || defined(FFLUSH_NULL)
3722     return PerlIO_flush(NULL);
3723 #else
3724 # if defined(HAS__FWALK)
3725     extern int fflush(FILE *);
3726     /* undocumented, unprototyped, but very useful BSDism */
3727     extern void _fwalk(int (*)(FILE *));
3728     _fwalk(&fflush);
3729     return 0;
3730 # else
3731 #  if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
3732     long open_max = -1;
3733 #   ifdef PERL_FFLUSH_ALL_FOPEN_MAX
3734     open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
3735 #   elif defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
3736     open_max = sysconf(_SC_OPEN_MAX);
3737 #   elif defined(FOPEN_MAX)
3738     open_max = FOPEN_MAX;
3739 #   elif defined(OPEN_MAX)
3740     open_max = OPEN_MAX;
3741 #   elif defined(_NFILE)
3742     open_max = _NFILE;
3743 #   endif
3744     if (open_max > 0) {
3745       long i;
3746       for (i = 0; i < open_max; i++)
3747             if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
3748                 STDIO_STREAM_ARRAY[i]._file < open_max &&
3749                 STDIO_STREAM_ARRAY[i]._flag)
3750                 PerlIO_flush(&STDIO_STREAM_ARRAY[i]);
3751       return 0;
3752     }
3753 #  endif
3754     SETERRNO(EBADF,RMS_IFI);
3755     return EOF;
3756 # endif
3757 #endif
3758 }
3759
3760 void
3761 Perl_report_wrongway_fh(pTHX_ const GV *gv, const char have)
3762 {
3763     if (ckWARN(WARN_IO)) {
3764         HEK * const name
3765            = gv && (isGV_with_GP(gv))
3766                 ? GvENAME_HEK((gv))
3767                 : NULL;
3768         const char * const direction = have == '>' ? "out" : "in";
3769
3770         if (name && HEK_LEN(name))
3771             Perl_warner(aTHX_ packWARN(WARN_IO),
3772                         "Filehandle %" HEKf " opened only for %sput",
3773                         HEKfARG(name), direction);
3774         else
3775             Perl_warner(aTHX_ packWARN(WARN_IO),
3776                         "Filehandle opened only for %sput", direction);
3777     }
3778 }
3779
3780 void
3781 Perl_report_evil_fh(pTHX_ const GV *gv)
3782 {
3783     const IO *io = gv ? GvIO(gv) : NULL;
3784     const PERL_BITFIELD16 op = PL_op->op_type;
3785     const char *vile;
3786     I32 warn_type;
3787
3788     if (io && IoTYPE(io) == IoTYPE_CLOSED) {
3789         vile = "closed";
3790         warn_type = WARN_CLOSED;
3791     }
3792     else {
3793         vile = "unopened";
3794         warn_type = WARN_UNOPENED;
3795     }
3796
3797     if (ckWARN(warn_type)) {
3798         SV * const name
3799             = gv && isGV_with_GP(gv) && GvENAMELEN(gv) ?
3800                                      newSVhek_mortal(GvENAME_HEK(gv)) : NULL;
3801         const char * const pars =
3802             (const char *)(OP_IS_FILETEST(op) ? "" : "()");
3803         const char * const func =
3804             (const char *)
3805             (op == OP_READLINE || op == OP_RCATLINE
3806                                  ? "readline"  :        /* "<HANDLE>" not nice */
3807              op == OP_LEAVEWRITE ? "write" :            /* "write exit" not nice */
3808              PL_op_desc[op]);
3809         const char * const type =
3810             (const char *)
3811             (OP_IS_SOCKET(op) || (io && IoTYPE(io) == IoTYPE_SOCKET)
3812              ? "socket" : "filehandle");
3813         const bool have_name = name && SvCUR(name);
3814         Perl_warner(aTHX_ packWARN(warn_type),
3815                    "%s%s on %s %s%s%" SVf, func, pars, vile, type,
3816                     have_name ? " " : "",
3817                     SVfARG(have_name ? name : &PL_sv_no));
3818         if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP))
3819                 Perl_warner(
3820                             aTHX_ packWARN(warn_type),
3821                         "\t(Are you trying to call %s%s on dirhandle%s%" SVf "?)\n",
3822                         func, pars, have_name ? " " : "",
3823                         SVfARG(have_name ? name : &PL_sv_no)
3824                             );
3825     }
3826 }
3827
3828 /* To workaround core dumps from the uninitialised tm_zone we get the
3829  * system to give us a reasonable struct to copy.  This fix means that
3830  * strftime uses the tm_zone and tm_gmtoff values returned by
3831  * localtime(time()). That should give the desired result most of the
3832  * time. But probably not always!
3833  *
3834  * This does not address tzname aspects of NETaa14816.
3835  *
3836  */
3837
3838 #ifdef __GLIBC__
3839 # ifndef STRUCT_TM_HASZONE
3840 #    define STRUCT_TM_HASZONE
3841 # endif
3842 #endif
3843
3844 #ifdef STRUCT_TM_HASZONE /* Backward compat */
3845 # ifndef HAS_TM_TM_ZONE
3846 #    define HAS_TM_TM_ZONE
3847 # endif
3848 #endif
3849
3850 void
3851 Perl_init_tm(pTHX_ struct tm *ptm)      /* see mktime, strftime and asctime */
3852 {
3853 #ifdef HAS_TM_TM_ZONE
3854     Time_t now;
3855     const struct tm* my_tm;
3856     PERL_UNUSED_CONTEXT;
3857     PERL_ARGS_ASSERT_INIT_TM;
3858     (void)time(&now);
3859
3860     LOCALTIME_LOCK;
3861     my_tm = localtime(&now);
3862     if (my_tm)
3863         Copy(my_tm, ptm, 1, struct tm);
3864     LOCALTIME_UNLOCK;
3865 #else
3866     PERL_UNUSED_CONTEXT;
3867     PERL_ARGS_ASSERT_INIT_TM;
3868     PERL_UNUSED_ARG(ptm);
3869 #endif
3870 }
3871
3872 /*
3873 =for apidoc_section $time
3874 =for apidoc mini_mktime
3875 normalise S<C<struct tm>> values without the localtime() semantics (and
3876 overhead) of mktime().
3877
3878 =cut
3879  */
3880 void
3881 Perl_mini_mktime(struct tm *ptm)
3882 {
3883     int yearday;
3884     int secs;
3885     int month, mday, year, jday;
3886     int odd_cent, odd_year;
3887
3888     PERL_ARGS_ASSERT_MINI_MKTIME;
3889
3890 #define DAYS_PER_YEAR   365
3891 #define DAYS_PER_QYEAR  (4*DAYS_PER_YEAR+1)
3892 #define DAYS_PER_CENT   (25*DAYS_PER_QYEAR-1)
3893 #define DAYS_PER_QCENT  (4*DAYS_PER_CENT+1)
3894 #define SECS_PER_HOUR   (60*60)
3895 #define SECS_PER_DAY    (24*SECS_PER_HOUR)
3896 /* parentheses deliberately absent on these two, otherwise they don't work */
3897 #define MONTH_TO_DAYS   153/5
3898 #define DAYS_TO_MONTH   5/153
3899 /* offset to bias by March (month 4) 1st between month/mday & year finding */
3900 #define YEAR_ADJUST     (4*MONTH_TO_DAYS+1)
3901 /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */
3902 #define WEEKDAY_BIAS    6       /* (1+6)%7 makes Sunday 0 again */
3903
3904 /*
3905  * Year/day algorithm notes:
3906  *
3907  * With a suitable offset for numeric value of the month, one can find
3908  * an offset into the year by considering months to have 30.6 (153/5) days,
3909  * using integer arithmetic (i.e., with truncation).  To avoid too much
3910  * messing about with leap days, we consider January and February to be
3911  * the 13th and 14th month of the previous year.  After that transformation,
3912  * we need the month index we use to be high by 1 from 'normal human' usage,
3913  * so the month index values we use run from 4 through 15.
3914  *
3915  * Given that, and the rules for the Gregorian calendar (leap years are those
3916  * divisible by 4 unless also divisible by 100, when they must be divisible
3917  * by 400 instead), we can simply calculate the number of days since some
3918  * arbitrary 'beginning of time' by futzing with the (adjusted) year number,
3919  * the days we derive from our month index, and adding in the day of the
3920  * month.  The value used here is not adjusted for the actual origin which
3921  * it normally would use (1 January A.D. 1), since we're not exposing it.
3922  * We're only building the value so we can turn around and get the
3923  * normalised values for the year, month, day-of-month, and day-of-year.
3924  *
3925  * For going backward, we need to bias the value we're using so that we find
3926  * the right year value.  (Basically, we don't want the contribution of
3927  * March 1st to the number to apply while deriving the year).  Having done
3928  * that, we 'count up' the contribution to the year number by accounting for
3929  * full quadracenturies (400-year periods) with their extra leap days, plus
3930  * the contribution from full centuries (to avoid counting in the lost leap
3931  * days), plus the contribution from full quad-years (to count in the normal
3932  * leap days), plus the leftover contribution from any non-leap years.
3933  * At this point, if we were working with an actual leap day, we'll have 0
3934  * days left over.  This is also true for March 1st, however.  So, we have
3935  * to special-case that result, and (earlier) keep track of the 'odd'
3936  * century and year contributions.  If we got 4 extra centuries in a qcent,
3937  * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb.
3938  * Otherwise, we add back in the earlier bias we removed (the 123 from
3939  * figuring in March 1st), find the month index (integer division by 30.6),
3940  * and the remainder is the day-of-month.  We then have to convert back to
3941  * 'real' months (including fixing January and February from being 14/15 in
3942  * the previous year to being in the proper year).  After that, to get
3943  * tm_yday, we work with the normalised year and get a new yearday value for
3944  * January 1st, which we subtract from the yearday value we had earlier,
3945  * representing the date we've re-built.  This is done from January 1
3946  * because tm_yday is 0-origin.
3947  *
3948  * Since POSIX time routines are only guaranteed to work for times since the
3949  * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm
3950  * applies Gregorian calendar rules even to dates before the 16th century
3951  * doesn't bother me.  Besides, you'd need cultural context for a given
3952  * date to know whether it was Julian or Gregorian calendar, and that's
3953  * outside the scope for this routine.  Since we convert back based on the
3954  * same rules we used to build the yearday, you'll only get strange results
3955  * for input which needed normalising, or for the 'odd' century years which
3956  * were leap years in the Julian calendar but not in the Gregorian one.
3957  * I can live with that.
3958  *
3959  * This algorithm also fails to handle years before A.D. 1 gracefully, but
3960  * that's still outside the scope for POSIX time manipulation, so I don't
3961  * care.
3962  *
3963  * - lwall
3964  */
3965
3966     year = 1900 + ptm->tm_year;
3967     month = ptm->tm_mon;
3968     mday = ptm->tm_mday;
3969     jday = 0;
3970     if (month >= 2)
3971         month+=2;
3972     else
3973         month+=14, year--;
3974     yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400;
3975     yearday += month*MONTH_TO_DAYS + mday + jday;
3976     /*
3977      * Note that we don't know when leap-seconds were or will be,
3978      * so we have to trust the user if we get something which looks
3979      * like a sensible leap-second.  Wild values for seconds will
3980      * be rationalised, however.
3981      */
3982     if ((unsigned) ptm->tm_sec <= 60) {
3983         secs = 0;
3984     }
3985     else {
3986         secs = ptm->tm_sec;
3987         ptm->tm_sec = 0;
3988     }
3989     secs += 60 * ptm->tm_min;
3990     secs += SECS_PER_HOUR * ptm->tm_hour;
3991     if (secs < 0) {
3992         if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) {
3993             /* got negative remainder, but need positive time */
3994             /* back off an extra day to compensate */
3995             yearday += (secs/SECS_PER_DAY)-1;
3996             secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1);
3997         }
3998         else {
3999             yearday += (secs/SECS_PER_DAY);
4000             secs -= SECS_PER_DAY * (secs/SECS_PER_DAY);
4001         }
4002     }
4003     else if (secs >= SECS_PER_DAY) {
4004         yearday += (secs/SECS_PER_DAY);
4005         secs %= SECS_PER_DAY;
4006     }
4007     ptm->tm_hour = secs/SECS_PER_HOUR;
4008     secs %= SECS_PER_HOUR;
4009     ptm->tm_min = secs/60;
4010     secs %= 60;
4011     ptm->tm_sec += secs;
4012     /* done with time of day effects */
4013     /*
4014      * The algorithm for yearday has (so far) left it high by 428.
4015      * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to
4016      * bias it by 123 while trying to figure out what year it
4017      * really represents.  Even with this tweak, the reverse
4018      * translation fails for years before A.D. 0001.
4019      * It would still fail for Feb 29, but we catch that one below.
4020      */
4021     jday = yearday;     /* save for later fixup vis-a-vis Jan 1 */
4022     yearday -= YEAR_ADJUST;
4023     year = (yearday / DAYS_PER_QCENT) * 400;
4024     yearday %= DAYS_PER_QCENT;
4025     odd_cent = yearday / DAYS_PER_CENT;
4026     year += odd_cent * 100;
4027     yearday %= DAYS_PER_CENT;
4028     year += (yearday / DAYS_PER_QYEAR) * 4;
4029     yearday %= DAYS_PER_QYEAR;
4030     odd_year = yearday / DAYS_PER_YEAR;
4031     year += odd_year;
4032     yearday %= DAYS_PER_YEAR;
4033     if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */
4034         month = 1;
4035         yearday = 29;
4036     }
4037     else {
4038         yearday += YEAR_ADJUST; /* recover March 1st crock */
4039         month = yearday*DAYS_TO_MONTH;
4040         yearday -= month*MONTH_TO_DAYS;
4041         /* recover other leap-year adjustment */
4042         if (month > 13) {
4043             month-=14;
4044             year++;
4045         }
4046         else {
4047             month-=2;
4048         }
4049     }
4050     ptm->tm_year = year - 1900;
4051     if (yearday) {
4052       ptm->tm_mday = yearday;
4053       ptm->tm_mon = month;
4054     }
4055     else {
4056       ptm->tm_mday = 31;
4057       ptm->tm_mon = month - 1;
4058     }
4059     /* re-build yearday based on Jan 1 to get tm_yday */
4060     year--;
4061     yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400;
4062     yearday += 14*MONTH_TO_DAYS + 1;
4063     ptm->tm_yday = jday - yearday;
4064     ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7;
4065 }
4066
4067 #define SV_CWD_RETURN_UNDEF \
4068     sv_set_undef(sv); \
4069     return FALSE
4070
4071 #define SV_CWD_ISDOT(dp) \
4072     (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
4073         (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
4074
4075 /*
4076 =for apidoc_section $utility
4077
4078 =for apidoc getcwd_sv
4079
4080 Fill C<sv> with current working directory
4081
4082 =cut
4083 */
4084
4085 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
4086  * rewritten again by dougm, optimized for use with xs TARG, and to prefer
4087  * getcwd(3) if available
4088  * Comments from the original:
4089  *     This is a faster version of getcwd.  It's also more dangerous
4090  *     because you might chdir out of a directory that you can't chdir
4091  *     back into. */
4092
4093 int
4094 Perl_getcwd_sv(pTHX_ SV *sv)
4095 {
4096 #ifndef PERL_MICRO
4097     SvTAINTED_on(sv);
4098
4099     PERL_ARGS_ASSERT_GETCWD_SV;
4100
4101 #ifdef HAS_GETCWD
4102     {
4103         char buf[MAXPATHLEN];
4104
4105         /* Some getcwd()s automatically allocate a buffer of the given
4106          * size from the heap if they are given a NULL buffer pointer.
4107          * The problem is that this behaviour is not portable. */
4108         if (getcwd(buf, sizeof(buf) - 1)) {
4109             sv_setpv(sv, buf);
4110             return TRUE;
4111         }
4112         else {
4113             SV_CWD_RETURN_UNDEF;
4114         }
4115     }
4116
4117 #else
4118
4119     Stat_t statbuf;
4120     int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
4121     int pathlen=0;
4122     Direntry_t *dp;
4123
4124     SvUPGRADE(sv, SVt_PV);
4125
4126     if (PerlLIO_lstat(".", &statbuf) < 0) {
4127         SV_CWD_RETURN_UNDEF;
4128     }
4129
4130     orig_cdev = statbuf.st_dev;
4131     orig_cino = statbuf.st_ino;
4132     cdev = orig_cdev;
4133     cino = orig_cino;
4134
4135     for (;;) {
4136         DIR *dir;
4137         int namelen;
4138         odev = cdev;
4139         oino = cino;
4140
4141         if (PerlDir_chdir("..") < 0) {
4142             SV_CWD_RETURN_UNDEF;
4143         }
4144         if (PerlLIO_stat(".", &statbuf) < 0) {
4145             SV_CWD_RETURN_UNDEF;
4146         }
4147
4148         cdev = statbuf.st_dev;
4149         cino = statbuf.st_ino;
4150
4151         if (odev == cdev && oino == cino) {
4152             break;
4153         }
4154         if (!(dir = PerlDir_open("."))) {
4155             SV_CWD_RETURN_UNDEF;
4156         }
4157
4158         while ((dp = PerlDir_read(dir)) != NULL) {
4159 #ifdef DIRNAMLEN
4160             namelen = dp->d_namlen;
4161 #else
4162             namelen = strlen(dp->d_name);
4163 #endif
4164             /* skip . and .. */
4165             if (SV_CWD_ISDOT(dp)) {
4166                 continue;
4167             }
4168
4169             if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
4170                 SV_CWD_RETURN_UNDEF;
4171             }
4172
4173             tdev = statbuf.st_dev;
4174             tino = statbuf.st_ino;
4175             if (tino == oino && tdev == odev) {
4176                 break;
4177             }
4178         }
4179
4180         if (!dp) {
4181             SV_CWD_RETURN_UNDEF;
4182         }
4183
4184         if (pathlen + namelen + 1 >= MAXPATHLEN) {
4185             SV_CWD_RETURN_UNDEF;
4186         }
4187
4188         SvGROW(sv, pathlen + namelen + 1);
4189
4190         if (pathlen) {
4191             /* shift down */
4192             Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char);
4193         }
4194
4195         /* prepend current directory to the front */
4196         *SvPVX(sv) = '/';
4197         Move(dp->d_name, SvPVX(sv)+1, namelen, char);
4198         pathlen += (namelen + 1);
4199
4200 #ifdef VOID_CLOSEDIR
4201         PerlDir_close(dir);
4202 #else
4203         if (PerlDir_close(dir) < 0) {
4204             SV_CWD_RETURN_UNDEF;
4205         }
4206 #endif
4207     }
4208
4209     if (pathlen) {
4210         SvCUR_set(sv, pathlen);
4211         *SvEND(sv) = '\0';
4212         SvPOK_only(sv);
4213
4214         if (PerlDir_chdir(SvPVX_const(sv)) < 0) {
4215             SV_CWD_RETURN_UNDEF;
4216         }
4217     }
4218     if (PerlLIO_stat(".", &statbuf) < 0) {
4219         SV_CWD_RETURN_UNDEF;
4220     }
4221
4222     cdev = statbuf.st_dev;
4223     cino = statbuf.st_ino;
4224
4225     if (cdev != orig_cdev || cino != orig_cino) {
4226         Perl_croak(aTHX_ "Unstable directory path, "
4227                    "current directory changed unexpectedly");
4228     }
4229
4230     return TRUE;
4231 #endif
4232
4233 #else
4234     return FALSE;
4235 #endif
4236 }
4237
4238 #include "vutil.c"
4239
4240 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT)
4241 #   define EMULATE_SOCKETPAIR_UDP
4242 #endif
4243
4244 #ifdef EMULATE_SOCKETPAIR_UDP
4245 static int
4246 S_socketpair_udp (int fd[2]) {
4247     dTHX;
4248     /* Fake a datagram socketpair using UDP to localhost.  */
4249     int sockets[2] = {-1, -1};
4250     struct sockaddr_in addresses[2];
4251     int i;
4252     Sock_size_t size = sizeof(struct sockaddr_in);
4253     unsigned short port;
4254     int got;
4255
4256     memset(&addresses, 0, sizeof(addresses));
4257     i = 1;
4258     do {
4259         sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET);
4260         if (sockets[i] == -1)
4261             goto tidy_up_and_fail;
4262
4263         addresses[i].sin_family = AF_INET;
4264         addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4265         addresses[i].sin_port = 0;      /* kernel chooses port.  */
4266         if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i],
4267                 sizeof(struct sockaddr_in)) == -1)
4268             goto tidy_up_and_fail;
4269     } while (i--);
4270
4271     /* Now have 2 UDP sockets. Find out which port each is connected to, and
4272        for each connect the other socket to it.  */
4273     i = 1;
4274     do {
4275         if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i],
4276                 &size) == -1)
4277             goto tidy_up_and_fail;
4278         if (size != sizeof(struct sockaddr_in))
4279             goto abort_tidy_up_and_fail;
4280         /* !1 is 0, !0 is 1 */
4281         if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i],
4282                 sizeof(struct sockaddr_in)) == -1)
4283             goto tidy_up_and_fail;
4284     } while (i--);
4285
4286     /* Now we have 2 sockets connected to each other. I don't trust some other
4287        process not to have already sent a packet to us (by random) so send
4288        a packet from each to the other.  */
4289     i = 1;
4290     do {
4291         /* I'm going to send my own port number.  As a short.
4292            (Who knows if someone somewhere has sin_port as a bitfield and needs
4293            this routine. (I'm assuming crays have socketpair)) */
4294         port = addresses[i].sin_port;
4295         got = PerlLIO_write(sockets[i], &port, sizeof(port));
4296         if (got != sizeof(port)) {
4297             if (got == -1)
4298                 goto tidy_up_and_fail;
4299             goto abort_tidy_up_and_fail;
4300         }
4301     } while (i--);
4302
4303     /* Packets sent. I don't trust them to have arrived though.
4304        (As I understand it Solaris TCP stack is multithreaded. Non-blocking
4305        connect to localhost will use a second kernel thread. In 2.6 the
4306        first thread running the connect() returns before the second completes,
4307        so EINPROGRESS> In 2.7 the improved stack is faster and connect()
4308        returns 0. Poor programs have tripped up. One poor program's authors'
4309        had a 50-1 reverse stock split. Not sure how connected these were.)
4310        So I don't trust someone not to have an unpredictable UDP stack.
4311     */
4312
4313     {
4314         struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */
4315         int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0];
4316         fd_set rset;
4317
4318         FD_ZERO(&rset);
4319         FD_SET((unsigned int)sockets[0], &rset);
4320         FD_SET((unsigned int)sockets[1], &rset);
4321
4322         got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor);
4323         if (got != 2 || !FD_ISSET(sockets[0], &rset)
4324                 || !FD_ISSET(sockets[1], &rset)) {
4325             /* I hope this is portable and appropriate.  */
4326             if (got == -1)
4327                 goto tidy_up_and_fail;
4328             goto abort_tidy_up_and_fail;
4329         }
4330     }
4331
4332     /* And the paranoia department even now doesn't trust it to have arrive
4333        (hence MSG_DONTWAIT). Or that what arrives was sent by us.  */
4334     {
4335         struct sockaddr_in readfrom;
4336         unsigned short buffer[2];
4337
4338         i = 1;
4339         do {
4340 #ifdef MSG_DONTWAIT
4341             got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4342                     sizeof(buffer), MSG_DONTWAIT,
4343                     (struct sockaddr *) &readfrom, &size);
4344 #else
4345             got = PerlSock_recvfrom(sockets[i], (char *) &buffer,
4346                     sizeof(buffer), 0,
4347                     (struct sockaddr *) &readfrom, &size);
4348 #endif
4349
4350             if (got == -1)
4351                 goto tidy_up_and_fail;
4352             if (got != sizeof(port)
4353                     || size != sizeof(struct sockaddr_in)
4354                     /* Check other socket sent us its port.  */
4355                     || buffer[0] != (unsigned short) addresses[!i].sin_port
4356                     /* Check kernel says we got the datagram from that socket */
4357                     || readfrom.sin_family != addresses[!i].sin_family
4358                     || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr
4359                     || readfrom.sin_port != addresses[!i].sin_port)
4360                 goto abort_tidy_up_and_fail;
4361         } while (i--);
4362     }
4363     /* My caller (my_socketpair) has validated that this is non-NULL  */
4364     fd[0] = sockets[0];
4365     fd[1] = sockets[1];
4366     /* I hereby declare this connection open.  May God bless all who cross
4367        her.  */
4368     return 0;
4369
4370   abort_tidy_up_and_fail:
4371     errno = ECONNABORTED;
4372   tidy_up_and_fail:
4373     {
4374         dSAVE_ERRNO;
4375         if (sockets[0] != -1)
4376             PerlLIO_close(sockets[0]);
4377         if (sockets[1] != -1)
4378             PerlLIO_close(sockets[1]);
4379         RESTORE_ERRNO;
4380         return -1;
4381     }
4382 }
4383 #endif /*  EMULATE_SOCKETPAIR_UDP */
4384
4385 #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET)
4386
4387 /*
4388 =for apidoc my_socketpair
4389
4390 Emulates L<socketpair(2)> on systems that don't have it, but which do have
4391 enough functionality for the emulation.
4392
4393 =cut
4394 */
4395
4396 int
4397 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4398     /* Stevens says that family must be AF_LOCAL, protocol 0.
4399        I'm going to enforce that, then ignore it, and use TCP (or UDP).  */
4400     dTHXa(NULL);
4401     int listener = -1;
4402     int connector = -1;
4403     int acceptor = -1;
4404     struct sockaddr_in listen_addr;
4405     struct sockaddr_in connect_addr;
4406     Sock_size_t size;
4407
4408     if (protocol
4409 #ifdef AF_UNIX
4410         || family != AF_UNIX
4411 #endif
4412     ) {
4413         errno = EAFNOSUPPORT;
4414         return -1;
4415     }
4416     if (!fd) {
4417         errno = EINVAL;
4418         return -1;
4419     }
4420
4421 #ifdef SOCK_CLOEXEC
4422     type &= ~SOCK_CLOEXEC;
4423 #endif
4424
4425 #ifdef EMULATE_SOCKETPAIR_UDP
4426     if (type == SOCK_DGRAM)
4427         return S_socketpair_udp(fd);
4428 #endif
4429
4430     aTHXa(PERL_GET_THX);
4431     listener = PerlSock_socket(AF_INET, type, 0);
4432     if (listener == -1)
4433         return -1;
4434     memset(&listen_addr, 0, sizeof(listen_addr));
4435     listen_addr.sin_family = AF_INET;
4436     listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4437     listen_addr.sin_port = 0;   /* kernel chooses port.  */
4438     if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr,
4439             sizeof(listen_addr)) == -1)
4440         goto tidy_up_and_fail;
4441     if (PerlSock_listen(listener, 1) == -1)
4442         goto tidy_up_and_fail;
4443
4444     connector = PerlSock_socket(AF_INET, type, 0);
4445     if (connector == -1)
4446         goto tidy_up_and_fail;
4447     /* We want to find out the port number to connect to.  */
4448     size = sizeof(connect_addr);
4449     if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr,
4450             &size) == -1)
4451         goto tidy_up_and_fail;
4452     if (size != sizeof(connect_addr))
4453         goto abort_tidy_up_and_fail;
4454     if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr,
4455             sizeof(connect_addr)) == -1)
4456         goto tidy_up_and_fail;
4457
4458     size = sizeof(listen_addr);
4459     acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr,
4460             &size);
4461     if (acceptor == -1)
4462         goto tidy_up_and_fail;
4463     if (size != sizeof(listen_addr))
4464         goto abort_tidy_up_and_fail;
4465     PerlLIO_close(listener);
4466     /* Now check we are talking to ourself by matching port and host on the
4467        two sockets.  */
4468     if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr,
4469             &size) == -1)
4470         goto tidy_up_and_fail;
4471     if (size != sizeof(connect_addr)
4472             || listen_addr.sin_family != connect_addr.sin_family
4473             || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
4474             || listen_addr.sin_port != connect_addr.sin_port) {
4475         goto abort_tidy_up_and_fail;
4476     }
4477     fd[0] = connector;
4478     fd[1] = acceptor;
4479     return 0;
4480
4481   abort_tidy_up_and_fail:
4482 #ifdef ECONNABORTED
4483   errno = ECONNABORTED; /* This would be the standard thing to do. */
4484 #elif defined(ECONNREFUSED)
4485   errno = ECONNREFUSED; /* some OSes might not have ECONNABORTED. */
4486 #else
4487   errno = ETIMEDOUT;    /* Desperation time. */
4488 #endif
4489   tidy_up_and_fail:
4490     {
4491         dSAVE_ERRNO;
4492         if (listener != -1)
4493             PerlLIO_close(listener);
4494         if (connector != -1)
4495             PerlLIO_close(connector);
4496         if (acceptor != -1)
4497             PerlLIO_close(acceptor);
4498         RESTORE_ERRNO;
4499         return -1;
4500     }
4501 }
4502 #else
4503 /* In any case have a stub so that there's code corresponding
4504  * to the my_socketpair in embed.fnc. */
4505 int
4506 Perl_my_socketpair (int family, int type, int protocol, int fd[2]) {
4507 #ifdef HAS_SOCKETPAIR
4508     return socketpair(family, type, protocol, fd);
4509 #else
4510     return -1;
4511 #endif
4512 }
4513 #endif
4514
4515 /*
4516
4517 =for apidoc sv_nosharing
4518
4519 Dummy routine which "shares" an SV when there is no sharing module present.
4520 Or "locks" it.  Or "unlocks" it.  In other
4521 words, ignores its single SV argument.
4522 Exists to avoid test for a C<NULL> function pointer and because it could
4523 potentially warn under some level of strict-ness.
4524
4525 =cut
4526 */
4527
4528 void
4529 Perl_sv_nosharing(pTHX_ SV *sv)
4530 {
4531     PERL_UNUSED_CONTEXT;
4532     PERL_UNUSED_ARG(sv);
4533 }
4534
4535 /*
4536
4537 =for apidoc sv_destroyable
4538
4539 Dummy routine which reports that object can be destroyed when there is no
4540 sharing module present.  It ignores its single SV argument, and returns
4541 'true'.  Exists to avoid test for a C<NULL> function pointer and because it
4542 could potentially warn under some level of strict-ness.
4543
4544 =cut
4545 */
4546
4547 bool
4548 Perl_sv_destroyable(pTHX_ SV *sv)
4549 {
4550     PERL_UNUSED_CONTEXT;
4551     PERL_UNUSED_ARG(sv);
4552     return TRUE;
4553 }
4554
4555 U32
4556 Perl_parse_unicode_opts(pTHX_ const char **popt)
4557 {
4558   const char *p = *popt;
4559   U32 opt = 0;
4560
4561   PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS;
4562
4563   if (*p) {
4564        if (isDIGIT(*p)) {
4565             const char* endptr = p + strlen(p);
4566             UV uv;
4567             if (grok_atoUV(p, &uv, &endptr) && uv <= U32_MAX) {
4568                 opt = (U32)uv;
4569                 p = endptr;
4570                 if (p && *p && *p != '\n' && *p != '\r') {
4571                     if (isSPACE(*p))
4572                         goto the_end_of_the_opts_parser;
4573                     else
4574                         Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p);
4575                 }
4576             }
4577             else {
4578                 Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p);
4579             }
4580         }
4581         else {
4582             for (; *p; p++) {
4583                  switch (*p) {
4584                  case PERL_UNICODE_STDIN:
4585                       opt |= PERL_UNICODE_STDIN_FLAG;   break;
4586                  case PERL_UNICODE_STDOUT:
4587                       opt |= PERL_UNICODE_STDOUT_FLAG;  break;
4588                  case PERL_UNICODE_STDERR:
4589                       opt |= PERL_UNICODE_STDERR_FLAG;  break;
4590                  case PERL_UNICODE_STD:
4591                       opt |= PERL_UNICODE_STD_FLAG;     break;
4592                  case PERL_UNICODE_IN:
4593                       opt |= PERL_UNICODE_IN_FLAG;      break;
4594                  case PERL_UNICODE_OUT:
4595                       opt |= PERL_UNICODE_OUT_FLAG;     break;
4596                  case PERL_UNICODE_INOUT:
4597                       opt |= PERL_UNICODE_INOUT_FLAG;   break;
4598                  case PERL_UNICODE_LOCALE:
4599                       opt |= PERL_UNICODE_LOCALE_FLAG;  break;
4600                  case PERL_UNICODE_ARGV:
4601                       opt |= PERL_UNICODE_ARGV_FLAG;    break;
4602                  case PERL_UNICODE_UTF8CACHEASSERT:
4603                       opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break;
4604                  default:
4605                       if (*p != '\n' && *p != '\r') {
4606                         if(isSPACE(*p)) goto the_end_of_the_opts_parser;
4607                         else
4608                           Perl_croak(aTHX_
4609                                      "Unknown Unicode option letter '%c'", *p);
4610                       }
4611                  }
4612             }
4613        }
4614   }
4615   else
4616        opt = PERL_UNICODE_DEFAULT_FLAGS;
4617
4618   the_end_of_the_opts_parser:
4619
4620   if (opt & ~PERL_UNICODE_ALL_FLAGS)
4621        Perl_croak(aTHX_ "Unknown Unicode option value %" UVuf,
4622                   (UV) (opt & ~PERL_UNICODE_ALL_FLAGS));
4623
4624   *popt = p;
4625
4626   return opt;
4627 }
4628
4629 #ifdef VMS
4630 #  include <starlet.h>
4631 #endif
4632
4633 U32
4634 Perl_seed(pTHX)
4635 {
4636     /*
4637      * This is really just a quick hack which grabs various garbage
4638      * values.  It really should be a real hash algorithm which
4639      * spreads the effect of every input bit onto every output bit,
4640      * if someone who knows about such things would bother to write it.
4641      * Might be a good idea to add that function to CORE as well.
4642      * No numbers below come from careful analysis or anything here,
4643      * except they are primes and SEED_C1 > 1E6 to get a full-width
4644      * value from (tv_sec * SEED_C1 + tv_usec).  The multipliers should
4645      * probably be bigger too.
4646      */
4647 #if RANDBITS > 16
4648 #  define SEED_C1       1000003
4649 #define   SEED_C4       73819
4650 #else
4651 #  define SEED_C1       25747
4652 #define   SEED_C4       20639
4653 #endif
4654 #define   SEED_C2       3
4655 #define   SEED_C3       269
4656 #define   SEED_C5       26107
4657
4658 #ifndef PERL_NO_DEV_RANDOM
4659     int fd;
4660 #endif
4661     U32 u;
4662 #ifdef HAS_GETTIMEOFDAY
4663     struct timeval when;
4664 #else
4665     Time_t when;
4666 #endif
4667
4668 /* This test is an escape hatch, this symbol isn't set by Configure. */
4669 #ifndef PERL_NO_DEV_RANDOM
4670 #ifndef PERL_RANDOM_DEVICE
4671    /* /dev/random isn't used by default because reads from it will block
4672     * if there isn't enough entropy available.  You can compile with
4673     * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
4674     * is enough real entropy to fill the seed. */
4675 #  ifdef __amigaos4__
4676 #    define PERL_RANDOM_DEVICE "RANDOM:SIZE=4"
4677 #  else
4678 #    define PERL_RANDOM_DEVICE "/dev/urandom"
4679 #  endif
4680 #endif
4681     fd = PerlLIO_open_cloexec(PERL_RANDOM_DEVICE, 0);
4682     if (fd != -1) {
4683         if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u)
4684             u = 0;
4685         PerlLIO_close(fd);
4686         if (u)
4687             return u;
4688     }
4689 #endif
4690
4691 #ifdef HAS_GETTIMEOFDAY
4692     PerlProc_gettimeofday(&when,NULL);
4693     u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
4694 #else
4695     (void)time(&when);
4696     u = (U32)SEED_C1 * when;
4697 #endif
4698     u += SEED_C3 * (U32)PerlProc_getpid();
4699     u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
4700 #ifndef PLAN9           /* XXX Plan9 assembler chokes on this; fix needed  */
4701     u += SEED_C5 * (U32)PTR2UV(&when);
4702 #endif
4703     return u;
4704 }
4705
4706 void
4707 Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer)
4708 {
4709 #ifndef NO_PERL_HASH_ENV
4710     const char *env_pv;
4711 #endif
4712     unsigned long i;
4713
4714     PERL_ARGS_ASSERT_GET_HASH_SEED;
4715
4716     Zero(seed_buffer, PERL_HASH_SEED_BYTES, U8);
4717     Zero((U8*)PL_hash_state_w, PERL_HASH_STATE_BYTES, U8);
4718
4719 #ifndef NO_PERL_HASH_ENV
4720     env_pv= PerlEnv_getenv("PERL_HASH_SEED");
4721
4722     if ( env_pv )
4723     {
4724         if (DEBUG_h_TEST)
4725             PerlIO_printf(Perl_debug_log,"Got PERL_HASH_SEED=<%s>\n", env_pv);
4726         /* ignore leading spaces */
4727         while (isSPACE(*env_pv))
4728             env_pv++;
4729 #    ifdef USE_PERL_PERTURB_KEYS
4730         /* if they set it to "0" we disable key traversal randomization completely */
4731         if (strEQ(env_pv,"0")) {
4732             PL_hash_rand_bits_enabled= 0;
4733         } else {
4734             /* otherwise switch to deterministic mode */
4735             PL_hash_rand_bits_enabled= 2;
4736         }
4737 #    endif
4738         /* ignore a leading 0x... if it is there */
4739         if (env_pv[0] == '0' && env_pv[1] == 'x')
4740             env_pv += 2;
4741
4742         for( i = 0; isXDIGIT(*env_pv) && i < PERL_HASH_SEED_BYTES; i++ ) {
4743             seed_buffer[i] = READ_XDIGIT(env_pv) << 4;
4744             if ( isXDIGIT(*env_pv)) {
4745                 seed_buffer[i] |= READ_XDIGIT(env_pv);
4746             }
4747         }
4748         while (isSPACE(*env_pv))
4749             env_pv++;
4750
4751         if (*env_pv && !isXDIGIT(*env_pv)) {
4752             Perl_warn(aTHX_ "perl: warning: Non hex character in '$ENV{PERL_HASH_SEED}', seed only partially set\n");
4753         }
4754         /* should we check for unparsed crap? */
4755         /* should we warn about unused hex? */
4756         /* should we warn about insufficient hex? */
4757     }
4758     else
4759 #endif /* NO_PERL_HASH_ENV */
4760     {
4761         for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) {
4762             seed_buffer[i] = (unsigned char)(Perl_internal_drand48() * (U8_MAX+1));
4763         }
4764     }
4765 #ifdef USE_PERL_PERTURB_KEYS
4766 #  ifndef NO_PERL_HASH_ENV
4767     env_pv= PerlEnv_getenv("PERL_PERTURB_KEYS");
4768     if (env_pv) {
4769         if (DEBUG_h_TEST)
4770             PerlIO_printf(Perl_debug_log,
4771                 "Got PERL_PERTURB_KEYS=<%s>\n", env_pv);
4772         if (strEQ(env_pv,"0") || strEQ(env_pv,"NO")) {
4773             PL_hash_rand_bits_enabled= 0;
4774         } else if (strEQ(env_pv,"1") || strEQ(env_pv,"RANDOM")) {
4775             PL_hash_rand_bits_enabled= 1;
4776         } else if (strEQ(env_pv,"2") || strEQ(env_pv,"DETERMINISTIC")) {
4777             PL_hash_rand_bits_enabled= 2;
4778         } else {
4779             Perl_warn(aTHX_ "perl: warning: strange setting in '$ENV{PERL_PERTURB_KEYS}': '%s'\n", env_pv);
4780         }
4781     }
4782 #  endif
4783     {   /* initialize PL_hash_rand_bits from the hash seed.
4784          * This value is highly volatile, it is updated every
4785          * hash insert, and is used as part of hash bucket chain
4786          * randomization and hash iterator randomization. */
4787         if (PL_hash_rand_bits_enabled == 1) {
4788             /* random mode initialize from seed() like we would our RNG() */
4789             PL_hash_rand_bits= seed();
4790         }
4791         else {
4792             /* Use a constant */
4793             PL_hash_rand_bits= 0xbe49d17f; /* I just picked a number */
4794             /* and then mix in the leading bytes of the hash seed */
4795             for( i = 0; i < sizeof(UV) ; i++ ) {
4796                 PL_hash_rand_bits ^= seed_buffer[i % PERL_HASH_SEED_BYTES];
4797                 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,8);
4798             }
4799         }
4800         if (!PL_hash_rand_bits) {
4801             /* we use an XORSHIFT RNG to munge PL_hash_rand_bits,
4802              * which means it cannot be 0 or it will stay 0 for the
4803              * lifetime of the process, so if by some insane chance we
4804              * ended up with a 0 after the above initialization
4805              * then set it to this. This really should not happen, or
4806              * very very very rarely.
4807              */
4808             PL_hash_rand_bits = 0x8110ba9d; /* a randomly chosen prime */
4809         }
4810     }
4811 #endif
4812 }
4813
4814 void
4815 Perl_debug_hash_seed(pTHX_ bool via_debug_h)
4816 {
4817     PERL_ARGS_ASSERT_DEBUG_HASH_SEED;
4818 #if (defined(USE_HASH_SEED) || defined(USE_HASH_SEED_DEBUG)) && !defined(NO_PERL_HASH_SEED_DEBUG)
4819     {
4820         const char * const s = PerlEnv_getenv("PERL_HASH_SEED_DEBUG");
4821         bool via_env = cBOOL(s && strNE(s, "0") && strNE(s,""));
4822
4823         if ( via_env != via_debug_h ) {
4824             const unsigned char *seed= PERL_HASH_SEED;
4825             const unsigned char *seed_end= PERL_HASH_SEED + PERL_HASH_SEED_BYTES;
4826             PerlIO_printf(Perl_debug_log, "HASH_FUNCTION = %s HASH_SEED = 0x", PERL_HASH_FUNC);
4827             while (seed < seed_end) {
4828                 PerlIO_printf(Perl_debug_log, "%02x", *seed++);
4829             }
4830 #ifdef PERL_HASH_RANDOMIZE_KEYS
4831             PerlIO_printf(Perl_debug_log, " PERTURB_KEYS = %d (%s)",
4832                     PL_HASH_RAND_BITS_ENABLED,
4833                     PL_HASH_RAND_BITS_ENABLED == 0 ? "NO" :
4834                     PL_HASH_RAND_BITS_ENABLED == 1 ? "RANDOM"
4835                                                    : "DETERMINISTIC");
4836             if (DEBUG_h_TEST)
4837                 PerlIO_printf(Perl_debug_log,
4838                         " RAND_BITS=0x%" UVxf, PL_hash_rand_bits);
4839 #endif
4840             PerlIO_printf(Perl_debug_log, "\n");
4841         }
4842     }
4843 #endif /* #if (defined(USE_HASH_SEED) ... */
4844 }
4845
4846
4847
4848
4849 #ifdef PERL_MEM_LOG
4850
4851 /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including
4852  * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also
4853  * given, and you supply your own implementation.
4854  *
4855  * The default implementation reads a single env var, PERL_MEM_LOG,
4856  * expecting one or more of the following:
4857  *
4858  *    \d+ - fd          fd to write to          : must be 1st (grok_atoUV)
4859  *    'm' - memlog      was PERL_MEM_LOG=1
4860  *    's' - svlog       was PERL_SV_LOG=1
4861  *    't' - timestamp   was PERL_MEM_LOG_TIMESTAMP=1
4862  *
4863  * This makes the logger controllable enough that it can reasonably be
4864  * added to the system perl.
4865  */
4866
4867 /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer
4868  * the Perl_mem_log_...() will use (either via sprintf or snprintf).
4869  */
4870 #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 256
4871
4872 /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...()
4873  * writes to.  In the default logger, this is settable at runtime.
4874  */
4875 #ifndef PERL_MEM_LOG_FD
4876 #  define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */
4877 #endif
4878
4879 #ifndef PERL_MEM_LOG_NOIMPL
4880
4881 # ifdef DEBUG_LEAKING_SCALARS
4882 #   define SV_LOG_SERIAL_FMT        " [%lu]"
4883 #   define _SV_LOG_SERIAL_ARG(sv)   , (unsigned long) (sv)->sv_debug_serial
4884 # else
4885 #   define SV_LOG_SERIAL_FMT
4886 #   define _SV_LOG_SERIAL_ARG(sv)
4887 # endif
4888
4889 static void
4890 S_mem_log_common(enum mem_log_type mlt, const UV n, 
4891                  const UV typesize, const char *type_name, const SV *sv,
4892                  Malloc_t oldalloc, Malloc_t newalloc,
4893                  const char *filename, const int linenumber,
4894                  const char *funcname)
4895 {
4896     const char *pmlenv;
4897     dTHX;
4898
4899     PERL_ARGS_ASSERT_MEM_LOG_COMMON;
4900
4901     PL_mem_log[0] |= 0x2;   /* Flag that the call is from this code */
4902     pmlenv = PerlEnv_getenv("PERL_MEM_LOG");
4903     PL_mem_log[0] &= ~0x2;
4904     if (!pmlenv)
4905         return;
4906     if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s'))
4907     {
4908         /* We can't use SVs or PerlIO for obvious reasons,
4909          * so we'll use stdio and low-level IO instead. */
4910         char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE];
4911
4912 #   ifdef HAS_GETTIMEOFDAY
4913 #     define MEM_LOG_TIME_FMT   "%10d.%06d: "
4914 #     define MEM_LOG_TIME_ARG   (int)tv.tv_sec, (int)tv.tv_usec
4915         struct timeval tv;
4916         PerlProc_gettimeofday(&tv, 0);
4917 #   else
4918 #     define MEM_LOG_TIME_FMT   "%10d: "
4919 #     define MEM_LOG_TIME_ARG   (int)when
4920         Time_t when;
4921         (void)time(&when);
4922 #   endif
4923         /* If there are other OS specific ways of hires time than
4924          * gettimeofday() (see dist/Time-HiRes), the easiest way is
4925          * probably that they would be used to fill in the struct
4926          * timeval. */
4927         {
4928             STRLEN len;
4929             const char* endptr = pmlenv + strlen(pmlenv);
4930             int fd;
4931             UV uv;
4932             if (grok_atoUV(pmlenv, &uv, &endptr) /* Ignore endptr. */
4933                 && uv && uv <= PERL_INT_MAX
4934             ) {
4935                 fd = (int)uv;
4936             } else {
4937                 fd = PERL_MEM_LOG_FD;
4938             }
4939
4940             if (strchr(pmlenv, 't')) {
4941                 len = my_snprintf(buf, sizeof(buf),
4942                                 MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG);
4943                 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4944             }
4945             switch (mlt) {
4946             case MLT_ALLOC:
4947                 len = my_snprintf(buf, sizeof(buf),
4948                         "alloc: %s:%d:%s: %" IVdf " %" UVuf
4949                         " %s = %" IVdf ": %" UVxf "\n",
4950                         filename, linenumber, funcname, n, typesize,
4951                         type_name, n * typesize, PTR2UV(newalloc));
4952                 break;
4953             case MLT_REALLOC:
4954                 len = my_snprintf(buf, sizeof(buf),
4955                         "realloc: %s:%d:%s: %" IVdf " %" UVuf
4956                         " %s = %" IVdf ": %" UVxf " -> %" UVxf "\n",
4957                         filename, linenumber, funcname, n, typesize,
4958                         type_name, n * typesize, PTR2UV(oldalloc),
4959                         PTR2UV(newalloc));
4960                 break;
4961             case MLT_FREE:
4962                 len = my_snprintf(buf, sizeof(buf),
4963                         "free: %s:%d:%s: %" UVxf "\n",
4964                         filename, linenumber, funcname,
4965                         PTR2UV(oldalloc));
4966                 break;
4967             case MLT_NEW_SV:
4968             case MLT_DEL_SV:
4969                 len = my_snprintf(buf, sizeof(buf),
4970                         "%s_SV: %s:%d:%s: %" UVxf SV_LOG_SERIAL_FMT "\n",
4971                         mlt == MLT_NEW_SV ? "new" : "del",
4972                         filename, linenumber, funcname,
4973                         PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv));
4974                 break;
4975             default:
4976                 len = 0;
4977             }
4978             PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4979 #ifdef USE_C_BACKTRACE
4980             if(strchr(pmlenv,'c') && (mlt == MLT_NEW_SV)) {
4981                 len = my_snprintf(buf, sizeof(buf),
4982                         "  caller %s at %s line %" LINE_Tf "\n",
4983                         /* CopSTASHPV can crash early on startup; use CopFILE to check */
4984                         CopFILE(PL_curcop) ? CopSTASHPV(PL_curcop) : "<unknown>",
4985                         CopFILE(PL_curcop), CopLINE(PL_curcop));
4986                 PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
4987
4988                 Perl_c_backtrace *bt = Perl_get_c_backtrace(aTHX_ 3, 3);
4989                 Perl_c_backtrace_frame *frame;
4990                 UV i;
4991                 for (i = 0, frame = bt->frame_info;
4992                         i < bt->header.frame_count;
4993                         i++, frame++) {
4994                     len = my_snprintf(buf, sizeof(buf),
4995                             "  frame[%" UVuf "]: %p %s at %s +0x%lx\n",
4996                             i,
4997                             frame->addr,
4998                             frame->symbol_name_size && frame->symbol_name_offset ? (char *)bt + frame->symbol_name_offset : "-",
4999                             frame->object_name_size && frame->object_name_offset ? (char *)bt + frame->object_name_offset : "?",
5000                             (char *)frame->addr - (char *)frame->object_base_addr);
5001                     PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len));
5002                 }
5003                 Perl_free_c_backtrace(bt);
5004             }
5005 #endif /* USE_C_BACKTRACE */
5006         }
5007     }
5008 }
5009 #endif /* !PERL_MEM_LOG_NOIMPL */
5010
5011 #ifndef PERL_MEM_LOG_NOIMPL
5012 # define \
5013     mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \
5014     mem_log_common   (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm)
5015 #else
5016 /* this is suboptimal, but bug compatible.  User is providing their
5017    own implementation, but is getting these functions anyway, and they
5018    do nothing. But _NOIMPL users should be able to cope or fix */
5019 # define \
5020     mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \
5021     /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */
5022 #endif
5023
5024 Malloc_t
5025 Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name,
5026                    Malloc_t newalloc, 
5027                    const char *filename, const int linenumber,
5028                    const char *funcname)
5029 {
5030     PERL_ARGS_ASSERT_MEM_LOG_ALLOC;
5031
5032     mem_log_common_if(MLT_ALLOC, n, typesize, type_name,
5033                       NULL, NULL, newalloc,
5034                       filename, linenumber, funcname);
5035     return newalloc;
5036 }
5037
5038 Malloc_t
5039 Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name,
5040                      Malloc_t oldalloc, Malloc_t newalloc, 
5041                      const char *filename, const int linenumber, 
5042                      const char *funcname)
5043 {
5044     PERL_ARGS_ASSERT_MEM_LOG_REALLOC;
5045
5046     mem_log_common_if(MLT_REALLOC, n, typesize, type_name,
5047                       NULL, oldalloc, newalloc, 
5048                       filename, linenumber, funcname);
5049     return newalloc;
5050 }
5051
5052 Malloc_t
5053 Perl_mem_log_free(Malloc_t oldalloc, 
5054                   const char *filename, const int linenumber, 
5055                   const char *funcname)
5056 {
5057     PERL_ARGS_ASSERT_MEM_LOG_FREE;
5058
5059     mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL, 
5060                       filename, linenumber, funcname);
5061     return oldalloc;
5062 }
5063
5064 void
5065 Perl_mem_log_new_sv(const SV *sv, 
5066                     const char *filename, const int linenumber,
5067                     const char *funcname)
5068 {
5069     PERL_ARGS_ASSERT_MEM_LOG_NEW_SV;
5070
5071     mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL,
5072                       filename, linenumber, funcname);
5073 }
5074
5075 void
5076 Perl_mem_log_del_sv(const SV *sv,
5077                     const char *filename, const int linenumber, 
5078                     const char *funcname)
5079 {
5080     PERL_ARGS_ASSERT_MEM_LOG_DEL_SV;
5081
5082     mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL, 
5083                       filename, linenumber, funcname);
5084 }
5085
5086 #endif /* PERL_MEM_LOG */
5087
5088 /*
5089 =for apidoc_section $string
5090 =for apidoc quadmath_format_valid
5091
5092 C<quadmath_snprintf()> is very strict about its C<format> string and will
5093 fail, returning -1, if the format is invalid.  It accepts exactly
5094 one format spec.
5095
5096 C<quadmath_format_valid()> checks that the intended single spec looks
5097 sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>,
5098 and has C<Q> before it.  This is not a full "printf syntax check",
5099 just the basics.
5100
5101 Returns true if it is valid, false if not.
5102
5103 See also L</quadmath_format_needed>.
5104
5105 =cut
5106 */
5107 #ifdef USE_QUADMATH
5108 bool
5109 Perl_quadmath_format_valid(const char* format)
5110 {
5111     STRLEN len;
5112
5113     PERL_ARGS_ASSERT_QUADMATH_FORMAT_VALID;
5114
5115     if (format[0] != '%' || strchr(format + 1, '%'))
5116         return FALSE;
5117     len = strlen(format);
5118     /* minimum length three: %Qg */
5119     if (len < 3 || memCHRs("efgaEFGA", format[len - 1]) == NULL)
5120         return FALSE;
5121     if (format[len - 2] != 'Q')
5122         return FALSE;
5123     return TRUE;
5124 }
5125 #endif
5126
5127 /*
5128 =for apidoc quadmath_format_needed
5129
5130 C<quadmath_format_needed()> returns true if the C<format> string seems to
5131 contain at least one non-Q-prefixed C<%[efgaEFGA]> format specifier,
5132 or returns false otherwise.
5133
5134 The format specifier detection is not complete printf-syntax detection,
5135 but it should catch most common cases.
5136
5137 If true is returned, those arguments B<should> in theory be processed
5138 with C<quadmath_snprintf()>, but in case there is more than one such
5139 format specifier (see L</quadmath_format_valid>), and if there is
5140 anything else beyond that one (even just a single byte), they
5141 B<cannot> be processed because C<quadmath_snprintf()> is very strict,
5142 accepting only one format spec, and nothing else.
5143 In this case, the code should probably fail.
5144
5145 =cut
5146 */
5147 #ifdef USE_QUADMATH
5148 bool
5149 Perl_quadmath_format_needed(const char* format)
5150 {
5151   const char *p = format;
5152   const char *q;
5153
5154   PERL_ARGS_ASSERT_QUADMATH_FORMAT_NEEDED;
5155
5156   while ((q = strchr(p, '%'))) {
5157     q++;
5158     if (*q == '+') /* plus */
5159       q++;
5160     if (*q == '#') /* alt */
5161       q++;
5162     if (*q == '*') /* width */
5163       q++;
5164     else {
5165       if (isDIGIT(*q)) {
5166         while (isDIGIT(*q)) q++;
5167       }
5168     }
5169     if (*q == '.' && (q[1] == '*' || isDIGIT(q[1]))) { /* prec */
5170       q++;
5171       if (*q == '*')
5172         q++;
5173       else
5174         while (isDIGIT(*q)) q++;
5175     }
5176     if (memCHRs("efgaEFGA", *q)) /* Would have needed 'Q' in front. */
5177       return TRUE;
5178     p = q + 1;
5179   }
5180   return FALSE;
5181 }
5182 #endif
5183
5184 /*
5185 =for apidoc my_snprintf
5186
5187 The C library C<snprintf> functionality, if available and
5188 standards-compliant (uses C<vsnprintf>, actually).  However, if the
5189 C<vsnprintf> is not available, will unfortunately use the unsafe
5190 C<vsprintf> which can overrun the buffer (there is an overrun check,
5191 but that may be too late).  Consider using C<sv_vcatpvf> instead, or
5192 getting C<vsnprintf>.
5193
5194 =cut
5195 */
5196 int
5197 Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...)
5198 {
5199     int retval = -1;
5200     va_list ap;
5201     PERL_ARGS_ASSERT_MY_SNPRINTF;
5202 #ifndef HAS_VSNPRINTF
5203     PERL_UNUSED_VAR(len);
5204 #endif
5205     va_start(ap, format);
5206 #ifdef USE_QUADMATH
5207     {
5208         bool quadmath_valid = FALSE;
5209         if (quadmath_format_valid(format)) {
5210             /* If the format looked promising, use it as quadmath. */
5211             retval = quadmath_snprintf(buffer, len, format, va_arg(ap, NV));
5212             if (retval == -1) {
5213                 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5214             }
5215             quadmath_valid = TRUE;
5216         }
5217         /* quadmath_format_single() will return false for example for
5218          * "foo = %g", or simply "%g".  We could handle the %g by
5219          * using quadmath for the NV args.  More complex cases of
5220          * course exist: "foo = %g, bar = %g", or "foo=%Qg" (otherwise
5221          * quadmath-valid but has stuff in front).
5222          *
5223          * Handling the "Q-less" cases right would require walking
5224          * through the va_list and rewriting the format, calling
5225          * quadmath for the NVs, building a new va_list, and then
5226          * letting vsnprintf/vsprintf to take care of the other
5227          * arguments.  This may be doable.
5228          *
5229          * We do not attempt that now.  But for paranoia, we here try
5230          * to detect some common (but not all) cases where the
5231          * "Q-less" %[efgaEFGA] formats are present, and die if
5232          * detected.  This doesn't fix the problem, but it stops the
5233          * vsnprintf/vsprintf pulling doubles off the va_list when
5234          * __float128 NVs should be pulled off instead.
5235          *
5236          * If quadmath_format_needed() returns false, we are reasonably
5237          * certain that we can call vnsprintf() or vsprintf() safely. */
5238         if (!quadmath_valid && quadmath_format_needed(format))
5239           Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format);
5240
5241     }
5242 #endif
5243     if (retval == -1)
5244 #ifdef HAS_VSNPRINTF
5245         retval = vsnprintf(buffer, len, format, ap);
5246 #else
5247         retval = vsprintf(buffer, format, ap);
5248 #endif
5249     va_end(ap);
5250     /* vsprintf() shows failure with < 0 */
5251     if (retval < 0
5252 #ifdef HAS_VSNPRINTF
5253     /* vsnprintf() shows failure with >= len */
5254         ||
5255         (len > 0 && (Size_t)retval >= len)
5256 #endif
5257     )
5258         Perl_croak_nocontext("panic: my_snprintf buffer overflow");
5259     return retval;
5260 }
5261
5262 /*
5263 =for apidoc my_vsnprintf
5264
5265 The C library C<vsnprintf> if available and standards-compliant.
5266 However, if the C<vsnprintf> is not available, will unfortunately
5267 use the unsafe C<vsprintf> which can overrun the buffer (there is an
5268 overrun check, but that may be too late).  Consider using
5269 C<sv_vcatpvf> instead, or getting C<vsnprintf>.
5270
5271 =cut
5272 */
5273 int
5274 Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap)
5275 {
5276 #ifdef USE_QUADMATH
5277     PERL_UNUSED_ARG(buffer);
5278     PERL_UNUSED_ARG(len);
5279     PERL_UNUSED_ARG(format);
5280     /* the cast is to avoid gcc -Wsizeof-array-argument complaining */
5281     PERL_UNUSED_ARG((void*)ap);
5282     Perl_croak_nocontext("panic: my_vsnprintf not available with quadmath");
5283     return 0;
5284 #else
5285     int retval;
5286 #ifdef NEED_VA_COPY
5287     va_list apc;
5288
5289     PERL_ARGS_ASSERT_MY_VSNPRINTF;
5290     Perl_va_copy(ap, apc);
5291 # ifdef HAS_VSNPRINTF
5292     retval = vsnprintf(buffer, len, format, apc);
5293 # else
5294     PERL_UNUSED_ARG(len);
5295     retval = vsprintf(buffer, format, apc);
5296 # endif
5297     va_end(apc);
5298 #else
5299 # ifdef HAS_VSNPRINTF
5300     retval = vsnprintf(buffer, len, format, ap);
5301 # else
5302     PERL_UNUSED_ARG(len);
5303     retval = vsprintf(buffer, format, ap);
5304 # endif
5305 #endif /* #ifdef NEED_VA_COPY */
5306     /* vsprintf() shows failure with < 0 */
5307     if (retval < 0
5308 #ifdef HAS_VSNPRINTF
5309     /* vsnprintf() shows failure with >= len */
5310         ||
5311         (len > 0 && (Size_t)retval >= len)
5312 #endif
5313     )
5314         Perl_croak_nocontext("panic: my_vsnprintf buffer overflow");
5315     return retval;
5316 #endif
5317 }
5318
5319 void
5320 Perl_my_clearenv(pTHX)
5321 {
5322 #if ! defined(PERL_MICRO)
5323 #  if defined(PERL_IMPLICIT_SYS) || defined(WIN32)
5324     PerlEnv_clearenv();
5325 #  else /* ! (PERL_IMPLICIT_SYS || WIN32) */
5326 #    if defined(USE_ENVIRON_ARRAY)
5327 #      if defined(USE_ITHREADS)
5328     /* only the parent thread can clobber the process environment, so no need
5329      * to use a mutex */
5330     if (PL_curinterp != aTHX)
5331         return;
5332 #      endif /* USE_ITHREADS */
5333 #      if defined(HAS_CLEARENV)
5334     clearenv();
5335 #      elif defined(HAS_UNSETENV)
5336     int bsiz = 80; /* Most envvar names will be shorter than this. */
5337     char *buf = (char*)safesysmalloc(bsiz);
5338     while (*environ != NULL) {
5339         char *e = strchr(*environ, '=');
5340         int l = e ? e - *environ : (int)strlen(*environ);
5341         if (bsiz < l + 1) {
5342             safesysfree(buf);
5343             bsiz = l + 1; /* + 1 for the \0. */
5344             buf = (char*)safesysmalloc(bsiz);
5345         }
5346         memcpy(buf, *environ, l);
5347         buf[l] = '\0';
5348         unsetenv(buf);
5349     }
5350     safesysfree(buf);
5351 #      else /* ! HAS_CLEARENV && ! HAS_UNSETENV */
5352     /* Just null environ and accept the leakage. */
5353     *environ = NULL;
5354 #      endif /* HAS_CLEARENV || HAS_UNSETENV */
5355 #    endif /* USE_ENVIRON_ARRAY */
5356 #  endif /* PERL_IMPLICIT_SYS || WIN32 */
5357 #endif /* PERL_MICRO */
5358 }
5359
5360 #ifdef MULTIPLICITY
5361
5362 /*
5363 =for apidoc my_cxt_init
5364
5365 Implements the L<perlxs/C<MY_CXT_INIT>> macro, which you should use instead.
5366
5367 The first time a module is loaded, the global C<PL_my_cxt_index> is incremented,
5368 and that value is assigned to that module's static C<my_cxt_index> (whose
5369 address is passed as an arg).  Then, for each interpreter this function is
5370 called for, it makes sure a C<void*> slot is available to hang the static data
5371 off, by allocating or extending the interpreter's C<PL_my_cxt_list> array
5372
5373 =cut
5374 */
5375
5376 void *
5377 Perl_my_cxt_init(pTHX_ int *indexp, size_t size)
5378 {
5379     void *p;
5380     int index;
5381
5382     PERL_ARGS_ASSERT_MY_CXT_INIT;
5383
5384     index = *indexp;
5385     /* do initial check without locking.
5386      * -1:    not allocated or another thread currently allocating
5387      *  other: already allocated by another thread
5388      */
5389     if (index == -1) {
5390         MUTEX_LOCK(&PL_my_ctx_mutex);
5391         /*now a stricter check with locking */
5392         index = *indexp;
5393         if (index == -1)
5394             /* this module hasn't been allocated an index yet */
5395             *indexp = PL_my_cxt_index++;
5396         index = *indexp;
5397         MUTEX_UNLOCK(&PL_my_ctx_mutex);
5398     }
5399
5400     /* make sure the array is big enough */
5401     if (PL_my_cxt_size <= index) {
5402         if (PL_my_cxt_size) {
5403             IV new_size = PL_my_cxt_size;
5404             while (new_size <= index)
5405                 new_size *= 2;
5406             Renew(PL_my_cxt_list, new_size, void *);
5407             PL_my_cxt_size = new_size;
5408         }
5409         else {
5410             PL_my_cxt_size = 16;
5411             Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
5412         }
5413     }
5414     /* newSV() allocates one more than needed */
5415     p = (void*)SvPVX(newSV(size-1));
5416     PL_my_cxt_list[index] = p;
5417     Zero(p, size, char);
5418     return p;
5419 }
5420
5421 #endif /* MULTIPLICITY */
5422
5423
5424 /* Perl_xs_handshake():
5425    implement the various XS_*_BOOTCHECK macros, which are added to .c
5426    files by ExtUtils::ParseXS, to check that the perl the module was built
5427    with is binary compatible with the running perl.
5428
5429    usage:
5430        Perl_xs_handshake(U32 key, void * v_my_perl, const char * file,
5431             [U32 items, U32 ax], [char * api_version], [char * xs_version])
5432
5433    The meaning of the varargs is determined the U32 key arg (which is not
5434    a format string). The fields of key are assembled by using HS_KEY().
5435
5436    Under PERL_IMPLICIT_CONTEX, the v_my_perl arg is of type
5437    "PerlInterpreter *" and represents the callers context; otherwise it is
5438    of type "CV *", and is the boot xsub's CV.
5439
5440    v_my_perl will catch where a threaded future perl526.dll calling IO.dll
5441    for example, and IO.dll was linked with threaded perl524.dll, and both
5442    perl526.dll and perl524.dll are in %PATH and the Win32 DLL loader
5443    successfully can load IO.dll into the process but simultaneously it
5444    loaded an interpreter of a different version into the process, and XS
5445    code will naturally pass SV*s created by perl524.dll for perl526.dll to
5446    use through perl526.dll's my_perl->Istack_base.
5447
5448    v_my_perl cannot be the first arg, since then 'key' will be out of
5449    place in a threaded vs non-threaded mixup; and analyzing the key
5450    number's bitfields won't reveal the problem, since it will be a valid
5451    key (unthreaded perl) on interp side, but croak will report the XS mod's
5452    key as gibberish (it is really a my_perl ptr) (threaded XS mod); or if
5453    it's a threaded perl and an unthreaded XS module, threaded perl will
5454    look at an uninit C stack or an uninit register to get 'key'
5455    (remember that it assumes that the 1st arg is the interp cxt).
5456
5457    'file' is the source filename of the caller.
5458 */
5459
5460 I32
5461 Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...)
5462 {
5463     va_list args;
5464     U32 items, ax;
5465     void * got;
5466     void * need;
5467     const char *stage = "first";
5468 #ifdef MULTIPLICITY
5469     dTHX;
5470     tTHX xs_interp;
5471 #else
5472     CV* cv;
5473     SV *** xs_spp;
5474 #endif
5475     PERL_ARGS_ASSERT_XS_HANDSHAKE;
5476     va_start(args, file);
5477
5478     got = INT2PTR(void*, (UV)(key & HSm_KEY_MATCH));
5479     need = (void *)(HS_KEY(FALSE, FALSE, "", "") & HSm_KEY_MATCH);
5480     if (UNLIKELY(got != need))
5481         goto bad_handshake;
5482 /* try to catch where a 2nd threaded perl interp DLL is loaded into a process
5483    by a XS DLL compiled against the wrong interl DLL b/c of bad @INC, and the
5484    2nd threaded perl interp DLL never initialized its TLS/PERL_SYS_INIT3 so
5485    dTHX call from 2nd interp DLL can't return the my_perl that pp_entersub
5486    passed to the XS DLL */
5487 #ifdef MULTIPLICITY
5488     xs_interp = (tTHX)v_my_perl;
5489     got = xs_interp;
5490     need = my_perl;
5491 #else
5492 /* try to catch where an unthreaded perl interp DLL (for ex. perl522.dll) is
5493    loaded into a process by a XS DLL built by an unthreaded perl522.dll perl,
5494    but the DynaLoder/Perl that started the process and loaded the XS DLL is
5495    unthreaded perl524.dll, since unthreadeds don't pass my_perl (a unique *)
5496    through pp_entersub, use a unique value (which is a pointer to PL_stack_sp's
5497    location in the unthreaded perl binary) stored in CV * to figure out if this
5498    Perl_xs_handshake was called by the same pp_entersub */
5499     cv = (CV*)v_my_perl;
5500     xs_spp = (SV***)CvHSCXT(cv);
5501     got = xs_spp;
5502     need = &PL_stack_sp;
5503 #endif
5504     stage = "second";
5505     if(UNLIKELY(got != need)) {
5506         bad_handshake:/* recycle branch and string from above */
5507         if(got != (void *)HSf_NOCHK)
5508             noperl_die("%s: loadable library and perl binaries are mismatched"
5509                        " (got %s handshake key %p, needed %p)\n",
5510                        file, stage, got, need);
5511     }
5512
5513     if(key & HSf_SETXSUBFN) {     /* this might be called from a module bootstrap */
5514         SAVEPPTR(PL_xsubfilename);/* which was require'd from a XSUB BEGIN */
5515         PL_xsubfilename = file;   /* so the old name must be restored for
5516                                      additional XSUBs to register themselves */
5517         /* XSUBs can't be perl lang/perl5db.pl debugged
5518         if (PERLDB_LINE_OR_SAVESRC)
5519             (void)gv_fetchfile(file); */
5520     }
5521
5522     if(key & HSf_POPMARK) {
5523         ax = POPMARK;
5524         {   SV **mark = PL_stack_base + ax++;
5525             {   dSP;
5526                 items = (I32)(SP - MARK);
5527             }
5528         }
5529     } else {
5530         items = va_arg(args, U32);
5531         ax = va_arg(args, U32);
5532     }
5533     {
5534         U32 apiverlen;
5535         assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX);
5536         if((apiverlen = HS_GETAPIVERLEN(key))) {
5537             char * api_p = va_arg(args, char*);
5538             if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1
5539                 || memNE(api_p, "v" PERL_API_VERSION_STRING,
5540                          sizeof("v" PERL_API_VERSION_STRING)-1))
5541                 Perl_croak_nocontext("Perl API version %s of %" SVf " does not match %s",
5542                                     api_p, SVfARG(PL_stack_base[ax + 0]),
5543                                     "v" PERL_API_VERSION_STRING);
5544         }
5545     }
5546     {
5547         U32 xsverlen;
5548         assert(HS_GETXSVERLEN(key) <= UCHAR_MAX && HS_GETXSVERLEN(key) <= HS_APIVERLEN_MAX);
5549         if((xsverlen = HS_GETXSVERLEN(key)))
5550             S_xs_version_bootcheck(aTHX_
5551                 items, ax, va_arg(args, char*), xsverlen);
5552     }
5553     va_end(args);
5554     return ax;
5555 }
5556
5557
5558 STATIC void
5559 S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p,
5560                           STRLEN xs_len)
5561 {
5562     SV *sv;
5563     const char *vn = NULL;
5564     SV *const module = PL_stack_base[ax];
5565
5566     PERL_ARGS_ASSERT_XS_VERSION_BOOTCHECK;
5567
5568     if (items >= 2)      /* version supplied as bootstrap arg */
5569         sv = PL_stack_base[ax + 1];
5570     else {
5571         /* XXX GV_ADDWARN */
5572         vn = "XS_VERSION";
5573         sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5574         if (!sv || !SvOK(sv)) {
5575             vn = "VERSION";
5576             sv = get_sv(Perl_form(aTHX_ "%" SVf "::%s", SVfARG(module), vn), 0);
5577         }
5578     }
5579     if (sv) {
5580         SV *xssv = Perl_newSVpvn_flags(aTHX_ xs_p, xs_len, SVs_TEMP);
5581         SV *pmsv = sv_isobject(sv) && sv_derived_from(sv, "version")
5582             ? sv : sv_2mortal(new_version(sv));
5583         xssv = upg_version(xssv, 0);
5584         if ( vcmp(pmsv,xssv) ) {
5585             SV *string = vstringify(xssv);
5586             SV *xpt = Perl_newSVpvf(aTHX_ "%" SVf " object version %" SVf
5587                                     " does not match ", SVfARG(module), SVfARG(string));
5588
5589             SvREFCNT_dec(string);
5590             string = vstringify(pmsv);
5591
5592             if (vn) {
5593                 Perl_sv_catpvf(aTHX_ xpt, "$%" SVf "::%s %" SVf, SVfARG(module), vn,
5594                                SVfARG(string));
5595             } else {
5596                 Perl_sv_catpvf(aTHX_ xpt, "bootstrap parameter %" SVf, SVfARG(string));
5597             }
5598             SvREFCNT_dec(string);
5599
5600             Perl_sv_2mortal(aTHX_ xpt);
5601             Perl_croak_sv(aTHX_ xpt);
5602         }
5603     }
5604 }
5605
5606 PERL_STATIC_INLINE bool
5607 S_gv_has_usable_name(pTHX_ GV *gv)
5608 {
5609     GV **gvp;
5610     return GvSTASH(gv)
5611         && HvHasENAME(GvSTASH(gv))
5612         && (gvp = (GV **)hv_fetchhek(
5613                         GvSTASH(gv), GvNAME_HEK(gv), 0
5614            ))
5615         && *gvp == gv;
5616 }
5617
5618 void
5619 Perl_get_db_sub(pTHX_ SV **svp, CV *cv)
5620 {
5621     SV * const dbsv = GvSVn(PL_DBsub);
5622     const bool save_taint = TAINT_get;
5623
5624     /* When we are called from pp_goto (svp is null),
5625      * we do not care about using dbsv to call CV;
5626      * it's for informational purposes only.
5627      */
5628
5629     PERL_ARGS_ASSERT_GET_DB_SUB;
5630
5631     TAINT_set(FALSE);
5632     save_item(dbsv);
5633     if (!PERLDB_SUB_NN) {
5634         GV *gv = CvGV(cv);
5635
5636         if (!svp && !CvLEXICAL(cv)) {
5637             gv_efullname3(dbsv, gv, NULL);
5638         }
5639         else if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) || CvLEXICAL(cv)
5640              || strEQ(GvNAME(gv), "END")
5641              || ( /* Could be imported, and old sub redefined. */
5642                  (GvCV(gv) != cv || !S_gv_has_usable_name(aTHX_ gv))
5643                  &&
5644                  !( (SvTYPE(*svp) == SVt_PVGV)
5645                     && (GvCV((const GV *)*svp) == cv)
5646                     /* Use GV from the stack as a fallback. */
5647                     && S_gv_has_usable_name(aTHX_ gv = (GV *)*svp) 
5648                   )
5649                 )
5650         ) {
5651             /* GV is potentially non-unique, or contain different CV. */
5652             SV * const tmp = newRV(MUTABLE_SV(cv));
5653             sv_setsv(dbsv, tmp);
5654             SvREFCNT_dec(tmp);
5655         }
5656         else {
5657             sv_sethek(dbsv, HvENAME_HEK(GvSTASH(gv)));
5658             sv_catpvs(dbsv, "::");
5659             sv_cathek(dbsv, GvNAME_HEK(gv));
5660         }
5661     }
5662     else {
5663         const int type = SvTYPE(dbsv);
5664         if (type < SVt_PVIV && type != SVt_IV)
5665             sv_upgrade(dbsv, SVt_PVIV);
5666         (void)SvIOK_on(dbsv);
5667         SvIV_set(dbsv, PTR2IV(cv));     /* Do it the quickest way  */
5668     }
5669     SvSETMAGIC(dbsv);
5670     TAINT_IF(save_taint);
5671 #ifdef NO_TAINT_SUPPORT
5672     PERL_UNUSED_VAR(save_taint);
5673 #endif
5674 }
5675
5676 /*
5677 =for apidoc_section $io
5678 =for apidoc my_dirfd
5679
5680 The C library C<L<dirfd(3)>> if available, or a Perl implementation of it, or die
5681 if not easily emulatable.
5682
5683 =cut
5684 */
5685
5686 int
5687 Perl_my_dirfd(DIR * dir) {
5688
5689     /* Most dirfd implementations have problems when passed NULL. */
5690     if(!dir)
5691         return -1;
5692 #ifdef HAS_DIRFD
5693     return dirfd(dir);
5694 #elif defined(HAS_DIR_DD_FD)
5695     return dir->dd_fd;
5696 #else
5697     Perl_croak_nocontext(PL_no_func, "dirfd");
5698     NOT_REACHED; /* NOTREACHED */
5699     return 0;
5700 #endif 
5701 }
5702
5703 #if !defined(HAS_MKOSTEMP) || !defined(HAS_MKSTEMP)
5704
5705 #define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
5706 #define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
5707
5708 static int
5709 S_my_mkostemp(char *templte, int flags) {
5710     dTHX;
5711     STRLEN len = strlen(templte);
5712     int fd;
5713     int attempts = 0;
5714 #ifdef VMS
5715     int delete_on_close = flags & O_VMS_DELETEONCLOSE;
5716
5717     flags &= ~O_VMS_DELETEONCLOSE;
5718 #endif
5719
5720     if (len < 6 ||
5721         templte[len-1] != 'X' || templte[len-2] != 'X' || templte[len-3] != 'X' ||
5722         templte[len-4] != 'X' || templte[len-5] != 'X' || templte[len-6] != 'X') {
5723         SETERRNO(EINVAL, LIB_INVARG);
5724         return -1;
5725     }
5726
5727     do {
5728         int i;
5729         for (i = 1; i <= 6; ++i) {
5730             templte[len-i] = TEMP_FILE_CH[(int)(Perl_internal_drand48() * TEMP_FILE_CH_COUNT)];
5731         }
5732 #ifdef VMS
5733         if (delete_on_close) {
5734             fd = open(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600, "fop=dlt");
5735         }
5736         else
5737 #endif
5738         {
5739             fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
5740         }
5741     } while (fd == -1 && errno == EEXIST && ++attempts <= 100);
5742
5743     return fd;
5744 }
5745
5746 #endif
5747
5748 #ifndef HAS_MKOSTEMP
5749
5750 /*
5751 =for apidoc my_mkostemp
5752
5753 The C library C<L<mkostemp(3)>> if available, or a Perl implementation of it.
5754
5755 =cut
5756 */
5757
5758 int
5759 Perl_my_mkostemp(char *templte, int flags)
5760 {
5761     PERL_ARGS_ASSERT_MY_MKOSTEMP;
5762     return S_my_mkostemp(templte, flags);
5763 }
5764 #endif
5765
5766 #ifndef HAS_MKSTEMP
5767
5768 /*
5769 =for apidoc my_mkstemp
5770
5771 The C library C<L<mkstemp(3)>> if available, or a Perl implementation of it.
5772
5773 =cut
5774 */
5775
5776 int
5777 Perl_my_mkstemp(char *templte)
5778 {
5779     PERL_ARGS_ASSERT_MY_MKSTEMP;
5780     return S_my_mkostemp(templte, 0);
5781 }
5782 #endif
5783
5784 REGEXP *
5785 Perl_get_re_arg(pTHX_ SV *sv) {
5786
5787     if (sv) {
5788         if (SvMAGICAL(sv))
5789             mg_get(sv);
5790         if (SvROK(sv))
5791             sv = MUTABLE_SV(SvRV(sv));
5792         if (SvTYPE(sv) == SVt_REGEXP)
5793             return (REGEXP*) sv;
5794     }
5795  
5796     return NULL;
5797 }
5798
5799 /*
5800  * This code is derived from drand48() implementation from FreeBSD,
5801  * found in lib/libc/gen/_rand48.c.
5802  *
5803  * The U64 implementation is original, based on the POSIX
5804  * specification for drand48().
5805  */
5806
5807 /*
5808 * Copyright (c) 1993 Martin Birgmeier
5809 * All rights reserved.
5810 *
5811 * You may redistribute unmodified or modified versions of this source
5812 * code provided that the above copyright notice and this and the
5813 * following conditions are retained.
5814 *
5815 * This software is provided ``as is'', and comes with no warranties
5816 * of any kind. I shall in no event be liable for anything that happens
5817 * to anyone/anything when using this software.
5818 */
5819
5820 #define FREEBSD_DRAND48_SEED_0   (0x330e)
5821
5822 #ifdef PERL_DRAND48_QUAD
5823
5824 #define DRAND48_MULT UINT64_C(0x5deece66d)
5825 #define DRAND48_ADD  0xb
5826 #define DRAND48_MASK UINT64_C(0xffffffffffff)
5827
5828 #else
5829
5830 #define FREEBSD_DRAND48_SEED_1   (0xabcd)
5831 #define FREEBSD_DRAND48_SEED_2   (0x1234)
5832 #define FREEBSD_DRAND48_MULT_0   (0xe66d)
5833 #define FREEBSD_DRAND48_MULT_1   (0xdeec)
5834 #define FREEBSD_DRAND48_MULT_2   (0x0005)
5835 #define FREEBSD_DRAND48_ADD      (0x000b)
5836
5837 const unsigned short _rand48_mult[3] = {
5838                 FREEBSD_DRAND48_MULT_0,
5839                 FREEBSD_DRAND48_MULT_1,
5840                 FREEBSD_DRAND48_MULT_2
5841 };
5842 const unsigned short _rand48_add = FREEBSD_DRAND48_ADD;
5843
5844 #endif
5845
5846 void
5847 Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed)
5848 {
5849     PERL_ARGS_ASSERT_DRAND48_INIT_R;
5850
5851 #ifdef PERL_DRAND48_QUAD
5852     *random_state = FREEBSD_DRAND48_SEED_0 + ((U64)seed << 16);
5853 #else
5854     random_state->seed[0] = FREEBSD_DRAND48_SEED_0;
5855     random_state->seed[1] = (U16) seed;
5856     random_state->seed[2] = (U16) (seed >> 16);
5857 #endif
5858 }
5859
5860 double
5861 Perl_drand48_r(perl_drand48_t *random_state)
5862 {
5863     PERL_ARGS_ASSERT_DRAND48_R;
5864
5865 #ifdef PERL_DRAND48_QUAD
5866     *random_state = (*random_state * DRAND48_MULT + DRAND48_ADD)
5867         & DRAND48_MASK;
5868
5869     return ldexp((double)*random_state, -48);
5870 #else
5871     {
5872     U32 accu;
5873     U16 temp[2];
5874
5875     accu = (U32) _rand48_mult[0] * (U32) random_state->seed[0]
5876          + (U32) _rand48_add;
5877     temp[0] = (U16) accu;        /* lower 16 bits */
5878     accu >>= sizeof(U16) * 8;
5879     accu += (U32) _rand48_mult[0] * (U32) random_state->seed[1]
5880           + (U32) _rand48_mult[1] * (U32) random_state->seed[0];
5881     temp[1] = (U16) accu;        /* middle 16 bits */
5882     accu >>= sizeof(U16) * 8;
5883     accu += _rand48_mult[0] * random_state->seed[2]
5884           + _rand48_mult[1] * random_state->seed[1]
5885           + _rand48_mult[2] * random_state->seed[0];
5886     random_state->seed[0] = temp[0];
5887     random_state->seed[1] = temp[1];
5888     random_state->seed[2] = (U16) accu;
5889
5890     return ldexp((double) random_state->seed[0], -48) +
5891            ldexp((double) random_state->seed[1], -32) +
5892            ldexp((double) random_state->seed[2], -16);
5893     }
5894 #endif
5895 }
5896
5897 #ifdef USE_C_BACKTRACE
5898
5899 /* Possibly move all this USE_C_BACKTRACE code into a new file. */
5900
5901 #ifdef USE_BFD
5902
5903 typedef struct {
5904     /* abfd is the BFD handle. */
5905     bfd* abfd;
5906     /* bfd_syms is the BFD symbol table. */
5907     asymbol** bfd_syms;
5908     /* bfd_text is handle to the the ".text" section of the object file. */
5909     asection* bfd_text;
5910     /* Since opening the executable and scanning its symbols is quite
5911      * heavy operation, we remember the filename we used the last time,
5912      * and do the opening and scanning only if the filename changes.
5913      * This removes most (but not all) open+scan cycles. */
5914     const char* fname_prev;
5915 } bfd_context;
5916
5917 /* Given a dl_info, update the BFD context if necessary. */
5918 static void bfd_update(bfd_context* ctx, Dl_info* dl_info)
5919 {
5920     /* BFD open and scan only if the filename changed. */
5921     if (ctx->fname_prev == NULL ||
5922         strNE(dl_info->dli_fname, ctx->fname_prev)) {
5923         if (ctx->abfd) {
5924             bfd_close(ctx->abfd);
5925         }
5926         ctx->abfd = bfd_openr(dl_info->dli_fname, 0);
5927         if (ctx->abfd) {
5928             if (bfd_check_format(ctx->abfd, bfd_object)) {
5929                 IV symbol_size = bfd_get_symtab_upper_bound(ctx->abfd);
5930                 if (symbol_size > 0) {
5931                     Safefree(ctx->bfd_syms);
5932                     Newx(ctx->bfd_syms, symbol_size, asymbol*);
5933                     ctx->bfd_text =
5934                         bfd_get_section_by_name(ctx->abfd, ".text");
5935                 }
5936                 else
5937                     ctx->abfd = NULL;
5938             }
5939             else
5940                 ctx->abfd = NULL;
5941         }
5942         ctx->fname_prev = dl_info->dli_fname;
5943     }
5944 }
5945
5946 /* Given a raw frame, try to symbolize it and store
5947  * symbol information (source file, line number) away. */
5948 static void bfd_symbolize(bfd_context* ctx,
5949                           void* raw_frame,
5950                           char** symbol_name,
5951                           STRLEN* symbol_name_size,
5952                           char** source_name,
5953                           STRLEN* source_name_size,
5954                           STRLEN* source_line)
5955 {
5956     *symbol_name = NULL;
5957     *symbol_name_size = 0;
5958     if (ctx->abfd) {
5959         IV offset = PTR2IV(raw_frame) - PTR2IV(ctx->bfd_text->vma);
5960         if (offset > 0 &&
5961             bfd_canonicalize_symtab(ctx->abfd, ctx->bfd_syms) > 0) {
5962             const char *file;
5963             const char *func;
5964             unsigned int line = 0;
5965             if (bfd_find_nearest_line(ctx->abfd, ctx->bfd_text,
5966                                       ctx->bfd_syms, offset,
5967                                       &file, &func, &line) &&
5968                 file && func && line > 0) {
5969                 /* Size and copy the source file, use only
5970                  * the basename of the source file.
5971                  *
5972                  * NOTE: the basenames are fine for the
5973                  * Perl source files, but may not always
5974                  * be the best idea for XS files. */
5975                 const char *p, *b = NULL;
5976                 /* Look for the last slash. */
5977                 for (p = file; *p; p++) {
5978                     if (*p == '/')
5979                         b = p + 1;
5980                 }
5981                 if (b == NULL || *b == 0) {
5982                     b = file;
5983                 }
5984                 *source_name_size = p - b + 1;
5985                 Newx(*source_name, *source_name_size + 1, char);
5986                 Copy(b, *source_name, *source_name_size + 1, char);
5987
5988                 *symbol_name_size = strlen(func);
5989                 Newx(*symbol_name, *symbol_name_size + 1, char);
5990                 Copy(func, *symbol_name, *symbol_name_size + 1, char);
5991
5992                 *source_line = line;
5993             }
5994         }
5995     }
5996 }
5997
5998 #endif /* #ifdef USE_BFD */
5999
6000 #ifdef PERL_DARWIN
6001
6002 /* OS X has no public API for for 'symbolicating' (Apple official term)
6003  * stack addresses to {function_name, source_file, line_number}.
6004  * Good news: there is command line utility atos(1) which does that.
6005  * Bad news 1: it's a command line utility.
6006  * Bad news 2: one needs to have the Developer Tools installed.
6007  * Bad news 3: in newer releases it needs to be run as 'xcrun atos'.
6008  *
6009  * To recap: we need to open a pipe for reading for a utility which
6010  * might not exist, or exists in different locations, and then parse
6011  * the output.  And since this is all for a low-level API, we cannot
6012  * use high-level stuff.  Thanks, Apple. */
6013
6014 typedef struct {
6015     /* tool is set to the absolute pathname of the tool to use:
6016      * xcrun or atos. */
6017     const char* tool;
6018     /* format is set to a printf format string used for building
6019      * the external command to run. */
6020     const char* format;
6021     /* unavail is set if e.g. xcrun cannot be found, or something
6022      * else happens that makes getting the backtrace dubious.  Note,
6023      * however, that the context isn't persistent, the next call to
6024      * get_c_backtrace() will start from scratch. */
6025     bool unavail;
6026     /* fname is the current object file name. */
6027     const char* fname;
6028     /* object_base_addr is the base address of the shared object. */
6029     void* object_base_addr;
6030 } atos_context;
6031
6032 /* Given |dl_info|, updates the context.  If the context has been
6033  * marked unavailable, return immediately.  If not but the tool has
6034  * not been set, set it to either "xcrun atos" or "atos" (also set the
6035  * format to use for creating commands for piping), or if neither is
6036  * unavailable (one needs the Developer Tools installed), mark the context
6037  * an unavailable.  Finally, update the filename (object name),
6038  * and its base address. */
6039
6040 static void atos_update(atos_context* ctx,
6041                         Dl_info* dl_info)
6042 {
6043     if (ctx->unavail)
6044         return;
6045     if (ctx->tool == NULL) {
6046         const char* tools[] = {
6047             "/usr/bin/xcrun",
6048             "/usr/bin/atos"
6049         };
6050         const char* formats[] = {
6051             "/usr/bin/xcrun atos -o '%s' -l %08x %08x 2>&1",
6052             "/usr/bin/atos -d -o '%s' -l %08x %08x 2>&1"
6053         };
6054         struct stat st;
6055         UV i;
6056         for (i = 0; i < C_ARRAY_LENGTH(tools); i++) {
6057             if (stat(tools[i], &st) == 0 && S_ISREG(st.st_mode)) {
6058                 ctx->tool = tools[i];
6059                 ctx->format = formats[i];
6060                 break;
6061             }
6062         }
6063         if (ctx->tool == NULL) {
6064             ctx->unavail = TRUE;
6065             return;
6066         }
6067     }
6068     if (ctx->fname == NULL ||
6069         strNE(dl_info->dli_fname, ctx->fname)) {
6070         ctx->fname = dl_info->dli_fname;
6071         ctx->object_base_addr = dl_info->dli_fbase;
6072     }
6073 }
6074
6075 /* Given an output buffer end |p| and its |start|, matches
6076  * for the atos output, extracting the source code location
6077  * and returning non-NULL if possible, returning NULL otherwise. */
6078 static const char* atos_parse(const char* p,
6079                               const char* start,
6080                               STRLEN* source_name_size,
6081                               STRLEN* source_line) {
6082     /* atos() output is something like:
6083      * perl_parse (in miniperl) (perl.c:2314)\n\n".
6084      * We cannot use Perl regular expressions, because we need to
6085      * stay low-level.  Therefore here we have a rolled-out version
6086      * of a state machine which matches _backwards_from_the_end_ and
6087      * if there's a success, returns the starts of the filename,
6088      * also setting the filename size and the source line number.
6089      * The matched regular expression is roughly "\(.*:\d+\)\s*$" */
6090     const char* source_number_start;
6091     const char* source_name_end;
6092     const char* source_line_end = start;
6093     const char* close_paren;
6094     UV uv;
6095
6096     /* Skip trailing whitespace. */
6097     while (p > start && isSPACE(*p)) p--;
6098     /* Now we should be at the close paren. */
6099     if (p == start || *p != ')')
6100         return NULL;
6101     close_paren = p;
6102     p--;
6103     /* Now we should be in the line number. */
6104     if (p == start || !isDIGIT(*p))
6105         return NULL;
6106     /* Skip over the digits. */
6107     while (p > start && isDIGIT(*p))
6108         p--;
6109     /* Now we should be at the colon. */
6110     if (p == start || *p != ':')
6111         return NULL;
6112     source_number_start = p + 1;
6113     source_name_end = p; /* Just beyond the end. */
6114     p--;
6115     /* Look for the open paren. */
6116     while (p > start && *p != '(')
6117         p--;
6118     if (p == start)
6119         return NULL;
6120     p++;
6121     *source_name_size = source_name_end - p;
6122     if (grok_atoUV(source_number_start, &uv,  &source_line_end)
6123         && source_line_end == close_paren
6124         && uv <= PERL_INT_MAX
6125     ) {
6126         *source_line = (STRLEN)uv;
6127         return p;
6128     }
6129     return NULL;
6130 }
6131
6132 /* Given a raw frame, read a pipe from the symbolicator (that's the
6133  * technical term) atos, reads the result, and parses the source code
6134  * location.  We must stay low-level, so we use snprintf(), pipe(),
6135  * and fread(), and then also parse the output ourselves. */
6136 static void atos_symbolize(atos_context* ctx,
6137                            void* raw_frame,
6138                            char** source_name,
6139                            STRLEN* source_name_size,
6140                            STRLEN* source_line)
6141 {
6142     char cmd[1024];
6143     const char* p;
6144     Size_t cnt;
6145
6146     if (ctx->unavail)
6147         return;
6148     /* Simple security measure: if there's any funny business with
6149      * the object name (used as "-o '%s'" ), leave since at least
6150      * partially the user controls it. */
6151     for (p = ctx->fname; *p; p++) {
6152         if (*p == '\'' || isCNTRL(*p)) {
6153             ctx->unavail = TRUE;
6154             return;
6155         }
6156     }
6157     cnt = snprintf(cmd, sizeof(cmd), ctx->format,
6158                    ctx->fname, ctx->object_base_addr, raw_frame);
6159     if (cnt < sizeof(cmd)) {
6160         /* Undo nostdio.h #defines that disable stdio.
6161          * This is somewhat naughty, but is used elsewhere
6162          * in the core, and affects only OS X. */
6163 #undef FILE
6164 #undef popen
6165 #undef fread
6166 #undef pclose
6167         FILE* fp = popen(cmd, "r");
6168         /* At the moment we open a new pipe for each stack frame.
6169          * This is naturally somewhat slow, but hopefully generating
6170          * stack traces is never going to in a performance critical path.
6171          *
6172          * We could play tricks with atos by batching the stack
6173          * addresses to be resolved: atos can either take multiple
6174          * addresses from the command line, or read addresses from
6175          * a file (though the mess of creating temporary files would
6176          * probably negate much of any possible speedup).
6177          *
6178          * Normally there are only two objects present in the backtrace:
6179          * perl itself, and the libdyld.dylib.  (Note that the object
6180          * filenames contain the full pathname, so perl may not always
6181          * be in the same place.)  Whenever the object in the
6182          * backtrace changes, the base address also changes.
6183          *
6184          * The problem with batching the addresses, though, would be
6185          * matching the results with the addresses: the parsing of
6186          * the results is already painful enough with a single address. */
6187         if (fp) {
6188             char out[1024];
6189             UV cnt = fread(out, 1, sizeof(out), fp);
6190             if (cnt < sizeof(out)) {
6191                 const char* p = atos_parse(out + cnt - 1, out,
6192                                            source_name_size,
6193                                            source_line);
6194                 if (p) {
6195                     Newx(*source_name,
6196                          *source_name_size, char);
6197                     Copy(p, *source_name,
6198                          *source_name_size,  char);
6199                 }
6200             }
6201             pclose(fp);
6202         }
6203     }
6204 }
6205
6206 #endif /* #ifdef PERL_DARWIN */
6207
6208 /*
6209 =for apidoc_section $debugging
6210 =for apidoc get_c_backtrace
6211
6212 Collects the backtrace (aka "stacktrace") into a single linear
6213 malloced buffer, which the caller B<must> C<Perl_free_c_backtrace()>.
6214
6215 Scans the frames back by S<C<depth + skip>>, then drops the C<skip> innermost,
6216 returning at most C<depth> frames.
6217
6218 =cut
6219 */
6220
6221 Perl_c_backtrace*
6222 Perl_get_c_backtrace(pTHX_ int depth, int skip)
6223 {
6224     /* Note that here we must stay as low-level as possible: Newx(),
6225      * Copy(), Safefree(); since we may be called from anywhere,
6226      * so we should avoid higher level constructs like SVs or AVs.
6227      *
6228      * Since we are using safesysmalloc() via Newx(), don't try
6229      * getting backtrace() there, unless you like deep recursion. */
6230
6231     /* Currently only implemented with backtrace() and dladdr(),
6232      * for other platforms NULL is returned. */
6233
6234 #if defined(HAS_BACKTRACE) && defined(HAS_DLADDR)
6235     /* backtrace() is available via <execinfo.h> in glibc and in most
6236      * modern BSDs; dladdr() is available via <dlfcn.h>. */
6237
6238     /* We try fetching this many frames total, but then discard
6239      * the |skip| first ones.  For the remaining ones we will try
6240      * retrieving more information with dladdr(). */
6241     int try_depth = skip +  depth;
6242
6243     /* The addresses (program counters) returned by backtrace(). */
6244     void** raw_frames;
6245
6246     /* Retrieved with dladdr() from the addresses returned by backtrace(). */
6247     Dl_info* dl_infos;
6248
6249     /* Sizes _including_ the terminating \0 of the object name
6250      * and symbol name strings. */
6251     STRLEN* object_name_sizes;
6252     STRLEN* symbol_name_sizes;
6253
6254 #ifdef USE_BFD
6255     /* The symbol names comes either from dli_sname,
6256      * or if using BFD, they can come from BFD. */
6257     char** symbol_names;
6258 #endif
6259
6260     /* The source code location information.  Dug out with e.g. BFD. */
6261     char** source_names;
6262     STRLEN* source_name_sizes;
6263     STRLEN* source_lines;
6264
6265     Perl_c_backtrace* bt = NULL;  /* This is what will be returned. */
6266     int got_depth; /* How many frames were returned from backtrace(). */
6267     UV frame_count = 0; /* How many frames we return. */
6268     UV total_bytes = 0; /* The size of the whole returned backtrace. */
6269
6270 #ifdef USE_BFD
6271     bfd_context bfd_ctx;
6272 #endif
6273 #ifdef PERL_DARWIN
6274     atos_context atos_ctx;
6275 #endif
6276
6277     /* Here are probably possibilities for optimizing.  We could for
6278      * example have a struct that contains most of these and then
6279      * allocate |try_depth| of them, saving a bunch of malloc calls.
6280      * Note, however, that |frames| could not be part of that struct
6281      * because backtrace() will want an array of just them.  Also be
6282      * careful about the name strings. */
6283     Newx(raw_frames, try_depth, void*);
6284     Newx(dl_infos, try_depth, Dl_info);
6285     Newx(object_name_sizes, try_depth, STRLEN);
6286     Newx(symbol_name_sizes, try_depth, STRLEN);
6287     Newx(source_names, try_depth, char*);
6288     Newx(source_name_sizes, try_depth, STRLEN);
6289     Newx(source_lines, try_depth, STRLEN);
6290 #ifdef USE_BFD
6291     Newx(symbol_names, try_depth, char*);
6292 #endif
6293
6294     /* Get the raw frames. */
6295     got_depth = (int)backtrace(raw_frames, try_depth);
6296
6297     /* We use dladdr() instead of backtrace_symbols() because we want
6298      * the full details instead of opaque strings.  This is useful for
6299      * two reasons: () the details are needed for further symbolic
6300      * digging, for example in OS X (2) by having the details we fully
6301      * control the output, which in turn is useful when more platforms
6302      * are added: we can keep out output "portable". */
6303
6304     /* We want a single linear allocation, which can then be freed
6305      * with a single swoop.  We will do the usual trick of first
6306      * walking over the structure and seeing how much we need to
6307      * allocate, then allocating, and then walking over the structure
6308      * the second time and populating it. */
6309
6310     /* First we must compute the total size of the buffer. */
6311     total_bytes = sizeof(Perl_c_backtrace_header);
6312     if (got_depth > skip) {
6313         int i;
6314 #ifdef USE_BFD
6315         bfd_init(); /* Is this safe to call multiple times? */
6316         Zero(&bfd_ctx, 1, bfd_context);
6317 #endif
6318 #ifdef PERL_DARWIN
6319         Zero(&atos_ctx, 1, atos_context);
6320 #endif
6321         for (i = skip; i < try_depth; i++) {
6322             Dl_info* dl_info = &dl_infos[i];
6323
6324             object_name_sizes[i] = 0;
6325             source_names[i] = NULL;
6326             source_name_sizes[i] = 0;
6327             source_lines[i] = 0;
6328
6329             /* Yes, zero from dladdr() is failure. */
6330             if (dladdr(raw_frames[i], dl_info)) {
6331                 total_bytes += sizeof(Perl_c_backtrace_frame);
6332
6333                 object_name_sizes[i] =
6334                     dl_info->dli_fname ? strlen(dl_info->dli_fname) : 0;
6335                 symbol_name_sizes[i] =
6336                     dl_info->dli_sname ? strlen(dl_info->dli_sname) : 0;
6337 #ifdef USE_BFD
6338                 bfd_update(&bfd_ctx, dl_info);
6339                 bfd_symbolize(&bfd_ctx, raw_frames[i],
6340                               &symbol_names[i],
6341                               &symbol_name_sizes[i],
6342                               &source_names[i],
6343                               &source_name_sizes[i],
6344                               &source_lines[i]);
6345 #endif
6346 #if PERL_DARWIN
6347                 atos_update(&atos_ctx, dl_info);
6348                 atos_symbolize(&atos_ctx,
6349                                raw_frames[i],
6350                                &source_names[i],
6351                                &source_name_sizes[i],
6352                                &source_lines[i]);
6353 #endif
6354
6355                 /* Plus ones for the terminating \0. */
6356                 total_bytes += object_name_sizes[i] + 1;
6357                 total_bytes += symbol_name_sizes[i] + 1;
6358                 total_bytes += source_name_sizes[i] + 1;
6359
6360                 frame_count++;
6361             } else {
6362                 break;
6363             }
6364         }
6365 #ifdef USE_BFD
6366         Safefree(bfd_ctx.bfd_syms);
6367 #endif
6368     }
6369
6370     /* Now we can allocate and populate the result buffer. */
6371     Newxc(bt, total_bytes, char, Perl_c_backtrace);
6372     Zero(bt, total_bytes, char);
6373     bt->header.frame_count = frame_count;
6374     bt->header.total_bytes = total_bytes;
6375     if (frame_count > 0) {
6376         Perl_c_backtrace_frame* frame = bt->frame_info;
6377         char* name_base = (char *)(frame + frame_count);
6378         char* name_curr = name_base; /* Outputting the name strings here. */
6379         UV i;
6380         for (i = skip; i < skip + frame_count; i++) {
6381             Dl_info* dl_info = &dl_infos[i];
6382
6383             frame->addr = raw_frames[i];
6384             frame->object_base_addr = dl_info->dli_fbase;
6385             frame->symbol_addr = dl_info->dli_saddr;
6386
6387             /* Copies a string, including the \0, and advances the name_curr.
6388              * Also copies the start and the size to the frame. */
6389 #define PERL_C_BACKTRACE_STRCPY(frame, doffset, src, dsize, size) \
6390             if (size && src) \
6391                 Copy(src, name_curr, size, char); \
6392             frame->doffset = name_curr - (char*)bt; \
6393             frame->dsize = size; \
6394             name_curr += size; \
6395             *name_curr++ = 0;
6396
6397             PERL_C_BACKTRACE_STRCPY(frame, object_name_offset,
6398                                     dl_info->dli_fname,
6399                                     object_name_size, object_name_sizes[i]);
6400
6401 #ifdef USE_BFD
6402             PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6403                                     symbol_names[i],
6404                                     symbol_name_size, symbol_name_sizes[i]);
6405             Safefree(symbol_names[i]);
6406 #else
6407             PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset,
6408                                     dl_info->dli_sname,
6409                                     symbol_name_size, symbol_name_sizes[i]);
6410 #endif
6411
6412             PERL_C_BACKTRACE_STRCPY(frame, source_name_offset,
6413                                     source_names[i],
6414                                     source_name_size, source_name_sizes[i]);
6415             Safefree(source_names[i]);
6416
6417 #undef PERL_C_BACKTRACE_STRCPY
6418
6419             frame->source_line_number = source_lines[i];
6420
6421             frame++;
6422         }
6423         assert(total_bytes ==
6424                (UV)(sizeof(Perl_c_backtrace_header) +
6425                     frame_count * sizeof(Perl_c_backtrace_frame) +
6426                     name_curr - name_base));
6427     }
6428 #ifdef USE_BFD
6429     Safefree(symbol_names);
6430     if (bfd_ctx.abfd) {
6431         bfd_close(bfd_ctx.abfd);
6432     }
6433 #endif
6434     Safefree(source_lines);
6435     Safefree(source_name_sizes);
6436     Safefree(source_names);
6437     Safefree(symbol_name_sizes);
6438     Safefree(object_name_sizes);
6439     /* Assuming the strings returned by dladdr() are pointers
6440      * to read-only static memory (the object file), so that
6441      * they do not need freeing (and cannot be). */
6442     Safefree(dl_infos);
6443     Safefree(raw_frames);
6444     return bt;
6445 #else
6446     PERL_UNUSED_ARG(depth);
6447     PERL_UNUSED_ARG(skip);
6448     return NULL;
6449 #endif
6450 }
6451
6452 /*
6453 =for apidoc free_c_backtrace
6454
6455 Deallocates a backtrace received from get_c_backtrace.
6456
6457 =cut
6458 */
6459
6460 /*
6461 =for apidoc get_c_backtrace_dump
6462
6463 Returns a SV containing a dump of C<depth> frames of the call stack, skipping
6464 the C<skip> innermost ones.  C<depth> of 20 is usually enough.
6465
6466 The appended output looks like:
6467
6468  ...
6469  1   10e004812:0082   Perl_croak   util.c:1716    /usr/bin/perl
6470  2   10df8d6d2:1d72   perl_parse   perl.c:3975    /usr/bin/perl
6471  ...
6472
6473 The fields are tab-separated.  The first column is the depth (zero
6474 being the innermost non-skipped frame).  In the hex:offset, the hex is
6475 where the program counter was in C<S_parse_body>, and the :offset (might
6476 be missing) tells how much inside the C<S_parse_body> the program counter was.
6477
6478 The C<util.c:1716> is the source code file and line number.
6479
6480 The F</usr/bin/perl> is obvious (hopefully).
6481
6482 Unknowns are C<"-">.  Unknowns can happen unfortunately quite easily:
6483 if the platform doesn't support retrieving the information;
6484 if the binary is missing the debug information;
6485 if the optimizer has transformed the code by for example inlining.
6486
6487 =cut
6488 */
6489
6490 SV*
6491 Perl_get_c_backtrace_dump(pTHX_ int depth, int skip)
6492 {
6493     Perl_c_backtrace* bt;
6494
6495     bt = get_c_backtrace(depth, skip + 1 /* Hide ourselves. */);
6496     if (bt) {
6497         Perl_c_backtrace_frame* frame;
6498         SV* dsv = newSVpvs("");
6499         UV i;
6500         for (i = 0, frame = bt->frame_info;
6501              i < bt->header.frame_count; i++, frame++) {
6502             Perl_sv_catpvf(aTHX_ dsv, "%d", (int)i);
6503             Perl_sv_catpvf(aTHX_ dsv, "\t%p", frame->addr ? frame->addr : "-");
6504             /* Symbol (function) names might disappear without debug info.
6505              *
6506              * The source code location might disappear in case of the
6507              * optimizer inlining or otherwise rearranging the code. */
6508             if (frame->symbol_addr) {
6509                 Perl_sv_catpvf(aTHX_ dsv, ":%04x",
6510                                (int)
6511                                ((char*)frame->addr - (char*)frame->symbol_addr));
6512             }
6513             Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6514                            frame->symbol_name_size &&
6515                            frame->symbol_name_offset ?
6516                            (char*)bt + frame->symbol_name_offset : "-");
6517             if (frame->source_name_size &&
6518                 frame->source_name_offset &&
6519                 frame->source_line_number) {
6520                 Perl_sv_catpvf(aTHX_ dsv, "\t%s:%" UVuf,
6521                                (char*)bt + frame->source_name_offset,
6522                                (UV)frame->source_line_number);
6523             } else {
6524                 Perl_sv_catpvf(aTHX_ dsv, "\t-");
6525             }
6526             Perl_sv_catpvf(aTHX_ dsv, "\t%s",
6527                            frame->object_name_size &&
6528                            frame->object_name_offset ?
6529                            (char*)bt + frame->object_name_offset : "-");
6530             /* The frame->object_base_addr is not output,
6531              * but it is used for symbolizing/symbolicating. */
6532             sv_catpvs(dsv, "\n");
6533         }
6534
6535         Perl_free_c_backtrace(bt);
6536
6537         return dsv;
6538     }
6539
6540     return NULL;
6541 }
6542
6543 /*
6544 =for apidoc dump_c_backtrace
6545
6546 Dumps the C backtrace to the given C<fp>.
6547
6548 Returns true if a backtrace could be retrieved, false if not.
6549
6550 =cut
6551 */
6552
6553 bool
6554 Perl_dump_c_backtrace(pTHX_ PerlIO* fp, int depth, int skip)
6555 {
6556     SV* sv;
6557
6558     PERL_ARGS_ASSERT_DUMP_C_BACKTRACE;
6559
6560     sv = Perl_get_c_backtrace_dump(aTHX_ depth, skip);
6561     if (sv) {
6562         sv_2mortal(sv);
6563         PerlIO_printf(fp, "%s", SvPV_nolen(sv));
6564         return TRUE;
6565     }
6566     return FALSE;
6567 }
6568
6569 #endif /* #ifdef USE_C_BACKTRACE */
6570
6571 #if defined(USE_ITHREADS) && defined(I_PTHREAD)
6572
6573 /* pthread_mutex_t and perl_mutex are typedef equivalent
6574  * so casting the pointers is fine. */
6575
6576 int perl_tsa_mutex_lock(perl_mutex* mutex)
6577 {
6578     return pthread_mutex_lock((pthread_mutex_t *) mutex);
6579 }
6580
6581 int perl_tsa_mutex_unlock(perl_mutex* mutex)
6582 {
6583     return pthread_mutex_unlock((pthread_mutex_t *) mutex);
6584 }
6585
6586 int perl_tsa_mutex_destroy(perl_mutex* mutex)
6587 {
6588     return pthread_mutex_destroy((pthread_mutex_t *) mutex);
6589 }
6590
6591 #endif
6592
6593 #ifdef USE_DTRACE
6594
6595 /* log a sub call or return */
6596
6597 void
6598 Perl_dtrace_probe_call(pTHX_ CV *cv, bool is_call)
6599 {
6600     const char *func;
6601     const char *file;
6602     const char *stash;
6603     const COP  *start;
6604     line_t      line;
6605
6606     PERL_ARGS_ASSERT_DTRACE_PROBE_CALL;
6607
6608     if (CvNAMED(cv)) {
6609         HEK *hek = CvNAME_HEK(cv);
6610         func = HEK_KEY(hek);
6611     }
6612     else {
6613         GV  *gv = CvGV(cv);
6614         func = GvENAME(gv);
6615     }
6616     start = (const COP *)CvSTART(cv);
6617     file  = CopFILE(start);
6618     line  = CopLINE(start);
6619     stash = CopSTASHPV(start);
6620
6621     if (is_call) {
6622         PERL_SUB_ENTRY(func, file, line, stash);
6623     }
6624     else {
6625         PERL_SUB_RETURN(func, file, line, stash);
6626     }
6627 }
6628
6629
6630 /* log a require file loading/loaded  */
6631
6632 void
6633 Perl_dtrace_probe_load(pTHX_ const char *name, bool is_loading)
6634 {
6635     PERL_ARGS_ASSERT_DTRACE_PROBE_LOAD;
6636
6637     if (is_loading) {
6638         PERL_LOADING_FILE(name);
6639     }
6640     else {
6641         PERL_LOADED_FILE(name);
6642     }
6643 }
6644
6645
6646 /* log an op execution */
6647
6648 void
6649 Perl_dtrace_probe_op(pTHX_ const OP *op)
6650 {
6651     PERL_ARGS_ASSERT_DTRACE_PROBE_OP;
6652
6653     PERL_OP_ENTRY(OP_NAME(op));
6654 }
6655
6656
6657 /* log a compile/run phase change */
6658
6659 void
6660 Perl_dtrace_probe_phase(pTHX_ enum perl_phase phase)
6661 {
6662     const char *ph_old = PL_phase_names[PL_phase];
6663     const char *ph_new = PL_phase_names[phase];
6664
6665     PERL_PHASE_CHANGE(ph_new, ph_old);
6666 }
6667
6668 #endif
6669
6670 /*
6671  * ex: set ts=8 sts=4 sw=4 et:
6672  */