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