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 | |
6f408c34 | 29 | #ifdef 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 | |
172d2248 OS |
40 | #ifdef __Lynx__ |
41 | /* Missing protos on LynxOS */ | |
42 | int putenv(char *); | |
43 | #endif | |
44 | ||
868439a2 JH |
45 | #ifdef HAS_SELECT |
46 | # ifdef I_SYS_SELECT | |
47 | # include <sys/select.h> | |
48 | # endif | |
49 | #endif | |
50 | ||
8d063cd8 | 51 | #define FLUSH |
8d063cd8 | 52 | |
16cebae2 GS |
53 | #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC) |
54 | # define FD_CLOEXEC 1 /* NeXT needs this */ | |
55 | #endif | |
56 | ||
a687059c LW |
57 | /* NOTE: Do not call the next three routines directly. Use the macros |
58 | * in handy.h, so that we can easily redefine everything to do tracking of | |
59 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 60 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
61 | */ |
62 | ||
79a92154 | 63 | #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL) |
1f4d2d4e NC |
64 | # define ALWAYS_NEED_THX |
65 | #endif | |
66 | ||
26fa51c3 AMS |
67 | /* paranoid version of system's malloc() */ |
68 | ||
bd4080b3 | 69 | Malloc_t |
4f63d024 | 70 | Perl_safesysmalloc(MEM_SIZE size) |
8d063cd8 | 71 | { |
1f4d2d4e | 72 | #ifdef ALWAYS_NEED_THX |
54aff467 | 73 | dTHX; |
0cb20dae | 74 | #endif |
bd4080b3 | 75 | Malloc_t ptr; |
55497cff | 76 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 77 | if (size > 0xffff) { |
bf49b057 | 78 | PerlIO_printf(Perl_error_log, |
16cebae2 | 79 | "Allocation too large: %lx\n", size) FLUSH; |
54aff467 | 80 | my_exit(1); |
62b28dd9 | 81 | } |
55497cff | 82 | #endif /* HAS_64K_LIMIT */ |
e8dda941 JD |
83 | #ifdef PERL_TRACK_MEMPOOL |
84 | size += sTHX; | |
85 | #endif | |
34de22dd | 86 | #ifdef DEBUGGING |
03c5309f | 87 | if ((SSize_t)size < 0) |
5637ef5b | 88 | Perl_croak_nocontext("panic: malloc, size=%"UVuf, (UV) size); |
34de22dd | 89 | #endif |
12ae5dfc | 90 | ptr = (Malloc_t)PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
da927450 | 91 | PERL_ALLOC_CHECK(ptr); |
bd61b366 | 92 | if (ptr != NULL) { |
e8dda941 | 93 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
94 | struct perl_memory_debug_header *const header |
95 | = (struct perl_memory_debug_header *)ptr; | |
9a083ecf NC |
96 | #endif |
97 | ||
98 | #ifdef PERL_POISON | |
7e337ee0 | 99 | PoisonNew(((char *)ptr), size, char); |
9a083ecf | 100 | #endif |
7cb608b5 | 101 | |
9a083ecf | 102 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
103 | header->interpreter = aTHX; |
104 | /* Link us into the list. */ | |
105 | header->prev = &PL_memory_debug_header; | |
106 | header->next = PL_memory_debug_header.next; | |
107 | PL_memory_debug_header.next = header; | |
108 | header->next->prev = header; | |
cd1541b2 | 109 | # ifdef PERL_POISON |
7cb608b5 | 110 | header->size = size; |
cd1541b2 | 111 | # endif |
e8dda941 JD |
112 | ptr = (Malloc_t)((char*)ptr+sTHX); |
113 | #endif | |
5dfff8f3 | 114 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
8d063cd8 | 115 | return ptr; |
e8dda941 | 116 | } |
8d063cd8 | 117 | else { |
1f4d2d4e | 118 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
119 | dTHX; |
120 | #endif | |
121 | if (PL_nomemok) | |
122 | return NULL; | |
123 | else { | |
4cbe3a7d | 124 | croak_no_mem(); |
0cb20dae | 125 | } |
8d063cd8 LW |
126 | } |
127 | /*NOTREACHED*/ | |
128 | } | |
129 | ||
f2517201 | 130 | /* paranoid version of system's realloc() */ |
8d063cd8 | 131 | |
bd4080b3 | 132 | Malloc_t |
4f63d024 | 133 | Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 134 | { |
1f4d2d4e | 135 | #ifdef ALWAYS_NEED_THX |
54aff467 | 136 | dTHX; |
0cb20dae | 137 | #endif |
bd4080b3 | 138 | Malloc_t ptr; |
9a34ef1d | 139 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO) |
6ad3d225 | 140 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 141 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 142 | |
a1d180c4 | 143 | #ifdef HAS_64K_LIMIT |
5f05dabc | 144 | if (size > 0xffff) { |
bf49b057 | 145 | PerlIO_printf(Perl_error_log, |
5f05dabc | 146 | "Reallocation too large: %lx\n", size) FLUSH; |
54aff467 | 147 | my_exit(1); |
5f05dabc | 148 | } |
55497cff | 149 | #endif /* HAS_64K_LIMIT */ |
7614df0c | 150 | if (!size) { |
f2517201 | 151 | safesysfree(where); |
7614df0c JD |
152 | return NULL; |
153 | } | |
154 | ||
378cc40b | 155 | if (!where) |
f2517201 | 156 | return safesysmalloc(size); |
e8dda941 JD |
157 | #ifdef PERL_TRACK_MEMPOOL |
158 | where = (Malloc_t)((char*)where-sTHX); | |
159 | size += sTHX; | |
7cb608b5 NC |
160 | { |
161 | struct perl_memory_debug_header *const header | |
162 | = (struct perl_memory_debug_header *)where; | |
163 | ||
164 | if (header->interpreter != aTHX) { | |
5637ef5b NC |
165 | Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p", |
166 | header->interpreter, aTHX); | |
7cb608b5 NC |
167 | } |
168 | assert(header->next->prev == header); | |
169 | assert(header->prev->next == header); | |
cd1541b2 | 170 | # ifdef PERL_POISON |
7cb608b5 NC |
171 | if (header->size > size) { |
172 | const MEM_SIZE freed_up = header->size - size; | |
173 | char *start_of_freed = ((char *)where) + size; | |
7e337ee0 | 174 | PoisonFree(start_of_freed, freed_up, char); |
7cb608b5 NC |
175 | } |
176 | header->size = size; | |
cd1541b2 | 177 | # endif |
7cb608b5 | 178 | } |
e8dda941 | 179 | #endif |
34de22dd | 180 | #ifdef DEBUGGING |
03c5309f | 181 | if ((SSize_t)size < 0) |
5637ef5b | 182 | Perl_croak_nocontext("panic: realloc, size=%"UVuf, (UV)size); |
34de22dd | 183 | #endif |
12ae5dfc | 184 | ptr = (Malloc_t)PerlMem_realloc(where,size); |
da927450 | 185 | PERL_ALLOC_CHECK(ptr); |
a1d180c4 | 186 | |
4fd0a9b8 NC |
187 | /* MUST do this fixup first, before doing ANYTHING else, as anything else |
188 | might allocate memory/free/move memory, and until we do the fixup, it | |
189 | may well be chasing (and writing to) free memory. */ | |
e8dda941 | 190 | #ifdef PERL_TRACK_MEMPOOL |
4fd0a9b8 | 191 | if (ptr != NULL) { |
7cb608b5 NC |
192 | struct perl_memory_debug_header *const header |
193 | = (struct perl_memory_debug_header *)ptr; | |
194 | ||
9a083ecf NC |
195 | # ifdef PERL_POISON |
196 | if (header->size < size) { | |
197 | const MEM_SIZE fresh = size - header->size; | |
198 | char *start_of_fresh = ((char *)ptr) + size; | |
7e337ee0 | 199 | PoisonNew(start_of_fresh, fresh, char); |
9a083ecf NC |
200 | } |
201 | # endif | |
202 | ||
7cb608b5 NC |
203 | header->next->prev = header; |
204 | header->prev->next = header; | |
205 | ||
e8dda941 | 206 | ptr = (Malloc_t)((char*)ptr+sTHX); |
4fd0a9b8 | 207 | } |
e8dda941 | 208 | #endif |
4fd0a9b8 NC |
209 | |
210 | /* In particular, must do that fixup above before logging anything via | |
211 | *printf(), as it can reallocate memory, which can cause SEGVs. */ | |
212 | ||
213 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++)); | |
214 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); | |
215 | ||
216 | ||
217 | if (ptr != NULL) { | |
8d063cd8 | 218 | return ptr; |
e8dda941 | 219 | } |
8d063cd8 | 220 | else { |
1f4d2d4e | 221 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
222 | dTHX; |
223 | #endif | |
224 | if (PL_nomemok) | |
225 | return NULL; | |
226 | else { | |
4cbe3a7d | 227 | croak_no_mem(); |
0cb20dae | 228 | } |
8d063cd8 LW |
229 | } |
230 | /*NOTREACHED*/ | |
231 | } | |
232 | ||
f2517201 | 233 | /* safe version of system's free() */ |
8d063cd8 | 234 | |
54310121 | 235 | Free_t |
4f63d024 | 236 | Perl_safesysfree(Malloc_t where) |
8d063cd8 | 237 | { |
79a92154 | 238 | #ifdef ALWAYS_NEED_THX |
54aff467 | 239 | dTHX; |
97aff369 JH |
240 | #else |
241 | dVAR; | |
155aba94 | 242 | #endif |
97835f67 | 243 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++)); |
378cc40b | 244 | if (where) { |
e8dda941 JD |
245 | #ifdef PERL_TRACK_MEMPOOL |
246 | where = (Malloc_t)((char*)where-sTHX); | |
cd1541b2 | 247 | { |
7cb608b5 NC |
248 | struct perl_memory_debug_header *const header |
249 | = (struct perl_memory_debug_header *)where; | |
250 | ||
251 | if (header->interpreter != aTHX) { | |
5637ef5b NC |
252 | Perl_croak_nocontext("panic: free from wrong pool, %p!=%p", |
253 | header->interpreter, aTHX); | |
7cb608b5 NC |
254 | } |
255 | if (!header->prev) { | |
cd1541b2 NC |
256 | Perl_croak_nocontext("panic: duplicate free"); |
257 | } | |
5637ef5b NC |
258 | if (!(header->next)) |
259 | Perl_croak_nocontext("panic: bad free, header->next==NULL"); | |
260 | if (header->next->prev != header || header->prev->next != header) { | |
261 | Perl_croak_nocontext("panic: bad free, ->next->prev=%p, " | |
262 | "header=%p, ->prev->next=%p", | |
263 | header->next->prev, header, | |
264 | header->prev->next); | |
cd1541b2 | 265 | } |
7cb608b5 NC |
266 | /* Unlink us from the chain. */ |
267 | header->next->prev = header->prev; | |
268 | header->prev->next = header->next; | |
269 | # ifdef PERL_POISON | |
7e337ee0 | 270 | PoisonNew(where, header->size, char); |
cd1541b2 | 271 | # endif |
7cb608b5 NC |
272 | /* Trigger the duplicate free warning. */ |
273 | header->next = NULL; | |
274 | } | |
e8dda941 | 275 | #endif |
6ad3d225 | 276 | PerlMem_free(where); |
378cc40b | 277 | } |
8d063cd8 LW |
278 | } |
279 | ||
f2517201 | 280 | /* safe version of system's calloc() */ |
1050c9ca | 281 | |
bd4080b3 | 282 | Malloc_t |
4f63d024 | 283 | Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 284 | { |
1f4d2d4e | 285 | #ifdef ALWAYS_NEED_THX |
54aff467 | 286 | dTHX; |
0cb20dae | 287 | #endif |
bd4080b3 | 288 | Malloc_t ptr; |
4b1123b9 | 289 | #if defined(PERL_TRACK_MEMPOOL) || defined(HAS_64K_LIMIT) || defined(DEBUGGING) |
ad7244db | 290 | MEM_SIZE total_size = 0; |
4b1123b9 | 291 | #endif |
1050c9ca | 292 | |
ad7244db | 293 | /* Even though calloc() for zero bytes is strange, be robust. */ |
4b1123b9 NC |
294 | if (size && (count <= MEM_SIZE_MAX / size)) { |
295 | #if defined(PERL_TRACK_MEMPOOL) || defined(HAS_64K_LIMIT) || defined(DEBUGGING) | |
ad7244db | 296 | total_size = size * count; |
4b1123b9 NC |
297 | #endif |
298 | } | |
ad7244db | 299 | else |
43387ee1 | 300 | Perl_croak_memory_wrap(); |
ad7244db | 301 | #ifdef PERL_TRACK_MEMPOOL |
19a94d75 | 302 | if (sTHX <= MEM_SIZE_MAX - (MEM_SIZE)total_size) |
ad7244db JH |
303 | total_size += sTHX; |
304 | else | |
43387ee1 | 305 | Perl_croak_memory_wrap(); |
ad7244db | 306 | #endif |
55497cff | 307 | #ifdef HAS_64K_LIMIT |
e1a95402 | 308 | if (total_size > 0xffff) { |
bf49b057 | 309 | PerlIO_printf(Perl_error_log, |
e1a95402 | 310 | "Allocation too large: %lx\n", total_size) FLUSH; |
54aff467 | 311 | my_exit(1); |
5f05dabc | 312 | } |
55497cff | 313 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 314 | #ifdef DEBUGGING |
03c5309f | 315 | if ((SSize_t)size < 0 || (SSize_t)count < 0) |
5637ef5b NC |
316 | Perl_croak_nocontext("panic: calloc, size=%"UVuf", count=%"UVuf, |
317 | (UV)size, (UV)count); | |
1050c9ca | 318 | #endif |
e8dda941 | 319 | #ifdef PERL_TRACK_MEMPOOL |
e1a95402 NC |
320 | /* Have to use malloc() because we've added some space for our tracking |
321 | header. */ | |
ad7244db JH |
322 | /* malloc(0) is non-portable. */ |
323 | ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1); | |
e1a95402 NC |
324 | #else |
325 | /* Use calloc() because it might save a memset() if the memory is fresh | |
326 | and clean from the OS. */ | |
ad7244db JH |
327 | if (count && size) |
328 | ptr = (Malloc_t)PerlMem_calloc(count, size); | |
329 | else /* calloc(0) is non-portable. */ | |
330 | ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1); | |
e8dda941 | 331 | #endif |
da927450 | 332 | PERL_ALLOC_CHECK(ptr); |
e1a95402 | 333 | 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 | 334 | if (ptr != NULL) { |
e8dda941 | 335 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
336 | { |
337 | struct perl_memory_debug_header *const header | |
338 | = (struct perl_memory_debug_header *)ptr; | |
339 | ||
e1a95402 | 340 | memset((void*)ptr, 0, total_size); |
7cb608b5 NC |
341 | header->interpreter = aTHX; |
342 | /* Link us into the list. */ | |
343 | header->prev = &PL_memory_debug_header; | |
344 | header->next = PL_memory_debug_header.next; | |
345 | PL_memory_debug_header.next = header; | |
346 | header->next->prev = header; | |
cd1541b2 | 347 | # ifdef PERL_POISON |
e1a95402 | 348 | header->size = total_size; |
cd1541b2 | 349 | # endif |
7cb608b5 NC |
350 | ptr = (Malloc_t)((char*)ptr+sTHX); |
351 | } | |
e8dda941 | 352 | #endif |
1050c9ca | 353 | return ptr; |
354 | } | |
0cb20dae | 355 | else { |
1f4d2d4e | 356 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
357 | dTHX; |
358 | #endif | |
359 | if (PL_nomemok) | |
360 | return NULL; | |
4cbe3a7d | 361 | croak_no_mem(); |
0cb20dae | 362 | } |
1050c9ca | 363 | } |
364 | ||
cae6d0e5 GS |
365 | /* These must be defined when not using Perl's malloc for binary |
366 | * compatibility */ | |
367 | ||
368 | #ifndef MYMALLOC | |
369 | ||
370 | Malloc_t Perl_malloc (MEM_SIZE nbytes) | |
371 | { | |
372 | dTHXs; | |
077a72a9 | 373 | return (Malloc_t)PerlMem_malloc(nbytes); |
cae6d0e5 GS |
374 | } |
375 | ||
376 | Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size) | |
377 | { | |
378 | dTHXs; | |
077a72a9 | 379 | return (Malloc_t)PerlMem_calloc(elements, size); |
cae6d0e5 GS |
380 | } |
381 | ||
382 | Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes) | |
383 | { | |
384 | dTHXs; | |
077a72a9 | 385 | return (Malloc_t)PerlMem_realloc(where, nbytes); |
cae6d0e5 GS |
386 | } |
387 | ||
388 | Free_t Perl_mfree (Malloc_t where) | |
389 | { | |
390 | dTHXs; | |
391 | PerlMem_free(where); | |
392 | } | |
393 | ||
394 | #endif | |
395 | ||
8d063cd8 LW |
396 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
397 | ||
398 | char * | |
5aaab254 | 399 | Perl_delimcpy(char *to, const char *toend, const char *from, const char *fromend, int delim, I32 *retlen) |
8d063cd8 | 400 | { |
eb578fdb | 401 | I32 tolen; |
35da51f7 | 402 | |
7918f24d NC |
403 | PERL_ARGS_ASSERT_DELIMCPY; |
404 | ||
fc36a67e | 405 | for (tolen = 0; from < fromend; from++, tolen++) { |
378cc40b | 406 | if (*from == '\\') { |
35da51f7 | 407 | if (from[1] != delim) { |
fc36a67e | 408 | if (to < toend) |
409 | *to++ = *from; | |
410 | tolen++; | |
fc36a67e | 411 | } |
35da51f7 | 412 | from++; |
378cc40b | 413 | } |
bedebaa5 | 414 | else if (*from == delim) |
8d063cd8 | 415 | break; |
fc36a67e | 416 | if (to < toend) |
417 | *to++ = *from; | |
8d063cd8 | 418 | } |
bedebaa5 CS |
419 | if (to < toend) |
420 | *to = '\0'; | |
fc36a67e | 421 | *retlen = tolen; |
73d840c0 | 422 | return (char *)from; |
8d063cd8 LW |
423 | } |
424 | ||
425 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 426 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
427 | |
428 | char * | |
5aaab254 | 429 | Perl_instr(const char *big, const char *little) |
378cc40b | 430 | { |
378cc40b | 431 | |
7918f24d NC |
432 | PERL_ARGS_ASSERT_INSTR; |
433 | ||
5d1d68e2 | 434 | /* libc prior to 4.6.27 did not work properly on a NULL 'little' */ |
a687059c | 435 | if (!little) |
08105a92 | 436 | return (char*)big; |
5d1d68e2 | 437 | return strstr((char*)big, (char*)little); |
378cc40b | 438 | } |
8d063cd8 | 439 | |
e057d092 KW |
440 | /* same as instr but allow embedded nulls. The end pointers point to 1 beyond |
441 | * the final character desired to be checked */ | |
a687059c LW |
442 | |
443 | char * | |
04c9e624 | 444 | Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend) |
8d063cd8 | 445 | { |
7918f24d | 446 | PERL_ARGS_ASSERT_NINSTR; |
4c8626be GA |
447 | if (little >= lend) |
448 | return (char*)big; | |
449 | { | |
8ba22ff4 | 450 | const char first = *little; |
4c8626be | 451 | const char *s, *x; |
8ba22ff4 | 452 | bigend -= lend - little++; |
4c8626be GA |
453 | OUTER: |
454 | while (big <= bigend) { | |
b0ca24ee JH |
455 | if (*big++ == first) { |
456 | for (x=big,s=little; s < lend; x++,s++) { | |
457 | if (*s != *x) | |
458 | goto OUTER; | |
459 | } | |
460 | return (char*)(big-1); | |
4c8626be | 461 | } |
4c8626be | 462 | } |
378cc40b | 463 | } |
bd61b366 | 464 | return NULL; |
a687059c LW |
465 | } |
466 | ||
467 | /* reverse of the above--find last substring */ | |
468 | ||
469 | char * | |
5aaab254 | 470 | Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 471 | { |
eb578fdb KW |
472 | const char *bigbeg; |
473 | const I32 first = *little; | |
474 | const char * const littleend = lend; | |
a687059c | 475 | |
7918f24d NC |
476 | PERL_ARGS_ASSERT_RNINSTR; |
477 | ||
260d78c9 | 478 | if (little >= littleend) |
08105a92 | 479 | return (char*)bigend; |
a687059c LW |
480 | bigbeg = big; |
481 | big = bigend - (littleend - little++); | |
482 | while (big >= bigbeg) { | |
eb578fdb | 483 | const char *s, *x; |
a687059c LW |
484 | if (*big-- != first) |
485 | continue; | |
486 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
4fc877ac | 487 | if (*s != *x) |
a687059c | 488 | break; |
4fc877ac AL |
489 | else { |
490 | x++; | |
491 | s++; | |
a687059c LW |
492 | } |
493 | } | |
494 | if (s >= littleend) | |
08105a92 | 495 | return (char*)(big+1); |
378cc40b | 496 | } |
bd61b366 | 497 | return NULL; |
378cc40b | 498 | } |
a687059c | 499 | |
cf93c79d IZ |
500 | /* As a space optimization, we do not compile tables for strings of length |
501 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
502 | special-cased in fbm_instr(). | |
503 | ||
504 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
505 | ||
954c1994 | 506 | /* |
ccfc67b7 JH |
507 | =head1 Miscellaneous Functions |
508 | ||
954c1994 GS |
509 | =for apidoc fbm_compile |
510 | ||
511 | Analyses the string in order to make fast searches on it using fbm_instr() | |
512 | -- the Boyer-Moore algorithm. | |
513 | ||
514 | =cut | |
515 | */ | |
516 | ||
378cc40b | 517 | void |
7506f9c3 | 518 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
378cc40b | 519 | { |
97aff369 | 520 | dVAR; |
eb578fdb | 521 | const U8 *s; |
ea725ce6 | 522 | STRLEN i; |
0b71040e | 523 | STRLEN len; |
ea725ce6 | 524 | STRLEN rarest = 0; |
79072805 | 525 | U32 frequency = 256; |
2bda37ba | 526 | MAGIC *mg; |
79072805 | 527 | |
7918f24d NC |
528 | PERL_ARGS_ASSERT_FBM_COMPILE; |
529 | ||
42bb8ff7 | 530 | if (isGV_with_GP(sv)) |
4265b45d NC |
531 | return; |
532 | ||
9402563a NC |
533 | if (SvVALID(sv)) |
534 | return; | |
535 | ||
c517dc2b | 536 | if (flags & FBMcf_TAIL) { |
890ce7af | 537 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
396482e1 | 538 | sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */ |
c517dc2b JH |
539 | if (mg && mg->mg_len >= 0) |
540 | mg->mg_len++; | |
541 | } | |
9cbe880b | 542 | s = (U8*)SvPV_force_mutable(sv, len); |
d1be9408 | 543 | if (len == 0) /* TAIL might be on a zero-length string. */ |
cf93c79d | 544 | return; |
c13a5c80 | 545 | SvUPGRADE(sv, SVt_PVMG); |
78d0cf80 | 546 | SvIOK_off(sv); |
8eeaf79a NC |
547 | SvNOK_off(sv); |
548 | SvVALID_on(sv); | |
2bda37ba NC |
549 | |
550 | /* "deep magic", the comment used to add. The use of MAGIC itself isn't | |
551 | really. MAGIC was originally added in 79072805bf63abe5 (perl 5.0 alpha 2) | |
552 | to call SvVALID_off() if the scalar was assigned to. | |
553 | ||
554 | The comment itself (and "deeper magic" below) date back to | |
555 | 378cc40b38293ffc (perl 2.0). "deep magic" was an annotation on | |
556 | str->str_pok |= 2; | |
557 | where the magic (presumably) was that the scalar had a BM table hidden | |
558 | inside itself. | |
559 | ||
560 | As MAGIC is always present on BMs [in Perl 5 :-)], we can use it to store | |
561 | the table instead of the previous (somewhat hacky) approach of co-opting | |
562 | the string buffer and storing it after the string. */ | |
563 | ||
564 | assert(!mg_find(sv, PERL_MAGIC_bm)); | |
565 | mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0); | |
566 | assert(mg); | |
567 | ||
02128f11 | 568 | if (len > 2) { |
21aeb718 NC |
569 | /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use |
570 | the BM table. */ | |
66a1b24b | 571 | const U8 mlen = (len>255) ? 255 : (U8)len; |
2bda37ba | 572 | const unsigned char *const sb = s + len - mlen; /* first char (maybe) */ |
eb578fdb | 573 | U8 *table; |
cf93c79d | 574 | |
2bda37ba | 575 | Newx(table, 256, U8); |
7506f9c3 | 576 | memset((void*)table, mlen, 256); |
2bda37ba NC |
577 | mg->mg_ptr = (char *)table; |
578 | mg->mg_len = 256; | |
579 | ||
580 | s += len - 1; /* last char */ | |
02128f11 | 581 | i = 0; |
cf93c79d IZ |
582 | while (s >= sb) { |
583 | if (table[*s] == mlen) | |
7506f9c3 | 584 | table[*s] = (U8)i; |
cf93c79d IZ |
585 | s--, i++; |
586 | } | |
378cc40b | 587 | } |
378cc40b | 588 | |
9cbe880b | 589 | s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */ |
bbce6d69 | 590 | for (i = 0; i < len; i++) { |
22c35a8c | 591 | if (PL_freq[s[i]] < frequency) { |
bbce6d69 | 592 | rarest = i; |
22c35a8c | 593 | frequency = PL_freq[s[i]]; |
378cc40b LW |
594 | } |
595 | } | |
79072805 | 596 | BmRARE(sv) = s[rarest]; |
44a10c71 | 597 | BmPREVIOUS(sv) = rarest; |
cf93c79d IZ |
598 | BmUSEFUL(sv) = 100; /* Initial value */ |
599 | if (flags & FBMcf_TAIL) | |
600 | SvTAIL_on(sv); | |
ea725ce6 NC |
601 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %"UVuf"\n", |
602 | BmRARE(sv), BmPREVIOUS(sv))); | |
378cc40b LW |
603 | } |
604 | ||
cf93c79d IZ |
605 | /* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
606 | /* If SvTAIL is actually due to \Z or \z, this gives false positives | |
607 | if multiline */ | |
608 | ||
954c1994 GS |
609 | /* |
610 | =for apidoc fbm_instr | |
611 | ||
3f4963df FC |
612 | Returns the location of the SV in the string delimited by C<big> and |
613 | C<bigend>. It returns C<NULL> if the string can't be found. The C<sv> | |
954c1994 GS |
614 | does not have to be fbm_compiled, but the search will not be as fast |
615 | then. | |
616 | ||
617 | =cut | |
618 | */ | |
619 | ||
378cc40b | 620 | char * |
5aaab254 | 621 | Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 622 | { |
eb578fdb | 623 | unsigned char *s; |
cf93c79d | 624 | STRLEN l; |
eb578fdb KW |
625 | const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l); |
626 | STRLEN littlelen = l; | |
627 | const I32 multiline = flags & FBMrf_MULTILINE; | |
cf93c79d | 628 | |
7918f24d NC |
629 | PERL_ARGS_ASSERT_FBM_INSTR; |
630 | ||
eb160463 | 631 | if ((STRLEN)(bigend - big) < littlelen) { |
a1d180c4 | 632 | if ( SvTAIL(littlestr) |
eb160463 | 633 | && ((STRLEN)(bigend - big) == littlelen - 1) |
a1d180c4 | 634 | && (littlelen == 1 |
12ae5dfc | 635 | || (*big == *little && |
27da23d5 | 636 | memEQ((char *)big, (char *)little, littlelen - 1)))) |
cf93c79d | 637 | return (char*)big; |
bd61b366 | 638 | return NULL; |
cf93c79d | 639 | } |
378cc40b | 640 | |
21aeb718 NC |
641 | switch (littlelen) { /* Special cases for 0, 1 and 2 */ |
642 | case 0: | |
643 | return (char*)big; /* Cannot be SvTAIL! */ | |
644 | case 1: | |
cf93c79d IZ |
645 | if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ |
646 | /* Know that bigend != big. */ | |
647 | if (bigend[-1] == '\n') | |
648 | return (char *)(bigend - 1); | |
649 | return (char *) bigend; | |
650 | } | |
651 | s = big; | |
652 | while (s < bigend) { | |
653 | if (*s == *little) | |
654 | return (char *)s; | |
655 | s++; | |
656 | } | |
657 | if (SvTAIL(littlestr)) | |
658 | return (char *) bigend; | |
bd61b366 | 659 | return NULL; |
21aeb718 | 660 | case 2: |
cf93c79d IZ |
661 | if (SvTAIL(littlestr) && !multiline) { |
662 | if (bigend[-1] == '\n' && bigend[-2] == *little) | |
663 | return (char*)bigend - 2; | |
664 | if (bigend[-1] == *little) | |
665 | return (char*)bigend - 1; | |
bd61b366 | 666 | return NULL; |
cf93c79d IZ |
667 | } |
668 | { | |
669 | /* This should be better than FBM if c1 == c2, and almost | |
670 | as good otherwise: maybe better since we do less indirection. | |
671 | And we save a lot of memory by caching no table. */ | |
66a1b24b AL |
672 | const unsigned char c1 = little[0]; |
673 | const unsigned char c2 = little[1]; | |
cf93c79d IZ |
674 | |
675 | s = big + 1; | |
676 | bigend--; | |
677 | if (c1 != c2) { | |
678 | while (s <= bigend) { | |
679 | if (s[0] == c2) { | |
680 | if (s[-1] == c1) | |
681 | return (char*)s - 1; | |
682 | s += 2; | |
683 | continue; | |
3fe6f2dc | 684 | } |
cf93c79d IZ |
685 | next_chars: |
686 | if (s[0] == c1) { | |
687 | if (s == bigend) | |
688 | goto check_1char_anchor; | |
689 | if (s[1] == c2) | |
690 | return (char*)s; | |
691 | else { | |
692 | s++; | |
693 | goto next_chars; | |
694 | } | |
695 | } | |
696 | else | |
697 | s += 2; | |
698 | } | |
699 | goto check_1char_anchor; | |
700 | } | |
701 | /* Now c1 == c2 */ | |
702 | while (s <= bigend) { | |
703 | if (s[0] == c1) { | |
704 | if (s[-1] == c1) | |
705 | return (char*)s - 1; | |
706 | if (s == bigend) | |
707 | goto check_1char_anchor; | |
708 | if (s[1] == c1) | |
709 | return (char*)s; | |
710 | s += 3; | |
02128f11 | 711 | } |
c277df42 | 712 | else |
cf93c79d | 713 | s += 2; |
c277df42 | 714 | } |
c277df42 | 715 | } |
cf93c79d IZ |
716 | check_1char_anchor: /* One char and anchor! */ |
717 | if (SvTAIL(littlestr) && (*bigend == *little)) | |
718 | return (char *)bigend; /* bigend is already decremented. */ | |
bd61b366 | 719 | return NULL; |
21aeb718 NC |
720 | default: |
721 | break; /* Only lengths 0 1 and 2 have special-case code. */ | |
d48672a2 | 722 | } |
21aeb718 | 723 | |
cf93c79d | 724 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 725 | s = bigend - littlelen; |
a1d180c4 | 726 | if (s >= big && bigend[-1] == '\n' && *s == *little |
cf93c79d IZ |
727 | /* Automatically of length > 2 */ |
728 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
7506f9c3 | 729 | { |
bbce6d69 | 730 | return (char*)s; /* how sweet it is */ |
7506f9c3 GS |
731 | } |
732 | if (s[1] == *little | |
733 | && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) | |
734 | { | |
cf93c79d | 735 | return (char*)s + 1; /* how sweet it is */ |
7506f9c3 | 736 | } |
bd61b366 | 737 | return NULL; |
02128f11 | 738 | } |
cecf5685 | 739 | if (!SvVALID(littlestr)) { |
c4420975 | 740 | char * const b = ninstr((char*)big,(char*)bigend, |
cf93c79d IZ |
741 | (char*)little, (char*)little + littlelen); |
742 | ||
743 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
744 | /* Chop \n from littlestr: */ | |
745 | s = bigend - littlelen + 1; | |
7506f9c3 GS |
746 | if (*s == *little |
747 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
748 | { | |
3fe6f2dc | 749 | return (char*)s; |
7506f9c3 | 750 | } |
bd61b366 | 751 | return NULL; |
a687059c | 752 | } |
cf93c79d | 753 | return b; |
a687059c | 754 | } |
a1d180c4 | 755 | |
3566a07d NC |
756 | /* Do actual FBM. */ |
757 | if (littlelen > (STRLEN)(bigend - big)) | |
758 | return NULL; | |
759 | ||
760 | { | |
2bda37ba NC |
761 | const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm); |
762 | const unsigned char * const table = (const unsigned char *) mg->mg_ptr; | |
eb578fdb | 763 | const unsigned char *oldlittle; |
cf93c79d | 764 | |
cf93c79d IZ |
765 | --littlelen; /* Last char found by table lookup */ |
766 | ||
767 | s = big + littlelen; | |
768 | little += littlelen; /* last char */ | |
769 | oldlittle = little; | |
770 | if (s < bigend) { | |
eb578fdb | 771 | I32 tmp; |
cf93c79d IZ |
772 | |
773 | top2: | |
7506f9c3 | 774 | if ((tmp = table[*s])) { |
cf93c79d | 775 | if ((s += tmp) < bigend) |
62b28dd9 | 776 | goto top2; |
cf93c79d IZ |
777 | goto check_end; |
778 | } | |
779 | else { /* less expensive than calling strncmp() */ | |
eb578fdb | 780 | unsigned char * const olds = s; |
cf93c79d IZ |
781 | |
782 | tmp = littlelen; | |
783 | ||
784 | while (tmp--) { | |
785 | if (*--s == *--little) | |
786 | continue; | |
cf93c79d IZ |
787 | s = olds + 1; /* here we pay the price for failure */ |
788 | little = oldlittle; | |
789 | if (s < bigend) /* fake up continue to outer loop */ | |
790 | goto top2; | |
791 | goto check_end; | |
792 | } | |
793 | return (char *)s; | |
a687059c | 794 | } |
378cc40b | 795 | } |
cf93c79d | 796 | check_end: |
c8029a41 | 797 | if ( s == bigend |
cffe132d | 798 | && SvTAIL(littlestr) |
12ae5dfc JH |
799 | && memEQ((char *)(bigend - littlelen), |
800 | (char *)(oldlittle - littlelen), littlelen) ) | |
cf93c79d | 801 | return (char*)bigend - littlelen; |
bd61b366 | 802 | return NULL; |
378cc40b | 803 | } |
378cc40b LW |
804 | } |
805 | ||
806 | char * | |
864dbfa3 | 807 | Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last) |
378cc40b | 808 | { |
97aff369 | 809 | dVAR; |
7918f24d | 810 | PERL_ARGS_ASSERT_SCREAMINSTR; |
9e3f0d16 FC |
811 | PERL_UNUSED_ARG(bigstr); |
812 | PERL_UNUSED_ARG(littlestr); | |
813 | PERL_UNUSED_ARG(start_shift); | |
814 | PERL_UNUSED_ARG(end_shift); | |
815 | PERL_UNUSED_ARG(old_posp); | |
816 | PERL_UNUSED_ARG(last); | |
817 | ||
818 | /* This function must only ever be called on a scalar with study magic, | |
819 | but those do not happen any more. */ | |
820 | Perl_croak(aTHX_ "panic: screaminstr"); | |
bd61b366 | 821 | return NULL; |
8d063cd8 LW |
822 | } |
823 | ||
e6226b18 KW |
824 | /* |
825 | =for apidoc foldEQ | |
826 | ||
827 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
828 | case-insensitively; false otherwise. Uppercase and lowercase ASCII range bytes | |
829 | match themselves and their opposite case counterparts. Non-cased and non-ASCII | |
830 | range bytes match only themselves. | |
831 | ||
832 | =cut | |
833 | */ | |
834 | ||
835 | ||
79072805 | 836 | I32 |
5aaab254 | 837 | Perl_foldEQ(const char *s1, const char *s2, I32 len) |
79072805 | 838 | { |
eb578fdb KW |
839 | const U8 *a = (const U8 *)s1; |
840 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 841 | |
e6226b18 | 842 | PERL_ARGS_ASSERT_FOLDEQ; |
7918f24d | 843 | |
223f01db KW |
844 | assert(len >= 0); |
845 | ||
79072805 | 846 | while (len--) { |
22c35a8c | 847 | if (*a != *b && *a != PL_fold[*b]) |
e6226b18 | 848 | return 0; |
bbce6d69 | 849 | a++,b++; |
850 | } | |
e6226b18 | 851 | return 1; |
bbce6d69 | 852 | } |
1b9f127b | 853 | I32 |
5aaab254 | 854 | Perl_foldEQ_latin1(const char *s1, const char *s2, I32 len) |
1b9f127b KW |
855 | { |
856 | /* Compare non-utf8 using Unicode (Latin1) semantics. Does not work on | |
857 | * MICRO_SIGN, LATIN_SMALL_LETTER_SHARP_S, nor | |
858 | * LATIN_SMALL_LETTER_Y_WITH_DIAERESIS, and does not check for these. Nor | |
859 | * does it check that the strings each have at least 'len' characters */ | |
860 | ||
eb578fdb KW |
861 | const U8 *a = (const U8 *)s1; |
862 | const U8 *b = (const U8 *)s2; | |
1b9f127b KW |
863 | |
864 | PERL_ARGS_ASSERT_FOLDEQ_LATIN1; | |
865 | ||
223f01db KW |
866 | assert(len >= 0); |
867 | ||
1b9f127b KW |
868 | while (len--) { |
869 | if (*a != *b && *a != PL_fold_latin1[*b]) { | |
870 | return 0; | |
871 | } | |
872 | a++, b++; | |
873 | } | |
874 | return 1; | |
875 | } | |
bbce6d69 | 876 | |
e6226b18 KW |
877 | /* |
878 | =for apidoc foldEQ_locale | |
879 | ||
880 | Returns true if the leading len bytes of the strings s1 and s2 are the same | |
881 | case-insensitively in the current locale; false otherwise. | |
882 | ||
883 | =cut | |
884 | */ | |
885 | ||
bbce6d69 | 886 | I32 |
5aaab254 | 887 | Perl_foldEQ_locale(const char *s1, const char *s2, I32 len) |
bbce6d69 | 888 | { |
27da23d5 | 889 | dVAR; |
eb578fdb KW |
890 | const U8 *a = (const U8 *)s1; |
891 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 892 | |
e6226b18 | 893 | PERL_ARGS_ASSERT_FOLDEQ_LOCALE; |
7918f24d | 894 | |
223f01db KW |
895 | assert(len >= 0); |
896 | ||
bbce6d69 | 897 | while (len--) { |
22c35a8c | 898 | if (*a != *b && *a != PL_fold_locale[*b]) |
e6226b18 | 899 | return 0; |
bbce6d69 | 900 | a++,b++; |
79072805 | 901 | } |
e6226b18 | 902 | return 1; |
79072805 LW |
903 | } |
904 | ||
8d063cd8 LW |
905 | /* copy a string to a safe spot */ |
906 | ||
954c1994 | 907 | /* |
ccfc67b7 JH |
908 | =head1 Memory Management |
909 | ||
954c1994 GS |
910 | =for apidoc savepv |
911 | ||
61a925ed AMS |
912 | Perl's version of C<strdup()>. Returns a pointer to a newly allocated |
913 | string which is a duplicate of C<pv>. The size of the string is | |
914 | determined by C<strlen()>. The memory allocated for the new string can | |
915 | be freed with the C<Safefree()> function. | |
954c1994 GS |
916 | |
917 | =cut | |
918 | */ | |
919 | ||
8d063cd8 | 920 | char * |
efdfce31 | 921 | Perl_savepv(pTHX_ const char *pv) |
8d063cd8 | 922 | { |
96a5add6 | 923 | PERL_UNUSED_CONTEXT; |
e90e2364 | 924 | if (!pv) |
bd61b366 | 925 | return NULL; |
66a1b24b AL |
926 | else { |
927 | char *newaddr; | |
928 | const STRLEN pvlen = strlen(pv)+1; | |
10edeb5d JH |
929 | Newx(newaddr, pvlen, char); |
930 | return (char*)memcpy(newaddr, pv, pvlen); | |
66a1b24b | 931 | } |
8d063cd8 LW |
932 | } |
933 | ||
a687059c LW |
934 | /* same thing but with a known length */ |
935 | ||
954c1994 GS |
936 | /* |
937 | =for apidoc savepvn | |
938 | ||
61a925ed AMS |
939 | Perl's version of what C<strndup()> would be if it existed. Returns a |
940 | pointer to a newly allocated string which is a duplicate of the first | |
cbf82dd0 NC |
941 | C<len> bytes from C<pv>, plus a trailing NUL byte. The memory allocated for |
942 | the new string can be freed with the C<Safefree()> function. | |
954c1994 GS |
943 | |
944 | =cut | |
945 | */ | |
946 | ||
a687059c | 947 | char * |
5aaab254 | 948 | Perl_savepvn(pTHX_ const char *pv, I32 len) |
a687059c | 949 | { |
eb578fdb | 950 | char *newaddr; |
96a5add6 | 951 | PERL_UNUSED_CONTEXT; |
a687059c | 952 | |
223f01db KW |
953 | assert(len >= 0); |
954 | ||
a02a5408 | 955 | Newx(newaddr,len+1,char); |
92110913 | 956 | /* Give a meaning to NULL pointer mainly for the use in sv_magic() */ |
efdfce31 | 957 | if (pv) { |
e90e2364 NC |
958 | /* might not be null terminated */ |
959 | newaddr[len] = '\0'; | |
07409e01 | 960 | return (char *) CopyD(pv,newaddr,len,char); |
92110913 NIS |
961 | } |
962 | else { | |
07409e01 | 963 | return (char *) ZeroD(newaddr,len+1,char); |
92110913 | 964 | } |
a687059c LW |
965 | } |
966 | ||
05ec9bb3 NIS |
967 | /* |
968 | =for apidoc savesharedpv | |
969 | ||
61a925ed AMS |
970 | A version of C<savepv()> which allocates the duplicate string in memory |
971 | which is shared between threads. | |
05ec9bb3 NIS |
972 | |
973 | =cut | |
974 | */ | |
975 | char * | |
efdfce31 | 976 | Perl_savesharedpv(pTHX_ const char *pv) |
05ec9bb3 | 977 | { |
eb578fdb | 978 | char *newaddr; |
490a0e98 | 979 | STRLEN pvlen; |
e90e2364 | 980 | if (!pv) |
bd61b366 | 981 | return NULL; |
e90e2364 | 982 | |
490a0e98 NC |
983 | pvlen = strlen(pv)+1; |
984 | newaddr = (char*)PerlMemShared_malloc(pvlen); | |
e90e2364 | 985 | if (!newaddr) { |
4cbe3a7d | 986 | croak_no_mem(); |
05ec9bb3 | 987 | } |
10edeb5d | 988 | return (char*)memcpy(newaddr, pv, pvlen); |
05ec9bb3 NIS |
989 | } |
990 | ||
2e0de35c | 991 | /* |
d9095cec NC |
992 | =for apidoc savesharedpvn |
993 | ||
994 | A version of C<savepvn()> which allocates the duplicate string in memory | |
995 | which is shared between threads. (With the specific difference that a NULL | |
996 | pointer is not acceptable) | |
997 | ||
998 | =cut | |
999 | */ | |
1000 | char * | |
1001 | Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len) | |
1002 | { | |
1003 | char *const newaddr = (char*)PerlMemShared_malloc(len + 1); | |
7918f24d | 1004 | |
6379d4a9 | 1005 | /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */ |
7918f24d | 1006 | |
d9095cec | 1007 | if (!newaddr) { |
4cbe3a7d | 1008 | croak_no_mem(); |
d9095cec NC |
1009 | } |
1010 | newaddr[len] = '\0'; | |
1011 | return (char*)memcpy(newaddr, pv, len); | |
1012 | } | |
1013 | ||
1014 | /* | |
2e0de35c NC |
1015 | =for apidoc savesvpv |
1016 | ||
6832267f | 1017 | A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from |
2e0de35c NC |
1018 | the passed in SV using C<SvPV()> |
1019 | ||
1020 | =cut | |
1021 | */ | |
1022 | ||
1023 | char * | |
1024 | Perl_savesvpv(pTHX_ SV *sv) | |
1025 | { | |
1026 | STRLEN len; | |
7452cf6a | 1027 | const char * const pv = SvPV_const(sv, len); |
eb578fdb | 1028 | char *newaddr; |
2e0de35c | 1029 | |
7918f24d NC |
1030 | PERL_ARGS_ASSERT_SAVESVPV; |
1031 | ||
26866f99 | 1032 | ++len; |
a02a5408 | 1033 | Newx(newaddr,len,char); |
07409e01 | 1034 | return (char *) CopyD(pv,newaddr,len,char); |
2e0de35c | 1035 | } |
05ec9bb3 | 1036 | |
9dcc53ea Z |
1037 | /* |
1038 | =for apidoc savesharedsvpv | |
1039 | ||
1040 | A version of C<savesharedpv()> which allocates the duplicate string in | |
1041 | memory which is shared between threads. | |
1042 | ||
1043 | =cut | |
1044 | */ | |
1045 | ||
1046 | char * | |
1047 | Perl_savesharedsvpv(pTHX_ SV *sv) | |
1048 | { | |
1049 | STRLEN len; | |
1050 | const char * const pv = SvPV_const(sv, len); | |
1051 | ||
1052 | PERL_ARGS_ASSERT_SAVESHAREDSVPV; | |
1053 | ||
1054 | return savesharedpvn(pv, len); | |
1055 | } | |
05ec9bb3 | 1056 | |
cea2e8a9 | 1057 | /* the SV for Perl_form() and mess() is not kept in an arena */ |
fc36a67e | 1058 | |
76e3520e | 1059 | STATIC SV * |
cea2e8a9 | 1060 | S_mess_alloc(pTHX) |
fc36a67e | 1061 | { |
97aff369 | 1062 | dVAR; |
fc36a67e | 1063 | SV *sv; |
1064 | XPVMG *any; | |
1065 | ||
627364f1 | 1066 | if (PL_phase != PERL_PHASE_DESTRUCT) |
84bafc02 | 1067 | return newSVpvs_flags("", SVs_TEMP); |
e72dc28c | 1068 | |
0372dbb6 GS |
1069 | if (PL_mess_sv) |
1070 | return PL_mess_sv; | |
1071 | ||
fc36a67e | 1072 | /* Create as PVMG now, to avoid any upgrading later */ |
a02a5408 JC |
1073 | Newx(sv, 1, SV); |
1074 | Newxz(any, 1, XPVMG); | |
fc36a67e | 1075 | SvFLAGS(sv) = SVt_PVMG; |
1076 | SvANY(sv) = (void*)any; | |
6136c704 | 1077 | SvPV_set(sv, NULL); |
fc36a67e | 1078 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ |
e72dc28c | 1079 | PL_mess_sv = sv; |
fc36a67e | 1080 | return sv; |
1081 | } | |
1082 | ||
c5be433b | 1083 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1084 | char * |
1085 | Perl_form_nocontext(const char* pat, ...) | |
1086 | { | |
1087 | dTHX; | |
c5be433b | 1088 | char *retval; |
cea2e8a9 | 1089 | va_list args; |
7918f24d | 1090 | PERL_ARGS_ASSERT_FORM_NOCONTEXT; |
cea2e8a9 | 1091 | va_start(args, pat); |
c5be433b | 1092 | retval = vform(pat, &args); |
cea2e8a9 | 1093 | va_end(args); |
c5be433b | 1094 | return retval; |
cea2e8a9 | 1095 | } |
c5be433b | 1096 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1097 | |
7c9e965c | 1098 | /* |
ccfc67b7 | 1099 | =head1 Miscellaneous Functions |
7c9e965c JP |
1100 | =for apidoc form |
1101 | ||
1102 | Takes a sprintf-style format pattern and conventional | |
1103 | (non-SV) arguments and returns the formatted string. | |
1104 | ||
1105 | (char *) Perl_form(pTHX_ const char* pat, ...) | |
1106 | ||
1107 | can be used any place a string (char *) is required: | |
1108 | ||
1109 | char * s = Perl_form("%d.%d",major,minor); | |
1110 | ||
1111 | Uses a single private buffer so if you want to format several strings you | |
1112 | must explicitly copy the earlier strings away (and free the copies when you | |
1113 | are done). | |
1114 | ||
1115 | =cut | |
1116 | */ | |
1117 | ||
8990e307 | 1118 | char * |
864dbfa3 | 1119 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1120 | { |
c5be433b | 1121 | char *retval; |
46fc3d4c | 1122 | va_list args; |
7918f24d | 1123 | PERL_ARGS_ASSERT_FORM; |
46fc3d4c | 1124 | va_start(args, pat); |
c5be433b | 1125 | retval = vform(pat, &args); |
46fc3d4c | 1126 | va_end(args); |
c5be433b GS |
1127 | return retval; |
1128 | } | |
1129 | ||
1130 | char * | |
1131 | Perl_vform(pTHX_ const char *pat, va_list *args) | |
1132 | { | |
2d03de9c | 1133 | SV * const sv = mess_alloc(); |
7918f24d | 1134 | PERL_ARGS_ASSERT_VFORM; |
4608196e | 1135 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
e72dc28c | 1136 | return SvPVX(sv); |
46fc3d4c | 1137 | } |
a687059c | 1138 | |
c5df3096 Z |
1139 | /* |
1140 | =for apidoc Am|SV *|mess|const char *pat|... | |
1141 | ||
1142 | Take a sprintf-style format pattern and argument list. These are used to | |
1143 | generate a string message. If the message does not end with a newline, | |
1144 | then it will be extended with some indication of the current location | |
1145 | in the code, as described for L</mess_sv>. | |
1146 | ||
1147 | Normally, the resulting message is returned in a new mortal SV. | |
1148 | During global destruction a single SV may be shared between uses of | |
1149 | this function. | |
1150 | ||
1151 | =cut | |
1152 | */ | |
1153 | ||
5a844595 GS |
1154 | #if defined(PERL_IMPLICIT_CONTEXT) |
1155 | SV * | |
1156 | Perl_mess_nocontext(const char *pat, ...) | |
1157 | { | |
1158 | dTHX; | |
1159 | SV *retval; | |
1160 | va_list args; | |
7918f24d | 1161 | PERL_ARGS_ASSERT_MESS_NOCONTEXT; |
5a844595 GS |
1162 | va_start(args, pat); |
1163 | retval = vmess(pat, &args); | |
1164 | va_end(args); | |
1165 | return retval; | |
1166 | } | |
1167 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1168 | ||
06bf62c7 | 1169 | SV * |
5a844595 GS |
1170 | Perl_mess(pTHX_ const char *pat, ...) |
1171 | { | |
1172 | SV *retval; | |
1173 | va_list args; | |
7918f24d | 1174 | PERL_ARGS_ASSERT_MESS; |
5a844595 GS |
1175 | va_start(args, pat); |
1176 | retval = vmess(pat, &args); | |
1177 | va_end(args); | |
1178 | return retval; | |
1179 | } | |
1180 | ||
5f66b61c AL |
1181 | STATIC const COP* |
1182 | S_closest_cop(pTHX_ const COP *cop, const OP *o) | |
ae7d165c | 1183 | { |
97aff369 | 1184 | dVAR; |
ae7d165c PJ |
1185 | /* Look for PL_op starting from o. cop is the last COP we've seen. */ |
1186 | ||
7918f24d NC |
1187 | PERL_ARGS_ASSERT_CLOSEST_COP; |
1188 | ||
fabdb6c0 AL |
1189 | if (!o || o == PL_op) |
1190 | return cop; | |
ae7d165c PJ |
1191 | |
1192 | if (o->op_flags & OPf_KIDS) { | |
5f66b61c | 1193 | const OP *kid; |
fabdb6c0 | 1194 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
5f66b61c | 1195 | const COP *new_cop; |
ae7d165c PJ |
1196 | |
1197 | /* If the OP_NEXTSTATE has been optimised away we can still use it | |
1198 | * the get the file and line number. */ | |
1199 | ||
1200 | if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) | |
5f66b61c | 1201 | cop = (const COP *)kid; |
ae7d165c PJ |
1202 | |
1203 | /* Keep searching, and return when we've found something. */ | |
1204 | ||
1205 | new_cop = closest_cop(cop, kid); | |
fabdb6c0 AL |
1206 | if (new_cop) |
1207 | return new_cop; | |
ae7d165c PJ |
1208 | } |
1209 | } | |
1210 | ||
1211 | /* Nothing found. */ | |
1212 | ||
5f66b61c | 1213 | return NULL; |
ae7d165c PJ |
1214 | } |
1215 | ||
c5df3096 Z |
1216 | /* |
1217 | =for apidoc Am|SV *|mess_sv|SV *basemsg|bool consume | |
1218 | ||
1219 | Expands a message, intended for the user, to include an indication of | |
1220 | the current location in the code, if the message does not already appear | |
1221 | to be complete. | |
1222 | ||
1223 | C<basemsg> is the initial message or object. If it is a reference, it | |
1224 | will be used as-is and will be the result of this function. Otherwise it | |
1225 | is used as a string, and if it already ends with a newline, it is taken | |
1226 | to be complete, and the result of this function will be the same string. | |
1227 | If the message does not end with a newline, then a segment such as C<at | |
1228 | foo.pl line 37> will be appended, and possibly other clauses indicating | |
1229 | the current state of execution. The resulting message will end with a | |
1230 | dot and a newline. | |
1231 | ||
1232 | Normally, the resulting message is returned in a new mortal SV. | |
1233 | During global destruction a single SV may be shared between uses of this | |
1234 | function. If C<consume> is true, then the function is permitted (but not | |
1235 | required) to modify and return C<basemsg> instead of allocating a new SV. | |
1236 | ||
1237 | =cut | |
1238 | */ | |
1239 | ||
5a844595 | 1240 | SV * |
c5df3096 | 1241 | Perl_mess_sv(pTHX_ SV *basemsg, bool consume) |
46fc3d4c | 1242 | { |
97aff369 | 1243 | dVAR; |
c5df3096 | 1244 | SV *sv; |
46fc3d4c | 1245 | |
c5df3096 Z |
1246 | PERL_ARGS_ASSERT_MESS_SV; |
1247 | ||
1248 | if (SvROK(basemsg)) { | |
1249 | if (consume) { | |
1250 | sv = basemsg; | |
1251 | } | |
1252 | else { | |
1253 | sv = mess_alloc(); | |
1254 | sv_setsv(sv, basemsg); | |
1255 | } | |
1256 | return sv; | |
1257 | } | |
1258 | ||
1259 | if (SvPOK(basemsg) && consume) { | |
1260 | sv = basemsg; | |
1261 | } | |
1262 | else { | |
1263 | sv = mess_alloc(); | |
1264 | sv_copypv(sv, basemsg); | |
1265 | } | |
7918f24d | 1266 | |
46fc3d4c | 1267 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
ae7d165c PJ |
1268 | /* |
1269 | * Try and find the file and line for PL_op. This will usually be | |
1270 | * PL_curcop, but it might be a cop that has been optimised away. We | |
1271 | * can try to find such a cop by searching through the optree starting | |
1272 | * from the sibling of PL_curcop. | |
1273 | */ | |
1274 | ||
e1ec3a88 | 1275 | const COP *cop = closest_cop(PL_curcop, PL_curcop->op_sibling); |
5f66b61c AL |
1276 | if (!cop) |
1277 | cop = PL_curcop; | |
ae7d165c PJ |
1278 | |
1279 | if (CopLINE(cop)) | |
ed094faf | 1280 | Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf, |
3aed30dc | 1281 | OutCopFILE(cop), (IV)CopLINE(cop)); |
191f87d5 DH |
1282 | /* Seems that GvIO() can be untrustworthy during global destruction. */ |
1283 | if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO) | |
1284 | && IoLINES(GvIOp(PL_last_in_gv))) | |
1285 | { | |
2748e602 | 1286 | STRLEN l; |
e1ec3a88 | 1287 | const bool line_mode = (RsSIMPLE(PL_rs) && |
2748e602 | 1288 | *SvPV_const(PL_rs,l) == '\n' && l == 1); |
3b46b707 BF |
1289 | Perl_sv_catpvf(aTHX_ sv, ", <%"SVf"> %s %"IVdf, |
1290 | SVfARG(PL_last_in_gv == PL_argvgv | |
1291 | ? &PL_sv_no | |
1292 | : sv_2mortal(newSVhek(GvNAME_HEK(PL_last_in_gv)))), | |
edc2eac3 JH |
1293 | line_mode ? "line" : "chunk", |
1294 | (IV)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1295 | } |
627364f1 | 1296 | if (PL_phase == PERL_PHASE_DESTRUCT) |
5f66b61c AL |
1297 | sv_catpvs(sv, " during global destruction"); |
1298 | sv_catpvs(sv, ".\n"); | |
a687059c | 1299 | } |
06bf62c7 | 1300 | return sv; |
a687059c LW |
1301 | } |
1302 | ||
c5df3096 Z |
1303 | /* |
1304 | =for apidoc Am|SV *|vmess|const char *pat|va_list *args | |
1305 | ||
1306 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1307 | argument list. These are used to generate a string message. If the | |
1308 | message does not end with a newline, then it will be extended with | |
1309 | some indication of the current location in the code, as described for | |
1310 | L</mess_sv>. | |
1311 | ||
1312 | Normally, the resulting message is returned in a new mortal SV. | |
1313 | During global destruction a single SV may be shared between uses of | |
1314 | this function. | |
1315 | ||
1316 | =cut | |
1317 | */ | |
1318 | ||
1319 | SV * | |
1320 | Perl_vmess(pTHX_ const char *pat, va_list *args) | |
1321 | { | |
1322 | dVAR; | |
1323 | SV * const sv = mess_alloc(); | |
1324 | ||
1325 | PERL_ARGS_ASSERT_VMESS; | |
1326 | ||
1327 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); | |
1328 | return mess_sv(sv, 1); | |
1329 | } | |
1330 | ||
7ff03255 | 1331 | void |
7d0994e0 | 1332 | Perl_write_to_stderr(pTHX_ SV* msv) |
7ff03255 | 1333 | { |
27da23d5 | 1334 | dVAR; |
7ff03255 SG |
1335 | IO *io; |
1336 | MAGIC *mg; | |
1337 | ||
7918f24d NC |
1338 | PERL_ARGS_ASSERT_WRITE_TO_STDERR; |
1339 | ||
7ff03255 SG |
1340 | if (PL_stderrgv && SvREFCNT(PL_stderrgv) |
1341 | && (io = GvIO(PL_stderrgv)) | |
daba3364 | 1342 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
d1d7a15d NC |
1343 | Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, "PRINT", |
1344 | G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv); | |
7ff03255 SG |
1345 | else { |
1346 | #ifdef USE_SFIO | |
1347 | /* SFIO can really mess with your errno */ | |
4ee39169 | 1348 | dSAVED_ERRNO; |
7ff03255 | 1349 | #endif |
53c1dcc0 | 1350 | PerlIO * const serr = Perl_error_log; |
7ff03255 | 1351 | |
83c55556 | 1352 | do_print(msv, serr); |
7ff03255 SG |
1353 | (void)PerlIO_flush(serr); |
1354 | #ifdef USE_SFIO | |
4ee39169 | 1355 | RESTORE_ERRNO; |
7ff03255 SG |
1356 | #endif |
1357 | } | |
1358 | } | |
1359 | ||
c5df3096 Z |
1360 | /* |
1361 | =head1 Warning and Dieing | |
1362 | */ | |
1363 | ||
1364 | /* Common code used in dieing and warning */ | |
1365 | ||
1366 | STATIC SV * | |
1367 | S_with_queued_errors(pTHX_ SV *ex) | |
1368 | { | |
1369 | PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS; | |
1370 | if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) { | |
1371 | sv_catsv(PL_errors, ex); | |
1372 | ex = sv_mortalcopy(PL_errors); | |
1373 | SvCUR_set(PL_errors, 0); | |
1374 | } | |
1375 | return ex; | |
1376 | } | |
3ab1ac99 | 1377 | |
46d9c920 | 1378 | STATIC bool |
c5df3096 | 1379 | S_invoke_exception_hook(pTHX_ SV *ex, bool warn) |
63315e18 | 1380 | { |
97aff369 | 1381 | dVAR; |
63315e18 NC |
1382 | HV *stash; |
1383 | GV *gv; | |
1384 | CV *cv; | |
46d9c920 NC |
1385 | SV **const hook = warn ? &PL_warnhook : &PL_diehook; |
1386 | /* sv_2cv might call Perl_croak() or Perl_warner() */ | |
1387 | SV * const oldhook = *hook; | |
1388 | ||
c5df3096 Z |
1389 | if (!oldhook) |
1390 | return FALSE; | |
63315e18 | 1391 | |
63315e18 | 1392 | ENTER; |
46d9c920 NC |
1393 | SAVESPTR(*hook); |
1394 | *hook = NULL; | |
1395 | cv = sv_2cv(oldhook, &stash, &gv, 0); | |
63315e18 NC |
1396 | LEAVE; |
1397 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1398 | dSP; | |
c5df3096 | 1399 | SV *exarg; |
63315e18 NC |
1400 | |
1401 | ENTER; | |
1402 | save_re_context(); | |
46d9c920 NC |
1403 | if (warn) { |
1404 | SAVESPTR(*hook); | |
1405 | *hook = NULL; | |
1406 | } | |
c5df3096 Z |
1407 | exarg = newSVsv(ex); |
1408 | SvREADONLY_on(exarg); | |
1409 | SAVEFREESV(exarg); | |
63315e18 | 1410 | |
46d9c920 | 1411 | PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK); |
63315e18 | 1412 | PUSHMARK(SP); |
c5df3096 | 1413 | XPUSHs(exarg); |
63315e18 | 1414 | PUTBACK; |
daba3364 | 1415 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
63315e18 NC |
1416 | POPSTACK; |
1417 | LEAVE; | |
46d9c920 | 1418 | return TRUE; |
63315e18 | 1419 | } |
46d9c920 | 1420 | return FALSE; |
63315e18 NC |
1421 | } |
1422 | ||
c5df3096 Z |
1423 | /* |
1424 | =for apidoc Am|OP *|die_sv|SV *baseex | |
e07360fa | 1425 | |
c5df3096 Z |
1426 | Behaves the same as L</croak_sv>, except for the return type. |
1427 | It should be used only where the C<OP *> return type is required. | |
1428 | The function never actually returns. | |
e07360fa | 1429 | |
c5df3096 Z |
1430 | =cut |
1431 | */ | |
e07360fa | 1432 | |
c5df3096 Z |
1433 | OP * |
1434 | Perl_die_sv(pTHX_ SV *baseex) | |
36477c24 | 1435 | { |
c5df3096 Z |
1436 | PERL_ARGS_ASSERT_DIE_SV; |
1437 | croak_sv(baseex); | |
118e2215 | 1438 | assert(0); /* NOTREACHED */ |
ad09800f | 1439 | return NULL; |
36477c24 | 1440 | } |
1441 | ||
c5df3096 Z |
1442 | /* |
1443 | =for apidoc Am|OP *|die|const char *pat|... | |
1444 | ||
1445 | Behaves the same as L</croak>, except for the return type. | |
1446 | It should be used only where the C<OP *> return type is required. | |
1447 | The function never actually returns. | |
1448 | ||
1449 | =cut | |
1450 | */ | |
1451 | ||
c5be433b | 1452 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1453 | OP * |
1454 | Perl_die_nocontext(const char* pat, ...) | |
a687059c | 1455 | { |
cea2e8a9 | 1456 | dTHX; |
a687059c | 1457 | va_list args; |
cea2e8a9 | 1458 | va_start(args, pat); |
c5df3096 | 1459 | vcroak(pat, &args); |
118e2215 | 1460 | assert(0); /* NOTREACHED */ |
cea2e8a9 | 1461 | va_end(args); |
c5df3096 | 1462 | return NULL; |
cea2e8a9 | 1463 | } |
c5be433b | 1464 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 GS |
1465 | |
1466 | OP * | |
1467 | Perl_die(pTHX_ const char* pat, ...) | |
1468 | { | |
cea2e8a9 GS |
1469 | va_list args; |
1470 | va_start(args, pat); | |
c5df3096 | 1471 | vcroak(pat, &args); |
118e2215 | 1472 | assert(0); /* NOTREACHED */ |
cea2e8a9 | 1473 | va_end(args); |
c5df3096 | 1474 | return NULL; |
cea2e8a9 GS |
1475 | } |
1476 | ||
c5df3096 Z |
1477 | /* |
1478 | =for apidoc Am|void|croak_sv|SV *baseex | |
1479 | ||
1480 | This is an XS interface to Perl's C<die> function. | |
1481 | ||
1482 | C<baseex> is the error message or object. If it is a reference, it | |
1483 | will be used as-is. Otherwise it is used as a string, and if it does | |
1484 | not end with a newline then it will be extended with some indication of | |
1485 | the current location in the code, as described for L</mess_sv>. | |
1486 | ||
1487 | The error message or object will be used as an exception, by default | |
1488 | returning control to the nearest enclosing C<eval>, but subject to | |
1489 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv> | |
1490 | function never returns normally. | |
1491 | ||
1492 | To die with a simple string message, the L</croak> function may be | |
1493 | more convenient. | |
1494 | ||
1495 | =cut | |
1496 | */ | |
1497 | ||
c5be433b | 1498 | void |
c5df3096 | 1499 | Perl_croak_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1500 | { |
c5df3096 Z |
1501 | SV *ex = with_queued_errors(mess_sv(baseex, 0)); |
1502 | PERL_ARGS_ASSERT_CROAK_SV; | |
1503 | invoke_exception_hook(ex, FALSE); | |
1504 | die_unwind(ex); | |
1505 | } | |
1506 | ||
1507 | /* | |
1508 | =for apidoc Am|void|vcroak|const char *pat|va_list *args | |
1509 | ||
1510 | This is an XS interface to Perl's C<die> function. | |
1511 | ||
1512 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1513 | argument list. These are used to generate a string message. If the | |
1514 | message does not end with a newline, then it will be extended with | |
1515 | some indication of the current location in the code, as described for | |
1516 | L</mess_sv>. | |
1517 | ||
1518 | The error message will be used as an exception, by default | |
1519 | returning control to the nearest enclosing C<eval>, but subject to | |
1520 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1521 | function never returns normally. | |
a687059c | 1522 | |
c5df3096 Z |
1523 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> |
1524 | (C<$@>) will be used as an error message or object instead of building an | |
1525 | error message from arguments. If you want to throw a non-string object, | |
1526 | or build an error message in an SV yourself, it is preferable to use | |
1527 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
5a844595 | 1528 | |
c5df3096 Z |
1529 | =cut |
1530 | */ | |
1531 | ||
1532 | void | |
1533 | Perl_vcroak(pTHX_ const char* pat, va_list *args) | |
1534 | { | |
1535 | SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0)); | |
1536 | invoke_exception_hook(ex, FALSE); | |
1537 | die_unwind(ex); | |
a687059c LW |
1538 | } |
1539 | ||
c5df3096 Z |
1540 | /* |
1541 | =for apidoc Am|void|croak|const char *pat|... | |
1542 | ||
1543 | This is an XS interface to Perl's C<die> function. | |
1544 | ||
1545 | Take a sprintf-style format pattern and argument list. These are used to | |
1546 | generate a string message. If the message does not end with a newline, | |
1547 | then it will be extended with some indication of the current location | |
1548 | in the code, as described for L</mess_sv>. | |
1549 | ||
1550 | The error message will be used as an exception, by default | |
1551 | returning control to the nearest enclosing C<eval>, but subject to | |
1552 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1553 | function never returns normally. | |
1554 | ||
1555 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> | |
1556 | (C<$@>) will be used as an error message or object instead of building an | |
1557 | error message from arguments. If you want to throw a non-string object, | |
1558 | or build an error message in an SV yourself, it is preferable to use | |
1559 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
1560 | ||
1561 | =cut | |
1562 | */ | |
1563 | ||
c5be433b | 1564 | #if defined(PERL_IMPLICIT_CONTEXT) |
8990e307 | 1565 | void |
cea2e8a9 | 1566 | Perl_croak_nocontext(const char *pat, ...) |
a687059c | 1567 | { |
cea2e8a9 | 1568 | dTHX; |
a687059c | 1569 | va_list args; |
cea2e8a9 | 1570 | va_start(args, pat); |
c5be433b | 1571 | vcroak(pat, &args); |
118e2215 | 1572 | assert(0); /* NOTREACHED */ |
cea2e8a9 GS |
1573 | va_end(args); |
1574 | } | |
1575 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1576 | ||
c5df3096 Z |
1577 | void |
1578 | Perl_croak(pTHX_ const char *pat, ...) | |
1579 | { | |
1580 | va_list args; | |
1581 | va_start(args, pat); | |
1582 | vcroak(pat, &args); | |
118e2215 | 1583 | assert(0); /* NOTREACHED */ |
c5df3096 Z |
1584 | va_end(args); |
1585 | } | |
1586 | ||
954c1994 | 1587 | /* |
6ad8f254 NC |
1588 | =for apidoc Am|void|croak_no_modify |
1589 | ||
1590 | Exactly equivalent to C<Perl_croak(aTHX_ "%s", PL_no_modify)>, but generates | |
1591 | terser object code than using C<Perl_croak>. Less code used on exception code | |
1592 | paths reduces CPU cache pressure. | |
1593 | ||
d8e47b5c | 1594 | =cut |
6ad8f254 NC |
1595 | */ |
1596 | ||
1597 | void | |
cb077ed2 | 1598 | Perl_croak_no_modify() |
6ad8f254 | 1599 | { |
cb077ed2 | 1600 | Perl_croak_nocontext( "%s", PL_no_modify); |
6ad8f254 NC |
1601 | } |
1602 | ||
4cbe3a7d DD |
1603 | /* does not return, used in util.c perlio.c and win32.c |
1604 | This is typically called when malloc returns NULL. | |
1605 | */ | |
1606 | void | |
1607 | Perl_croak_no_mem() | |
1608 | { | |
1609 | dTHX; | |
77c1c05b | 1610 | |
4cbe3a7d DD |
1611 | /* Can't use PerlIO to write as it allocates memory */ |
1612 | PerlLIO_write(PerlIO_fileno(Perl_error_log), | |
1613 | PL_no_mem, sizeof(PL_no_mem)-1); | |
1614 | my_exit(1); | |
1615 | } | |
1616 | ||
43387ee1 AD |
1617 | /* saves machine code for a common noreturn idiom typically used in Newx*() */ |
1618 | void | |
1619 | Perl_croak_memory_wrap(void) | |
1620 | { | |
1621 | Perl_croak_nocontext("%s",PL_memory_wrap); | |
1622 | } | |
1623 | ||
1624 | ||
3d04513d DD |
1625 | /* does not return, used only in POPSTACK */ |
1626 | void | |
1627 | Perl_croak_popstack(void) | |
1628 | { | |
1629 | dTHX; | |
1630 | PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); | |
1631 | my_exit(1); | |
1632 | } | |
1633 | ||
6ad8f254 | 1634 | /* |
c5df3096 | 1635 | =for apidoc Am|void|warn_sv|SV *baseex |
ccfc67b7 | 1636 | |
c5df3096 | 1637 | This is an XS interface to Perl's C<warn> function. |
954c1994 | 1638 | |
c5df3096 Z |
1639 | C<baseex> is the error message or object. If it is a reference, it |
1640 | will be used as-is. Otherwise it is used as a string, and if it does | |
1641 | not end with a newline then it will be extended with some indication of | |
1642 | the current location in the code, as described for L</mess_sv>. | |
9983fa3c | 1643 | |
c5df3096 Z |
1644 | The error message or object will by default be written to standard error, |
1645 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
9983fa3c | 1646 | |
c5df3096 Z |
1647 | To warn with a simple string message, the L</warn> function may be |
1648 | more convenient. | |
954c1994 GS |
1649 | |
1650 | =cut | |
1651 | */ | |
1652 | ||
cea2e8a9 | 1653 | void |
c5df3096 | 1654 | Perl_warn_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1655 | { |
c5df3096 Z |
1656 | SV *ex = mess_sv(baseex, 0); |
1657 | PERL_ARGS_ASSERT_WARN_SV; | |
1658 | if (!invoke_exception_hook(ex, TRUE)) | |
1659 | write_to_stderr(ex); | |
cea2e8a9 GS |
1660 | } |
1661 | ||
c5df3096 Z |
1662 | /* |
1663 | =for apidoc Am|void|vwarn|const char *pat|va_list *args | |
1664 | ||
1665 | This is an XS interface to Perl's C<warn> function. | |
1666 | ||
1667 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1668 | argument list. These are used to generate a string message. If the | |
1669 | message does not end with a newline, then it will be extended with | |
1670 | some indication of the current location in the code, as described for | |
1671 | L</mess_sv>. | |
1672 | ||
1673 | The error message or object will by default be written to standard error, | |
1674 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1675 | ||
1676 | Unlike with L</vcroak>, C<pat> is not permitted to be null. | |
1677 | ||
1678 | =cut | |
1679 | */ | |
1680 | ||
c5be433b GS |
1681 | void |
1682 | Perl_vwarn(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 | 1683 | { |
c5df3096 | 1684 | SV *ex = vmess(pat, args); |
7918f24d | 1685 | PERL_ARGS_ASSERT_VWARN; |
c5df3096 Z |
1686 | if (!invoke_exception_hook(ex, TRUE)) |
1687 | write_to_stderr(ex); | |
1688 | } | |
7918f24d | 1689 | |
c5df3096 Z |
1690 | /* |
1691 | =for apidoc Am|void|warn|const char *pat|... | |
87582a92 | 1692 | |
c5df3096 Z |
1693 | This is an XS interface to Perl's C<warn> function. |
1694 | ||
1695 | Take a sprintf-style format pattern and argument list. These are used to | |
1696 | generate a string message. If the message does not end with a newline, | |
1697 | then it will be extended with some indication of the current location | |
1698 | in the code, as described for L</mess_sv>. | |
1699 | ||
1700 | The error message or object will by default be written to standard error, | |
1701 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1702 | ||
1703 | Unlike with L</croak>, C<pat> is not permitted to be null. | |
1704 | ||
1705 | =cut | |
1706 | */ | |
8d063cd8 | 1707 | |
c5be433b | 1708 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1709 | void |
1710 | Perl_warn_nocontext(const char *pat, ...) | |
1711 | { | |
1712 | dTHX; | |
1713 | va_list args; | |
7918f24d | 1714 | PERL_ARGS_ASSERT_WARN_NOCONTEXT; |
cea2e8a9 | 1715 | va_start(args, pat); |
c5be433b | 1716 | vwarn(pat, &args); |
cea2e8a9 GS |
1717 | va_end(args); |
1718 | } | |
1719 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1720 | ||
1721 | void | |
1722 | Perl_warn(pTHX_ const char *pat, ...) | |
1723 | { | |
1724 | va_list args; | |
7918f24d | 1725 | PERL_ARGS_ASSERT_WARN; |
cea2e8a9 | 1726 | va_start(args, pat); |
c5be433b | 1727 | vwarn(pat, &args); |
cea2e8a9 GS |
1728 | va_end(args); |
1729 | } | |
1730 | ||
c5be433b GS |
1731 | #if defined(PERL_IMPLICIT_CONTEXT) |
1732 | void | |
1733 | Perl_warner_nocontext(U32 err, const char *pat, ...) | |
1734 | { | |
27da23d5 | 1735 | dTHX; |
c5be433b | 1736 | va_list args; |
7918f24d | 1737 | PERL_ARGS_ASSERT_WARNER_NOCONTEXT; |
c5be433b GS |
1738 | va_start(args, pat); |
1739 | vwarner(err, pat, &args); | |
1740 | va_end(args); | |
1741 | } | |
1742 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1743 | ||
599cee73 | 1744 | void |
9b387841 NC |
1745 | Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...) |
1746 | { | |
1747 | PERL_ARGS_ASSERT_CK_WARNER_D; | |
1748 | ||
1749 | if (Perl_ckwarn_d(aTHX_ err)) { | |
1750 | va_list args; | |
1751 | va_start(args, pat); | |
1752 | vwarner(err, pat, &args); | |
1753 | va_end(args); | |
1754 | } | |
1755 | } | |
1756 | ||
1757 | void | |
a2a5de95 NC |
1758 | Perl_ck_warner(pTHX_ U32 err, const char* pat, ...) |
1759 | { | |
1760 | PERL_ARGS_ASSERT_CK_WARNER; | |
1761 | ||
1762 | if (Perl_ckwarn(aTHX_ err)) { | |
1763 | va_list args; | |
1764 | va_start(args, pat); | |
1765 | vwarner(err, pat, &args); | |
1766 | va_end(args); | |
1767 | } | |
1768 | } | |
1769 | ||
1770 | void | |
864dbfa3 | 1771 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 PM |
1772 | { |
1773 | va_list args; | |
7918f24d | 1774 | PERL_ARGS_ASSERT_WARNER; |
c5be433b GS |
1775 | va_start(args, pat); |
1776 | vwarner(err, pat, &args); | |
1777 | va_end(args); | |
1778 | } | |
1779 | ||
1780 | void | |
1781 | Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) | |
1782 | { | |
27da23d5 | 1783 | dVAR; |
7918f24d | 1784 | PERL_ARGS_ASSERT_VWARNER; |
5f2d9966 | 1785 | if (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) { |
a3b680e6 | 1786 | SV * const msv = vmess(pat, args); |
599cee73 | 1787 | |
c5df3096 Z |
1788 | invoke_exception_hook(msv, FALSE); |
1789 | die_unwind(msv); | |
599cee73 PM |
1790 | } |
1791 | else { | |
d13b0d77 | 1792 | Perl_vwarn(aTHX_ pat, args); |
599cee73 PM |
1793 | } |
1794 | } | |
1795 | ||
f54ba1c2 DM |
1796 | /* implements the ckWARN? macros */ |
1797 | ||
1798 | bool | |
1799 | Perl_ckwarn(pTHX_ U32 w) | |
1800 | { | |
97aff369 | 1801 | dVAR; |
ad287e37 NC |
1802 | /* If lexical warnings have not been set, use $^W. */ |
1803 | if (isLEXWARN_off) | |
1804 | return PL_dowarn & G_WARN_ON; | |
1805 | ||
26c7b074 | 1806 | return ckwarn_common(w); |
f54ba1c2 DM |
1807 | } |
1808 | ||
1809 | /* implements the ckWARN?_d macro */ | |
1810 | ||
1811 | bool | |
1812 | Perl_ckwarn_d(pTHX_ U32 w) | |
1813 | { | |
97aff369 | 1814 | dVAR; |
ad287e37 NC |
1815 | /* If lexical warnings have not been set then default classes warn. */ |
1816 | if (isLEXWARN_off) | |
1817 | return TRUE; | |
1818 | ||
26c7b074 NC |
1819 | return ckwarn_common(w); |
1820 | } | |
1821 | ||
1822 | static bool | |
1823 | S_ckwarn_common(pTHX_ U32 w) | |
1824 | { | |
ad287e37 NC |
1825 | if (PL_curcop->cop_warnings == pWARN_ALL) |
1826 | return TRUE; | |
1827 | ||
1828 | if (PL_curcop->cop_warnings == pWARN_NONE) | |
1829 | return FALSE; | |
1830 | ||
98fe6610 NC |
1831 | /* Check the assumption that at least the first slot is non-zero. */ |
1832 | assert(unpackWARN1(w)); | |
1833 | ||
1834 | /* Check the assumption that it is valid to stop as soon as a zero slot is | |
1835 | seen. */ | |
1836 | if (!unpackWARN2(w)) { | |
1837 | assert(!unpackWARN3(w)); | |
1838 | assert(!unpackWARN4(w)); | |
1839 | } else if (!unpackWARN3(w)) { | |
1840 | assert(!unpackWARN4(w)); | |
1841 | } | |
1842 | ||
26c7b074 NC |
1843 | /* Right, dealt with all the special cases, which are implemented as non- |
1844 | pointers, so there is a pointer to a real warnings mask. */ | |
98fe6610 NC |
1845 | do { |
1846 | if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))) | |
1847 | return TRUE; | |
1848 | } while (w >>= WARNshift); | |
1849 | ||
1850 | return FALSE; | |
f54ba1c2 DM |
1851 | } |
1852 | ||
72dc9ed5 NC |
1853 | /* Set buffer=NULL to get a new one. */ |
1854 | STRLEN * | |
8ee4cf24 | 1855 | Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits, |
72dc9ed5 | 1856 | STRLEN size) { |
5af88345 FC |
1857 | const MEM_SIZE len_wanted = |
1858 | sizeof(STRLEN) + (size > WARNsize ? size : WARNsize); | |
35da51f7 | 1859 | PERL_UNUSED_CONTEXT; |
7918f24d | 1860 | PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD; |
72dc9ed5 | 1861 | |
10edeb5d JH |
1862 | buffer = (STRLEN*) |
1863 | (specialWARN(buffer) ? | |
1864 | PerlMemShared_malloc(len_wanted) : | |
1865 | PerlMemShared_realloc(buffer, len_wanted)); | |
72dc9ed5 NC |
1866 | buffer[0] = size; |
1867 | Copy(bits, (buffer + 1), size, char); | |
5af88345 FC |
1868 | if (size < WARNsize) |
1869 | Zero((char *)(buffer + 1) + size, WARNsize - size, char); | |
72dc9ed5 NC |
1870 | return buffer; |
1871 | } | |
f54ba1c2 | 1872 | |
e6587932 DM |
1873 | /* since we've already done strlen() for both nam and val |
1874 | * we can use that info to make things faster than | |
1875 | * sprintf(s, "%s=%s", nam, val) | |
1876 | */ | |
1877 | #define my_setenv_format(s, nam, nlen, val, vlen) \ | |
1878 | Copy(nam, s, nlen, char); \ | |
1879 | *(s+nlen) = '='; \ | |
1880 | Copy(val, s+(nlen+1), vlen, char); \ | |
1881 | *(s+(nlen+1+vlen)) = '\0' | |
1882 | ||
c5d12488 JH |
1883 | #ifdef USE_ENVIRON_ARRAY |
1884 | /* VMS' my_setenv() is in vms.c */ | |
1885 | #if !defined(WIN32) && !defined(NETWARE) | |
8d063cd8 | 1886 | void |
e1ec3a88 | 1887 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
8d063cd8 | 1888 | { |
27da23d5 | 1889 | dVAR; |
4efc5df6 GS |
1890 | #ifdef USE_ITHREADS |
1891 | /* only parent thread can modify process environment */ | |
1892 | if (PL_curinterp == aTHX) | |
1893 | #endif | |
1894 | { | |
f2517201 | 1895 | #ifndef PERL_USE_SAFE_PUTENV |
50acdf95 | 1896 | if (!PL_use_safe_putenv) { |
c5d12488 | 1897 | /* most putenv()s leak, so we manipulate environ directly */ |
eb578fdb KW |
1898 | I32 i; |
1899 | const I32 len = strlen(nam); | |
c5d12488 JH |
1900 | int nlen, vlen; |
1901 | ||
3a9222be JH |
1902 | /* where does it go? */ |
1903 | for (i = 0; environ[i]; i++) { | |
1904 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') | |
1905 | break; | |
1906 | } | |
1907 | ||
c5d12488 JH |
1908 | if (environ == PL_origenviron) { /* need we copy environment? */ |
1909 | I32 j; | |
1910 | I32 max; | |
1911 | char **tmpenv; | |
1912 | ||
1913 | max = i; | |
1914 | while (environ[max]) | |
1915 | max++; | |
1916 | tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); | |
1917 | for (j=0; j<max; j++) { /* copy environment */ | |
1918 | const int len = strlen(environ[j]); | |
1919 | tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char)); | |
1920 | Copy(environ[j], tmpenv[j], len+1, char); | |
1921 | } | |
1922 | tmpenv[max] = NULL; | |
1923 | environ = tmpenv; /* tell exec where it is now */ | |
1924 | } | |
1925 | if (!val) { | |
1926 | safesysfree(environ[i]); | |
1927 | while (environ[i]) { | |
1928 | environ[i] = environ[i+1]; | |
1929 | i++; | |
a687059c | 1930 | } |
c5d12488 JH |
1931 | return; |
1932 | } | |
1933 | if (!environ[i]) { /* does not exist yet */ | |
1934 | environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); | |
1935 | environ[i+1] = NULL; /* make sure it's null terminated */ | |
1936 | } | |
1937 | else | |
1938 | safesysfree(environ[i]); | |
1939 | nlen = strlen(nam); | |
1940 | vlen = strlen(val); | |
1941 | ||
1942 | environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char)); | |
1943 | /* all that work just for this */ | |
1944 | my_setenv_format(environ[i], nam, nlen, val, vlen); | |
50acdf95 | 1945 | } else { |
c5d12488 | 1946 | # endif |
739a0b84 | 1947 | # if defined(__CYGWIN__)|| defined(__SYMBIAN32__) || defined(__riscos__) |
88f5bc07 AB |
1948 | # if defined(HAS_UNSETENV) |
1949 | if (val == NULL) { | |
1950 | (void)unsetenv(nam); | |
1951 | } else { | |
1952 | (void)setenv(nam, val, 1); | |
1953 | } | |
1954 | # else /* ! HAS_UNSETENV */ | |
1955 | (void)setenv(nam, val, 1); | |
1956 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 1957 | # else |
88f5bc07 AB |
1958 | # if defined(HAS_UNSETENV) |
1959 | if (val == NULL) { | |
ba88ff58 MJ |
1960 | if (environ) /* old glibc can crash with null environ */ |
1961 | (void)unsetenv(nam); | |
88f5bc07 | 1962 | } else { |
c4420975 AL |
1963 | const int nlen = strlen(nam); |
1964 | const int vlen = strlen(val); | |
1965 | char * const new_env = | |
88f5bc07 AB |
1966 | (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); |
1967 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
1968 | (void)putenv(new_env); | |
1969 | } | |
1970 | # else /* ! HAS_UNSETENV */ | |
1971 | char *new_env; | |
c4420975 AL |
1972 | const int nlen = strlen(nam); |
1973 | int vlen; | |
88f5bc07 AB |
1974 | if (!val) { |
1975 | val = ""; | |
1976 | } | |
1977 | vlen = strlen(val); | |
1978 | new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); | |
1979 | /* all that work just for this */ | |
1980 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
1981 | (void)putenv(new_env); | |
1982 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 1983 | # endif /* __CYGWIN__ */ |
50acdf95 MS |
1984 | #ifndef PERL_USE_SAFE_PUTENV |
1985 | } | |
1986 | #endif | |
4efc5df6 | 1987 | } |
8d063cd8 LW |
1988 | } |
1989 | ||
c5d12488 | 1990 | #else /* WIN32 || NETWARE */ |
68dc0745 | 1991 | |
1992 | void | |
72229eff | 1993 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
68dc0745 | 1994 | { |
27da23d5 | 1995 | dVAR; |
eb578fdb | 1996 | char *envstr; |
c5d12488 JH |
1997 | const int nlen = strlen(nam); |
1998 | int vlen; | |
e6587932 | 1999 | |
c5d12488 JH |
2000 | if (!val) { |
2001 | val = ""; | |
ac5c734f | 2002 | } |
c5d12488 JH |
2003 | vlen = strlen(val); |
2004 | Newx(envstr, nlen+vlen+2, char); | |
2005 | my_setenv_format(envstr, nam, nlen, val, vlen); | |
2006 | (void)PerlEnv_putenv(envstr); | |
2007 | Safefree(envstr); | |
3e3baf6d TB |
2008 | } |
2009 | ||
c5d12488 | 2010 | #endif /* WIN32 || NETWARE */ |
3e3baf6d | 2011 | |
739a0b84 | 2012 | #endif /* !VMS */ |
378cc40b | 2013 | |
16d20bd9 | 2014 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 2015 | I32 |
6e732051 | 2016 | Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */ |
378cc40b | 2017 | { |
35da51f7 | 2018 | I32 retries = 0; |
378cc40b | 2019 | |
7918f24d NC |
2020 | PERL_ARGS_ASSERT_UNLNK; |
2021 | ||
35da51f7 AL |
2022 | while (PerlLIO_unlink(f) >= 0) |
2023 | retries++; | |
2024 | return retries ? 0 : -1; | |
378cc40b LW |
2025 | } |
2026 | #endif | |
2027 | ||
7a3f2258 | 2028 | /* this is a drop-in replacement for bcopy() */ |
2253333f | 2029 | #if (!defined(HAS_MEMCPY) && !defined(HAS_BCOPY)) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY) && !defined(HAS_SAFE_BCOPY)) |
378cc40b | 2030 | char * |
5aaab254 | 2031 | Perl_my_bcopy(const char *from, char *to, I32 len) |
378cc40b | 2032 | { |
2d03de9c | 2033 | char * const retval = to; |
378cc40b | 2034 | |
7918f24d NC |
2035 | PERL_ARGS_ASSERT_MY_BCOPY; |
2036 | ||
223f01db KW |
2037 | assert(len >= 0); |
2038 | ||
7c0587c8 LW |
2039 | if (from - to >= 0) { |
2040 | while (len--) | |
2041 | *to++ = *from++; | |
2042 | } | |
2043 | else { | |
2044 | to += len; | |
2045 | from += len; | |
2046 | while (len--) | |
faf8582f | 2047 | *(--to) = *(--from); |
7c0587c8 | 2048 | } |
378cc40b LW |
2049 | return retval; |
2050 | } | |
ffed7fef | 2051 | #endif |
378cc40b | 2052 | |
7a3f2258 | 2053 | /* this is a drop-in replacement for memset() */ |
fc36a67e | 2054 | #ifndef HAS_MEMSET |
2055 | void * | |
5aaab254 | 2056 | Perl_my_memset(char *loc, I32 ch, I32 len) |
fc36a67e | 2057 | { |
2d03de9c | 2058 | char * const retval = loc; |
fc36a67e | 2059 | |
7918f24d NC |
2060 | PERL_ARGS_ASSERT_MY_MEMSET; |
2061 | ||
223f01db KW |
2062 | assert(len >= 0); |
2063 | ||
fc36a67e | 2064 | while (len--) |
2065 | *loc++ = ch; | |
2066 | return retval; | |
2067 | } | |
2068 | #endif | |
2069 | ||
7a3f2258 | 2070 | /* this is a drop-in replacement for bzero() */ |
7c0587c8 | 2071 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 2072 | char * |
5aaab254 | 2073 | Perl_my_bzero(char *loc, I32 len) |
378cc40b | 2074 | { |
2d03de9c | 2075 | char * const retval = loc; |
378cc40b | 2076 | |
7918f24d NC |
2077 | PERL_ARGS_ASSERT_MY_BZERO; |
2078 | ||
223f01db KW |
2079 | assert(len >= 0); |
2080 | ||
378cc40b LW |
2081 | while (len--) |
2082 | *loc++ = 0; | |
2083 | return retval; | |
2084 | } | |
2085 | #endif | |
7c0587c8 | 2086 | |
7a3f2258 | 2087 | /* this is a drop-in replacement for memcmp() */ |
36477c24 | 2088 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 2089 | I32 |
5aaab254 | 2090 | Perl_my_memcmp(const char *s1, const char *s2, I32 len) |
7c0587c8 | 2091 | { |
eb578fdb KW |
2092 | const U8 *a = (const U8 *)s1; |
2093 | const U8 *b = (const U8 *)s2; | |
2094 | I32 tmp; | |
7c0587c8 | 2095 | |
7918f24d NC |
2096 | PERL_ARGS_ASSERT_MY_MEMCMP; |
2097 | ||
223f01db KW |
2098 | assert(len >= 0); |
2099 | ||
7c0587c8 | 2100 | while (len--) { |
27da23d5 | 2101 | if ((tmp = *a++ - *b++)) |
7c0587c8 LW |
2102 | return tmp; |
2103 | } | |
2104 | return 0; | |
2105 | } | |
36477c24 | 2106 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 2107 | |
fe14fcc3 | 2108 | #ifndef HAS_VPRINTF |
d05d9be5 AD |
2109 | /* This vsprintf replacement should generally never get used, since |
2110 | vsprintf was available in both System V and BSD 2.11. (There may | |
2111 | be some cross-compilation or embedded set-ups where it is needed, | |
2112 | however.) | |
2113 | ||
2114 | If you encounter a problem in this function, it's probably a symptom | |
2115 | that Configure failed to detect your system's vprintf() function. | |
2116 | See the section on "item vsprintf" in the INSTALL file. | |
2117 | ||
2118 | This version may compile on systems with BSD-ish <stdio.h>, | |
2119 | but probably won't on others. | |
2120 | */ | |
a687059c | 2121 | |
85e6fe83 | 2122 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2123 | char * |
2124 | #else | |
2125 | int | |
2126 | #endif | |
d05d9be5 | 2127 | vsprintf(char *dest, const char *pat, void *args) |
a687059c LW |
2128 | { |
2129 | FILE fakebuf; | |
2130 | ||
d05d9be5 AD |
2131 | #if defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) |
2132 | FILE_ptr(&fakebuf) = (STDCHAR *) dest; | |
2133 | FILE_cnt(&fakebuf) = 32767; | |
2134 | #else | |
2135 | /* These probably won't compile -- If you really need | |
2136 | this, you'll have to figure out some other method. */ | |
a687059c LW |
2137 | fakebuf._ptr = dest; |
2138 | fakebuf._cnt = 32767; | |
d05d9be5 | 2139 | #endif |
35c8bce7 LW |
2140 | #ifndef _IOSTRG |
2141 | #define _IOSTRG 0 | |
2142 | #endif | |
a687059c LW |
2143 | fakebuf._flag = _IOWRT|_IOSTRG; |
2144 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
d05d9be5 AD |
2145 | #if defined(STDIO_PTR_LVALUE) |
2146 | *(FILE_ptr(&fakebuf)++) = '\0'; | |
2147 | #else | |
2148 | /* PerlIO has probably #defined away fputc, but we want it here. */ | |
2149 | # ifdef fputc | |
2150 | # undef fputc /* XXX Should really restore it later */ | |
2151 | # endif | |
2152 | (void)fputc('\0', &fakebuf); | |
2153 | #endif | |
85e6fe83 | 2154 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2155 | return(dest); |
2156 | #else | |
2157 | return 0; /* perl doesn't use return value */ | |
2158 | #endif | |
2159 | } | |
2160 | ||
fe14fcc3 | 2161 | #endif /* HAS_VPRINTF */ |
a687059c LW |
2162 | |
2163 | #ifdef MYSWAP | |
ffed7fef | 2164 | #if BYTEORDER != 0x4321 |
a687059c | 2165 | short |
864dbfa3 | 2166 | Perl_my_swap(pTHX_ short s) |
a687059c LW |
2167 | { |
2168 | #if (BYTEORDER & 1) == 0 | |
2169 | short result; | |
2170 | ||
2171 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
2172 | return result; | |
2173 | #else | |
2174 | return s; | |
2175 | #endif | |
2176 | } | |
2177 | ||
2178 | long | |
864dbfa3 | 2179 | Perl_my_htonl(pTHX_ long l) |
a687059c LW |
2180 | { |
2181 | union { | |
2182 | long result; | |
ffed7fef | 2183 | char c[sizeof(long)]; |
a687059c LW |
2184 | } u; |
2185 | ||
cef6ea9d JH |
2186 | #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 |
2187 | #if BYTEORDER == 0x12345678 | |
2188 | u.result = 0; | |
2189 | #endif | |
a687059c LW |
2190 | u.c[0] = (l >> 24) & 255; |
2191 | u.c[1] = (l >> 16) & 255; | |
2192 | u.c[2] = (l >> 8) & 255; | |
2193 | u.c[3] = l & 255; | |
2194 | return u.result; | |
2195 | #else | |
ffed7fef | 2196 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2197 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2198 | #else |
eb578fdb KW |
2199 | I32 o; |
2200 | I32 s; | |
a687059c | 2201 | |
ffed7fef LW |
2202 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2203 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
2204 | } |
2205 | return u.result; | |
2206 | #endif | |
2207 | #endif | |
2208 | } | |
2209 | ||
2210 | long | |
864dbfa3 | 2211 | Perl_my_ntohl(pTHX_ long l) |
a687059c LW |
2212 | { |
2213 | union { | |
2214 | long l; | |
ffed7fef | 2215 | char c[sizeof(long)]; |
a687059c LW |
2216 | } u; |
2217 | ||
ffed7fef | 2218 | #if BYTEORDER == 0x1234 |
a687059c LW |
2219 | u.c[0] = (l >> 24) & 255; |
2220 | u.c[1] = (l >> 16) & 255; | |
2221 | u.c[2] = (l >> 8) & 255; | |
2222 | u.c[3] = l & 255; | |
2223 | return u.l; | |
2224 | #else | |
ffed7fef | 2225 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2226 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2227 | #else |
eb578fdb KW |
2228 | I32 o; |
2229 | I32 s; | |
a687059c LW |
2230 | |
2231 | u.l = l; | |
2232 | l = 0; | |
ffed7fef LW |
2233 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2234 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
2235 | } |
2236 | return l; | |
2237 | #endif | |
2238 | #endif | |
2239 | } | |
2240 | ||
ffed7fef | 2241 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
2242 | #endif /* MYSWAP */ |
2243 | ||
2244 | /* | |
2245 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
2246 | * If these functions are defined, | |
2247 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
2248 | * However, this is not assumed. | |
2249 | * -DWS | |
2250 | */ | |
2251 | ||
1109a392 | 2252 | #define HTOLE(name,type) \ |
988174c1 | 2253 | type \ |
5aaab254 | 2254 | name (type n) \ |
988174c1 LW |
2255 | { \ |
2256 | union { \ | |
2257 | type value; \ | |
2258 | char c[sizeof(type)]; \ | |
2259 | } u; \ | |
5aaab254 KW |
2260 | U32 i; \ |
2261 | U32 s = 0; \ | |
1109a392 | 2262 | for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
988174c1 LW |
2263 | u.c[i] = (n >> s) & 0xFF; \ |
2264 | } \ | |
2265 | return u.value; \ | |
2266 | } | |
2267 | ||
1109a392 | 2268 | #define LETOH(name,type) \ |
988174c1 | 2269 | type \ |
5aaab254 | 2270 | name (type n) \ |
988174c1 LW |
2271 | { \ |
2272 | union { \ | |
2273 | type value; \ | |
2274 | char c[sizeof(type)]; \ | |
2275 | } u; \ | |
5aaab254 KW |
2276 | U32 i; \ |
2277 | U32 s = 0; \ | |
988174c1 LW |
2278 | u.value = n; \ |
2279 | n = 0; \ | |
1109a392 MHM |
2280 | for (i = 0; i < sizeof(u.c); i++, s += 8) { \ |
2281 | n |= ((type)(u.c[i] & 0xFF)) << s; \ | |
988174c1 LW |
2282 | } \ |
2283 | return n; \ | |
2284 | } | |
2285 | ||
1109a392 MHM |
2286 | /* |
2287 | * Big-endian byte order functions. | |
2288 | */ | |
2289 | ||
2290 | #define HTOBE(name,type) \ | |
2291 | type \ | |
5aaab254 | 2292 | name (type n) \ |
1109a392 MHM |
2293 | { \ |
2294 | union { \ | |
2295 | type value; \ | |
2296 | char c[sizeof(type)]; \ | |
2297 | } u; \ | |
5aaab254 KW |
2298 | U32 i; \ |
2299 | U32 s = 8*(sizeof(u.c)-1); \ | |
1109a392 MHM |
2300 | for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ |
2301 | u.c[i] = (n >> s) & 0xFF; \ | |
2302 | } \ | |
2303 | return u.value; \ | |
2304 | } | |
2305 | ||
2306 | #define BETOH(name,type) \ | |
2307 | type \ | |
5aaab254 | 2308 | name (type n) \ |
1109a392 MHM |
2309 | { \ |
2310 | union { \ | |
2311 | type value; \ | |
2312 | char c[sizeof(type)]; \ | |
2313 | } u; \ | |
5aaab254 KW |
2314 | U32 i; \ |
2315 | U32 s = 8*(sizeof(u.c)-1); \ | |
1109a392 MHM |
2316 | u.value = n; \ |
2317 | n = 0; \ | |
2318 | for (i = 0; i < sizeof(u.c); i++, s -= 8) { \ | |
2319 | n |= ((type)(u.c[i] & 0xFF)) << s; \ | |
2320 | } \ | |
2321 | return n; \ | |
2322 | } | |
2323 | ||
2324 | /* | |
2325 | * If we just can't do it... | |
2326 | */ | |
2327 | ||
2328 | #define NOT_AVAIL(name,type) \ | |
2329 | type \ | |
5aaab254 | 2330 | name (type n) \ |
1109a392 MHM |
2331 | { \ |
2332 | Perl_croak_nocontext(#name "() not available"); \ | |
2333 | return n; /* not reached */ \ | |
2334 | } | |
2335 | ||
2336 | ||
988174c1 | 2337 | #if defined(HAS_HTOVS) && !defined(htovs) |
1109a392 | 2338 | HTOLE(htovs,short) |
988174c1 LW |
2339 | #endif |
2340 | #if defined(HAS_HTOVL) && !defined(htovl) | |
1109a392 | 2341 | HTOLE(htovl,long) |
988174c1 LW |
2342 | #endif |
2343 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
1109a392 | 2344 | LETOH(vtohs,short) |
988174c1 LW |
2345 | #endif |
2346 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
1109a392 MHM |
2347 | LETOH(vtohl,long) |
2348 | #endif | |
2349 | ||
2350 | #ifdef PERL_NEED_MY_HTOLE16 | |
2351 | # if U16SIZE == 2 | |
2352 | HTOLE(Perl_my_htole16,U16) | |
2353 | # else | |
2354 | NOT_AVAIL(Perl_my_htole16,U16) | |
2355 | # endif | |
2356 | #endif | |
2357 | #ifdef PERL_NEED_MY_LETOH16 | |
2358 | # if U16SIZE == 2 | |
2359 | LETOH(Perl_my_letoh16,U16) | |
2360 | # else | |
2361 | NOT_AVAIL(Perl_my_letoh16,U16) | |
2362 | # endif | |
2363 | #endif | |
2364 | #ifdef PERL_NEED_MY_HTOBE16 | |
2365 | # if U16SIZE == 2 | |
2366 | HTOBE(Perl_my_htobe16,U16) | |
2367 | # else | |
2368 | NOT_AVAIL(Perl_my_htobe16,U16) | |
2369 | # endif | |
2370 | #endif | |
2371 | #ifdef PERL_NEED_MY_BETOH16 | |
2372 | # if U16SIZE == 2 | |
2373 | BETOH(Perl_my_betoh16,U16) | |
2374 | # else | |
2375 | NOT_AVAIL(Perl_my_betoh16,U16) | |
2376 | # endif | |
2377 | #endif | |
2378 | ||
2379 | #ifdef PERL_NEED_MY_HTOLE32 | |
2380 | # if U32SIZE == 4 | |
2381 | HTOLE(Perl_my_htole32,U32) | |
2382 | # else | |
2383 | NOT_AVAIL(Perl_my_htole32,U32) | |
2384 | # endif | |
2385 | #endif | |
2386 | #ifdef PERL_NEED_MY_LETOH32 | |
2387 | # if U32SIZE == 4 | |
2388 | LETOH(Perl_my_letoh32,U32) | |
2389 | # else | |
2390 | NOT_AVAIL(Perl_my_letoh32,U32) | |
2391 | # endif | |
2392 | #endif | |
2393 | #ifdef PERL_NEED_MY_HTOBE32 | |
2394 | # if U32SIZE == 4 | |
2395 | HTOBE(Perl_my_htobe32,U32) | |
2396 | # else | |
2397 | NOT_AVAIL(Perl_my_htobe32,U32) | |
2398 | # endif | |
2399 | #endif | |
2400 | #ifdef PERL_NEED_MY_BETOH32 | |
2401 | # if U32SIZE == 4 | |
2402 | BETOH(Perl_my_betoh32,U32) | |
2403 | # else | |
2404 | NOT_AVAIL(Perl_my_betoh32,U32) | |
2405 | # endif | |
2406 | #endif | |
2407 | ||
2408 | #ifdef PERL_NEED_MY_HTOLE64 | |
2409 | # if U64SIZE == 8 | |
2410 | HTOLE(Perl_my_htole64,U64) | |
2411 | # else | |
2412 | NOT_AVAIL(Perl_my_htole64,U64) | |
2413 | # endif | |
2414 | #endif | |
2415 | #ifdef PERL_NEED_MY_LETOH64 | |
2416 | # if U64SIZE == 8 | |
2417 | LETOH(Perl_my_letoh64,U64) | |
2418 | # else | |
2419 | NOT_AVAIL(Perl_my_letoh64,U64) | |
2420 | # endif | |
2421 | #endif | |
2422 | #ifdef PERL_NEED_MY_HTOBE64 | |
2423 | # if U64SIZE == 8 | |
2424 | HTOBE(Perl_my_htobe64,U64) | |
2425 | # else | |
2426 | NOT_AVAIL(Perl_my_htobe64,U64) | |
2427 | # endif | |
2428 | #endif | |
2429 | #ifdef PERL_NEED_MY_BETOH64 | |
2430 | # if U64SIZE == 8 | |
2431 | BETOH(Perl_my_betoh64,U64) | |
2432 | # else | |
2433 | NOT_AVAIL(Perl_my_betoh64,U64) | |
2434 | # endif | |
988174c1 | 2435 | #endif |
a687059c | 2436 | |
1109a392 MHM |
2437 | #ifdef PERL_NEED_MY_HTOLES |
2438 | HTOLE(Perl_my_htoles,short) | |
2439 | #endif | |
2440 | #ifdef PERL_NEED_MY_LETOHS | |
2441 | LETOH(Perl_my_letohs,short) | |
2442 | #endif | |
2443 | #ifdef PERL_NEED_MY_HTOBES | |
2444 | HTOBE(Perl_my_htobes,short) | |
2445 | #endif | |
2446 | #ifdef PERL_NEED_MY_BETOHS | |
2447 | BETOH(Perl_my_betohs,short) | |
2448 | #endif | |
2449 | ||
2450 | #ifdef PERL_NEED_MY_HTOLEI | |
2451 | HTOLE(Perl_my_htolei,int) | |
2452 | #endif | |
2453 | #ifdef PERL_NEED_MY_LETOHI | |
2454 | LETOH(Perl_my_letohi,int) | |
2455 | #endif | |
2456 | #ifdef PERL_NEED_MY_HTOBEI | |
2457 | HTOBE(Perl_my_htobei,int) | |
2458 | #endif | |
2459 | #ifdef PERL_NEED_MY_BETOHI | |
2460 | BETOH(Perl_my_betohi,int) | |
2461 | #endif | |
2462 | ||
2463 | #ifdef PERL_NEED_MY_HTOLEL | |
2464 | HTOLE(Perl_my_htolel,long) | |
2465 | #endif | |
2466 | #ifdef PERL_NEED_MY_LETOHL | |
2467 | LETOH(Perl_my_letohl,long) | |
2468 | #endif | |
2469 | #ifdef PERL_NEED_MY_HTOBEL | |
2470 | HTOBE(Perl_my_htobel,long) | |
2471 | #endif | |
2472 | #ifdef PERL_NEED_MY_BETOHL | |
2473 | BETOH(Perl_my_betohl,long) | |
2474 | #endif | |
2475 | ||
2476 | void | |
2477 | Perl_my_swabn(void *ptr, int n) | |
2478 | { | |
eb578fdb KW |
2479 | char *s = (char *)ptr; |
2480 | char *e = s + (n-1); | |
2481 | char tc; | |
1109a392 | 2482 | |
7918f24d NC |
2483 | PERL_ARGS_ASSERT_MY_SWABN; |
2484 | ||
1109a392 MHM |
2485 | for (n /= 2; n > 0; s++, e--, n--) { |
2486 | tc = *s; | |
2487 | *s = *e; | |
2488 | *e = tc; | |
2489 | } | |
2490 | } | |
2491 | ||
4a7d1889 | 2492 | PerlIO * |
c9289b7b | 2493 | Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args) |
4a7d1889 | 2494 | { |
739a0b84 | 2495 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(OS2) && !defined(VMS) && !defined(NETWARE) && !defined(__LIBCATAMOUNT__) |
97aff369 | 2496 | dVAR; |
1f852d0d | 2497 | int p[2]; |
eb578fdb KW |
2498 | I32 This, that; |
2499 | Pid_t pid; | |
1f852d0d NIS |
2500 | SV *sv; |
2501 | I32 did_pipes = 0; | |
2502 | int pp[2]; | |
2503 | ||
7918f24d NC |
2504 | PERL_ARGS_ASSERT_MY_POPEN_LIST; |
2505 | ||
1f852d0d NIS |
2506 | PERL_FLUSHALL_FOR_CHILD; |
2507 | This = (*mode == 'w'); | |
2508 | that = !This; | |
284167a5 | 2509 | if (TAINTING_get) { |
1f852d0d NIS |
2510 | taint_env(); |
2511 | taint_proper("Insecure %s%s", "EXEC"); | |
2512 | } | |
2513 | if (PerlProc_pipe(p) < 0) | |
4608196e | 2514 | return NULL; |
1f852d0d NIS |
2515 | /* Try for another pipe pair for error return */ |
2516 | if (PerlProc_pipe(pp) >= 0) | |
2517 | did_pipes = 1; | |
52e18b1f | 2518 | while ((pid = PerlProc_fork()) < 0) { |
1f852d0d NIS |
2519 | if (errno != EAGAIN) { |
2520 | PerlLIO_close(p[This]); | |
4e6dfe71 | 2521 | PerlLIO_close(p[that]); |
1f852d0d NIS |
2522 | if (did_pipes) { |
2523 | PerlLIO_close(pp[0]); | |
2524 | PerlLIO_close(pp[1]); | |
2525 | } | |
4608196e | 2526 | return NULL; |
1f852d0d | 2527 | } |
a2a5de95 | 2528 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
1f852d0d NIS |
2529 | sleep(5); |
2530 | } | |
2531 | if (pid == 0) { | |
2532 | /* Child */ | |
1f852d0d NIS |
2533 | #undef THIS |
2534 | #undef THAT | |
2535 | #define THIS that | |
2536 | #define THAT This | |
1f852d0d NIS |
2537 | /* Close parent's end of error status pipe (if any) */ |
2538 | if (did_pipes) { | |
2539 | PerlLIO_close(pp[0]); | |
2540 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2541 | /* Close error pipe automatically if exec works */ | |
2542 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2543 | #endif | |
2544 | } | |
2545 | /* Now dup our end of _the_ pipe to right position */ | |
2546 | if (p[THIS] != (*mode == 'r')) { | |
2547 | PerlLIO_dup2(p[THIS], *mode == 'r'); | |
2548 | PerlLIO_close(p[THIS]); | |
4e6dfe71 GS |
2549 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2550 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d | 2551 | } |
4e6dfe71 GS |
2552 | else |
2553 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d NIS |
2554 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
2555 | /* No automatic close - do it by hand */ | |
b7953727 JH |
2556 | # ifndef NOFILE |
2557 | # define NOFILE 20 | |
2558 | # endif | |
a080fe3d NIS |
2559 | { |
2560 | int fd; | |
2561 | ||
2562 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) { | |
3aed30dc | 2563 | if (fd != pp[1]) |
a080fe3d NIS |
2564 | PerlLIO_close(fd); |
2565 | } | |
1f852d0d NIS |
2566 | } |
2567 | #endif | |
a0714e2c | 2568 | do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes); |
1f852d0d NIS |
2569 | PerlProc__exit(1); |
2570 | #undef THIS | |
2571 | #undef THAT | |
2572 | } | |
2573 | /* Parent */ | |
52e18b1f | 2574 | do_execfree(); /* free any memory malloced by child on fork */ |
1f852d0d NIS |
2575 | if (did_pipes) |
2576 | PerlLIO_close(pp[1]); | |
2577 | /* Keep the lower of the two fd numbers */ | |
2578 | if (p[that] < p[This]) { | |
2579 | PerlLIO_dup2(p[This], p[that]); | |
2580 | PerlLIO_close(p[This]); | |
2581 | p[This] = p[that]; | |
2582 | } | |
4e6dfe71 GS |
2583 | else |
2584 | PerlLIO_close(p[that]); /* close child's end of pipe */ | |
2585 | ||
1f852d0d | 2586 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2587 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2588 | SvIV_set(sv, pid); |
1f852d0d NIS |
2589 | PL_forkprocess = pid; |
2590 | /* If we managed to get status pipe check for exec fail */ | |
2591 | if (did_pipes && pid > 0) { | |
2592 | int errkid; | |
bb7a0f54 MHM |
2593 | unsigned n = 0; |
2594 | SSize_t n1; | |
1f852d0d NIS |
2595 | |
2596 | while (n < sizeof(int)) { | |
2597 | n1 = PerlLIO_read(pp[0], | |
2598 | (void*)(((char*)&errkid)+n), | |
2599 | (sizeof(int)) - n); | |
2600 | if (n1 <= 0) | |
2601 | break; | |
2602 | n += n1; | |
2603 | } | |
2604 | PerlLIO_close(pp[0]); | |
2605 | did_pipes = 0; | |
2606 | if (n) { /* Error */ | |
2607 | int pid2, status; | |
8c51524e | 2608 | PerlLIO_close(p[This]); |
1f852d0d | 2609 | if (n != sizeof(int)) |
5637ef5b | 2610 | Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n); |
1f852d0d NIS |
2611 | do { |
2612 | pid2 = wait4pid(pid, &status, 0); | |
2613 | } while (pid2 == -1 && errno == EINTR); | |
2614 | errno = errkid; /* Propagate errno from kid */ | |
4608196e | 2615 | return NULL; |
1f852d0d NIS |
2616 | } |
2617 | } | |
2618 | if (did_pipes) | |
2619 | PerlLIO_close(pp[0]); | |
2620 | return PerlIO_fdopen(p[This], mode); | |
2621 | #else | |
9d419b5f | 2622 | # ifdef OS2 /* Same, without fork()ing and all extra overhead... */ |
4e205ed6 | 2623 | return my_syspopen4(aTHX_ NULL, mode, n, args); |
9d419b5f | 2624 | # else |
4a7d1889 NIS |
2625 | Perl_croak(aTHX_ "List form of piped open not implemented"); |
2626 | return (PerlIO *) NULL; | |
9d419b5f | 2627 | # endif |
1f852d0d | 2628 | #endif |
4a7d1889 NIS |
2629 | } |
2630 | ||
5f05dabc | 2631 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
739a0b84 | 2632 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) |
760ac839 | 2633 | PerlIO * |
3dd43144 | 2634 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
a687059c | 2635 | { |
97aff369 | 2636 | dVAR; |
a687059c | 2637 | int p[2]; |
eb578fdb KW |
2638 | I32 This, that; |
2639 | Pid_t pid; | |
79072805 | 2640 | SV *sv; |
bfce84ec | 2641 | const I32 doexec = !(*cmd == '-' && cmd[1] == '\0'); |
e446cec8 IZ |
2642 | I32 did_pipes = 0; |
2643 | int pp[2]; | |
a687059c | 2644 | |
7918f24d NC |
2645 | PERL_ARGS_ASSERT_MY_POPEN; |
2646 | ||
45bc9206 | 2647 | PERL_FLUSHALL_FOR_CHILD; |
ddcf38b7 IZ |
2648 | #ifdef OS2 |
2649 | if (doexec) { | |
23da6c43 | 2650 | return my_syspopen(aTHX_ cmd,mode); |
ddcf38b7 | 2651 | } |
a1d180c4 | 2652 | #endif |
8ac85365 NIS |
2653 | This = (*mode == 'w'); |
2654 | that = !This; | |
284167a5 | 2655 | if (doexec && TAINTING_get) { |
bbce6d69 | 2656 | taint_env(); |
2657 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 2658 | } |
c2267164 | 2659 | if (PerlProc_pipe(p) < 0) |
4608196e | 2660 | return NULL; |
e446cec8 IZ |
2661 | if (doexec && PerlProc_pipe(pp) >= 0) |
2662 | did_pipes = 1; | |
52e18b1f | 2663 | while ((pid = PerlProc_fork()) < 0) { |
a687059c | 2664 | if (errno != EAGAIN) { |
6ad3d225 | 2665 | PerlLIO_close(p[This]); |
b5ac89c3 | 2666 | PerlLIO_close(p[that]); |
e446cec8 IZ |
2667 | if (did_pipes) { |
2668 | PerlLIO_close(pp[0]); | |
2669 | PerlLIO_close(pp[1]); | |
2670 | } | |
a687059c | 2671 | if (!doexec) |
b3647a36 | 2672 | Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno)); |
4608196e | 2673 | return NULL; |
a687059c | 2674 | } |
a2a5de95 | 2675 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
a687059c LW |
2676 | sleep(5); |
2677 | } | |
2678 | if (pid == 0) { | |
79072805 | 2679 | |
30ac6d9b GS |
2680 | #undef THIS |
2681 | #undef THAT | |
a687059c | 2682 | #define THIS that |
8ac85365 | 2683 | #define THAT This |
e446cec8 IZ |
2684 | if (did_pipes) { |
2685 | PerlLIO_close(pp[0]); | |
2686 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2687 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2688 | #endif | |
2689 | } | |
a687059c | 2690 | if (p[THIS] != (*mode == 'r')) { |
6ad3d225 GS |
2691 | PerlLIO_dup2(p[THIS], *mode == 'r'); |
2692 | PerlLIO_close(p[THIS]); | |
b5ac89c3 NIS |
2693 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2694 | PerlLIO_close(p[THAT]); | |
a687059c | 2695 | } |
b5ac89c3 NIS |
2696 | else |
2697 | PerlLIO_close(p[THAT]); | |
4435c477 | 2698 | #ifndef OS2 |
a687059c | 2699 | if (doexec) { |
a0d0e21e | 2700 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
2701 | #ifndef NOFILE |
2702 | #define NOFILE 20 | |
2703 | #endif | |
a080fe3d | 2704 | { |
3aed30dc | 2705 | int fd; |
a080fe3d NIS |
2706 | |
2707 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) | |
2708 | if (fd != pp[1]) | |
3aed30dc | 2709 | PerlLIO_close(fd); |
a080fe3d | 2710 | } |
ae986130 | 2711 | #endif |
a080fe3d NIS |
2712 | /* may or may not use the shell */ |
2713 | do_exec3(cmd, pp[1], did_pipes); | |
6ad3d225 | 2714 | PerlProc__exit(1); |
a687059c | 2715 | } |
4435c477 | 2716 | #endif /* defined OS2 */ |
713cef20 IZ |
2717 | |
2718 | #ifdef PERLIO_USING_CRLF | |
2719 | /* Since we circumvent IO layers when we manipulate low-level | |
2720 | filedescriptors directly, need to manually switch to the | |
2721 | default, binary, low-level mode; see PerlIOBuf_open(). */ | |
2722 | PerlLIO_setmode((*mode == 'r'), O_BINARY); | |
2723 | #endif | |
3280af22 | 2724 | PL_forkprocess = 0; |
ca0c25f6 | 2725 | #ifdef PERL_USES_PL_PIDSTATUS |
3280af22 | 2726 | hv_clear(PL_pidstatus); /* we have no children */ |
ca0c25f6 | 2727 | #endif |
4608196e | 2728 | return NULL; |
a687059c LW |
2729 | #undef THIS |
2730 | #undef THAT | |
2731 | } | |
b5ac89c3 | 2732 | do_execfree(); /* free any memory malloced by child on vfork */ |
e446cec8 IZ |
2733 | if (did_pipes) |
2734 | PerlLIO_close(pp[1]); | |
8ac85365 | 2735 | if (p[that] < p[This]) { |
6ad3d225 GS |
2736 | PerlLIO_dup2(p[This], p[that]); |
2737 | PerlLIO_close(p[This]); | |
8ac85365 | 2738 | p[This] = p[that]; |
62b28dd9 | 2739 | } |
b5ac89c3 NIS |
2740 | else |
2741 | PerlLIO_close(p[that]); | |
2742 | ||
3280af22 | 2743 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2744 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2745 | SvIV_set(sv, pid); |
3280af22 | 2746 | PL_forkprocess = pid; |
e446cec8 IZ |
2747 | if (did_pipes && pid > 0) { |
2748 | int errkid; | |
bb7a0f54 MHM |
2749 | unsigned n = 0; |
2750 | SSize_t n1; | |
e446cec8 IZ |
2751 | |
2752 | while (n < sizeof(int)) { | |
2753 | n1 = PerlLIO_read(pp[0], | |
2754 | (void*)(((char*)&errkid)+n), | |
2755 | (sizeof(int)) - n); | |
2756 | if (n1 <= 0) | |
2757 | break; | |
2758 | n += n1; | |
2759 | } | |
2f96c702 IZ |
2760 | PerlLIO_close(pp[0]); |
2761 | did_pipes = 0; | |
e446cec8 | 2762 | if (n) { /* Error */ |
faa466a7 | 2763 | int pid2, status; |
8c51524e | 2764 | PerlLIO_close(p[This]); |
e446cec8 | 2765 | if (n != sizeof(int)) |
5637ef5b | 2766 | Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n); |
faa466a7 RG |
2767 | do { |
2768 | pid2 = wait4pid(pid, &status, 0); | |
2769 | } while (pid2 == -1 && errno == EINTR); | |
e446cec8 | 2770 | errno = errkid; /* Propagate errno from kid */ |
4608196e | 2771 | return NULL; |
e446cec8 IZ |
2772 | } |
2773 | } | |
2774 | if (did_pipes) | |
2775 | PerlLIO_close(pp[0]); | |
8ac85365 | 2776 | return PerlIO_fdopen(p[This], mode); |
a687059c | 2777 | } |
7c0587c8 | 2778 | #else |
2b96b0a5 JH |
2779 | #if defined(DJGPP) |
2780 | FILE *djgpp_popen(); | |
2781 | PerlIO * | |
cef6ea9d | 2782 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
2b96b0a5 JH |
2783 | { |
2784 | PERL_FLUSHALL_FOR_CHILD; | |
2785 | /* Call system's popen() to get a FILE *, then import it. | |
2786 | used 0 for 2nd parameter to PerlIO_importFILE; | |
2787 | apparently not used | |
2788 | */ | |
2789 | return PerlIO_importFILE(djgpp_popen(cmd, mode), 0); | |
2790 | } | |
9c12f1e5 RGS |
2791 | #else |
2792 | #if defined(__LIBCATAMOUNT__) | |
2793 | PerlIO * | |
2794 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) | |
2795 | { | |
2796 | return NULL; | |
2797 | } | |
2798 | #endif | |
2b96b0a5 | 2799 | #endif |
7c0587c8 LW |
2800 | |
2801 | #endif /* !DOSISH */ | |
a687059c | 2802 | |
52e18b1f GS |
2803 | /* this is called in parent before the fork() */ |
2804 | void | |
2805 | Perl_atfork_lock(void) | |
2806 | { | |
27da23d5 | 2807 | dVAR; |
3db8f154 | 2808 | #if defined(USE_ITHREADS) |
52e18b1f | 2809 | /* locks must be held in locking order (if any) */ |
4da80956 P |
2810 | # ifdef USE_PERLIO |
2811 | MUTEX_LOCK(&PL_perlio_mutex); | |
2812 | # endif | |
52e18b1f GS |
2813 | # ifdef MYMALLOC |
2814 | MUTEX_LOCK(&PL_malloc_mutex); | |
2815 | # endif | |
2816 | OP_REFCNT_LOCK; | |
2817 | #endif | |
2818 | } | |
2819 | ||
2820 | /* this is called in both parent and child after the fork() */ | |
2821 | void | |
2822 | Perl_atfork_unlock(void) | |
2823 | { | |
27da23d5 | 2824 | dVAR; |
3db8f154 | 2825 | #if defined(USE_ITHREADS) |
52e18b1f | 2826 | /* locks must be released in same order as in atfork_lock() */ |
4da80956 P |
2827 | # ifdef USE_PERLIO |
2828 | MUTEX_UNLOCK(&PL_perlio_mutex); | |
2829 | # endif | |
52e18b1f GS |
2830 | # ifdef MYMALLOC |
2831 | MUTEX_UNLOCK(&PL_malloc_mutex); | |
2832 | # endif | |
2833 | OP_REFCNT_UNLOCK; | |
2834 | #endif | |
2835 | } | |
2836 | ||
2837 | Pid_t | |
2838 | Perl_my_fork(void) | |
2839 | { | |
2840 | #if defined(HAS_FORK) | |
2841 | Pid_t pid; | |
3db8f154 | 2842 | #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK) |
52e18b1f GS |
2843 | atfork_lock(); |
2844 | pid = fork(); | |
2845 | atfork_unlock(); | |
2846 | #else | |
2847 | /* atfork_lock() and atfork_unlock() are installed as pthread_atfork() | |
2848 | * handlers elsewhere in the code */ | |
2849 | pid = fork(); | |
2850 | #endif | |
2851 | return pid; | |
2852 | #else | |
2853 | /* this "canna happen" since nothing should be calling here if !HAS_FORK */ | |
2854 | Perl_croak_nocontext("fork() not available"); | |
b961a566 | 2855 | return 0; |
52e18b1f GS |
2856 | #endif /* HAS_FORK */ |
2857 | } | |
2858 | ||
748a9306 | 2859 | #ifdef DUMP_FDS |
35ff7856 | 2860 | void |
c9289b7b | 2861 | Perl_dump_fds(pTHX_ const char *const s) |
ae986130 LW |
2862 | { |
2863 | int fd; | |
c623ac67 | 2864 | Stat_t tmpstatbuf; |
ae986130 | 2865 | |
7918f24d NC |
2866 | PERL_ARGS_ASSERT_DUMP_FDS; |
2867 | ||
bf49b057 | 2868 | PerlIO_printf(Perl_debug_log,"%s", s); |
ae986130 | 2869 | for (fd = 0; fd < 32; fd++) { |
6ad3d225 | 2870 | if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0) |
bf49b057 | 2871 | PerlIO_printf(Perl_debug_log," %d",fd); |
ae986130 | 2872 | } |
bf49b057 | 2873 | PerlIO_printf(Perl_debug_log,"\n"); |
27da23d5 | 2874 | return; |
ae986130 | 2875 | } |
35ff7856 | 2876 | #endif /* DUMP_FDS */ |
ae986130 | 2877 | |
fe14fcc3 | 2878 | #ifndef HAS_DUP2 |
fec02dd3 | 2879 | int |
ba106d47 | 2880 | dup2(int oldfd, int newfd) |
a687059c | 2881 | { |
a0d0e21e | 2882 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
2883 | if (oldfd == newfd) |
2884 | return oldfd; | |
6ad3d225 | 2885 | PerlLIO_close(newfd); |
fec02dd3 | 2886 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 2887 | #else |
fc36a67e | 2888 | #define DUP2_MAX_FDS 256 |
2889 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 2890 | I32 fdx = 0; |
ae986130 LW |
2891 | int fd; |
2892 | ||
fe14fcc3 | 2893 | if (oldfd == newfd) |
fec02dd3 | 2894 | return oldfd; |
6ad3d225 | 2895 | PerlLIO_close(newfd); |
fc36a67e | 2896 | /* good enough for low fd's... */ |
6ad3d225 | 2897 | while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
fc36a67e | 2898 | if (fdx >= DUP2_MAX_FDS) { |
6ad3d225 | 2899 | PerlLIO_close(fd); |
fc36a67e | 2900 | fd = -1; |
2901 | break; | |
2902 | } | |
ae986130 | 2903 | fdtmp[fdx++] = fd; |
fc36a67e | 2904 | } |
ae986130 | 2905 | while (fdx > 0) |
6ad3d225 | 2906 | PerlLIO_close(fdtmp[--fdx]); |
fec02dd3 | 2907 | return fd; |
62b28dd9 | 2908 | #endif |
a687059c LW |
2909 | } |
2910 | #endif | |
2911 | ||
64ca3a65 | 2912 | #ifndef PERL_MICRO |
ff68c719 | 2913 | #ifdef HAS_SIGACTION |
2914 | ||
2915 | Sighandler_t | |
864dbfa3 | 2916 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2917 | { |
27da23d5 | 2918 | dVAR; |
ff68c719 | 2919 | struct sigaction act, oact; |
2920 | ||
a10b1e10 JH |
2921 | #ifdef USE_ITHREADS |
2922 | /* only "parent" interpreter can diddle signals */ | |
2923 | if (PL_curinterp != aTHX) | |
8aad04aa | 2924 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
2925 | #endif |
2926 | ||
8aad04aa | 2927 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2928 | sigemptyset(&act.sa_mask); |
2929 | act.sa_flags = 0; | |
2930 | #ifdef SA_RESTART | |
4ffa73a3 JH |
2931 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
2932 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 2933 | #endif |
358837b8 | 2934 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 2935 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
2936 | act.sa_flags |= SA_NOCLDWAIT; |
2937 | #endif | |
ff68c719 | 2938 | if (sigaction(signo, &act, &oact) == -1) |
8aad04aa | 2939 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2940 | else |
8aad04aa | 2941 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2942 | } |
2943 | ||
2944 | Sighandler_t | |
864dbfa3 | 2945 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2946 | { |
2947 | struct sigaction oact; | |
96a5add6 | 2948 | PERL_UNUSED_CONTEXT; |
ff68c719 | 2949 | |
2950 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
8aad04aa | 2951 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2952 | else |
8aad04aa | 2953 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2954 | } |
2955 | ||
2956 | int | |
864dbfa3 | 2957 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2958 | { |
27da23d5 | 2959 | dVAR; |
ff68c719 | 2960 | struct sigaction act; |
2961 | ||
7918f24d NC |
2962 | PERL_ARGS_ASSERT_RSIGNAL_SAVE; |
2963 | ||
a10b1e10 JH |
2964 | #ifdef USE_ITHREADS |
2965 | /* only "parent" interpreter can diddle signals */ | |
2966 | if (PL_curinterp != aTHX) | |
2967 | return -1; | |
2968 | #endif | |
2969 | ||
8aad04aa | 2970 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2971 | sigemptyset(&act.sa_mask); |
2972 | act.sa_flags = 0; | |
2973 | #ifdef SA_RESTART | |
4ffa73a3 JH |
2974 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
2975 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 2976 | #endif |
36b5d377 | 2977 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 2978 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
2979 | act.sa_flags |= SA_NOCLDWAIT; |
2980 | #endif | |
ff68c719 | 2981 | return sigaction(signo, &act, save); |
2982 | } | |
2983 | ||
2984 | int | |
864dbfa3 | 2985 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2986 | { |
27da23d5 | 2987 | dVAR; |
a10b1e10 JH |
2988 | #ifdef USE_ITHREADS |
2989 | /* only "parent" interpreter can diddle signals */ | |
2990 | if (PL_curinterp != aTHX) | |
2991 | return -1; | |
2992 | #endif | |
2993 | ||
ff68c719 | 2994 | return sigaction(signo, save, (struct sigaction *)NULL); |
2995 | } | |
2996 | ||
2997 | #else /* !HAS_SIGACTION */ | |
2998 | ||
2999 | Sighandler_t | |
864dbfa3 | 3000 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 3001 | { |
39f1703b | 3002 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3003 | /* only "parent" interpreter can diddle signals */ |
3004 | if (PL_curinterp != aTHX) | |
8aad04aa | 3005 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
3006 | #endif |
3007 | ||
6ad3d225 | 3008 | return PerlProc_signal(signo, handler); |
ff68c719 | 3009 | } |
3010 | ||
fabdb6c0 | 3011 | static Signal_t |
4e35701f | 3012 | sig_trap(int signo) |
ff68c719 | 3013 | { |
27da23d5 JH |
3014 | dVAR; |
3015 | PL_sig_trapped++; | |
ff68c719 | 3016 | } |
3017 | ||
3018 | Sighandler_t | |
864dbfa3 | 3019 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 3020 | { |
27da23d5 | 3021 | dVAR; |
ff68c719 | 3022 | Sighandler_t oldsig; |
3023 | ||
39f1703b | 3024 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3025 | /* only "parent" interpreter can diddle signals */ |
3026 | if (PL_curinterp != aTHX) | |
8aad04aa | 3027 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
3028 | #endif |
3029 | ||
27da23d5 | 3030 | PL_sig_trapped = 0; |
6ad3d225 GS |
3031 | oldsig = PerlProc_signal(signo, sig_trap); |
3032 | PerlProc_signal(signo, oldsig); | |
27da23d5 | 3033 | if (PL_sig_trapped) |
3aed30dc | 3034 | PerlProc_kill(PerlProc_getpid(), signo); |
ff68c719 | 3035 | return oldsig; |
3036 | } | |
3037 | ||
3038 | int | |
864dbfa3 | 3039 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 3040 | { |
39f1703b | 3041 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3042 | /* only "parent" interpreter can diddle signals */ |
3043 | if (PL_curinterp != aTHX) | |
3044 | return -1; | |
3045 | #endif | |
6ad3d225 | 3046 | *save = PerlProc_signal(signo, handler); |
8aad04aa | 3047 | return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3048 | } |
3049 | ||
3050 | int | |
864dbfa3 | 3051 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 3052 | { |
39f1703b | 3053 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3054 | /* only "parent" interpreter can diddle signals */ |
3055 | if (PL_curinterp != aTHX) | |
3056 | return -1; | |
3057 | #endif | |
8aad04aa | 3058 | return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3059 | } |
3060 | ||
3061 | #endif /* !HAS_SIGACTION */ | |
64ca3a65 | 3062 | #endif /* !PERL_MICRO */ |
ff68c719 | 3063 | |
5f05dabc | 3064 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
739a0b84 | 3065 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) |
79072805 | 3066 | I32 |
864dbfa3 | 3067 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a687059c | 3068 | { |
97aff369 | 3069 | dVAR; |
ff68c719 | 3070 | Sigsave_t hstat, istat, qstat; |
a687059c | 3071 | int status; |
a0d0e21e | 3072 | SV **svp; |
d8a83dd3 | 3073 | Pid_t pid; |
2e0cfa16 | 3074 | Pid_t pid2 = 0; |
03136e13 | 3075 | bool close_failed; |
4ee39169 | 3076 | dSAVEDERRNO; |
2e0cfa16 FC |
3077 | const int fd = PerlIO_fileno(ptr); |
3078 | ||
b6ae43b7 | 3079 | #ifdef USE_PERLIO |
2e0cfa16 FC |
3080 | /* Find out whether the refcount is low enough for us to wait for the |
3081 | child proc without blocking. */ | |
3082 | const bool should_wait = PerlIOUnix_refcnt(fd) == 1; | |
b6ae43b7 NC |
3083 | #else |
3084 | const bool should_wait = 1; | |
3085 | #endif | |
a687059c | 3086 | |
2e0cfa16 | 3087 | svp = av_fetch(PL_fdpid,fd,TRUE); |
25d92023 | 3088 | pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1; |
a0d0e21e | 3089 | SvREFCNT_dec(*svp); |
3280af22 | 3090 | *svp = &PL_sv_undef; |
ddcf38b7 IZ |
3091 | #ifdef OS2 |
3092 | if (pid == -1) { /* Opened by popen. */ | |
3093 | return my_syspclose(ptr); | |
3094 | } | |
a1d180c4 | 3095 | #endif |
f1618b10 CS |
3096 | close_failed = (PerlIO_close(ptr) == EOF); |
3097 | SAVE_ERRNO; | |
64ca3a65 | 3098 | #ifndef PERL_MICRO |
8aad04aa JH |
3099 | rsignal_save(SIGHUP, (Sighandler_t) SIG_IGN, &hstat); |
3100 | rsignal_save(SIGINT, (Sighandler_t) SIG_IGN, &istat); | |
3101 | rsignal_save(SIGQUIT, (Sighandler_t) SIG_IGN, &qstat); | |
64ca3a65 | 3102 | #endif |
2e0cfa16 | 3103 | if (should_wait) do { |
1d3434b8 GS |
3104 | pid2 = wait4pid(pid, &status, 0); |
3105 | } while (pid2 == -1 && errno == EINTR); | |
64ca3a65 | 3106 | #ifndef PERL_MICRO |
ff68c719 | 3107 | rsignal_restore(SIGHUP, &hstat); |
3108 | rsignal_restore(SIGINT, &istat); | |
3109 | rsignal_restore(SIGQUIT, &qstat); | |
64ca3a65 | 3110 | #endif |
03136e13 | 3111 | if (close_failed) { |
4ee39169 | 3112 | RESTORE_ERRNO; |
03136e13 CS |
3113 | return -1; |
3114 | } | |
2e0cfa16 FC |
3115 | return( |
3116 | should_wait | |
3117 | ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status) | |
3118 | : 0 | |
3119 | ); | |
20188a90 | 3120 | } |
9c12f1e5 RGS |
3121 | #else |
3122 | #if defined(__LIBCATAMOUNT__) | |
3123 | I32 | |
3124 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3125 | { | |
3126 | return -1; | |
3127 | } | |
3128 | #endif | |
4633a7c4 LW |
3129 | #endif /* !DOSISH */ |
3130 | ||
e37778c2 | 3131 | #if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(__LIBCATAMOUNT__) |
79072805 | 3132 | I32 |
d8a83dd3 | 3133 | Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags) |
20188a90 | 3134 | { |
97aff369 | 3135 | dVAR; |
27da23d5 | 3136 | I32 result = 0; |
7918f24d | 3137 | PERL_ARGS_ASSERT_WAIT4PID; |
b7953727 JH |
3138 | if (!pid) |
3139 | return -1; | |
ca0c25f6 | 3140 | #ifdef PERL_USES_PL_PIDSTATUS |
b7953727 | 3141 | { |
3aed30dc | 3142 | if (pid > 0) { |
12072db5 NC |
3143 | /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the |
3144 | pid, rather than a string form. */ | |
c4420975 | 3145 | SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE); |
3aed30dc HS |
3146 | if (svp && *svp != &PL_sv_undef) { |
3147 | *statusp = SvIVX(*svp); | |
12072db5 NC |
3148 | (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t), |
3149 | G_DISCARD); | |
3aed30dc HS |
3150 | return pid; |
3151 | } | |
3152 | } | |
3153 | else { | |
3154 | HE *entry; | |
3155 | ||
3156 | hv_iterinit(PL_pidstatus); | |
3157 | if ((entry = hv_iternext(PL_pidstatus))) { | |
c4420975 | 3158 | SV * const sv = hv_iterval(PL_pidstatus,entry); |
7ea75b61 | 3159 | I32 len; |
0bcc34c2 | 3160 | const char * const spid = hv_iterkey(entry,&len); |
27da23d5 | 3161 | |
12072db5 NC |
3162 | assert (len == sizeof(Pid_t)); |
3163 | memcpy((char *)&pid, spid, len); | |
3aed30dc | 3164 | *statusp = SvIVX(sv); |
7b9a3241 NC |
3165 | /* The hash iterator is currently on this entry, so simply |
3166 | calling hv_delete would trigger the lazy delete, which on | |
3167 | aggregate does more work, beacuse next call to hv_iterinit() | |
3168 | would spot the flag, and have to call the delete routine, | |
3169 | while in the meantime any new entries can't re-use that | |
3170 | memory. */ | |
3171 | hv_iterinit(PL_pidstatus); | |
7ea75b61 | 3172 | (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD); |
3aed30dc HS |
3173 | return pid; |
3174 | } | |
20188a90 LW |
3175 | } |
3176 | } | |
68a29c53 | 3177 | #endif |
79072805 | 3178 | #ifdef HAS_WAITPID |
367f3c24 IZ |
3179 | # ifdef HAS_WAITPID_RUNTIME |
3180 | if (!HAS_WAITPID_RUNTIME) | |
3181 | goto hard_way; | |
3182 | # endif | |
cddd4526 | 3183 | result = PerlProc_waitpid(pid,statusp,flags); |
dfcfdb64 | 3184 | goto finish; |
367f3c24 IZ |
3185 | #endif |
3186 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
4608196e | 3187 | result = wait4((pid==-1)?0:pid,statusp,flags,NULL); |
dfcfdb64 | 3188 | goto finish; |
367f3c24 | 3189 | #endif |
ca0c25f6 | 3190 | #ifdef PERL_USES_PL_PIDSTATUS |
27da23d5 | 3191 | #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME) |
367f3c24 | 3192 | hard_way: |
27da23d5 | 3193 | #endif |
a0d0e21e | 3194 | { |
a0d0e21e | 3195 | if (flags) |
cea2e8a9 | 3196 | Perl_croak(aTHX_ "Can't do waitpid with flags"); |
a0d0e21e | 3197 | else { |
76e3520e | 3198 | while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
a0d0e21e LW |
3199 | pidgone(result,*statusp); |
3200 | if (result < 0) | |
3201 | *statusp = -1; | |
3202 | } | |
a687059c LW |
3203 | } |
3204 | #endif | |
27da23d5 | 3205 | #if defined(HAS_WAITPID) || defined(HAS_WAIT4) |
dfcfdb64 | 3206 | finish: |
27da23d5 | 3207 | #endif |
cddd4526 NIS |
3208 | if (result < 0 && errno == EINTR) { |
3209 | PERL_ASYNC_CHECK(); | |
48dbb59e | 3210 | errno = EINTR; /* reset in case a signal handler changed $! */ |
cddd4526 NIS |
3211 | } |
3212 | return result; | |
a687059c | 3213 | } |
2986a63f | 3214 | #endif /* !DOSISH || OS2 || WIN32 || NETWARE */ |
a687059c | 3215 | |
ca0c25f6 | 3216 | #ifdef PERL_USES_PL_PIDSTATUS |
7c0587c8 | 3217 | void |
ed4173ef | 3218 | S_pidgone(pTHX_ Pid_t pid, int status) |
a687059c | 3219 | { |
eb578fdb | 3220 | SV *sv; |
a687059c | 3221 | |
12072db5 | 3222 | sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE); |
862a34c6 | 3223 | SvUPGRADE(sv,SVt_IV); |
45977657 | 3224 | SvIV_set(sv, status); |
20188a90 | 3225 | return; |
a687059c | 3226 | } |
ca0c25f6 | 3227 | #endif |
a687059c | 3228 | |
739a0b84 | 3229 | #if defined(OS2) |
7c0587c8 | 3230 | int pclose(); |
ddcf38b7 IZ |
3231 | #ifdef HAS_FORK |
3232 | int /* Cannot prototype with I32 | |
3233 | in os2ish.h. */ | |
ba106d47 | 3234 | my_syspclose(PerlIO *ptr) |
ddcf38b7 | 3235 | #else |
79072805 | 3236 | I32 |
864dbfa3 | 3237 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a1d180c4 | 3238 | #endif |
a687059c | 3239 | { |
760ac839 | 3240 | /* Needs work for PerlIO ! */ |
c4420975 | 3241 | FILE * const f = PerlIO_findFILE(ptr); |
7452cf6a | 3242 | const I32 result = pclose(f); |
2b96b0a5 JH |
3243 | PerlIO_releaseFILE(ptr,f); |
3244 | return result; | |
3245 | } | |
3246 | #endif | |
3247 | ||
933fea7f | 3248 | #if defined(DJGPP) |
2b96b0a5 JH |
3249 | int djgpp_pclose(); |
3250 | I32 | |
3251 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3252 | { | |
3253 | /* Needs work for PerlIO ! */ | |
c4420975 | 3254 | FILE * const f = PerlIO_findFILE(ptr); |
2b96b0a5 | 3255 | I32 result = djgpp_pclose(f); |
933fea7f | 3256 | result = (result << 8) & 0xff00; |
760ac839 LW |
3257 | PerlIO_releaseFILE(ptr,f); |
3258 | return result; | |
a687059c | 3259 | } |
7c0587c8 | 3260 | #endif |
9f68db38 | 3261 | |
16fa5c11 | 3262 | #define PERL_REPEATCPY_LINEAR 4 |
9f68db38 | 3263 | void |
5aaab254 | 3264 | Perl_repeatcpy(char *to, const char *from, I32 len, IV count) |
9f68db38 | 3265 | { |
7918f24d NC |
3266 | PERL_ARGS_ASSERT_REPEATCPY; |
3267 | ||
223f01db KW |
3268 | assert(len >= 0); |
3269 | ||
2709980d | 3270 | if (count < 0) |
43387ee1 | 3271 | Perl_croak_memory_wrap(); |
2709980d | 3272 | |
16fa5c11 VP |
3273 | if (len == 1) |
3274 | memset(to, *from, count); | |
3275 | else if (count) { | |
eb578fdb | 3276 | char *p = to; |
26e1303d | 3277 | IV items, linear, half; |
16fa5c11 VP |
3278 | |
3279 | linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR; | |
3280 | for (items = 0; items < linear; ++items) { | |
eb578fdb | 3281 | const char *q = from; |
26e1303d | 3282 | IV todo; |
16fa5c11 VP |
3283 | for (todo = len; todo > 0; todo--) |
3284 | *p++ = *q++; | |
3285 | } | |
3286 | ||
3287 | half = count / 2; | |
3288 | while (items <= half) { | |
26e1303d | 3289 | IV size = items * len; |
16fa5c11 VP |
3290 | memcpy(p, to, size); |
3291 | p += size; | |
3292 | items *= 2; | |
9f68db38 | 3293 | } |
16fa5c11 VP |
3294 | |
3295 | if (count > items) | |
3296 | memcpy(p, to, (count - items) * len); | |
9f68db38 LW |
3297 | } |
3298 | } | |
0f85fab0 | 3299 | |
fe14fcc3 | 3300 | #ifndef HAS_RENAME |
79072805 | 3301 | I32 |
4373e329 | 3302 | Perl_same_dirent(pTHX_ const char *a, const char *b) |
62b28dd9 | 3303 | { |
93a17b20 LW |
3304 | char *fa = strrchr(a,'/'); |
3305 | char *fb = strrchr(b,'/'); | |
c623ac67 GS |
3306 | Stat_t tmpstatbuf1; |
3307 | Stat_t tmpstatbuf2; | |
c4420975 | 3308 | SV * const tmpsv = sv_newmortal(); |
62b28dd9 | 3309 | |
7918f24d NC |
3310 | PERL_ARGS_ASSERT_SAME_DIRENT; |
3311 | ||
62b28dd9 LW |
3312 | if (fa) |
3313 | fa++; | |
3314 | else | |
3315 | fa = a; | |
3316 | if (fb) | |
3317 | fb++; | |
3318 | else | |
3319 | fb = b; | |
3320 | if (strNE(a,b)) | |
3321 | return FALSE; | |
3322 | if (fa == a) | |
76f68e9b | 3323 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3324 | else |
46fc3d4c | 3325 | sv_setpvn(tmpsv, a, fa - a); |
95a20fc0 | 3326 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0) |
62b28dd9 LW |
3327 | return FALSE; |
3328 | if (fb == b) | |
76f68e9b | 3329 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3330 | else |
46fc3d4c | 3331 | sv_setpvn(tmpsv, b, fb - b); |
95a20fc0 | 3332 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0) |
62b28dd9 LW |
3333 | return FALSE; |
3334 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
3335 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
3336 | } | |
fe14fcc3 LW |
3337 | #endif /* !HAS_RENAME */ |
3338 | ||
491527d0 | 3339 | char* |
7f315aed NC |
3340 | Perl_find_script(pTHX_ const char *scriptname, bool dosearch, |
3341 | const char *const *const search_ext, I32 flags) | |
491527d0 | 3342 | { |
97aff369 | 3343 | dVAR; |
bd61b366 SS |
3344 | const char *xfound = NULL; |
3345 | char *xfailed = NULL; | |
0f31cffe | 3346 | char tmpbuf[MAXPATHLEN]; |
eb578fdb | 3347 | char *s; |
5f74f29c | 3348 | I32 len = 0; |
491527d0 | 3349 | int retval; |
39a02377 | 3350 | char *bufend; |
7c458fae | 3351 | #if defined(DOSISH) && !defined(OS2) |
491527d0 GS |
3352 | # define SEARCH_EXTS ".bat", ".cmd", NULL |
3353 | # define MAX_EXT_LEN 4 | |
3354 | #endif | |
3355 | #ifdef OS2 | |
3356 | # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL | |
3357 | # define MAX_EXT_LEN 4 | |
3358 | #endif | |
3359 | #ifdef VMS | |
3360 | # define SEARCH_EXTS ".pl", ".com", NULL | |
3361 | # define MAX_EXT_LEN 4 | |
3362 | #endif | |
3363 | /* additional extensions to try in each dir if scriptname not found */ | |
3364 | #ifdef SEARCH_EXTS | |
0bcc34c2 | 3365 | static const char *const exts[] = { SEARCH_EXTS }; |
7f315aed | 3366 | const char *const *const ext = search_ext ? search_ext : exts; |
491527d0 | 3367 | int extidx = 0, i = 0; |
bd61b366 | 3368 | const char *curext = NULL; |
491527d0 | 3369 | #else |
53c1dcc0 | 3370 | PERL_UNUSED_ARG(search_ext); |
491527d0 GS |
3371 | # define MAX_EXT_LEN 0 |
3372 | #endif | |
3373 | ||
7918f24d NC |
3374 | PERL_ARGS_ASSERT_FIND_SCRIPT; |
3375 | ||
491527d0 GS |
3376 | /* |
3377 | * If dosearch is true and if scriptname does not contain path | |
3378 | * delimiters, search the PATH for scriptname. | |
3379 | * | |
3380 | * If SEARCH_EXTS is also defined, will look for each | |
3381 | * scriptname{SEARCH_EXTS} whenever scriptname is not found | |
3382 | * while searching the PATH. | |
3383 | * | |
3384 | * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search | |
3385 | * proceeds as follows: | |
3386 | * If DOSISH or VMSISH: | |
3387 | * + look for ./scriptname{,.foo,.bar} | |
3388 | * + search the PATH for scriptname{,.foo,.bar} | |
3389 | * | |
3390 | * If !DOSISH: | |
3391 | * + look *only* in the PATH for scriptname{,.foo,.bar} (note | |
3392 | * this will not look in '.' if it's not in the PATH) | |
3393 | */ | |
84486fc6 | 3394 | tmpbuf[0] = '\0'; |
491527d0 GS |
3395 | |
3396 | #ifdef VMS | |
3397 | # ifdef ALWAYS_DEFTYPES | |
3398 | len = strlen(scriptname); | |
3399 | if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { | |
c4420975 | 3400 | int idx = 0, deftypes = 1; |
491527d0 GS |
3401 | bool seen_dot = 1; |
3402 | ||
bd61b366 | 3403 | const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3404 | # else |
3405 | if (dosearch) { | |
c4420975 | 3406 | int idx = 0, deftypes = 1; |
491527d0 GS |
3407 | bool seen_dot = 1; |
3408 | ||
bd61b366 | 3409 | const int hasdir = (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3410 | # endif |
3411 | /* The first time through, just add SEARCH_EXTS to whatever we | |
3412 | * already have, so we can check for default file types. */ | |
3413 | while (deftypes || | |
84486fc6 | 3414 | (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
491527d0 GS |
3415 | { |
3416 | if (deftypes) { | |
3417 | deftypes = 0; | |
84486fc6 | 3418 | *tmpbuf = '\0'; |
491527d0 | 3419 | } |
84486fc6 GS |
3420 | if ((strlen(tmpbuf) + strlen(scriptname) |
3421 | + MAX_EXT_LEN) >= sizeof tmpbuf) | |
491527d0 | 3422 | continue; /* don't search dir with too-long name */ |
6fca0082 | 3423 | my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf)); |
491527d0 GS |
3424 | #else /* !VMS */ |
3425 | ||
3426 | #ifdef DOSISH | |
3427 | if (strEQ(scriptname, "-")) | |
3428 | dosearch = 0; | |
3429 | if (dosearch) { /* Look in '.' first. */ | |
fe2774ed | 3430 | const char *cur = scriptname; |
491527d0 GS |
3431 | #ifdef SEARCH_EXTS |
3432 | if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ | |
3433 | while (ext[i]) | |
3434 | if (strEQ(ext[i++],curext)) { | |
3435 | extidx = -1; /* already has an ext */ | |
3436 | break; | |
3437 | } | |
3438 | do { | |
3439 | #endif | |
3440 | DEBUG_p(PerlIO_printf(Perl_debug_log, | |
3441 | "Looking for %s\n",cur)); | |
017f25f1 IZ |
3442 | if (PerlLIO_stat(cur,&PL_statbuf) >= 0 |
3443 | && !S_ISDIR(PL_statbuf.st_mode)) { | |
491527d0 GS |
3444 | dosearch = 0; |
3445 | scriptname = cur; | |
3446 | #ifdef SEARCH_EXTS | |
3447 | break; | |
3448 | #endif | |
3449 | } | |
3450 | #ifdef SEARCH_EXTS | |
3451 | if (cur == scriptname) { | |
3452 | len = strlen(scriptname); | |
84486fc6 | 3453 | if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
491527d0 | 3454 | break; |
9e4425f7 SH |
3455 | my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf)); |
3456 | cur = tmpbuf; | |
491527d0 GS |
3457 | } |
3458 | } while (extidx >= 0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3459 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)); |
491527d0 GS |
3460 | #endif |
3461 | } | |
3462 | #endif | |
3463 | ||
3464 | if (dosearch && !strchr(scriptname, '/') | |
3465 | #ifdef DOSISH | |
3466 | && !strchr(scriptname, '\\') | |
3467 | #endif | |
cd39f2b6 | 3468 | && (s = PerlEnv_getenv("PATH"))) |
cd39f2b6 | 3469 | { |
491527d0 | 3470 | bool seen_dot = 0; |
92f0c265 | 3471 | |
39a02377 DM |
3472 | bufend = s + strlen(s); |
3473 | while (s < bufend) { | |
7c458fae | 3474 | # ifdef DOSISH |
491527d0 | 3475 | for (len = 0; *s |
491527d0 | 3476 | && *s != ';'; len++, s++) { |
84486fc6 GS |
3477 | if (len < sizeof tmpbuf) |
3478 | tmpbuf[len] = *s; | |
491527d0 | 3479 | } |
84486fc6 GS |
3480 | if (len < sizeof tmpbuf) |
3481 | tmpbuf[len] = '\0'; | |
7c458fae | 3482 | # else |
39a02377 | 3483 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend, |
491527d0 GS |
3484 | ':', |
3485 | &len); | |
7c458fae | 3486 | # endif |
39a02377 | 3487 | if (s < bufend) |
491527d0 | 3488 | s++; |
84486fc6 | 3489 | if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
491527d0 GS |
3490 | continue; /* don't search dir with too-long name */ |
3491 | if (len | |
7c458fae | 3492 | # ifdef DOSISH |
84486fc6 GS |
3493 | && tmpbuf[len - 1] != '/' |
3494 | && tmpbuf[len - 1] != '\\' | |
490a0e98 | 3495 | # endif |
491527d0 | 3496 | ) |
84486fc6 GS |
3497 | tmpbuf[len++] = '/'; |
3498 | if (len == 2 && tmpbuf[0] == '.') | |
491527d0 | 3499 | seen_dot = 1; |
28f0d0ec | 3500 | (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len); |
491527d0 GS |
3501 | #endif /* !VMS */ |
3502 | ||
3503 | #ifdef SEARCH_EXTS | |
84486fc6 | 3504 | len = strlen(tmpbuf); |
491527d0 GS |
3505 | if (extidx > 0) /* reset after previous loop */ |
3506 | extidx = 0; | |
3507 | do { | |
3508 | #endif | |
84486fc6 | 3509 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
3280af22 | 3510 | retval = PerlLIO_stat(tmpbuf,&PL_statbuf); |
017f25f1 IZ |
3511 | if (S_ISDIR(PL_statbuf.st_mode)) { |
3512 | retval = -1; | |
3513 | } | |
491527d0 GS |
3514 | #ifdef SEARCH_EXTS |
3515 | } while ( retval < 0 /* not there */ | |
3516 | && extidx>=0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3517 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len) |
491527d0 GS |
3518 | ); |
3519 | #endif | |
3520 | if (retval < 0) | |
3521 | continue; | |
3280af22 NIS |
3522 | if (S_ISREG(PL_statbuf.st_mode) |
3523 | && cando(S_IRUSR,TRUE,&PL_statbuf) | |
e37778c2 | 3524 | #if !defined(DOSISH) |
3280af22 | 3525 | && cando(S_IXUSR,TRUE,&PL_statbuf) |
491527d0 GS |
3526 | #endif |
3527 | ) | |
3528 | { | |
3aed30dc | 3529 | xfound = tmpbuf; /* bingo! */ |
491527d0 GS |
3530 | break; |
3531 | } | |
3532 | if (!xfailed) | |
84486fc6 | 3533 | xfailed = savepv(tmpbuf); |
491527d0 GS |
3534 | } |
3535 | #ifndef DOSISH | |
017f25f1 | 3536 | if (!xfound && !seen_dot && !xfailed && |
a1d180c4 | 3537 | (PerlLIO_stat(scriptname,&PL_statbuf) < 0 |
017f25f1 | 3538 | || S_ISDIR(PL_statbuf.st_mode))) |
491527d0 GS |
3539 | #endif |
3540 | seen_dot = 1; /* Disable message. */ | |
9ccb31f9 GS |
3541 | if (!xfound) { |
3542 | if (flags & 1) { /* do or die? */ | |
6ad282c7 | 3543 | /* diag_listed_as: Can't execute %s */ |
3aed30dc | 3544 | Perl_croak(aTHX_ "Can't %s %s%s%s", |
9ccb31f9 GS |
3545 | (xfailed ? "execute" : "find"), |
3546 | (xfailed ? xfailed : scriptname), | |
3547 | (xfailed ? "" : " on PATH"), | |
3548 | (xfailed || seen_dot) ? "" : ", '.' not in PATH"); | |
3549 | } | |
bd61b366 | 3550 | scriptname = NULL; |
9ccb31f9 | 3551 | } |
43c5f42d | 3552 | Safefree(xfailed); |
491527d0 GS |
3553 | scriptname = xfound; |
3554 | } | |
bd61b366 | 3555 | return (scriptname ? savepv(scriptname) : NULL); |
491527d0 GS |
3556 | } |
3557 | ||
ba869deb GS |
3558 | #ifndef PERL_GET_CONTEXT_DEFINED |
3559 | ||
3560 | void * | |
3561 | Perl_get_context(void) | |
3562 | { | |
27da23d5 | 3563 | dVAR; |
3db8f154 | 3564 | #if defined(USE_ITHREADS) |
ba869deb GS |
3565 | # ifdef OLD_PTHREADS_API |
3566 | pthread_addr_t t; | |
5637ef5b NC |
3567 | int error = pthread_getspecific(PL_thr_key, &t) |
3568 | if (error) | |
3569 | Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error); | |
ba869deb GS |
3570 | return (void*)t; |
3571 | # else | |
bce813aa | 3572 | # ifdef I_MACH_CTHREADS |
8b8b35ab | 3573 | return (void*)cthread_data(cthread_self()); |
bce813aa | 3574 | # else |
8b8b35ab JH |
3575 | return (void*)PTHREAD_GETSPECIFIC(PL_thr_key); |
3576 | # endif | |
c44d3fdb | 3577 | # endif |
ba869deb GS |
3578 | #else |
3579 | return (void*)NULL; | |
3580 | #endif | |
3581 | } | |
3582 | ||
3583 | void | |
3584 | Perl_set_context(void *t) | |
3585 | { | |
8772537c | 3586 | dVAR; |
7918f24d | 3587 | PERL_ARGS_ASSERT_SET_CONTEXT; |
3db8f154 | 3588 | #if defined(USE_ITHREADS) |
c44d3fdb GS |
3589 | # ifdef I_MACH_CTHREADS |
3590 | cthread_set_data(cthread_self(), t); | |
3591 | # else | |
5637ef5b NC |
3592 | { |
3593 | const int error = pthread_setspecific(PL_thr_key, t); | |
3594 | if (error) | |
3595 | Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error); | |
3596 | } | |
c44d3fdb | 3597 | # endif |
b464bac0 | 3598 | #else |
8772537c | 3599 | PERL_UNUSED_ARG(t); |
ba869deb GS |
3600 | #endif |
3601 | } | |
3602 | ||
3603 | #endif /* !PERL_GET_CONTEXT_DEFINED */ | |
491527d0 | 3604 | |
27da23d5 | 3605 | #if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GLOBAL_STRUCT_PRIVATE) |
22239a37 | 3606 | struct perl_vars * |
864dbfa3 | 3607 | Perl_GetVars(pTHX) |
22239a37 | 3608 | { |
533c011a | 3609 | return &PL_Vars; |
22239a37 | 3610 | } |
31fb1209 NIS |
3611 | #endif |
3612 | ||
1cb0ed9b | 3613 | char ** |
864dbfa3 | 3614 | Perl_get_op_names(pTHX) |
31fb1209 | 3615 | { |
96a5add6 AL |
3616 | PERL_UNUSED_CONTEXT; |
3617 | return (char **)PL_op_name; | |
31fb1209 NIS |
3618 | } |
3619 | ||
1cb0ed9b | 3620 | char ** |