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