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