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