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