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