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