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