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 PP |
35 | #ifndef SIG_ERR |
36 | # define SIG_ERR ((Sighandler_t) -1) | |
37 | #endif | |
64ca3a65 | 38 | #endif |
36477c24 | 39 | |
3be8f094 TC |
40 | #include <math.h> |
41 | #include <stdlib.h> | |
42 | ||
172d2248 OS |
43 | #ifdef __Lynx__ |
44 | /* Missing protos on LynxOS */ | |
45 | int putenv(char *); | |
46 | #endif | |
47 | ||
868439a2 JH |
48 | #ifdef HAS_SELECT |
49 | # ifdef I_SYS_SELECT | |
50 | # include <sys/select.h> | |
51 | # endif | |
52 | #endif | |
53 | ||
470dd224 | 54 | #ifdef USE_C_BACKTRACE |
0762e42f JH |
55 | # ifdef I_BFD |
56 | # define USE_BFD | |
57 | # ifdef PERL_DARWIN | |
58 | # undef USE_BFD /* BFD is useless in OS X. */ | |
59 | # endif | |
60 | # ifdef USE_BFD | |
61 | # include <bfd.h> | |
62 | # endif | |
63 | # endif | |
470dd224 JH |
64 | # ifdef I_DLFCN |
65 | # include <dlfcn.h> | |
66 | # endif | |
67 | # ifdef I_EXECINFO | |
68 | # include <execinfo.h> | |
69 | # endif | |
70 | #endif | |
71 | ||
b001a0d1 FC |
72 | #ifdef PERL_DEBUG_READONLY_COW |
73 | # include <sys/mman.h> | |
74 | #endif | |
75 | ||
8d063cd8 | 76 | #define FLUSH |
8d063cd8 | 77 | |
a687059c LW |
78 | /* NOTE: Do not call the next three routines directly. Use the macros |
79 | * in handy.h, so that we can easily redefine everything to do tracking of | |
80 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 81 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
82 | */ |
83 | ||
79a92154 | 84 | #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL) |
1f4d2d4e NC |
85 | # define ALWAYS_NEED_THX |
86 | #endif | |
87 | ||
b001a0d1 FC |
88 | #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW) |
89 | static void | |
90 | S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header) | |
91 | { | |
92 | if (header->readonly | |
93 | && mprotect(header, header->size, PROT_READ|PROT_WRITE)) | |
94 | Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d", | |
95 | header, header->size, errno); | |
96 | } | |
97 | ||
98 | static void | |
99 | S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header) | |
100 | { | |
101 | if (header->readonly | |
102 | && mprotect(header, header->size, PROT_READ)) | |
103 | Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d", | |
104 | header, header->size, errno); | |
105 | } | |
106 | # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo) | |
107 | # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo) | |
108 | #else | |
109 | # define maybe_protect_rw(foo) NOOP | |
110 | # define maybe_protect_ro(foo) NOOP | |
111 | #endif | |
112 | ||
3f07c2bc FC |
113 | #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW) |
114 | /* Use memory_debug_header */ | |
115 | # define USE_MDH | |
116 | # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \ | |
117 | || defined(PERL_DEBUG_READONLY_COW) | |
118 | # define MDH_HAS_SIZE | |
119 | # endif | |
120 | #endif | |
121 | ||
26fa51c3 AMS |
122 | /* paranoid version of system's malloc() */ |
123 | ||
bd4080b3 | 124 | Malloc_t |
4f63d024 | 125 | Perl_safesysmalloc(MEM_SIZE size) |
8d063cd8 | 126 | { |
1f4d2d4e | 127 | #ifdef ALWAYS_NEED_THX |
54aff467 | 128 | dTHX; |
0cb20dae | 129 | #endif |
bd4080b3 | 130 | Malloc_t ptr; |
9efda33a TC |
131 | |
132 | #ifdef USE_MDH | |
133 | if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size) | |
134 | goto out_of_memory; | |
a78adc84 | 135 | size += PERL_MEMORY_DEBUG_HEADER_SIZE; |
9efda33a | 136 | #endif |
34de22dd | 137 | #ifdef DEBUGGING |
03c5309f | 138 | if ((SSize_t)size < 0) |
5637ef5b | 139 | Perl_croak_nocontext("panic: malloc, size=%"UVuf, (UV) size); |
34de22dd | 140 | #endif |
b001a0d1 FC |
141 | if (!size) size = 1; /* malloc(0) is NASTY on our system */ |
142 | #ifdef PERL_DEBUG_READONLY_COW | |
143 | if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE, | |
144 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
145 | perror("mmap failed"); | |
146 | abort(); | |
147 | } | |
148 | #else | |
149 | ptr = (Malloc_t)PerlMem_malloc(size?size:1); | |
150 | #endif | |
da927450 | 151 | PERL_ALLOC_CHECK(ptr); |
bd61b366 | 152 | if (ptr != NULL) { |
3f07c2bc | 153 | #ifdef USE_MDH |
7cb608b5 NC |
154 | struct perl_memory_debug_header *const header |
155 | = (struct perl_memory_debug_header *)ptr; | |
9a083ecf NC |
156 | #endif |
157 | ||
158 | #ifdef PERL_POISON | |
7e337ee0 | 159 | PoisonNew(((char *)ptr), size, char); |
9a083ecf | 160 | #endif |
7cb608b5 | 161 | |
9a083ecf | 162 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
163 | header->interpreter = aTHX; |
164 | /* Link us into the list. */ | |
165 | header->prev = &PL_memory_debug_header; | |
166 | header->next = PL_memory_debug_header.next; | |
167 | PL_memory_debug_header.next = header; | |
b001a0d1 | 168 | maybe_protect_rw(header->next); |
7cb608b5 | 169 | header->next->prev = header; |
b001a0d1 FC |
170 | maybe_protect_ro(header->next); |
171 | # ifdef PERL_DEBUG_READONLY_COW | |
172 | header->readonly = 0; | |
cd1541b2 | 173 | # endif |
e8dda941 | 174 | #endif |
3f07c2bc | 175 | #ifdef MDH_HAS_SIZE |
b001a0d1 FC |
176 | header->size = size; |
177 | #endif | |
b033d668 | 178 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
5dfff8f3 | 179 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
b033d668 DD |
180 | |
181 | } | |
8d063cd8 | 182 | else { |
296f0d56 | 183 | #ifdef USE_MDH |
9efda33a | 184 | out_of_memory: |
296f0d56 TC |
185 | #endif |
186 | { | |
187 | #ifndef ALWAYS_NEED_THX | |
188 | dTHX; | |
189 | #endif | |
190 | if (PL_nomemok) | |
191 | ptr = NULL; | |
192 | else | |
193 | croak_no_mem(); | |
194 | } | |
8d063cd8 | 195 | } |
b033d668 | 196 | return ptr; |
8d063cd8 LW |
197 | } |
198 | ||
f2517201 | 199 | /* paranoid version of system's realloc() */ |
8d063cd8 | 200 | |
bd4080b3 | 201 | Malloc_t |
4f63d024 | 202 | Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 203 | { |
1f4d2d4e | 204 | #ifdef ALWAYS_NEED_THX |
54aff467 | 205 | dTHX; |
0cb20dae | 206 | #endif |
bd4080b3 | 207 | Malloc_t ptr; |
b001a0d1 FC |
208 | #ifdef PERL_DEBUG_READONLY_COW |
209 | const MEM_SIZE oldsize = where | |
a78adc84 | 210 | ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size |
b001a0d1 FC |
211 | : 0; |
212 | #endif | |
9a34ef1d | 213 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO) |
6ad3d225 | 214 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 215 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 216 | |
7614df0c | 217 | if (!size) { |
f2517201 | 218 | safesysfree(where); |
b033d668 | 219 | ptr = NULL; |
7614df0c | 220 | } |
b033d668 DD |
221 | else if (!where) { |
222 | ptr = safesysmalloc(size); | |
223 | } | |
224 | else { | |
3f07c2bc | 225 | #ifdef USE_MDH |
b033d668 | 226 | where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE); |
9efda33a TC |
227 | if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size) |
228 | goto out_of_memory; | |
b033d668 DD |
229 | size += PERL_MEMORY_DEBUG_HEADER_SIZE; |
230 | { | |
231 | struct perl_memory_debug_header *const header | |
232 | = (struct perl_memory_debug_header *)where; | |
7cb608b5 | 233 | |
b001a0d1 | 234 | # ifdef PERL_TRACK_MEMPOOL |
b033d668 DD |
235 | if (header->interpreter != aTHX) { |
236 | Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p", | |
237 | header->interpreter, aTHX); | |
238 | } | |
239 | assert(header->next->prev == header); | |
240 | assert(header->prev->next == header); | |
cd1541b2 | 241 | # ifdef PERL_POISON |
b033d668 DD |
242 | if (header->size > size) { |
243 | const MEM_SIZE freed_up = header->size - size; | |
244 | char *start_of_freed = ((char *)where) + size; | |
245 | PoisonFree(start_of_freed, freed_up, char); | |
246 | } | |
cd1541b2 | 247 | # endif |
b001a0d1 | 248 | # endif |
3f07c2bc | 249 | # ifdef MDH_HAS_SIZE |
b033d668 | 250 | header->size = size; |
b001a0d1 | 251 | # endif |
b033d668 | 252 | } |
e8dda941 | 253 | #endif |
34de22dd | 254 | #ifdef DEBUGGING |
b033d668 DD |
255 | if ((SSize_t)size < 0) |
256 | Perl_croak_nocontext("panic: realloc, size=%"UVuf, (UV)size); | |
34de22dd | 257 | #endif |
b001a0d1 | 258 | #ifdef PERL_DEBUG_READONLY_COW |
b033d668 DD |
259 | if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE, |
260 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
261 | perror("mmap failed"); | |
262 | abort(); | |
263 | } | |
264 | Copy(where,ptr,oldsize < size ? oldsize : size,char); | |
265 | if (munmap(where, oldsize)) { | |
266 | perror("munmap failed"); | |
267 | abort(); | |
268 | } | |
b001a0d1 | 269 | #else |
b033d668 | 270 | ptr = (Malloc_t)PerlMem_realloc(where,size); |
b001a0d1 | 271 | #endif |
b033d668 | 272 | PERL_ALLOC_CHECK(ptr); |
a1d180c4 | 273 | |
4fd0a9b8 NC |
274 | /* MUST do this fixup first, before doing ANYTHING else, as anything else |
275 | might allocate memory/free/move memory, and until we do the fixup, it | |
276 | may well be chasing (and writing to) free memory. */ | |
b033d668 | 277 | if (ptr != NULL) { |
b001a0d1 | 278 | #ifdef PERL_TRACK_MEMPOOL |
b033d668 DD |
279 | struct perl_memory_debug_header *const header |
280 | = (struct perl_memory_debug_header *)ptr; | |
7cb608b5 | 281 | |
9a083ecf | 282 | # ifdef PERL_POISON |
b033d668 DD |
283 | if (header->size < size) { |
284 | const MEM_SIZE fresh = size - header->size; | |
285 | char *start_of_fresh = ((char *)ptr) + size; | |
286 | PoisonNew(start_of_fresh, fresh, char); | |
287 | } | |
9a083ecf NC |
288 | # endif |
289 | ||
b033d668 DD |
290 | maybe_protect_rw(header->next); |
291 | header->next->prev = header; | |
292 | maybe_protect_ro(header->next); | |
293 | maybe_protect_rw(header->prev); | |
294 | header->prev->next = header; | |
295 | maybe_protect_ro(header->prev); | |
b001a0d1 | 296 | #endif |
b033d668 DD |
297 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
298 | } | |
4fd0a9b8 NC |
299 | |
300 | /* In particular, must do that fixup above before logging anything via | |
301 | *printf(), as it can reallocate memory, which can cause SEGVs. */ | |
302 | ||
b033d668 DD |
303 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++)); |
304 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); | |
4fd0a9b8 | 305 | |
b033d668 | 306 | if (ptr == NULL) { |
296f0d56 | 307 | #ifdef USE_MDH |
9efda33a | 308 | out_of_memory: |
296f0d56 TC |
309 | #endif |
310 | { | |
311 | #ifndef ALWAYS_NEED_THX | |
312 | dTHX; | |
313 | #endif | |
314 | if (PL_nomemok) | |
315 | ptr = NULL; | |
316 | else | |
317 | croak_no_mem(); | |
318 | } | |
0cb20dae | 319 | } |
8d063cd8 | 320 | } |
b033d668 | 321 | return ptr; |
8d063cd8 LW |
322 | } |
323 | ||
f2517201 | 324 | /* safe version of system's free() */ |
8d063cd8 | 325 | |
54310121 | 326 | Free_t |
4f63d024 | 327 | Perl_safesysfree(Malloc_t where) |
8d063cd8 | 328 | { |
79a92154 | 329 | #ifdef ALWAYS_NEED_THX |
54aff467 | 330 | dTHX; |
155aba94 | 331 | #endif |
97835f67 | 332 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++)); |
378cc40b | 333 | if (where) { |
3f07c2bc | 334 | #ifdef USE_MDH |
6edcbed6 | 335 | Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE); |
cd1541b2 | 336 | { |
7cb608b5 | 337 | struct perl_memory_debug_header *const header |
6edcbed6 | 338 | = (struct perl_memory_debug_header *)where_intrn; |
7cb608b5 | 339 | |
3f07c2bc | 340 | # ifdef MDH_HAS_SIZE |
b001a0d1 FC |
341 | const MEM_SIZE size = header->size; |
342 | # endif | |
343 | # ifdef PERL_TRACK_MEMPOOL | |
7cb608b5 | 344 | if (header->interpreter != aTHX) { |
5637ef5b NC |
345 | Perl_croak_nocontext("panic: free from wrong pool, %p!=%p", |
346 | header->interpreter, aTHX); | |
7cb608b5 NC |
347 | } |
348 | if (!header->prev) { | |
cd1541b2 NC |
349 | Perl_croak_nocontext("panic: duplicate free"); |
350 | } | |
5637ef5b NC |
351 | if (!(header->next)) |
352 | Perl_croak_nocontext("panic: bad free, header->next==NULL"); | |
353 | if (header->next->prev != header || header->prev->next != header) { | |
354 | Perl_croak_nocontext("panic: bad free, ->next->prev=%p, " | |
355 | "header=%p, ->prev->next=%p", | |
356 | header->next->prev, header, | |
357 | header->prev->next); | |
cd1541b2 | 358 | } |
7cb608b5 | 359 | /* Unlink us from the chain. */ |
b001a0d1 | 360 | maybe_protect_rw(header->next); |
7cb608b5 | 361 | header->next->prev = header->prev; |
b001a0d1 FC |
362 | maybe_protect_ro(header->next); |
363 | maybe_protect_rw(header->prev); | |
7cb608b5 | 364 | header->prev->next = header->next; |
b001a0d1 FC |
365 | maybe_protect_ro(header->prev); |
366 | maybe_protect_rw(header); | |
7cb608b5 | 367 | # ifdef PERL_POISON |
6edcbed6 | 368 | PoisonNew(where_intrn, size, char); |
cd1541b2 | 369 | # endif |
7cb608b5 NC |
370 | /* Trigger the duplicate free warning. */ |
371 | header->next = NULL; | |
b001a0d1 FC |
372 | # endif |
373 | # ifdef PERL_DEBUG_READONLY_COW | |
6edcbed6 | 374 | if (munmap(where_intrn, size)) { |
b001a0d1 FC |
375 | perror("munmap failed"); |
376 | abort(); | |
377 | } | |
378 | # endif | |
7cb608b5 | 379 | } |
6edcbed6 DD |
380 | #else |
381 | Malloc_t where_intrn = where; | |
382 | #endif /* USE_MDH */ | |
b001a0d1 | 383 | #ifndef PERL_DEBUG_READONLY_COW |
6edcbed6 | 384 | PerlMem_free(where_intrn); |
b001a0d1 | 385 | #endif |
378cc40b | 386 | } |
8d063cd8 LW |
387 | } |
388 | ||
f2517201 | 389 | /* safe version of system's calloc() */ |
1050c9ca | 390 | |
bd4080b3 | 391 | Malloc_t |
4f63d024 | 392 | Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 393 | { |
1f4d2d4e | 394 | #ifdef ALWAYS_NEED_THX |
54aff467 | 395 | dTHX; |
0cb20dae | 396 | #endif |
bd4080b3 | 397 | Malloc_t ptr; |
3f07c2bc | 398 | #if defined(USE_MDH) || defined(DEBUGGING) |
ad7244db | 399 | MEM_SIZE total_size = 0; |
4b1123b9 | 400 | #endif |
1050c9ca | 401 | |
ad7244db | 402 | /* Even though calloc() for zero bytes is strange, be robust. */ |
4b1123b9 | 403 | if (size && (count <= MEM_SIZE_MAX / size)) { |
3f07c2bc | 404 | #if defined(USE_MDH) || defined(DEBUGGING) |
ad7244db | 405 | total_size = size * count; |
4b1123b9 NC |
406 | #endif |
407 | } | |
ad7244db | 408 | else |
d1decf2b | 409 | croak_memory_wrap(); |
3f07c2bc | 410 | #ifdef USE_MDH |
a78adc84 DM |
411 | if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size) |
412 | total_size += PERL_MEMORY_DEBUG_HEADER_SIZE; | |
ad7244db | 413 | else |
d1decf2b | 414 | croak_memory_wrap(); |
ad7244db | 415 | #endif |
1050c9ca | 416 | #ifdef DEBUGGING |
03c5309f | 417 | if ((SSize_t)size < 0 || (SSize_t)count < 0) |
5637ef5b NC |
418 | Perl_croak_nocontext("panic: calloc, size=%"UVuf", count=%"UVuf, |
419 | (UV)size, (UV)count); | |
1050c9ca | 420 | #endif |
b001a0d1 FC |
421 | #ifdef PERL_DEBUG_READONLY_COW |
422 | if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE, | |
423 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
424 | perror("mmap failed"); | |
425 | abort(); | |
426 | } | |
427 | #elif defined(PERL_TRACK_MEMPOOL) | |
e1a95402 NC |
428 | /* Have to use malloc() because we've added some space for our tracking |
429 | header. */ | |
ad7244db JH |
430 | /* malloc(0) is non-portable. */ |
431 | ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1); | |
e1a95402 NC |
432 | #else |
433 | /* Use calloc() because it might save a memset() if the memory is fresh | |
434 | and clean from the OS. */ | |
ad7244db JH |
435 | if (count && size) |
436 | ptr = (Malloc_t)PerlMem_calloc(count, size); | |
437 | else /* calloc(0) is non-portable. */ | |
438 | ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1); | |
e8dda941 | 439 | #endif |
da927450 | 440 | PERL_ALLOC_CHECK(ptr); |
e1a95402 | 441 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)total_size)); |
bd61b366 | 442 | if (ptr != NULL) { |
3f07c2bc | 443 | #ifdef USE_MDH |
7cb608b5 NC |
444 | { |
445 | struct perl_memory_debug_header *const header | |
446 | = (struct perl_memory_debug_header *)ptr; | |
447 | ||
b001a0d1 | 448 | # ifndef PERL_DEBUG_READONLY_COW |
e1a95402 | 449 | memset((void*)ptr, 0, total_size); |
b001a0d1 FC |
450 | # endif |
451 | # ifdef PERL_TRACK_MEMPOOL | |
7cb608b5 NC |
452 | header->interpreter = aTHX; |
453 | /* Link us into the list. */ | |
454 | header->prev = &PL_memory_debug_header; | |
455 | header->next = PL_memory_debug_header.next; | |
456 | PL_memory_debug_header.next = header; | |
b001a0d1 | 457 | maybe_protect_rw(header->next); |
7cb608b5 | 458 | header->next->prev = header; |
b001a0d1 FC |
459 | maybe_protect_ro(header->next); |
460 | # ifdef PERL_DEBUG_READONLY_COW | |
461 | header->readonly = 0; | |
462 | # endif | |
463 | # endif | |
3f07c2bc | 464 | # ifdef MDH_HAS_SIZE |
e1a95402 | 465 | header->size = total_size; |
cd1541b2 | 466 | # endif |
a78adc84 | 467 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
7cb608b5 | 468 | } |
e8dda941 | 469 | #endif |
1050c9ca PP |
470 | return ptr; |
471 | } | |
0cb20dae | 472 | else { |
1f4d2d4e | 473 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
474 | dTHX; |
475 | #endif | |
476 | if (PL_nomemok) | |
477 | return NULL; | |
4cbe3a7d | 478 | croak_no_mem(); |
0cb20dae | 479 | } |
1050c9ca PP |
480 | } |
481 | ||
cae6d0e5 GS |
482 | /* These must be defined when not using Perl's malloc for binary |
483 | * compatibility */ | |
484 | ||
485 | #ifndef MYMALLOC | |
486 | ||
487 | Malloc_t Perl_malloc (MEM_SIZE nbytes) | |
488 | { | |
20b7effb JH |
489 | #ifdef PERL_IMPLICIT_SYS |
490 | dTHX; | |
491 | #endif | |
077a72a9 | 492 | return (Malloc_t)PerlMem_malloc(nbytes); |
cae6d0e5 GS |
493 | } |
494 | ||
495 | Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size) | |
496 | { | |
20b7effb JH |
497 | #ifdef PERL_IMPLICIT_SYS |
498 | dTHX; | |
499 | #endif | |
077a72a9 | 500 | return (Malloc_t)PerlMem_calloc(elements, size); |
cae6d0e5 GS |
501 | } |
502 | ||
503 | Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes) | |
504 | { | |
20b7effb JH |
505 | #ifdef PERL_IMPLICIT_SYS |
506 | dTHX; | |
507 | #endif | |
077a72a9 | 508 | return (Malloc_t)PerlMem_realloc(where, nbytes); |
cae6d0e5 GS |
509 | } |
510 | ||
511 | Free_t Perl_mfree (Malloc_t where) | |
512 | { | |
20b7effb JH |
513 | #ifdef PERL_IMPLICIT_SYS |
514 | dTHX; | |
515 | #endif | |
cae6d0e5 GS |
516 | PerlMem_free(where); |
517 | } | |
518 | ||
519 | #endif | |
520 | ||
8d063cd8 LW |
521 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
522 | ||
523 | char * | |
5aaab254 | 524 | Perl_delimcpy(char *to, const char *toend, const char *from, const char *fromend, int delim, I32 *retlen) |
8d063cd8 | 525 | { |
eb578fdb | 526 | I32 tolen; |
35da51f7 | 527 | |
7918f24d NC |
528 | PERL_ARGS_ASSERT_DELIMCPY; |
529 | ||
fc36a67e | 530 | for (tolen = 0; from < fromend; from++, tolen++) { |
378cc40b | 531 | if (*from == '\\') { |
35da51f7 | 532 | if (from[1] != delim) { |
fc36a67e PP |
533 | if (to < toend) |
534 | *to++ = *from; | |
535 | tolen++; | |
fc36a67e | 536 | } |
35da51f7 | 537 | from++; |
378cc40b | 538 | } |
bedebaa5 | 539 | else if (*from == delim) |
8d063cd8 | 540 | break; |
fc36a67e PP |
541 | if (to < toend) |
542 | *to++ = *from; | |
8d063cd8 | 543 | } |
bedebaa5 CS |
544 | if (to < toend) |
545 | *to = '\0'; | |
fc36a67e | 546 | *retlen = tolen; |
73d840c0 | 547 | return (char *)from; |
8d063cd8 LW |
548 | } |
549 | ||
550 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 551 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
552 | |
553 | char * | |
5aaab254 | 554 | Perl_instr(const char *big, const char *little) |
378cc40b | 555 | { |
378cc40b | 556 | |
7918f24d NC |
557 | PERL_ARGS_ASSERT_INSTR; |
558 | ||
5d1d68e2 | 559 | return strstr((char*)big, (char*)little); |
378cc40b | 560 | } |
8d063cd8 | 561 | |
e057d092 KW |
562 | /* same as instr but allow embedded nulls. The end pointers point to 1 beyond |
563 | * the final character desired to be checked */ | |
a687059c LW |
564 | |
565 | char * | |
04c9e624 | 566 | Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend) |
8d063cd8 | 567 | { |
7918f24d | 568 | PERL_ARGS_ASSERT_NINSTR; |
4c8626be GA |
569 | if (little >= lend) |
570 | return (char*)big; | |
571 | { | |
8ba22ff4 | 572 | const char first = *little; |
4c8626be | 573 | const char *s, *x; |
8ba22ff4 | 574 | bigend -= lend - little++; |
4c8626be GA |
575 | OUTER: |
576 | while (big <= bigend) { | |
b0ca24ee JH |
577 | if (*big++ == first) { |
578 | for (x=big,s=little; s < lend; x++,s++) { | |
579 | if (*s != *x) | |
580 | goto OUTER; | |
581 | } | |
582 | return (char*)(big-1); | |
4c8626be | 583 | } |
4c8626be | 584 | } |
378cc40b | 585 | } |
bd61b366 | 586 | return NULL; |
a687059c LW |
587 | } |
588 | ||
589 | /* reverse of the above--find last substring */ | |
590 | ||
591 | char * | |
5aaab254 | 592 | Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 593 | { |
eb578fdb KW |
594 | const char *bigbeg; |
595 | const I32 first = *little; | |
596 | const char * const littleend = lend; | |
a687059c | 597 | |
7918f24d NC |
598 | PERL_ARGS_ASSERT_RNINSTR; |
599 | ||
260d78c9 | 600 | if (little >= littleend) |
08105a92 | 601 | return (char*)bigend; |
a687059c LW |
602 | bigbeg = big; |
603 | big = bigend - (littleend - little++); | |
604 | while (big >= bigbeg) { | |
eb578fdb | 605 | const char *s, *x; |
a687059c LW |
606 | if (*big-- != first) |
607 | continue; | |
608 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
4fc877ac | 609 | if (*s != *x) |
a687059c | 610 | break; |
4fc877ac AL |
611 | else { |
612 | x++; | |
613 | s++; | |
a687059c LW |
614 | } |
615 | } | |
616 | if (s >= littleend) | |
08105a92 | 617 | return (char*)(big+1); |
378cc40b | 618 | } |
bd61b366 | 619 | return NULL; |
378cc40b | 620 | } |
a687059c | 621 | |
cf93c79d IZ |
622 | /* As a space optimization, we do not compile tables for strings of length |
623 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
624 | special-cased in fbm_instr(). | |
625 | ||
626 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
627 | ||
954c1994 | 628 | /* |
ccfc67b7 JH |
629 | =head1 Miscellaneous Functions |
630 | ||
954c1994 GS |
631 | =for apidoc fbm_compile |
632 | ||
633 | Analyses the string in order to make fast searches on it using fbm_instr() | |
634 | -- the Boyer-Moore algorithm. | |
635 | ||
636 | =cut | |
637 | */ | |
638 | ||
378cc40b | 639 | void |
7506f9c3 | 640 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
378cc40b | 641 | { |
eb578fdb | 642 | const U8 *s; |
ea725ce6 | 643 | STRLEN i; |
0b71040e | 644 | STRLEN len; |
79072805 | 645 | U32 frequency = 256; |
2bda37ba | 646 | MAGIC *mg; |
00cccd05 | 647 | PERL_DEB( STRLEN rarest = 0 ); |
79072805 | 648 | |
7918f24d NC |
649 | PERL_ARGS_ASSERT_FBM_COMPILE; |
650 | ||
948d2370 | 651 | if (isGV_with_GP(sv) || SvROK(sv)) |
4265b45d NC |
652 | return; |
653 | ||
9402563a NC |
654 | if (SvVALID(sv)) |
655 | return; | |
656 | ||
c517dc2b | 657 | if (flags & FBMcf_TAIL) { |
890ce7af | 658 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
396482e1 | 659 | sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */ |
c517dc2b JH |
660 | if (mg && mg->mg_len >= 0) |
661 | mg->mg_len++; | |
662 | } | |
11609d9c | 663 | if (!SvPOK(sv) || SvNIOKp(sv)) |
66379c06 FC |
664 | s = (U8*)SvPV_force_mutable(sv, len); |
665 | else s = (U8 *)SvPV_mutable(sv, len); | |
d1be9408 | 666 | if (len == 0) /* TAIL might be on a zero-length string. */ |
cf93c79d | 667 | return; |
c13a5c80 | 668 | SvUPGRADE(sv, SVt_PVMG); |
78d0cf80 | 669 | SvIOK_off(sv); |
8eeaf79a NC |
670 | SvNOK_off(sv); |
671 | SvVALID_on(sv); | |
2bda37ba NC |
672 | |
673 | /* "deep magic", the comment used to add. The use of MAGIC itself isn't | |
674 | really. MAGIC was originally added in 79072805bf63abe5 (perl 5.0 alpha 2) | |
675 | to call SvVALID_off() if the scalar was assigned to. | |
676 | ||
677 | The comment itself (and "deeper magic" below) date back to | |
678 | 378cc40b38293ffc (perl 2.0). "deep magic" was an annotation on | |
679 | str->str_pok |= 2; | |
680 | where the magic (presumably) was that the scalar had a BM table hidden | |
681 | inside itself. | |
682 | ||
683 | As MAGIC is always present on BMs [in Perl 5 :-)], we can use it to store | |
684 | the table instead of the previous (somewhat hacky) approach of co-opting | |
685 | the string buffer and storing it after the string. */ | |
686 | ||
687 | assert(!mg_find(sv, PERL_MAGIC_bm)); | |
688 | mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0); | |
689 | assert(mg); | |
690 | ||
02128f11 | 691 | if (len > 2) { |
21aeb718 NC |
692 | /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use |
693 | the BM table. */ | |
66a1b24b | 694 | const U8 mlen = (len>255) ? 255 : (U8)len; |
2bda37ba | 695 | const unsigned char *const sb = s + len - mlen; /* first char (maybe) */ |
eb578fdb | 696 | U8 *table; |
cf93c79d | 697 | |
2bda37ba | 698 | Newx(table, 256, U8); |
7506f9c3 | 699 | memset((void*)table, mlen, 256); |
2bda37ba NC |
700 | mg->mg_ptr = (char *)table; |
701 | mg->mg_len = 256; | |
702 | ||
703 | s += len - 1; /* last char */ | |
02128f11 | 704 | i = 0; |
cf93c79d IZ |
705 | while (s >= sb) { |
706 | if (table[*s] == mlen) | |
7506f9c3 | 707 | table[*s] = (U8)i; |
cf93c79d IZ |
708 | s--, i++; |
709 | } | |
378cc40b | 710 | } |
378cc40b | 711 | |
9cbe880b | 712 | s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */ |
bbce6d69 | 713 | for (i = 0; i < len; i++) { |
22c35a8c | 714 | if (PL_freq[s[i]] < frequency) { |
00cccd05 | 715 | PERL_DEB( rarest = i ); |
22c35a8c | 716 | frequency = PL_freq[s[i]]; |
378cc40b LW |
717 | } |
718 | } | |
cf93c79d IZ |
719 | BmUSEFUL(sv) = 100; /* Initial value */ |
720 | if (flags & FBMcf_TAIL) | |
721 | SvTAIL_on(sv); | |
ea725ce6 | 722 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %"UVuf"\n", |
d80cf470 | 723 | s[rarest], (UV)rarest)); |
378cc40b LW |
724 | } |
725 | ||
cf93c79d IZ |
726 | /* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
727 | /* If SvTAIL is actually due to \Z or \z, this gives false positives | |
728 | if multiline */ | |
729 | ||
954c1994 GS |
730 | /* |
731 | =for apidoc fbm_instr | |
732 | ||
3f4963df FC |
733 | Returns the location of the SV in the string delimited by C<big> and |
734 | C<bigend>. It returns C<NULL> if the string can't be found. The C<sv> | |
954c1994 GS |
735 | does not have to be fbm_compiled, but the search will not be as fast |
736 | then. | |
737 | ||
738 | =cut | |
739 | */ | |
740 | ||
378cc40b | 741 | char * |
5aaab254 | 742 | Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 743 | { |
eb578fdb | 744 | unsigned char *s; |
cf93c79d | 745 | STRLEN l; |
eb578fdb KW |
746 | const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l); |
747 | STRLEN littlelen = l; | |
748 | const I32 multiline = flags & FBMrf_MULTILINE; | |
cf93c79d | 749 | |
7918f24d NC |
750 | PERL_ARGS_ASSERT_FBM_INSTR; |
751 | ||
eb160463 | 752 | if ((STRLEN)(bigend - big) < littlelen) { |
a1d180c4 | 753 | if ( SvTAIL(littlestr) |
eb160463 | 754 | && ((STRLEN)(bigend - big) == littlelen - 1) |
a1d180c4 | 755 | && (littlelen == 1 |
12ae5dfc | 756 | || (*big == *little && |
27da23d5 | 757 | memEQ((char *)big, (char *)little, littlelen - 1)))) |
cf93c79d | 758 | return (char*)big; |
bd61b366 | 759 | return NULL; |
cf93c79d | 760 | } |
378cc40b | 761 | |
21aeb718 NC |
762 | switch (littlelen) { /* Special cases for 0, 1 and 2 */ |
763 | case 0: | |
764 | return (char*)big; /* Cannot be SvTAIL! */ | |
765 | case 1: | |
cf93c79d IZ |
766 | if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ |
767 | /* Know that bigend != big. */ | |
768 | if (bigend[-1] == '\n') | |
769 | return (char *)(bigend - 1); | |
770 | return (char *) bigend; | |
771 | } | |
772 | s = big; | |
773 | while (s < bigend) { | |
774 | if (*s == *little) | |
775 | return (char *)s; | |
776 | s++; | |
777 | } | |
778 | if (SvTAIL(littlestr)) | |
779 | return (char *) bigend; | |
bd61b366 | 780 | return NULL; |
21aeb718 | 781 | case 2: |
cf93c79d IZ |
782 | if (SvTAIL(littlestr) && !multiline) { |
783 | if (bigend[-1] == '\n' && bigend[-2] == *little) | |
784 | return (char*)bigend - 2; | |
785 | if (bigend[-1] == *little) | |
786 | return (char*)bigend - 1; | |
bd61b366 | 787 | return NULL; |
cf93c79d IZ |
788 | } |
789 | { | |
790 | /* This should be better than FBM if c1 == c2, and almost | |
791 | as good otherwise: maybe better since we do less indirection. | |
792 | And we save a lot of memory by caching no table. */ | |
66a1b24b AL |
793 | const unsigned char c1 = little[0]; |
794 | const unsigned char c2 = little[1]; | |
cf93c79d IZ |
795 | |
796 | s = big + 1; | |
797 | bigend--; | |
798 | if (c1 != c2) { | |
799 | while (s <= bigend) { | |
800 | if (s[0] == c2) { | |
801 | if (s[-1] == c1) | |
802 | return (char*)s - 1; | |
803 | s += 2; | |
804 | continue; | |
3fe6f2dc | 805 | } |
cf93c79d IZ |
806 | next_chars: |
807 | if (s[0] == c1) { | |
808 | if (s == bigend) | |
809 | goto check_1char_anchor; | |
810 | if (s[1] == c2) | |
811 | return (char*)s; | |
812 | else { | |
813 | s++; | |
814 | goto next_chars; | |
815 | } | |
816 | } | |
817 | else | |
818 | s += 2; | |
819 | } | |
820 | goto check_1char_anchor; | |
821 | } | |
822 | /* Now c1 == c2 */ | |
823 | while (s <= bigend) { | |
824 | if (s[0] == c1) { | |
825 | if (s[-1] == c1) | |
826 | return (char*)s - 1; | |
827 | if (s == bigend) | |
828 | goto check_1char_anchor; | |
829 | if (s[1] == c1) | |
830 | return (char*)s; | |
831 | s += 3; | |
02128f11 | 832 | } |
c277df42 | 833 | else |
cf93c79d | 834 | s += 2; |
c277df42 | 835 | } |
c277df42 | 836 | } |
cf93c79d IZ |
837 | check_1char_anchor: /* One char and anchor! */ |
838 | if (SvTAIL(littlestr) && (*bigend == *little)) | |
839 | return (char *)bigend; /* bigend is already decremented. */ | |
bd61b366 | 840 | return NULL; |
21aeb718 NC |
841 | default: |
842 | break; /* Only lengths 0 1 and 2 have special-case code. */ | |
d48672a2 | 843 | } |
21aeb718 | 844 | |
cf93c79d | 845 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 846 | s = bigend - littlelen; |
a1d180c4 | 847 | if (s >= big && bigend[-1] == '\n' && *s == *little |
cf93c79d IZ |
848 | /* Automatically of length > 2 */ |
849 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
7506f9c3 | 850 | { |
bbce6d69 | 851 | return (char*)s; /* how sweet it is */ |
7506f9c3 GS |
852 | } |
853 | if (s[1] == *little | |
854 | && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) | |
855 | { | |
cf93c79d | 856 | return (char*)s + 1; /* how sweet it is */ |
7506f9c3 | 857 | } |
bd61b366 | 858 | return NULL; |
02128f11 | 859 | } |
cecf5685 | 860 | if (!SvVALID(littlestr)) { |
c4420975 | 861 | char * const b = ninstr((char*)big,(char*)bigend, |
cf93c79d IZ |
862 | (char*)little, (char*)little + littlelen); |
863 | ||
864 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
865 | /* Chop \n from littlestr: */ | |
866 | s = bigend - littlelen + 1; | |
7506f9c3 GS |
867 | if (*s == *little |
868 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
869 | { | |
3fe6f2dc | 870 | return (char*)s; |
7506f9c3 | 871 | } |
bd61b366 | 872 | return NULL; |
a687059c | 873 | } |
cf93c79d | 874 | return b; |
a687059c | 875 | } |
a1d180c4 | 876 | |
3566a07d NC |
877 | /* Do actual FBM. */ |
878 | if (littlelen > (STRLEN)(bigend - big)) | |
879 | return NULL; | |
880 | ||
881 | { | |
2bda37ba | 882 | const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm); |
eb578fdb | 883 | const unsigned char *oldlittle; |
cf93c79d | 884 | |
316ebaf2 JH |
885 | assert(mg); |
886 | ||
cf93c79d IZ |
887 | --littlelen; /* Last char found by table lookup */ |
888 | ||
889 | s = big + littlelen; | |
890 | little += littlelen; /* last char */ | |
891 | oldlittle = little; | |
892 | if (s < bigend) { | |
316ebaf2 | 893 | const unsigned char * const table = (const unsigned char *) mg->mg_ptr; |
eb578fdb | 894 | I32 tmp; |
cf93c79d IZ |
895 | |
896 | top2: | |
7506f9c3 | 897 | if ((tmp = table[*s])) { |
cf93c79d | 898 | if ((s += tmp) < bigend) |
62b28dd9 | 899 | goto top2; |
cf93c79d IZ |
900 | goto check_end; |
901 | } | |
902 | else { /* less expensive than calling strncmp() */ | |
eb578fdb | 903 | unsigned char * const olds = s; |
cf93c79d IZ |
904 | |
905 | tmp = littlelen; | |
906 | ||
907 | while (tmp--) { | |
908 | if (*--s == *--little) | |
909 | continue; | |
cf93c79d IZ |
910 | s = olds + 1; /* here we pay the price for failure */ |
911 | little = oldlittle; | |
912 | if (s < bigend) /* fake up continue to outer loop */ | |
913 | goto top2; | |
914 | goto check_end; | |
915 | } | |
916 | return (char *)s; | |
a687059c | 917 | } |
378cc40b | 918 | } |
cf93c79d | 919 | check_end: |
c8029a41 | 920 | if ( s == bigend |
cffe132d | 921 | && SvTAIL(littlestr) |
12ae5dfc JH |
922 | && memEQ((char *)(bigend - littlelen), |
923 | (char *)(oldlittle - littlelen), littlelen) ) | |
cf93c79d | 924 | return (char*)bigend - littlelen; |
bd61b366 | 925 | return NULL; |
378cc40b | 926 | } |
378cc40b LW |
927 | } |
928 | ||
e6226b18 KW |
929 | /* |
930 | =for apidoc foldEQ | |
931 | ||
932 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
933 | case-insensitively; false otherwise. Uppercase and lowercase ASCII range bytes | |
934 | match themselves and their opposite case counterparts. Non-cased and non-ASCII | |
935 | range bytes match only themselves. | |
936 | ||
937 | =cut | |
938 | */ | |
939 | ||
940 | ||
79072805 | 941 | I32 |
5aaab254 | 942 | Perl_foldEQ(const char *s1, const char *s2, I32 len) |
79072805 | 943 | { |
eb578fdb KW |
944 | const U8 *a = (const U8 *)s1; |
945 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 946 | |
e6226b18 | 947 | PERL_ARGS_ASSERT_FOLDEQ; |
7918f24d | 948 | |
223f01db KW |
949 | assert(len >= 0); |
950 | ||
79072805 | 951 | while (len--) { |
22c35a8c | 952 | if (*a != *b && *a != PL_fold[*b]) |
e6226b18 | 953 | return 0; |
bbce6d69 PP |
954 | a++,b++; |
955 | } | |
e6226b18 | 956 | return 1; |
bbce6d69 | 957 | } |
1b9f127b | 958 | I32 |
5aaab254 | 959 | Perl_foldEQ_latin1(const char *s1, const char *s2, I32 len) |
1b9f127b KW |
960 | { |
961 | /* Compare non-utf8 using Unicode (Latin1) semantics. Does not work on | |
962 | * MICRO_SIGN, LATIN_SMALL_LETTER_SHARP_S, nor | |
963 | * LATIN_SMALL_LETTER_Y_WITH_DIAERESIS, and does not check for these. Nor | |
964 | * does it check that the strings each have at least 'len' characters */ | |
965 | ||
eb578fdb KW |
966 | const U8 *a = (const U8 *)s1; |
967 | const U8 *b = (const U8 *)s2; | |
1b9f127b KW |
968 | |
969 | PERL_ARGS_ASSERT_FOLDEQ_LATIN1; | |
970 | ||
223f01db KW |
971 | assert(len >= 0); |
972 | ||
1b9f127b KW |
973 | while (len--) { |
974 | if (*a != *b && *a != PL_fold_latin1[*b]) { | |
975 | return 0; | |
976 | } | |
977 | a++, b++; | |
978 | } | |
979 | return 1; | |
980 | } | |
bbce6d69 | 981 | |
e6226b18 KW |
982 | /* |
983 | =for apidoc foldEQ_locale | |
984 | ||
985 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
986 | case-insensitively in the current locale; false otherwise. | |
987 | ||
988 | =cut | |
989 | */ | |
990 | ||
bbce6d69 | 991 | I32 |
5aaab254 | 992 | Perl_foldEQ_locale(const char *s1, const char *s2, I32 len) |
bbce6d69 | 993 | { |
27da23d5 | 994 | dVAR; |
eb578fdb KW |
995 | const U8 *a = (const U8 *)s1; |
996 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 997 | |
e6226b18 | 998 | PERL_ARGS_ASSERT_FOLDEQ_LOCALE; |
7918f24d | 999 | |
223f01db KW |
1000 | assert(len >= 0); |
1001 | ||
bbce6d69 | 1002 | while (len--) { |
22c35a8c | 1003 | if (*a != *b && *a != PL_fold_locale[*b]) |
e6226b18 | 1004 | return 0; |
bbce6d69 | 1005 | a++,b++; |
79072805 | 1006 | } |
e6226b18 | 1007 | return 1; |
79072805 LW |
1008 | } |
1009 | ||
8d063cd8 LW |
1010 | /* copy a string to a safe spot */ |
1011 | ||
954c1994 | 1012 | /* |
ccfc67b7 JH |
1013 | =head1 Memory Management |
1014 | ||
954c1994 GS |
1015 | =for apidoc savepv |
1016 | ||
72d33970 FC |
1017 | Perl's version of C<strdup()>. Returns a pointer to a newly allocated |
1018 | string which is a duplicate of C<pv>. The size of the string is | |
30a15352 KW |
1019 | determined by C<strlen()>, which means it may not contain embedded C<NUL> |
1020 | characters and must have a trailing C<NUL>. The memory allocated for the new | |
1021 | string can be freed with the C<Safefree()> function. | |
954c1994 | 1022 | |
0358c255 KW |
1023 | On some platforms, Windows for example, all allocated memory owned by a thread |
1024 | is deallocated when that thread ends. So if you need that not to happen, you | |
1025 | need to use the shared memory functions, such as C<L</savesharedpv>>. | |
1026 | ||
954c1994 GS |
1027 | =cut |
1028 | */ | |
1029 | ||
8d063cd8 | 1030 | char * |
efdfce31 | 1031 | Perl_savepv(pTHX_ const char *pv) |
8d063cd8 | 1032 | { |
96a5add6 | 1033 | PERL_UNUSED_CONTEXT; |
e90e2364 | 1034 | if (!pv) |
bd61b366 | 1035 | return NULL; |
66a1b24b AL |
1036 | else { |
1037 | char *newaddr; | |
1038 | const STRLEN pvlen = strlen(pv)+1; | |
10edeb5d JH |
1039 | Newx(newaddr, pvlen, char); |
1040 | return (char*)memcpy(newaddr, pv, pvlen); | |
66a1b24b | 1041 | } |
8d063cd8 LW |
1042 | } |
1043 | ||
a687059c LW |
1044 | /* same thing but with a known length */ |
1045 | ||
954c1994 GS |
1046 | /* |
1047 | =for apidoc savepvn | |
1048 | ||
72d33970 | 1049 | Perl's version of what C<strndup()> would be if it existed. Returns a |
61a925ed | 1050 | pointer to a newly allocated string which is a duplicate of the first |
72d33970 | 1051 | C<len> bytes from C<pv>, plus a trailing |
6602b933 | 1052 | C<NUL> byte. The memory allocated for |
cbf82dd0 | 1053 | the new string can be freed with the C<Safefree()> function. |
954c1994 | 1054 | |
0358c255 KW |
1055 | On some platforms, Windows for example, all allocated memory owned by a thread |
1056 | is deallocated when that thread ends. So if you need that not to happen, you | |
1057 | need to use the shared memory functions, such as C<L</savesharedpvn>>. | |
1058 | ||
954c1994 GS |
1059 | =cut |
1060 | */ | |
1061 | ||
a687059c | 1062 | char * |
5aaab254 | 1063 | Perl_savepvn(pTHX_ const char *pv, I32 len) |
a687059c | 1064 | { |
eb578fdb | 1065 | char *newaddr; |
96a5add6 | 1066 | PERL_UNUSED_CONTEXT; |
a687059c | 1067 | |
223f01db KW |
1068 | assert(len >= 0); |
1069 | ||
a02a5408 | 1070 | Newx(newaddr,len+1,char); |
92110913 | 1071 | /* Give a meaning to NULL pointer mainly for the use in sv_magic() */ |
efdfce31 | 1072 | if (pv) { |
e90e2364 NC |
1073 | /* might not be null terminated */ |
1074 | newaddr[len] = '\0'; | |
07409e01 | 1075 | return (char *) CopyD(pv,newaddr,len,char); |
92110913 NIS |
1076 | } |
1077 | else { | |
07409e01 | 1078 | return (char *) ZeroD(newaddr,len+1,char); |
92110913 | 1079 | } |
a687059c LW |
1080 | } |
1081 | ||
05ec9bb3 NIS |
1082 | /* |
1083 | =for apidoc savesharedpv | |
1084 | ||
61a925ed AMS |
1085 | A version of C<savepv()> which allocates the duplicate string in memory |
1086 | which is shared between threads. | |
05ec9bb3 NIS |
1087 | |
1088 | =cut | |
1089 | */ | |
1090 | char * | |
efdfce31 | 1091 | Perl_savesharedpv(pTHX_ const char *pv) |
05ec9bb3 | 1092 | { |
eb578fdb | 1093 | char *newaddr; |
490a0e98 | 1094 | STRLEN pvlen; |
dc3bf405 BF |
1095 | |
1096 | PERL_UNUSED_CONTEXT; | |
1097 | ||
e90e2364 | 1098 | if (!pv) |
bd61b366 | 1099 | return NULL; |
e90e2364 | 1100 | |
490a0e98 NC |
1101 | pvlen = strlen(pv)+1; |
1102 | newaddr = (char*)PerlMemShared_malloc(pvlen); | |
e90e2364 | 1103 | if (!newaddr) { |
4cbe3a7d | 1104 | croak_no_mem(); |
05ec9bb3 | 1105 | } |
10edeb5d | 1106 | return (char*)memcpy(newaddr, pv, pvlen); |
05ec9bb3 NIS |
1107 | } |
1108 | ||
2e0de35c | 1109 | /* |
d9095cec NC |
1110 | =for apidoc savesharedpvn |
1111 | ||
1112 | A version of C<savepvn()> which allocates the duplicate string in memory | |
72d33970 | 1113 | which is shared between threads. (With the specific difference that a NULL |
d9095cec NC |
1114 | pointer is not acceptable) |
1115 | ||
1116 | =cut | |
1117 | */ | |
1118 | char * | |
1119 | Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len) | |
1120 | { | |
1121 | char *const newaddr = (char*)PerlMemShared_malloc(len + 1); | |
7918f24d | 1122 | |
dc3bf405 | 1123 | PERL_UNUSED_CONTEXT; |
6379d4a9 | 1124 | /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */ |
7918f24d | 1125 | |
d9095cec | 1126 | if (!newaddr) { |
4cbe3a7d | 1127 | croak_no_mem(); |
d9095cec NC |
1128 | } |
1129 | newaddr[len] = '\0'; | |
1130 | return (char*)memcpy(newaddr, pv, len); | |
1131 | } | |
1132 | ||
1133 | /* | |
2e0de35c NC |
1134 | =for apidoc savesvpv |
1135 | ||
6832267f | 1136 | A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from |
2e0de35c NC |
1137 | the passed in SV using C<SvPV()> |
1138 | ||
0358c255 KW |
1139 | On some platforms, Windows for example, all allocated memory owned by a thread |
1140 | is deallocated when that thread ends. So if you need that not to happen, you | |
1141 | need to use the shared memory functions, such as C<L</savesharedsvpv>>. | |
1142 | ||
2e0de35c NC |
1143 | =cut |
1144 | */ | |
1145 | ||
1146 | char * | |
1147 | Perl_savesvpv(pTHX_ SV *sv) | |
1148 | { | |
1149 | STRLEN len; | |
7452cf6a | 1150 | const char * const pv = SvPV_const(sv, len); |
eb578fdb | 1151 | char *newaddr; |
2e0de35c | 1152 | |
7918f24d NC |
1153 | PERL_ARGS_ASSERT_SAVESVPV; |
1154 | ||
26866f99 | 1155 | ++len; |
a02a5408 | 1156 | Newx(newaddr,len,char); |
07409e01 | 1157 | return (char *) CopyD(pv,newaddr,len,char); |
2e0de35c | 1158 | } |
05ec9bb3 | 1159 | |
9dcc53ea Z |
1160 | /* |
1161 | =for apidoc savesharedsvpv | |
1162 | ||
1163 | A version of C<savesharedpv()> which allocates the duplicate string in | |
1164 | memory which is shared between threads. | |
1165 | ||
1166 | =cut | |
1167 | */ | |
1168 | ||
1169 | char * | |
1170 | Perl_savesharedsvpv(pTHX_ SV *sv) | |
1171 | { | |
1172 | STRLEN len; | |
1173 | const char * const pv = SvPV_const(sv, len); | |
1174 | ||
1175 | PERL_ARGS_ASSERT_SAVESHAREDSVPV; | |
1176 | ||
1177 | return savesharedpvn(pv, len); | |
1178 | } | |
05ec9bb3 | 1179 | |
cea2e8a9 | 1180 | /* the SV for Perl_form() and mess() is not kept in an arena */ |
fc36a67e | 1181 | |
76e3520e | 1182 | STATIC SV * |
cea2e8a9 | 1183 | S_mess_alloc(pTHX) |
fc36a67e PP |
1184 | { |
1185 | SV *sv; | |
1186 | XPVMG *any; | |
1187 | ||
627364f1 | 1188 | if (PL_phase != PERL_PHASE_DESTRUCT) |
84bafc02 | 1189 | return newSVpvs_flags("", SVs_TEMP); |
e72dc28c | 1190 | |
0372dbb6 GS |
1191 | if (PL_mess_sv) |
1192 | return PL_mess_sv; | |
1193 | ||
fc36a67e | 1194 | /* Create as PVMG now, to avoid any upgrading later */ |
a02a5408 JC |
1195 | Newx(sv, 1, SV); |
1196 | Newxz(any, 1, XPVMG); | |
fc36a67e PP |
1197 | SvFLAGS(sv) = SVt_PVMG; |
1198 | SvANY(sv) = (void*)any; | |
6136c704 | 1199 | SvPV_set(sv, NULL); |
fc36a67e | 1200 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ |
e72dc28c | 1201 | PL_mess_sv = sv; |
fc36a67e PP |
1202 | return sv; |
1203 | } | |
1204 | ||
c5be433b | 1205 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1206 | char * |
1207 | Perl_form_nocontext(const char* pat, ...) | |
1208 | { | |
1209 | dTHX; | |
c5be433b | 1210 | char *retval; |
cea2e8a9 | 1211 | va_list args; |
7918f24d | 1212 | PERL_ARGS_ASSERT_FORM_NOCONTEXT; |
cea2e8a9 | 1213 | va_start(args, pat); |
c5be433b | 1214 | retval = vform(pat, &args); |
cea2e8a9 | 1215 | va_end(args); |
c5be433b | 1216 | return retval; |
cea2e8a9 | 1217 | } |
c5be433b | 1218 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1219 | |
7c9e965c | 1220 | /* |
ccfc67b7 | 1221 | =head1 Miscellaneous Functions |
7c9e965c JP |
1222 | =for apidoc form |
1223 | ||
1224 | Takes a sprintf-style format pattern and conventional | |
1225 | (non-SV) arguments and returns the formatted string. | |
1226 | ||
1227 | (char *) Perl_form(pTHX_ const char* pat, ...) | |
1228 | ||
1229 | can be used any place a string (char *) is required: | |
1230 | ||
1231 | char * s = Perl_form("%d.%d",major,minor); | |
1232 | ||
1233 | Uses a single private buffer so if you want to format several strings you | |
1234 | must explicitly copy the earlier strings away (and free the copies when you | |
1235 | are done). | |
1236 | ||
1237 | =cut | |
1238 | */ | |
1239 | ||
8990e307 | 1240 | char * |
864dbfa3 | 1241 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1242 | { |
c5be433b | 1243 | char *retval; |
46fc3d4c | 1244 | va_list args; |
7918f24d | 1245 | PERL_ARGS_ASSERT_FORM; |
46fc3d4c | 1246 | va_start(args, pat); |
c5be433b | 1247 | retval = vform(pat, &args); |
46fc3d4c | 1248 | va_end(args); |
c5be433b GS |
1249 | return retval; |
1250 | } | |
1251 | ||
1252 | char * | |
1253 | Perl_vform(pTHX_ const char *pat, va_list *args) | |
1254 | { | |
2d03de9c | 1255 | SV * const sv = mess_alloc(); |
7918f24d | 1256 | PERL_ARGS_ASSERT_VFORM; |
4608196e | 1257 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
e72dc28c | 1258 | return SvPVX(sv); |
46fc3d4c | 1259 | } |
a687059c | 1260 | |
c5df3096 Z |
1261 | /* |
1262 | =for apidoc Am|SV *|mess|const char *pat|... | |
1263 | ||
1264 | Take a sprintf-style format pattern and argument list. These are used to | |
1265 | generate a string message. If the message does not end with a newline, | |
1266 | then it will be extended with some indication of the current location | |
1267 | in the code, as described for L</mess_sv>. | |
1268 | ||
1269 | Normally, the resulting message is returned in a new mortal SV. | |
1270 | During global destruction a single SV may be shared between uses of | |
1271 | this function. | |
1272 | ||
1273 | =cut | |
1274 | */ | |
1275 | ||
5a844595 GS |
1276 | #if defined(PERL_IMPLICIT_CONTEXT) |
1277 | SV * | |
1278 | Perl_mess_nocontext(const char *pat, ...) | |
1279 | { | |
1280 | dTHX; | |
1281 | SV *retval; | |
1282 | va_list args; | |
7918f24d | 1283 | PERL_ARGS_ASSERT_MESS_NOCONTEXT; |
5a844595 GS |
1284 | va_start(args, pat); |
1285 | retval = vmess(pat, &args); | |
1286 | va_end(args); | |
1287 | return retval; | |
1288 | } | |
1289 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1290 | ||
06bf62c7 | 1291 | SV * |
5a844595 GS |
1292 | Perl_mess(pTHX_ const char *pat, ...) |
1293 | { | |
1294 | SV *retval; | |
1295 | va_list args; | |
7918f24d | 1296 | PERL_ARGS_ASSERT_MESS; |
5a844595 GS |
1297 | va_start(args, pat); |
1298 | retval = vmess(pat, &args); | |
1299 | va_end(args); | |
1300 | return retval; | |
1301 | } | |
1302 | ||
25502127 FC |
1303 | const COP* |
1304 | Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop, | |
1305 | bool opnext) | |
ae7d165c | 1306 | { |
25502127 FC |
1307 | /* Look for curop starting from o. cop is the last COP we've seen. */ |
1308 | /* opnext means that curop is actually the ->op_next of the op we are | |
1309 | seeking. */ | |
ae7d165c | 1310 | |
7918f24d NC |
1311 | PERL_ARGS_ASSERT_CLOSEST_COP; |
1312 | ||
25502127 FC |
1313 | if (!o || !curop || ( |
1314 | opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop | |
1315 | )) | |
fabdb6c0 | 1316 | return cop; |
ae7d165c PJ |
1317 | |
1318 | if (o->op_flags & OPf_KIDS) { | |
5f66b61c | 1319 | const OP *kid; |
e6dae479 | 1320 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { |
5f66b61c | 1321 | const COP *new_cop; |
ae7d165c PJ |
1322 | |
1323 | /* If the OP_NEXTSTATE has been optimised away we can still use it | |
1324 | * the get the file and line number. */ | |
1325 | ||
1326 | if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) | |
5f66b61c | 1327 | cop = (const COP *)kid; |
ae7d165c PJ |
1328 | |
1329 | /* Keep searching, and return when we've found something. */ | |
1330 | ||
25502127 | 1331 | new_cop = closest_cop(cop, kid, curop, opnext); |
fabdb6c0 AL |
1332 | if (new_cop) |
1333 | return new_cop; | |
ae7d165c PJ |
1334 | } |
1335 | } | |
1336 | ||
1337 | /* Nothing found. */ | |
1338 | ||
5f66b61c | 1339 | return NULL; |
ae7d165c PJ |
1340 | } |
1341 | ||
c5df3096 Z |
1342 | /* |
1343 | =for apidoc Am|SV *|mess_sv|SV *basemsg|bool consume | |
1344 | ||
1345 | Expands a message, intended for the user, to include an indication of | |
1346 | the current location in the code, if the message does not already appear | |
1347 | to be complete. | |
1348 | ||
1349 | C<basemsg> is the initial message or object. If it is a reference, it | |
1350 | will be used as-is and will be the result of this function. Otherwise it | |
1351 | is used as a string, and if it already ends with a newline, it is taken | |
1352 | to be complete, and the result of this function will be the same string. | |
1353 | If the message does not end with a newline, then a segment such as C<at | |
1354 | foo.pl line 37> will be appended, and possibly other clauses indicating | |
1355 | the current state of execution. The resulting message will end with a | |
1356 | dot and a newline. | |
1357 | ||
1358 | Normally, the resulting message is returned in a new mortal SV. | |
1359 | During global destruction a single SV may be shared between uses of this | |
1360 | function. If C<consume> is true, then the function is permitted (but not | |
1361 | required) to modify and return C<basemsg> instead of allocating a new SV. | |
1362 | ||
1363 | =cut | |
1364 | */ | |
1365 | ||
5a844595 | 1366 | SV * |
c5df3096 | 1367 | Perl_mess_sv(pTHX_ SV *basemsg, bool consume) |
46fc3d4c | 1368 | { |
c5df3096 | 1369 | SV *sv; |
46fc3d4c | 1370 | |
0762e42f | 1371 | #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR) |
470dd224 JH |
1372 | { |
1373 | char *ws; | |
22ff3130 | 1374 | UV wi; |
470dd224 | 1375 | /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */ |
22ff3130 HS |
1376 | if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR")) |
1377 | && grok_atoUV(ws, &wi, NULL) | |
1378 | && wi <= PERL_INT_MAX | |
1379 | ) { | |
1380 | Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1); | |
470dd224 JH |
1381 | } |
1382 | } | |
1383 | #endif | |
1384 | ||
c5df3096 Z |
1385 | PERL_ARGS_ASSERT_MESS_SV; |
1386 | ||
1387 | if (SvROK(basemsg)) { | |
1388 | if (consume) { | |
1389 | sv = basemsg; | |
1390 | } | |
1391 | else { | |
1392 | sv = mess_alloc(); | |
1393 | sv_setsv(sv, basemsg); | |
1394 | } | |
1395 | return sv; | |
1396 | } | |
1397 | ||
1398 | if (SvPOK(basemsg) && consume) { | |
1399 | sv = basemsg; | |
1400 | } | |
1401 | else { | |
1402 | sv = mess_alloc(); | |
1403 | sv_copypv(sv, basemsg); | |
1404 | } | |
7918f24d | 1405 | |
46fc3d4c | 1406 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
ae7d165c PJ |
1407 | /* |
1408 | * Try and find the file and line for PL_op. This will usually be | |
1409 | * PL_curcop, but it might be a cop that has been optimised away. We | |
1410 | * can try to find such a cop by searching through the optree starting | |
1411 | * from the sibling of PL_curcop. | |
1412 | */ | |
1413 | ||
25502127 | 1414 | const COP *cop = |
e6dae479 | 1415 | closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE); |
5f66b61c AL |
1416 | if (!cop) |
1417 | cop = PL_curcop; | |
ae7d165c PJ |
1418 | |
1419 | if (CopLINE(cop)) | |
ed094faf | 1420 | Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf, |
3aed30dc | 1421 | OutCopFILE(cop), (IV)CopLINE(cop)); |
191f87d5 DH |
1422 | /* Seems that GvIO() can be untrustworthy during global destruction. */ |
1423 | if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO) | |
1424 | && IoLINES(GvIOp(PL_last_in_gv))) | |
1425 | { | |
2748e602 | 1426 | STRLEN l; |
e1ec3a88 | 1427 | const bool line_mode = (RsSIMPLE(PL_rs) && |
2748e602 | 1428 | *SvPV_const(PL_rs,l) == '\n' && l == 1); |
3b46b707 BF |
1429 | Perl_sv_catpvf(aTHX_ sv, ", <%"SVf"> %s %"IVdf, |
1430 | SVfARG(PL_last_in_gv == PL_argvgv | |
1431 | ? &PL_sv_no | |
1432 | : sv_2mortal(newSVhek(GvNAME_HEK(PL_last_in_gv)))), | |
edc2eac3 JH |
1433 | line_mode ? "line" : "chunk", |
1434 | (IV)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1435 | } |
627364f1 | 1436 | if (PL_phase == PERL_PHASE_DESTRUCT) |
5f66b61c AL |
1437 | sv_catpvs(sv, " during global destruction"); |
1438 | sv_catpvs(sv, ".\n"); | |
a687059c | 1439 | } |
06bf62c7 | 1440 | return sv; |
a687059c LW |
1441 | } |
1442 | ||
c5df3096 Z |
1443 | /* |
1444 | =for apidoc Am|SV *|vmess|const char *pat|va_list *args | |
1445 | ||
1446 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1447 | argument list. These are used to generate a string message. If the | |
1448 | message does not end with a newline, then it will be extended with | |
1449 | some indication of the current location in the code, as described for | |
1450 | L</mess_sv>. | |
1451 | ||
1452 | Normally, the resulting message is returned in a new mortal SV. | |
1453 | During global destruction a single SV may be shared between uses of | |
1454 | this function. | |
1455 | ||
1456 | =cut | |
1457 | */ | |
1458 | ||
1459 | SV * | |
1460 | Perl_vmess(pTHX_ const char *pat, va_list *args) | |
1461 | { | |
c5df3096 Z |
1462 | SV * const sv = mess_alloc(); |
1463 | ||
1464 | PERL_ARGS_ASSERT_VMESS; | |
1465 | ||
1466 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); | |
1467 | return mess_sv(sv, 1); | |
1468 | } | |
1469 | ||
7ff03255 | 1470 | void |
7d0994e0 | 1471 | Perl_write_to_stderr(pTHX_ SV* msv) |
7ff03255 SG |
1472 | { |
1473 | IO *io; | |
1474 | MAGIC *mg; | |
1475 | ||
7918f24d NC |
1476 | PERL_ARGS_ASSERT_WRITE_TO_STDERR; |
1477 | ||
7ff03255 SG |
1478 | if (PL_stderrgv && SvREFCNT(PL_stderrgv) |
1479 | && (io = GvIO(PL_stderrgv)) | |
daba3364 | 1480 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
36925d9e | 1481 | Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT), |
d1d7a15d | 1482 | G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv); |
7ff03255 | 1483 | else { |
53c1dcc0 | 1484 | PerlIO * const serr = Perl_error_log; |
7ff03255 | 1485 | |
83c55556 | 1486 | do_print(msv, serr); |
7ff03255 | 1487 | (void)PerlIO_flush(serr); |
7ff03255 SG |
1488 | } |
1489 | } | |
1490 | ||
c5df3096 Z |
1491 | /* |
1492 | =head1 Warning and Dieing | |
1493 | */ | |
1494 | ||
1495 | /* Common code used in dieing and warning */ | |
1496 | ||
1497 | STATIC SV * | |
1498 | S_with_queued_errors(pTHX_ SV *ex) | |
1499 | { | |
1500 | PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS; | |
1501 | if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) { | |
1502 | sv_catsv(PL_errors, ex); | |
1503 | ex = sv_mortalcopy(PL_errors); | |
1504 | SvCUR_set(PL_errors, 0); | |
1505 | } | |
1506 | return ex; | |
1507 | } | |
3ab1ac99 | 1508 | |
46d9c920 | 1509 | STATIC bool |
c5df3096 | 1510 | S_invoke_exception_hook(pTHX_ SV *ex, bool warn) |
63315e18 NC |
1511 | { |
1512 | HV *stash; | |
1513 | GV *gv; | |
1514 | CV *cv; | |
46d9c920 NC |
1515 | SV **const hook = warn ? &PL_warnhook : &PL_diehook; |
1516 | /* sv_2cv might call Perl_croak() or Perl_warner() */ | |
1517 | SV * const oldhook = *hook; | |
1518 | ||
c5df3096 Z |
1519 | if (!oldhook) |
1520 | return FALSE; | |
63315e18 | 1521 | |
63315e18 | 1522 | ENTER; |
46d9c920 NC |
1523 | SAVESPTR(*hook); |
1524 | *hook = NULL; | |
1525 | cv = sv_2cv(oldhook, &stash, &gv, 0); | |
63315e18 NC |
1526 | LEAVE; |
1527 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1528 | dSP; | |
c5df3096 | 1529 | SV *exarg; |
63315e18 NC |
1530 | |
1531 | ENTER; | |
46d9c920 NC |
1532 | if (warn) { |
1533 | SAVESPTR(*hook); | |
1534 | *hook = NULL; | |
1535 | } | |
c5df3096 Z |
1536 | exarg = newSVsv(ex); |
1537 | SvREADONLY_on(exarg); | |
1538 | SAVEFREESV(exarg); | |
63315e18 | 1539 | |
46d9c920 | 1540 | PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK); |
63315e18 | 1541 | PUSHMARK(SP); |
c5df3096 | 1542 | XPUSHs(exarg); |
63315e18 | 1543 | PUTBACK; |
daba3364 | 1544 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
63315e18 NC |
1545 | POPSTACK; |
1546 | LEAVE; | |
46d9c920 | 1547 | return TRUE; |
63315e18 | 1548 | } |
46d9c920 | 1549 | return FALSE; |
63315e18 NC |
1550 | } |
1551 | ||
c5df3096 Z |
1552 | /* |
1553 | =for apidoc Am|OP *|die_sv|SV *baseex | |
e07360fa | 1554 | |
c5df3096 Z |
1555 | Behaves the same as L</croak_sv>, except for the return type. |
1556 | It should be used only where the C<OP *> return type is required. | |
1557 | The function never actually returns. | |
e07360fa | 1558 | |
c5df3096 Z |
1559 | =cut |
1560 | */ | |
e07360fa | 1561 | |
f8d5a522 DD |
1562 | #ifdef _MSC_VER |
1563 | # pragma warning( push ) | |
1564 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1565 | __declspec(noreturn) has non-void return type */ | |
1566 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1567 | __declspec(noreturn) has a return statement */ | |
1568 | #endif | |
c5df3096 Z |
1569 | OP * |
1570 | Perl_die_sv(pTHX_ SV *baseex) | |
36477c24 | 1571 | { |
c5df3096 Z |
1572 | PERL_ARGS_ASSERT_DIE_SV; |
1573 | croak_sv(baseex); | |
e5964223 | 1574 | /* NOTREACHED */ |
117af67d | 1575 | NORETURN_FUNCTION_END; |
36477c24 | 1576 | } |
f8d5a522 DD |
1577 | #ifdef _MSC_VER |
1578 | # pragma warning( pop ) | |
1579 | #endif | |
36477c24 | 1580 | |
c5df3096 Z |
1581 | /* |
1582 | =for apidoc Am|OP *|die|const char *pat|... | |
1583 | ||
1584 | Behaves the same as L</croak>, except for the return type. | |
1585 | It should be used only where the C<OP *> return type is required. | |
1586 | The function never actually returns. | |
1587 | ||
1588 | =cut | |
1589 | */ | |
1590 | ||
c5be433b | 1591 | #if defined(PERL_IMPLICIT_CONTEXT) |
f8d5a522 DD |
1592 | #ifdef _MSC_VER |
1593 | # pragma warning( push ) | |
1594 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1595 | __declspec(noreturn) has non-void return type */ | |
1596 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1597 | __declspec(noreturn) has a return statement */ | |
1598 | #endif | |
cea2e8a9 GS |
1599 | OP * |
1600 | Perl_die_nocontext(const char* pat, ...) | |
a687059c | 1601 | { |
cea2e8a9 | 1602 | dTHX; |
a687059c | 1603 | va_list args; |
cea2e8a9 | 1604 | va_start(args, pat); |
c5df3096 | 1605 | vcroak(pat, &args); |
e5964223 | 1606 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 | 1607 | va_end(args); |
117af67d | 1608 | NORETURN_FUNCTION_END; |
cea2e8a9 | 1609 | } |
f8d5a522 DD |
1610 | #ifdef _MSC_VER |
1611 | # pragma warning( pop ) | |
1612 | #endif | |
c5be433b | 1613 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1614 | |
f8d5a522 DD |
1615 | #ifdef _MSC_VER |
1616 | # pragma warning( push ) | |
1617 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1618 | __declspec(noreturn) has non-void return type */ | |
1619 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1620 | __declspec(noreturn) has a return statement */ | |
1621 | #endif | |
cea2e8a9 GS |
1622 | OP * |
1623 | Perl_die(pTHX_ const char* pat, ...) | |
1624 | { | |
cea2e8a9 GS |
1625 | va_list args; |
1626 | va_start(args, pat); | |
c5df3096 | 1627 | vcroak(pat, &args); |
e5964223 | 1628 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 | 1629 | va_end(args); |
117af67d | 1630 | NORETURN_FUNCTION_END; |
cea2e8a9 | 1631 | } |
f8d5a522 DD |
1632 | #ifdef _MSC_VER |
1633 | # pragma warning( pop ) | |
1634 | #endif | |
cea2e8a9 | 1635 | |
c5df3096 Z |
1636 | /* |
1637 | =for apidoc Am|void|croak_sv|SV *baseex | |
1638 | ||
1639 | This is an XS interface to Perl's C<die> function. | |
1640 | ||
1641 | C<baseex> is the error message or object. If it is a reference, it | |
1642 | will be used as-is. Otherwise it is used as a string, and if it does | |
1643 | not end with a newline then it will be extended with some indication of | |
1644 | the current location in the code, as described for L</mess_sv>. | |
1645 | ||
1646 | The error message or object will be used as an exception, by default | |
1647 | returning control to the nearest enclosing C<eval>, but subject to | |
1648 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv> | |
1649 | function never returns normally. | |
1650 | ||
1651 | To die with a simple string message, the L</croak> function may be | |
1652 | more convenient. | |
1653 | ||
1654 | =cut | |
1655 | */ | |
1656 | ||
c5be433b | 1657 | void |
c5df3096 | 1658 | Perl_croak_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1659 | { |
c5df3096 Z |
1660 | SV *ex = with_queued_errors(mess_sv(baseex, 0)); |
1661 | PERL_ARGS_ASSERT_CROAK_SV; | |
1662 | invoke_exception_hook(ex, FALSE); | |
1663 | die_unwind(ex); | |
1664 | } | |
1665 | ||
1666 | /* | |
1667 | =for apidoc Am|void|vcroak|const char *pat|va_list *args | |
1668 | ||
1669 | This is an XS interface to Perl's C<die> function. | |
1670 | ||
1671 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1672 | argument list. These are used to generate a string message. If the | |
1673 | message does not end with a newline, then it will be extended with | |
1674 | some indication of the current location in the code, as described for | |
1675 | L</mess_sv>. | |
1676 | ||
1677 | The error message will be used as an exception, by default | |
1678 | returning control to the nearest enclosing C<eval>, but subject to | |
1679 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1680 | function never returns normally. | |
a687059c | 1681 | |
c5df3096 Z |
1682 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> |
1683 | (C<$@>) will be used as an error message or object instead of building an | |
1684 | error message from arguments. If you want to throw a non-string object, | |
1685 | or build an error message in an SV yourself, it is preferable to use | |
1686 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
5a844595 | 1687 | |
c5df3096 Z |
1688 | =cut |
1689 | */ | |
1690 | ||
1691 | void | |
1692 | Perl_vcroak(pTHX_ const char* pat, va_list *args) | |
1693 | { | |
1694 | SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0)); | |
1695 | invoke_exception_hook(ex, FALSE); | |
1696 | die_unwind(ex); | |
a687059c LW |
1697 | } |
1698 | ||
c5df3096 Z |
1699 | /* |
1700 | =for apidoc Am|void|croak|const char *pat|... | |
1701 | ||
1702 | This is an XS interface to Perl's C<die> function. | |
1703 | ||
1704 | Take a sprintf-style format pattern and argument list. These are used to | |
1705 | generate a string message. If the message does not end with a newline, | |
1706 | then it will be extended with some indication of the current location | |
1707 | in the code, as described for L</mess_sv>. | |
1708 | ||
1709 | The error message will be used as an exception, by default | |
1710 | returning control to the nearest enclosing C<eval>, but subject to | |
1711 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1712 | function never returns normally. | |
1713 | ||
1714 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> | |
1715 | (C<$@>) will be used as an error message or object instead of building an | |
1716 | error message from arguments. If you want to throw a non-string object, | |
1717 | or build an error message in an SV yourself, it is preferable to use | |
1718 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
1719 | ||
1720 | =cut | |
1721 | */ | |
1722 | ||
c5be433b | 1723 | #if defined(PERL_IMPLICIT_CONTEXT) |
8990e307 | 1724 | void |
cea2e8a9 | 1725 | Perl_croak_nocontext(const char *pat, ...) |
a687059c | 1726 | { |
cea2e8a9 | 1727 | dTHX; |
a687059c | 1728 | va_list args; |
cea2e8a9 | 1729 | va_start(args, pat); |
c5be433b | 1730 | vcroak(pat, &args); |
e5964223 | 1731 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 GS |
1732 | va_end(args); |
1733 | } | |
1734 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1735 | ||
c5df3096 Z |
1736 | void |
1737 | Perl_croak(pTHX_ const char *pat, ...) | |
1738 | { | |
1739 | va_list args; | |
1740 | va_start(args, pat); | |
1741 | vcroak(pat, &args); | |
e5964223 | 1742 | NOT_REACHED; /* NOTREACHED */ |
c5df3096 Z |
1743 | va_end(args); |
1744 | } | |
1745 | ||
954c1994 | 1746 | /* |
6ad8f254 NC |
1747 | =for apidoc Am|void|croak_no_modify |
1748 | ||
1749 | Exactly equivalent to C<Perl_croak(aTHX_ "%s", PL_no_modify)>, but generates | |
72d33970 | 1750 | terser object code than using C<Perl_croak>. Less code used on exception code |
6ad8f254 NC |
1751 | paths reduces CPU cache pressure. |
1752 | ||
d8e47b5c | 1753 | =cut |
6ad8f254 NC |
1754 | */ |
1755 | ||
1756 | void | |
88772978 | 1757 | Perl_croak_no_modify(void) |
6ad8f254 | 1758 | { |
cb077ed2 | 1759 | Perl_croak_nocontext( "%s", PL_no_modify); |
6ad8f254 NC |
1760 | } |
1761 | ||
4cbe3a7d DD |
1762 | /* does not return, used in util.c perlio.c and win32.c |
1763 | This is typically called when malloc returns NULL. | |
1764 | */ | |
1765 | void | |
88772978 | 1766 | Perl_croak_no_mem(void) |
4cbe3a7d DD |
1767 | { |
1768 | dTHX; | |
77c1c05b | 1769 | |
375ed12a JH |
1770 | int fd = PerlIO_fileno(Perl_error_log); |
1771 | if (fd < 0) | |
1772 | SETERRNO(EBADF,RMS_IFI); | |
1773 | else { | |
1774 | /* Can't use PerlIO to write as it allocates memory */ | |
b469f1e0 | 1775 | PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1)); |
375ed12a | 1776 | } |
4cbe3a7d DD |
1777 | my_exit(1); |
1778 | } | |
1779 | ||
3d04513d DD |
1780 | /* does not return, used only in POPSTACK */ |
1781 | void | |
1782 | Perl_croak_popstack(void) | |
1783 | { | |
1784 | dTHX; | |
1785 | PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); | |
1786 | my_exit(1); | |
1787 | } | |
1788 | ||
6ad8f254 | 1789 | /* |
c5df3096 | 1790 | =for apidoc Am|void|warn_sv|SV *baseex |
ccfc67b7 | 1791 | |
c5df3096 | 1792 | This is an XS interface to Perl's C<warn> function. |
954c1994 | 1793 | |
c5df3096 Z |
1794 | C<baseex> is the error message or object. If it is a reference, it |
1795 | will be used as-is. Otherwise it is used as a string, and if it does | |
1796 | not end with a newline then it will be extended with some indication of | |
1797 | the current location in the code, as described for L</mess_sv>. | |
9983fa3c | 1798 | |
c5df3096 Z |
1799 | The error message or object will by default be written to standard error, |
1800 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
9983fa3c | 1801 | |
c5df3096 Z |
1802 | To warn with a simple string message, the L</warn> function may be |
1803 | more convenient. | |
954c1994 GS |
1804 | |
1805 | =cut | |
1806 | */ | |
1807 | ||
cea2e8a9 | 1808 | void |
c5df3096 | 1809 | Perl_warn_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1810 | { |
c5df3096 Z |
1811 | SV *ex = mess_sv(baseex, 0); |
1812 | PERL_ARGS_ASSERT_WARN_SV; | |
1813 | if (!invoke_exception_hook(ex, TRUE)) | |
1814 | write_to_stderr(ex); | |
cea2e8a9 GS |
1815 | } |
1816 | ||
c5df3096 Z |
1817 | /* |
1818 | =for apidoc Am|void|vwarn|const char *pat|va_list *args | |
1819 | ||
1820 | This is an XS interface to Perl's C<warn> function. | |
1821 | ||
1822 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1823 | argument list. These are used to generate a string message. If the | |
1824 | message does not end with a newline, then it will be extended with | |
1825 | some indication of the current location in the code, as described for | |
1826 | L</mess_sv>. | |
1827 | ||
1828 | The error message or object will by default be written to standard error, | |
1829 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1830 | ||
1831 | Unlike with L</vcroak>, C<pat> is not permitted to be null. | |
1832 | ||
1833 | =cut | |
1834 | */ | |
1835 | ||
c5be433b GS |
1836 | void |
1837 | Perl_vwarn(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 | 1838 | { |
c5df3096 | 1839 | SV *ex = vmess(pat, args); |
7918f24d | 1840 | PERL_ARGS_ASSERT_VWARN; |
c5df3096 Z |
1841 | if (!invoke_exception_hook(ex, TRUE)) |
1842 | write_to_stderr(ex); | |
1843 | } | |
7918f24d | 1844 | |
c5df3096 Z |
1845 | /* |
1846 | =for apidoc Am|void|warn|const char *pat|... | |
87582a92 | 1847 | |
c5df3096 Z |
1848 | This is an XS interface to Perl's C<warn> function. |
1849 | ||
1850 | Take a sprintf-style format pattern and argument list. These are used to | |
1851 | generate a string message. If the message does not end with a newline, | |
1852 | then it will be extended with some indication of the current location | |
1853 | in the code, as described for L</mess_sv>. | |
1854 | ||
1855 | The error message or object will by default be written to standard error, | |
1856 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1857 | ||
1858 | Unlike with L</croak>, C<pat> is not permitted to be null. | |
1859 | ||
1860 | =cut | |
1861 | */ | |
8d063cd8 | 1862 | |
c5be433b | 1863 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1864 | void |
1865 | Perl_warn_nocontext(const char *pat, ...) | |
1866 | { | |
1867 | dTHX; | |
1868 | va_list args; | |
7918f24d | 1869 | PERL_ARGS_ASSERT_WARN_NOCONTEXT; |
cea2e8a9 | 1870 | va_start(args, pat); |
c5be433b | 1871 | vwarn(pat, &args); |
cea2e8a9 GS |
1872 | va_end(args); |
1873 | } | |
1874 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1875 | ||
1876 | void | |
1877 | Perl_warn(pTHX_ const char *pat, ...) | |
1878 | { | |
1879 | va_list args; | |
7918f24d | 1880 | PERL_ARGS_ASSERT_WARN; |
cea2e8a9 | 1881 | va_start(args, pat); |
c5be433b | 1882 | vwarn(pat, &args); |
cea2e8a9 GS |
1883 | va_end(args); |
1884 | } | |
1885 | ||
c5be433b GS |
1886 | #if defined(PERL_IMPLICIT_CONTEXT) |
1887 | void | |
1888 | Perl_warner_nocontext(U32 err, const char *pat, ...) | |
1889 | { | |
27da23d5 | 1890 | dTHX; |
c5be433b | 1891 | va_list args; |
7918f24d | 1892 | PERL_ARGS_ASSERT_WARNER_NOCONTEXT; |
c5be433b GS |
1893 | va_start(args, pat); |
1894 | vwarner(err, pat, &args); | |
1895 | va_end(args); | |
1896 | } | |
1897 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1898 | ||
599cee73 | 1899 | void |
9b387841 NC |
1900 | Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...) |
1901 | { | |
1902 | PERL_ARGS_ASSERT_CK_WARNER_D; | |
1903 | ||
1904 | if (Perl_ckwarn_d(aTHX_ err)) { | |
1905 | va_list args; | |
1906 | va_start(args, pat); | |
1907 | vwarner(err, pat, &args); | |
1908 | va_end(args); | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | void | |
a2a5de95 NC |
1913 | Perl_ck_warner(pTHX_ U32 err, const char* pat, ...) |
1914 | { | |
1915 | PERL_ARGS_ASSERT_CK_WARNER; | |
1916 | ||
1917 | if (Perl_ckwarn(aTHX_ err)) { | |
1918 | va_list args; | |
1919 | va_start(args, pat); | |
1920 | vwarner(err, pat, &args); | |
1921 | va_end(args); | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | void | |
864dbfa3 | 1926 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 PM |
1927 | { |
1928 | va_list args; | |
7918f24d | 1929 | PERL_ARGS_ASSERT_WARNER; |
c5be433b GS |
1930 | va_start(args, pat); |
1931 | vwarner(err, pat, &args); | |
1932 | va_end(args); | |
1933 | } | |
1934 | ||
1935 | void | |
1936 | Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) | |
1937 | { | |
27da23d5 | 1938 | dVAR; |
7918f24d | 1939 | PERL_ARGS_ASSERT_VWARNER; |
5f2d9966 | 1940 | if (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) { |
a3b680e6 | 1941 | SV * const msv = vmess(pat, args); |
599cee73 | 1942 | |
594b6fac LM |
1943 | if (PL_parser && PL_parser->error_count) { |
1944 | qerror(msv); | |
1945 | } | |
1946 | else { | |
1947 | invoke_exception_hook(msv, FALSE); | |
1948 | die_unwind(msv); | |
1949 | } | |
599cee73 PM |
1950 | } |
1951 | else { | |
d13b0d77 | 1952 | Perl_vwarn(aTHX_ pat, args); |
599cee73 PM |
1953 | } |
1954 | } | |
1955 | ||
f54ba1c2 DM |
1956 | /* implements the ckWARN? macros */ |
1957 | ||
1958 | bool | |
1959 | Perl_ckwarn(pTHX_ U32 w) | |
1960 | { | |
ad287e37 | 1961 | /* If lexical warnings have not been set, use $^W. */ |
3c3f8cd6 AB |
1962 | if (isLEXWARN_off) |
1963 | return PL_dowarn & G_WARN_ON; | |
ad287e37 | 1964 | |
26c7b074 | 1965 | return ckwarn_common(w); |
f54ba1c2 DM |
1966 | } |
1967 | ||
1968 | /* implements the ckWARN?_d macro */ | |
1969 | ||
1970 | bool | |
1971 | Perl_ckwarn_d(pTHX_ U32 w) | |
1972 | { | |
ad287e37 | 1973 | /* If lexical warnings have not been set then default classes warn. */ |
3c3f8cd6 AB |
1974 | if (isLEXWARN_off) |
1975 | return TRUE; | |
ad287e37 | 1976 | |
26c7b074 NC |
1977 | return ckwarn_common(w); |
1978 | } | |
1979 | ||
1980 | static bool | |
1981 | S_ckwarn_common(pTHX_ U32 w) | |
1982 | { | |
3c3f8cd6 AB |
1983 | if (PL_curcop->cop_warnings == pWARN_ALL) |
1984 | return TRUE; | |
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 PP |
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 PP |
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 PP |
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 PP |
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 PP |
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 PP |
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 PP |
2729 | #ifdef HAS_SIGACTION |
2730 | ||
2731 | Sighandler_t | |
864dbfa3 | 2732 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 PP |
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 PP |
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 PP |
2758 | } |
2759 | ||
2760 | Sighandler_t | |
864dbfa3 | 2761 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 PP |
2762 | { |
2763 | struct sigaction oact; | |
96a5add6 | 2764 | PERL_UNUSED_CONTEXT; |
ff68c719 PP |
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 PP |
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 PP |
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 PP |
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 PP |
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 PP |
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 PP |
2830 | } |
2831 | ||
fabdb6c0 | 2832 | static Signal_t |
4e35701f | 2833 | sig_trap(int signo) |
ff68c719 | 2834 | { |
27da23d5 JH |
2835 | dVAR; |
2836 | PL_sig_trapped++; | |
ff68c719 PP |
2837 | } |
2838 | ||
2839 | Sighandler_t | |
864dbfa3 | 2840 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2841 | { |
27da23d5 | 2842 | dVAR; |
ff68c719 PP |
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 PP |
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 PP |
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 PP |
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 | |
f6bab5f6 | 2984 | aggregate does more work, because next call to hv_iterinit() |
7b9a3241 NC |
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 | |