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