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