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