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