Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* util.c |
a687059c | 2 | * |
4eb8286e | 3 | * Copyright (c) 1991-1999, Larry Wall |
a687059c | 4 | * |
d48672a2 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 | 7 | * |
8d063cd8 | 8 | */ |
a0d0e21e LW |
9 | |
10 | /* | |
11 | * "Very useful, no doubt, that was to Saruman; yet it seems that he was | |
12 | * not content." --Gandalf | |
13 | */ | |
8d063cd8 | 14 | |
8d063cd8 | 15 | #include "EXTERN.h" |
864dbfa3 | 16 | #define PERL_IN_UTIL_C |
8d063cd8 | 17 | #include "perl.h" |
62b28dd9 | 18 | |
e1dfb34b | 19 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
a687059c | 20 | #include <signal.h> |
62b28dd9 | 21 | #endif |
a687059c | 22 | |
36477c24 | 23 | #ifndef SIG_ERR |
24 | # define SIG_ERR ((Sighandler_t) -1) | |
25 | #endif | |
26 | ||
bd4080b3 | 27 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ |
85e6fe83 | 28 | #ifdef I_UNISTD |
8990e307 LW |
29 | # include <unistd.h> |
30 | #endif | |
31 | ||
a687059c LW |
32 | #ifdef I_VFORK |
33 | # include <vfork.h> | |
34 | #endif | |
35 | ||
94b6baf5 AD |
36 | /* Put this after #includes because fork and vfork prototypes may |
37 | conflict. | |
38 | */ | |
39 | #ifndef HAS_VFORK | |
40 | # define vfork fork | |
41 | #endif | |
42 | ||
fe14fcc3 LW |
43 | #ifdef I_FCNTL |
44 | # include <fcntl.h> | |
45 | #endif | |
46 | #ifdef I_SYS_FILE | |
47 | # include <sys/file.h> | |
48 | #endif | |
49 | ||
ff68c719 | 50 | #ifdef I_SYS_WAIT |
51 | # include <sys/wait.h> | |
52 | #endif | |
53 | ||
8d063cd8 | 54 | #define FLUSH |
8d063cd8 | 55 | |
a0d0e21e | 56 | #ifdef LEAKTEST |
a0d0e21e | 57 | |
8c52afec IZ |
58 | long xcount[MAXXCOUNT]; |
59 | long lastxcount[MAXXCOUNT]; | |
60 | long xycount[MAXXCOUNT][MAXYCOUNT]; | |
61 | long lastxycount[MAXXCOUNT][MAXYCOUNT]; | |
62 | ||
a0d0e21e | 63 | #endif |
a863c7d1 | 64 | |
f2517201 | 65 | /* paranoid version of system's malloc() */ |
8d063cd8 | 66 | |
a687059c LW |
67 | /* NOTE: Do not call the next three routines directly. Use the macros |
68 | * in handy.h, so that we can easily redefine everything to do tracking of | |
69 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 70 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
71 | */ |
72 | ||
bd4080b3 | 73 | Malloc_t |
864dbfa3 | 74 | Perl_safesysmalloc(pTHX_ MEM_SIZE size) |
8d063cd8 | 75 | { |
bd4080b3 | 76 | Malloc_t ptr; |
55497cff | 77 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 78 | if (size > 0xffff) { |
760ac839 | 79 | PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH; |
79072805 | 80 | my_exit(1); |
62b28dd9 | 81 | } |
55497cff | 82 | #endif /* HAS_64K_LIMIT */ |
34de22dd LW |
83 | #ifdef DEBUGGING |
84 | if ((long)size < 0) | |
463ee0b2 | 85 | croak("panic: malloc"); |
34de22dd | 86 | #endif |
6ad3d225 | 87 | ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
79072805 | 88 | #if !(defined(I286) || defined(atarist)) |
3280af22 | 89 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size)); |
79072805 | 90 | #else |
6b88bc9c | 91 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size)); |
8d063cd8 LW |
92 | #endif |
93 | if (ptr != Nullch) | |
94 | return ptr; | |
3280af22 | 95 | else if (PL_nomemok) |
7c0587c8 | 96 | return Nullch; |
8d063cd8 | 97 | else { |
22c35a8c | 98 | PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH; |
79072805 | 99 | my_exit(1); |
4e35701f | 100 | return Nullch; |
8d063cd8 LW |
101 | } |
102 | /*NOTREACHED*/ | |
103 | } | |
104 | ||
f2517201 | 105 | /* paranoid version of system's realloc() */ |
8d063cd8 | 106 | |
bd4080b3 | 107 | Malloc_t |
864dbfa3 | 108 | Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size) |
8d063cd8 | 109 | { |
bd4080b3 | 110 | Malloc_t ptr; |
ecfc5424 | 111 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) |
6ad3d225 | 112 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 113 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 114 | |
55497cff | 115 | #ifdef HAS_64K_LIMIT |
5f05dabc | 116 | if (size > 0xffff) { |
117 | PerlIO_printf(PerlIO_stderr(), | |
118 | "Reallocation too large: %lx\n", size) FLUSH; | |
119 | my_exit(1); | |
120 | } | |
55497cff | 121 | #endif /* HAS_64K_LIMIT */ |
7614df0c | 122 | if (!size) { |
f2517201 | 123 | safesysfree(where); |
7614df0c JD |
124 | return NULL; |
125 | } | |
126 | ||
378cc40b | 127 | if (!where) |
f2517201 | 128 | return safesysmalloc(size); |
34de22dd LW |
129 | #ifdef DEBUGGING |
130 | if ((long)size < 0) | |
463ee0b2 | 131 | croak("panic: realloc"); |
34de22dd | 132 | #endif |
7614df0c | 133 | ptr = PerlMem_realloc(where,size); |
79072805 LW |
134 | |
135 | #if !(defined(I286) || defined(atarist)) | |
136 | DEBUG_m( { | |
3280af22 NIS |
137 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++); |
138 | PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size); | |
79072805 LW |
139 | } ) |
140 | #else | |
141 | DEBUG_m( { | |
6b88bc9c GS |
142 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++); |
143 | PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size); | |
79072805 | 144 | } ) |
8d063cd8 | 145 | #endif |
79072805 | 146 | |
8d063cd8 LW |
147 | if (ptr != Nullch) |
148 | return ptr; | |
3280af22 | 149 | else if (PL_nomemok) |
7c0587c8 | 150 | return Nullch; |
8d063cd8 | 151 | else { |
22c35a8c | 152 | PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH; |
79072805 | 153 | my_exit(1); |
4e35701f | 154 | return Nullch; |
8d063cd8 LW |
155 | } |
156 | /*NOTREACHED*/ | |
157 | } | |
158 | ||
f2517201 | 159 | /* safe version of system's free() */ |
8d063cd8 | 160 | |
54310121 | 161 | Free_t |
864dbfa3 | 162 | Perl_safesysfree(pTHX_ Malloc_t where) |
8d063cd8 | 163 | { |
79072805 | 164 | #if !(defined(I286) || defined(atarist)) |
3280af22 | 165 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++)); |
79072805 | 166 | #else |
6b88bc9c | 167 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++)); |
8d063cd8 | 168 | #endif |
378cc40b | 169 | if (where) { |
de3bb511 | 170 | /*SUPPRESS 701*/ |
6ad3d225 | 171 | PerlMem_free(where); |
378cc40b | 172 | } |
8d063cd8 LW |
173 | } |
174 | ||
f2517201 | 175 | /* safe version of system's calloc() */ |
1050c9ca | 176 | |
bd4080b3 | 177 | Malloc_t |
864dbfa3 | 178 | Perl_safesyscalloc(pTHX_ MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 179 | { |
bd4080b3 | 180 | Malloc_t ptr; |
1050c9ca | 181 | |
55497cff | 182 | #ifdef HAS_64K_LIMIT |
5f05dabc | 183 | if (size * count > 0xffff) { |
184 | PerlIO_printf(PerlIO_stderr(), | |
185 | "Allocation too large: %lx\n", size * count) FLUSH; | |
186 | my_exit(1); | |
187 | } | |
55497cff | 188 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 189 | #ifdef DEBUGGING |
190 | if ((long)size < 0 || (long)count < 0) | |
191 | croak("panic: calloc"); | |
192 | #endif | |
0b7c1c42 | 193 | size *= count; |
6ad3d225 | 194 | ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
1050c9ca | 195 | #if !(defined(I286) || defined(atarist)) |
3280af22 | 196 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size)); |
1050c9ca | 197 | #else |
6b88bc9c | 198 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size)); |
1050c9ca | 199 | #endif |
1050c9ca | 200 | if (ptr != Nullch) { |
201 | memset((void*)ptr, 0, size); | |
202 | return ptr; | |
203 | } | |
3280af22 | 204 | else if (PL_nomemok) |
1050c9ca | 205 | return Nullch; |
206 | else { | |
22c35a8c | 207 | PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH; |
1050c9ca | 208 | my_exit(1); |
4e35701f | 209 | return Nullch; |
1050c9ca | 210 | } |
211 | /*NOTREACHED*/ | |
212 | } | |
213 | ||
a687059c LW |
214 | #ifdef LEAKTEST |
215 | ||
8c52afec IZ |
216 | struct mem_test_strut { |
217 | union { | |
218 | long type; | |
219 | char c[2]; | |
220 | } u; | |
221 | long size; | |
222 | }; | |
223 | ||
224 | # define ALIGN sizeof(struct mem_test_strut) | |
225 | ||
226 | # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size) | |
227 | # define typeof_chunk(ch) \ | |
228 | (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100) | |
229 | # define set_typeof_chunk(ch,t) \ | |
230 | (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100) | |
231 | #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \ | |
232 | ? MAXYCOUNT - 1 \ | |
233 | : ( (size) > 40 \ | |
234 | ? ((size) - 1)/8 + 5 \ | |
235 | : ((size) - 1)/4)) | |
8d063cd8 | 236 | |
bd4080b3 | 237 | Malloc_t |
864dbfa3 | 238 | Perl_safexmalloc(pTHX_ I32 x, MEM_SIZE size) |
8d063cd8 | 239 | { |
8c52afec | 240 | register char* where = (char*)safemalloc(size + ALIGN); |
8d063cd8 | 241 | |
8c52afec IZ |
242 | xcount[x] += size; |
243 | xycount[x][SIZE_TO_Y(size)]++; | |
244 | set_typeof_chunk(where, x); | |
245 | sizeof_chunk(where) = size; | |
246 | return (Malloc_t)(where + ALIGN); | |
8d063cd8 | 247 | } |
8d063cd8 | 248 | |
bd4080b3 | 249 | Malloc_t |
864dbfa3 | 250 | Perl_safexrealloc(pTHX_ Malloc_t wh, MEM_SIZE size) |
a687059c | 251 | { |
8c52afec IZ |
252 | char *where = (char*)wh; |
253 | ||
254 | if (!wh) | |
255 | return safexmalloc(0,size); | |
256 | ||
257 | { | |
258 | MEM_SIZE old = sizeof_chunk(where - ALIGN); | |
259 | int t = typeof_chunk(where - ALIGN); | |
260 | register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN); | |
261 | ||
262 | xycount[t][SIZE_TO_Y(old)]--; | |
263 | xycount[t][SIZE_TO_Y(size)]++; | |
264 | xcount[t] += size - old; | |
265 | sizeof_chunk(new) = size; | |
266 | return (Malloc_t)(new + ALIGN); | |
267 | } | |
a687059c LW |
268 | } |
269 | ||
270 | void | |
864dbfa3 | 271 | Perl_safexfree(pTHX_ Malloc_t wh) |
a687059c | 272 | { |
79072805 | 273 | I32 x; |
8c52afec IZ |
274 | char *where = (char*)wh; |
275 | MEM_SIZE size; | |
276 | ||
a687059c LW |
277 | if (!where) |
278 | return; | |
279 | where -= ALIGN; | |
8c52afec | 280 | size = sizeof_chunk(where); |
a687059c | 281 | x = where[0] + 100 * where[1]; |
8c52afec IZ |
282 | xcount[x] -= size; |
283 | xycount[x][SIZE_TO_Y(size)]--; | |
a687059c LW |
284 | safefree(where); |
285 | } | |
286 | ||
bd4080b3 | 287 | Malloc_t |
864dbfa3 | 288 | Perl_safexcalloc(pTHX_ I32 x,MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 289 | { |
8c52afec IZ |
290 | register char * where = (char*)safexmalloc(x, size * count + ALIGN); |
291 | xcount[x] += size; | |
292 | xycount[x][SIZE_TO_Y(size)]++; | |
293 | memset((void*)(where + ALIGN), 0, size * count); | |
294 | set_typeof_chunk(where, x); | |
295 | sizeof_chunk(where) = size; | |
296 | return (Malloc_t)(where + ALIGN); | |
1050c9ca | 297 | } |
298 | ||
864dbfa3 GS |
299 | STATIC void |
300 | xstat(pTHX_ int flag) | |
8d063cd8 | 301 | { |
8c52afec IZ |
302 | register I32 i, j, total = 0; |
303 | I32 subtot[MAXYCOUNT]; | |
8d063cd8 | 304 | |
8c52afec IZ |
305 | for (j = 0; j < MAXYCOUNT; j++) { |
306 | subtot[j] = 0; | |
307 | } | |
308 | ||
309 | PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total); | |
a687059c | 310 | for (i = 0; i < MAXXCOUNT; i++) { |
8c52afec IZ |
311 | total += xcount[i]; |
312 | for (j = 0; j < MAXYCOUNT; j++) { | |
313 | subtot[j] += xycount[i][j]; | |
314 | } | |
315 | if (flag == 0 | |
316 | ? xcount[i] /* Have something */ | |
317 | : (flag == 2 | |
318 | ? xcount[i] != lastxcount[i] /* Changed */ | |
319 | : xcount[i] > lastxcount[i])) { /* Growed */ | |
320 | PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100, | |
321 | flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]); | |
a687059c | 322 | lastxcount[i] = xcount[i]; |
8c52afec IZ |
323 | for (j = 0; j < MAXYCOUNT; j++) { |
324 | if ( flag == 0 | |
325 | ? xycount[i][j] /* Have something */ | |
326 | : (flag == 2 | |
327 | ? xycount[i][j] != lastxycount[i][j] /* Changed */ | |
328 | : xycount[i][j] > lastxycount[i][j])) { /* Growed */ | |
329 | PerlIO_printf(PerlIO_stderr(),"%3ld ", | |
330 | flag == 2 | |
331 | ? xycount[i][j] - lastxycount[i][j] | |
332 | : xycount[i][j]); | |
333 | lastxycount[i][j] = xycount[i][j]; | |
334 | } else { | |
335 | PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]); | |
336 | } | |
337 | } | |
338 | PerlIO_printf(PerlIO_stderr(), "\n"); | |
339 | } | |
340 | } | |
341 | if (flag != 2) { | |
342 | PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total); | |
343 | for (j = 0; j < MAXYCOUNT; j++) { | |
344 | if (subtot[j]) { | |
345 | PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]); | |
346 | } else { | |
347 | PerlIO_printf(PerlIO_stderr(), " . "); | |
348 | } | |
8d063cd8 | 349 | } |
8c52afec | 350 | PerlIO_printf(PerlIO_stderr(), "\n"); |
8d063cd8 | 351 | } |
8d063cd8 | 352 | } |
a687059c LW |
353 | |
354 | #endif /* LEAKTEST */ | |
8d063cd8 LW |
355 | |
356 | /* copy a string up to some (non-backslashed) delimiter, if any */ | |
357 | ||
358 | char * | |
864dbfa3 | 359 | Perl_delimcpy(pTHX_ register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen) |
8d063cd8 | 360 | { |
fc36a67e | 361 | register I32 tolen; |
362 | for (tolen = 0; from < fromend; from++, tolen++) { | |
378cc40b LW |
363 | if (*from == '\\') { |
364 | if (from[1] == delim) | |
365 | from++; | |
fc36a67e | 366 | else { |
367 | if (to < toend) | |
368 | *to++ = *from; | |
369 | tolen++; | |
370 | from++; | |
371 | } | |
378cc40b | 372 | } |
bedebaa5 | 373 | else if (*from == delim) |
8d063cd8 | 374 | break; |
fc36a67e | 375 | if (to < toend) |
376 | *to++ = *from; | |
8d063cd8 | 377 | } |
bedebaa5 CS |
378 | if (to < toend) |
379 | *to = '\0'; | |
fc36a67e | 380 | *retlen = tolen; |
8d063cd8 LW |
381 | return from; |
382 | } | |
383 | ||
384 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 385 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
386 | |
387 | char * | |
864dbfa3 | 388 | Perl_instr(pTHX_ register const char *big, register const char *little) |
378cc40b | 389 | { |
08105a92 | 390 | register const char *s, *x; |
79072805 | 391 | register I32 first; |
378cc40b | 392 | |
a687059c | 393 | if (!little) |
08105a92 | 394 | return (char*)big; |
a687059c | 395 | first = *little++; |
378cc40b | 396 | if (!first) |
08105a92 | 397 | return (char*)big; |
378cc40b LW |
398 | while (*big) { |
399 | if (*big++ != first) | |
400 | continue; | |
401 | for (x=big,s=little; *s; /**/ ) { | |
402 | if (!*x) | |
403 | return Nullch; | |
404 | if (*s++ != *x++) { | |
405 | s--; | |
406 | break; | |
407 | } | |
408 | } | |
409 | if (!*s) | |
08105a92 | 410 | return (char*)(big-1); |
378cc40b LW |
411 | } |
412 | return Nullch; | |
413 | } | |
8d063cd8 | 414 | |
a687059c LW |
415 | /* same as instr but allow embedded nulls */ |
416 | ||
417 | char * | |
864dbfa3 | 418 | Perl_ninstr(pTHX_ register const char *big, register const char *bigend, const char *little, const char *lend) |
8d063cd8 | 419 | { |
08105a92 | 420 | register const char *s, *x; |
79072805 | 421 | register I32 first = *little; |
08105a92 | 422 | register const char *littleend = lend; |
378cc40b | 423 | |
a0d0e21e | 424 | if (!first && little >= littleend) |
08105a92 | 425 | return (char*)big; |
de3bb511 LW |
426 | if (bigend - big < littleend - little) |
427 | return Nullch; | |
a687059c LW |
428 | bigend -= littleend - little++; |
429 | while (big <= bigend) { | |
430 | if (*big++ != first) | |
431 | continue; | |
432 | for (x=big,s=little; s < littleend; /**/ ) { | |
433 | if (*s++ != *x++) { | |
434 | s--; | |
435 | break; | |
436 | } | |
437 | } | |
438 | if (s >= littleend) | |
08105a92 | 439 | return (char*)(big-1); |
378cc40b | 440 | } |
a687059c LW |
441 | return Nullch; |
442 | } | |
443 | ||
444 | /* reverse of the above--find last substring */ | |
445 | ||
446 | char * | |
864dbfa3 | 447 | Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 448 | { |
08105a92 GS |
449 | register const char *bigbeg; |
450 | register const char *s, *x; | |
79072805 | 451 | register I32 first = *little; |
08105a92 | 452 | register const char *littleend = lend; |
a687059c | 453 | |
a0d0e21e | 454 | if (!first && little >= littleend) |
08105a92 | 455 | return (char*)bigend; |
a687059c LW |
456 | bigbeg = big; |
457 | big = bigend - (littleend - little++); | |
458 | while (big >= bigbeg) { | |
459 | if (*big-- != first) | |
460 | continue; | |
461 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
462 | if (*s++ != *x++) { | |
463 | s--; | |
464 | break; | |
465 | } | |
466 | } | |
467 | if (s >= littleend) | |
08105a92 | 468 | return (char*)(big+1); |
378cc40b | 469 | } |
a687059c | 470 | return Nullch; |
378cc40b | 471 | } |
a687059c | 472 | |
bbce6d69 | 473 | /* |
474 | * Set up for a new ctype locale. | |
475 | */ | |
55497cff | 476 | void |
864dbfa3 | 477 | Perl_new_ctype(pTHX_ const char *newctype) |
ef7eada9 | 478 | { |
36477c24 | 479 | #ifdef USE_LOCALE_CTYPE |
480 | ||
bbce6d69 | 481 | int i; |
ef7eada9 | 482 | |
bbce6d69 | 483 | for (i = 0; i < 256; i++) { |
484 | if (isUPPER_LC(i)) | |
22c35a8c | 485 | PL_fold_locale[i] = toLOWER_LC(i); |
bbce6d69 | 486 | else if (isLOWER_LC(i)) |
22c35a8c | 487 | PL_fold_locale[i] = toUPPER_LC(i); |
bbce6d69 | 488 | else |
22c35a8c | 489 | PL_fold_locale[i] = i; |
bbce6d69 | 490 | } |
bbce6d69 | 491 | |
36477c24 | 492 | #endif /* USE_LOCALE_CTYPE */ |
493 | } | |
bbce6d69 | 494 | |
495 | /* | |
496 | * Set up for a new collation locale. | |
497 | */ | |
498 | void | |
864dbfa3 | 499 | Perl_new_collate(pTHX_ const char *newcoll) |
bbce6d69 | 500 | { |
36477c24 | 501 | #ifdef USE_LOCALE_COLLATE |
502 | ||
bbce6d69 | 503 | if (! newcoll) { |
3280af22 NIS |
504 | if (PL_collation_name) { |
505 | ++PL_collation_ix; | |
506 | Safefree(PL_collation_name); | |
507 | PL_collation_name = NULL; | |
508 | PL_collation_standard = TRUE; | |
509 | PL_collxfrm_base = 0; | |
510 | PL_collxfrm_mult = 2; | |
bbce6d69 | 511 | } |
512 | return; | |
513 | } | |
514 | ||
3280af22 NIS |
515 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
516 | ++PL_collation_ix; | |
517 | Safefree(PL_collation_name); | |
518 | PL_collation_name = savepv(newcoll); | |
519 | PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX")); | |
bbce6d69 | 520 | |
bbce6d69 | 521 | { |
522 | /* 2: at most so many chars ('a', 'b'). */ | |
523 | /* 50: surely no system expands a char more. */ | |
524 | #define XFRMBUFSIZE (2 * 50) | |
525 | char xbuf[XFRMBUFSIZE]; | |
526 | Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); | |
527 | Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
528 | SSize_t mult = fb - fa; | |
529 | if (mult < 1) | |
530 | croak("strxfrm() gets absurd"); | |
3280af22 NIS |
531 | PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0; |
532 | PL_collxfrm_mult = mult; | |
bbce6d69 | 533 | } |
bbce6d69 | 534 | } |
bbce6d69 | 535 | |
36477c24 | 536 | #endif /* USE_LOCALE_COLLATE */ |
537 | } | |
bbce6d69 | 538 | |
539 | /* | |
540 | * Set up for a new numeric locale. | |
541 | */ | |
542 | void | |
864dbfa3 | 543 | Perl_new_numeric(pTHX_ const char *newnum) |
bbce6d69 | 544 | { |
36477c24 | 545 | #ifdef USE_LOCALE_NUMERIC |
546 | ||
bbce6d69 | 547 | if (! newnum) { |
3280af22 NIS |
548 | if (PL_numeric_name) { |
549 | Safefree(PL_numeric_name); | |
550 | PL_numeric_name = NULL; | |
551 | PL_numeric_standard = TRUE; | |
552 | PL_numeric_local = TRUE; | |
bbce6d69 | 553 | } |
554 | return; | |
555 | } | |
556 | ||
3280af22 NIS |
557 | if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) { |
558 | Safefree(PL_numeric_name); | |
559 | PL_numeric_name = savepv(newnum); | |
560 | PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX")); | |
561 | PL_numeric_local = TRUE; | |
bbce6d69 | 562 | } |
36477c24 | 563 | |
564 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 565 | } |
566 | ||
567 | void | |
864dbfa3 | 568 | Perl_set_numeric_standard(pTHX) |
bbce6d69 | 569 | { |
5f05dabc | 570 | #ifdef USE_LOCALE_NUMERIC |
571 | ||
3280af22 | 572 | if (! PL_numeric_standard) { |
bbce6d69 | 573 | setlocale(LC_NUMERIC, "C"); |
3280af22 NIS |
574 | PL_numeric_standard = TRUE; |
575 | PL_numeric_local = FALSE; | |
bbce6d69 | 576 | } |
5f05dabc | 577 | |
578 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 579 | } |
580 | ||
581 | void | |
864dbfa3 | 582 | Perl_set_numeric_local(pTHX) |
bbce6d69 | 583 | { |
5f05dabc | 584 | #ifdef USE_LOCALE_NUMERIC |
585 | ||
3280af22 NIS |
586 | if (! PL_numeric_local) { |
587 | setlocale(LC_NUMERIC, PL_numeric_name); | |
588 | PL_numeric_standard = FALSE; | |
589 | PL_numeric_local = TRUE; | |
bbce6d69 | 590 | } |
bbce6d69 | 591 | |
36477c24 | 592 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 593 | } |
36477c24 | 594 | |
bbce6d69 | 595 | |
36477c24 | 596 | /* |
597 | * Initialize locale awareness. | |
598 | */ | |
f0c5b223 | 599 | int |
864dbfa3 | 600 | Perl_init_i18nl10n(pTHX_ int printwarn) |
f0c5b223 TB |
601 | { |
602 | int ok = 1; | |
603 | /* returns | |
604 | * 1 = set ok or not applicable, | |
605 | * 0 = fallback to C locale, | |
606 | * -1 = fallback to C locale failed | |
607 | */ | |
bbce6d69 | 608 | |
36477c24 | 609 | #ifdef USE_LOCALE |
bbce6d69 | 610 | |
36477c24 | 611 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 612 | char *curctype = NULL; |
36477c24 | 613 | #endif /* USE_LOCALE_CTYPE */ |
614 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 615 | char *curcoll = NULL; |
36477c24 | 616 | #endif /* USE_LOCALE_COLLATE */ |
617 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 618 | char *curnum = NULL; |
36477c24 | 619 | #endif /* USE_LOCALE_NUMERIC */ |
3aeabbed JH |
620 | #ifdef __GLIBC__ |
621 | char *language = PerlEnv_getenv("LANGUAGE"); | |
622 | #endif | |
76e3520e GS |
623 | char *lc_all = PerlEnv_getenv("LC_ALL"); |
624 | char *lang = PerlEnv_getenv("LANG"); | |
bbce6d69 | 625 | bool setlocale_failure = FALSE; |
f0c5b223 | 626 | |
02b32252 CS |
627 | #ifdef LOCALE_ENVIRON_REQUIRED |
628 | ||
629 | /* | |
630 | * Ultrix setlocale(..., "") fails if there are no environment | |
631 | * variables from which to get a locale name. | |
632 | */ | |
633 | ||
634 | bool done = FALSE; | |
635 | ||
636 | #ifdef LC_ALL | |
637 | if (lang) { | |
638 | if (setlocale(LC_ALL, "")) | |
639 | done = TRUE; | |
640 | else | |
641 | setlocale_failure = TRUE; | |
642 | } | |
0644c9cb | 643 | if (!setlocale_failure) { |
02b32252 | 644 | #ifdef USE_LOCALE_CTYPE |
0644c9cb IS |
645 | if (! (curctype = |
646 | setlocale(LC_CTYPE, | |
647 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) | |
02b32252 CS |
648 | ? "" : Nullch))) |
649 | setlocale_failure = TRUE; | |
650 | #endif /* USE_LOCALE_CTYPE */ | |
651 | #ifdef USE_LOCALE_COLLATE | |
0644c9cb IS |
652 | if (! (curcoll = |
653 | setlocale(LC_COLLATE, | |
654 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) | |
02b32252 CS |
655 | ? "" : Nullch))) |
656 | setlocale_failure = TRUE; | |
657 | #endif /* USE_LOCALE_COLLATE */ | |
658 | #ifdef USE_LOCALE_NUMERIC | |
0644c9cb IS |
659 | if (! (curnum = |
660 | setlocale(LC_NUMERIC, | |
661 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) | |
02b32252 CS |
662 | ? "" : Nullch))) |
663 | setlocale_failure = TRUE; | |
664 | #endif /* USE_LOCALE_NUMERIC */ | |
665 | } | |
666 | ||
0644c9cb | 667 | #endif /* LC_ALL */ |
02b32252 | 668 | |
0644c9cb | 669 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
5f05dabc | 670 | |
0644c9cb | 671 | #ifdef LC_ALL |
bbce6d69 | 672 | if (! setlocale(LC_ALL, "")) |
673 | setlocale_failure = TRUE; | |
0644c9cb | 674 | #endif /* LC_ALL */ |
bbce6d69 | 675 | |
0644c9cb | 676 | if (!setlocale_failure) { |
36477c24 | 677 | #ifdef USE_LOCALE_CTYPE |
0644c9cb IS |
678 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
679 | setlocale_failure = TRUE; | |
36477c24 | 680 | #endif /* USE_LOCALE_CTYPE */ |
681 | #ifdef USE_LOCALE_COLLATE | |
0644c9cb IS |
682 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
683 | setlocale_failure = TRUE; | |
36477c24 | 684 | #endif /* USE_LOCALE_COLLATE */ |
685 | #ifdef USE_LOCALE_NUMERIC | |
0644c9cb IS |
686 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
687 | setlocale_failure = TRUE; | |
36477c24 | 688 | #endif /* USE_LOCALE_NUMERIC */ |
0644c9cb | 689 | } |
02b32252 | 690 | |
5f05dabc | 691 | if (setlocale_failure) { |
692 | char *p; | |
693 | bool locwarn = (printwarn > 1 || | |
694 | printwarn && | |
76e3520e | 695 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))); |
20cec16a | 696 | |
5f05dabc | 697 | if (locwarn) { |
698 | #ifdef LC_ALL | |
699 | ||
700 | PerlIO_printf(PerlIO_stderr(), | |
701 | "perl: warning: Setting locale failed.\n"); | |
702 | ||
703 | #else /* !LC_ALL */ | |
704 | ||
ef7eada9 | 705 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 706 | "perl: warning: Setting locale failed for the categories:\n\t"); |
36477c24 | 707 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 708 | if (! curctype) |
5f05dabc | 709 | PerlIO_printf(PerlIO_stderr(), "LC_CTYPE "); |
36477c24 | 710 | #endif /* USE_LOCALE_CTYPE */ |
711 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 712 | if (! curcoll) |
5f05dabc | 713 | PerlIO_printf(PerlIO_stderr(), "LC_COLLATE "); |
36477c24 | 714 | #endif /* USE_LOCALE_COLLATE */ |
715 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 716 | if (! curnum) |
5f05dabc | 717 | PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC "); |
36477c24 | 718 | #endif /* USE_LOCALE_NUMERIC */ |
bbce6d69 | 719 | PerlIO_printf(PerlIO_stderr(), "\n"); |
720 | ||
5f05dabc | 721 | #endif /* LC_ALL */ |
722 | ||
760ac839 | 723 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 724 | "perl: warning: Please check that your locale settings:\n"); |
ef7eada9 | 725 | |
3aeabbed JH |
726 | #ifdef __GLIBC__ |
727 | PerlIO_printf(PerlIO_stderr(), | |
728 | "\tLANGUAGE = %c%s%c,\n", | |
729 | language ? '"' : '(', | |
730 | language ? language : "unset", | |
731 | language ? '"' : ')'); | |
732 | #endif | |
733 | ||
ef7eada9 | 734 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 735 | "\tLC_ALL = %c%s%c,\n", |
736 | lc_all ? '"' : '(', | |
737 | lc_all ? lc_all : "unset", | |
738 | lc_all ? '"' : ')'); | |
5f05dabc | 739 | |
740 | { | |
741 | char **e; | |
742 | for (e = environ; *e; e++) { | |
743 | if (strnEQ(*e, "LC_", 3) | |
744 | && strnNE(*e, "LC_ALL=", 7) | |
745 | && (p = strchr(*e, '='))) | |
746 | PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n", | |
fb73857a | 747 | (int)(p - *e), *e, p + 1); |
5f05dabc | 748 | } |
749 | } | |
750 | ||
ef7eada9 | 751 | PerlIO_printf(PerlIO_stderr(), |
bbce6d69 | 752 | "\tLANG = %c%s%c\n", |
5f05dabc | 753 | lang ? '"' : '(', |
bbce6d69 | 754 | lang ? lang : "unset", |
755 | lang ? '"' : ')'); | |
ef7eada9 | 756 | |
bbce6d69 | 757 | PerlIO_printf(PerlIO_stderr(), |
758 | " are supported and installed on your system.\n"); | |
5f05dabc | 759 | } |
ef7eada9 | 760 | |
5f05dabc | 761 | #ifdef LC_ALL |
762 | ||
763 | if (setlocale(LC_ALL, "C")) { | |
764 | if (locwarn) | |
765 | PerlIO_printf(PerlIO_stderr(), | |
766 | "perl: warning: Falling back to the standard locale (\"C\").\n"); | |
bbce6d69 | 767 | ok = 0; |
ef7eada9 | 768 | } |
5f05dabc | 769 | else { |
770 | if (locwarn) | |
771 | PerlIO_printf(PerlIO_stderr(), | |
772 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); | |
773 | ok = -1; | |
774 | } | |
bbce6d69 | 775 | |
5f05dabc | 776 | #else /* ! LC_ALL */ |
777 | ||
778 | if (0 | |
36477c24 | 779 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 780 | || !(curctype || setlocale(LC_CTYPE, "C")) |
36477c24 | 781 | #endif /* USE_LOCALE_CTYPE */ |
782 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 783 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
36477c24 | 784 | #endif /* USE_LOCALE_COLLATE */ |
785 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 786 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
36477c24 | 787 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 788 | ) |
789 | { | |
790 | if (locwarn) | |
bbce6d69 | 791 | PerlIO_printf(PerlIO_stderr(), |
5f05dabc | 792 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
793 | ok = -1; | |
bbce6d69 | 794 | } |
5f05dabc | 795 | |
bbce6d69 | 796 | #endif /* ! LC_ALL */ |
5f05dabc | 797 | |
798 | #ifdef USE_LOCALE_CTYPE | |
799 | curctype = setlocale(LC_CTYPE, Nullch); | |
800 | #endif /* USE_LOCALE_CTYPE */ | |
801 | #ifdef USE_LOCALE_COLLATE | |
802 | curcoll = setlocale(LC_COLLATE, Nullch); | |
803 | #endif /* USE_LOCALE_COLLATE */ | |
804 | #ifdef USE_LOCALE_NUMERIC | |
805 | curnum = setlocale(LC_NUMERIC, Nullch); | |
806 | #endif /* USE_LOCALE_NUMERIC */ | |
ef7eada9 JH |
807 | } |
808 | ||
36477c24 | 809 | #ifdef USE_LOCALE_CTYPE |
864dbfa3 | 810 | new_ctype(curctype); |
36477c24 | 811 | #endif /* USE_LOCALE_CTYPE */ |
bbce6d69 | 812 | |
36477c24 | 813 | #ifdef USE_LOCALE_COLLATE |
864dbfa3 | 814 | new_collate(curcoll); |
36477c24 | 815 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 816 | |
36477c24 | 817 | #ifdef USE_LOCALE_NUMERIC |
864dbfa3 | 818 | new_numeric(curnum); |
36477c24 | 819 | #endif /* USE_LOCALE_NUMERIC */ |
ef7eada9 | 820 | |
36477c24 | 821 | #endif /* USE_LOCALE */ |
ef7eada9 | 822 | |
f0c5b223 TB |
823 | return ok; |
824 | } | |
825 | ||
bbce6d69 | 826 | /* Backwards compatibility. */ |
827 | int | |
864dbfa3 | 828 | Perl_init_i18nl14n(pTHX_ int printwarn) |
bbce6d69 | 829 | { |
864dbfa3 | 830 | return init_i18nl10n(printwarn); |
bbce6d69 | 831 | } |
ef7eada9 | 832 | |
36477c24 | 833 | #ifdef USE_LOCALE_COLLATE |
ef7eada9 | 834 | |
bbce6d69 | 835 | /* |
836 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
837 | * differences. First, it handles embedded NULs. Second, it allocates | |
838 | * a bit more memory than needed for the transformed data itself. | |
839 | * The real transformed data begins at offset sizeof(collationix). | |
840 | * Please see sv_collxfrm() to see how this is used. | |
841 | */ | |
842 | char * | |
864dbfa3 | 843 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) |
bbce6d69 | 844 | { |
845 | char *xbuf; | |
76e3520e | 846 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ |
bbce6d69 | 847 | |
848 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ | |
849 | /* the +1 is for the terminating NUL. */ | |
850 | ||
3280af22 | 851 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
76e3520e | 852 | New(171, xbuf, xAlloc, char); |
bbce6d69 | 853 | if (! xbuf) |
854 | goto bad; | |
855 | ||
3280af22 NIS |
856 | *(U32*)xbuf = PL_collation_ix; |
857 | xout = sizeof(PL_collation_ix); | |
bbce6d69 | 858 | for (xin = 0; xin < len; ) { |
859 | SSize_t xused; | |
860 | ||
861 | for (;;) { | |
76e3520e | 862 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); |
bbce6d69 | 863 | if (xused == -1) |
864 | goto bad; | |
76e3520e | 865 | if (xused < xAlloc - xout) |
bbce6d69 | 866 | break; |
76e3520e GS |
867 | xAlloc = (2 * xAlloc) + 1; |
868 | Renew(xbuf, xAlloc, char); | |
bbce6d69 | 869 | if (! xbuf) |
870 | goto bad; | |
871 | } | |
ef7eada9 | 872 | |
bbce6d69 | 873 | xin += strlen(s + xin) + 1; |
874 | xout += xused; | |
875 | ||
876 | /* Embedded NULs are understood but silently skipped | |
877 | * because they make no sense in locale collation. */ | |
878 | } | |
ef7eada9 | 879 | |
bbce6d69 | 880 | xbuf[xout] = '\0'; |
3280af22 | 881 | *xlen = xout - sizeof(PL_collation_ix); |
bbce6d69 | 882 | return xbuf; |
883 | ||
884 | bad: | |
885 | Safefree(xbuf); | |
886 | *xlen = 0; | |
887 | return NULL; | |
ef7eada9 JH |
888 | } |
889 | ||
36477c24 | 890 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 891 | |
cf93c79d IZ |
892 | #define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/ |
893 | ||
894 | /* As a space optimization, we do not compile tables for strings of length | |
895 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
896 | special-cased in fbm_instr(). | |
897 | ||
898 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
899 | ||
378cc40b | 900 | void |
864dbfa3 | 901 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags /* not used yet */) |
378cc40b | 902 | { |
942e002e GS |
903 | register U8 *s; |
904 | register U8 *table; | |
79072805 | 905 | register U32 i; |
0b71040e | 906 | STRLEN len; |
79072805 LW |
907 | I32 rarest = 0; |
908 | U32 frequency = 256; | |
909 | ||
cf93c79d IZ |
910 | if (flags & FBMcf_TAIL) |
911 | sv_catpvn(sv, "\n", 1); /* Taken into account in fbm_instr() */ | |
942e002e | 912 | s = (U8*)SvPV_force(sv, len); |
07f14f54 | 913 | (void)SvUPGRADE(sv, SVt_PVBM); |
cf93c79d IZ |
914 | if (len == 0) /* TAIL might be on on a zero-length string. */ |
915 | return; | |
02128f11 | 916 | if (len > 2) { |
cf93c79d IZ |
917 | I32 mlen = len; |
918 | unsigned char *sb; | |
919 | ||
920 | if (mlen > 255) | |
921 | mlen = 255; | |
922 | Sv_Grow(sv,len + 256 + FBM_TABLE_OFFSET); | |
923 | table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET); | |
924 | s = table - 1 - FBM_TABLE_OFFSET; /* Last char */ | |
02128f11 | 925 | for (i = 0; i < 256; i++) { |
cf93c79d | 926 | table[i] = mlen; |
02128f11 | 927 | } |
cf93c79d | 928 | table[-1] = flags; /* Not used yet */ |
02128f11 | 929 | i = 0; |
cf93c79d IZ |
930 | sb = s - mlen; |
931 | while (s >= sb) { | |
932 | if (table[*s] == mlen) | |
933 | table[*s] = i; | |
934 | s--, i++; | |
935 | } | |
378cc40b | 936 | } |
bbce6d69 | 937 | sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */ |
79072805 | 938 | SvVALID_on(sv); |
378cc40b | 939 | |
463ee0b2 | 940 | s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
bbce6d69 | 941 | for (i = 0; i < len; i++) { |
22c35a8c | 942 | if (PL_freq[s[i]] < frequency) { |
bbce6d69 | 943 | rarest = i; |
22c35a8c | 944 | frequency = PL_freq[s[i]]; |
378cc40b LW |
945 | } |
946 | } | |
79072805 LW |
947 | BmRARE(sv) = s[rarest]; |
948 | BmPREVIOUS(sv) = rarest; | |
cf93c79d IZ |
949 | BmUSEFUL(sv) = 100; /* Initial value */ |
950 | if (flags & FBMcf_TAIL) | |
951 | SvTAIL_on(sv); | |
760ac839 | 952 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv))); |
378cc40b LW |
953 | } |
954 | ||
cf93c79d IZ |
955 | /* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
956 | /* If SvTAIL is actually due to \Z or \z, this gives false positives | |
957 | if multiline */ | |
958 | ||
378cc40b | 959 | char * |
864dbfa3 | 960 | Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 961 | { |
a687059c | 962 | register unsigned char *s; |
cf93c79d IZ |
963 | STRLEN l; |
964 | register unsigned char *little = (unsigned char *)SvPV(littlestr,l); | |
965 | register STRLEN littlelen = l; | |
966 | register I32 multiline = flags & FBMrf_MULTILINE; | |
967 | ||
968 | if (bigend - big < littlelen) { | |
969 | check_tail: | |
970 | if ( SvTAIL(littlestr) | |
971 | && (bigend - big == littlelen - 1) | |
972 | && (littlelen == 1 | |
973 | || *big == *little && memEQ(big, little, littlelen - 1))) | |
974 | return (char*)big; | |
975 | return Nullch; | |
976 | } | |
378cc40b | 977 | |
cf93c79d IZ |
978 | if (littlelen <= 2) { /* Special-cased */ |
979 | register char c; | |
980 | ||
981 | if (littlelen == 1) { | |
982 | if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ | |
983 | /* Know that bigend != big. */ | |
984 | if (bigend[-1] == '\n') | |
985 | return (char *)(bigend - 1); | |
986 | return (char *) bigend; | |
987 | } | |
988 | s = big; | |
989 | while (s < bigend) { | |
990 | if (*s == *little) | |
991 | return (char *)s; | |
992 | s++; | |
993 | } | |
994 | if (SvTAIL(littlestr)) | |
995 | return (char *) bigend; | |
996 | return Nullch; | |
997 | } | |
998 | if (!littlelen) | |
999 | return (char*)big; /* Cannot be SvTAIL! */ | |
1000 | ||
1001 | /* littlelen is 2 */ | |
1002 | if (SvTAIL(littlestr) && !multiline) { | |
1003 | if (bigend[-1] == '\n' && bigend[-2] == *little) | |
1004 | return (char*)bigend - 2; | |
1005 | if (bigend[-1] == *little) | |
1006 | return (char*)bigend - 1; | |
1007 | return Nullch; | |
1008 | } | |
1009 | { | |
1010 | /* This should be better than FBM if c1 == c2, and almost | |
1011 | as good otherwise: maybe better since we do less indirection. | |
1012 | And we save a lot of memory by caching no table. */ | |
1013 | register unsigned char c1 = little[0]; | |
1014 | register unsigned char c2 = little[1]; | |
1015 | ||
1016 | s = big + 1; | |
1017 | bigend--; | |
1018 | if (c1 != c2) { | |
1019 | while (s <= bigend) { | |
1020 | if (s[0] == c2) { | |
1021 | if (s[-1] == c1) | |
1022 | return (char*)s - 1; | |
1023 | s += 2; | |
1024 | continue; | |
3fe6f2dc | 1025 | } |
cf93c79d IZ |
1026 | next_chars: |
1027 | if (s[0] == c1) { | |
1028 | if (s == bigend) | |
1029 | goto check_1char_anchor; | |
1030 | if (s[1] == c2) | |
1031 | return (char*)s; | |
1032 | else { | |
1033 | s++; | |
1034 | goto next_chars; | |
1035 | } | |
1036 | } | |
1037 | else | |
1038 | s += 2; | |
1039 | } | |
1040 | goto check_1char_anchor; | |
1041 | } | |
1042 | /* Now c1 == c2 */ | |
1043 | while (s <= bigend) { | |
1044 | if (s[0] == c1) { | |
1045 | if (s[-1] == c1) | |
1046 | return (char*)s - 1; | |
1047 | if (s == bigend) | |
1048 | goto check_1char_anchor; | |
1049 | if (s[1] == c1) | |
1050 | return (char*)s; | |
1051 | s += 3; | |
02128f11 | 1052 | } |
c277df42 | 1053 | else |
cf93c79d | 1054 | s += 2; |
c277df42 | 1055 | } |
c277df42 | 1056 | } |
cf93c79d IZ |
1057 | check_1char_anchor: /* One char and anchor! */ |
1058 | if (SvTAIL(littlestr) && (*bigend == *little)) | |
1059 | return (char *)bigend; /* bigend is already decremented. */ | |
1060 | return Nullch; | |
d48672a2 | 1061 | } |
cf93c79d | 1062 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 1063 | s = bigend - littlelen; |
cf93c79d | 1064 | if (s >= big |
02128f11 | 1065 | && bigend[-1] == '\n' |
cf93c79d IZ |
1066 | && *s == *little |
1067 | /* Automatically of length > 2 */ | |
1068 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
bbce6d69 | 1069 | return (char*)s; /* how sweet it is */ |
cf93c79d IZ |
1070 | if (s[1] == *little && memEQ((char*)s + 2,(char*)little + 1, |
1071 | littlelen - 2)) | |
1072 | return (char*)s + 1; /* how sweet it is */ | |
02128f11 IZ |
1073 | return Nullch; |
1074 | } | |
cf93c79d IZ |
1075 | if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
1076 | char *b = ninstr((char*)big,(char*)bigend, | |
1077 | (char*)little, (char*)little + littlelen); | |
1078 | ||
1079 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
1080 | /* Chop \n from littlestr: */ | |
1081 | s = bigend - littlelen + 1; | |
1082 | if (*s == *little && memEQ((char*)s + 1, (char*)little + 1, | |
1083 | littlelen - 2)) | |
3fe6f2dc | 1084 | return (char*)s; |
cf93c79d | 1085 | return Nullch; |
a687059c | 1086 | } |
cf93c79d | 1087 | return b; |
a687059c | 1088 | } |
cf93c79d IZ |
1089 | |
1090 | { /* Do actual FBM. */ | |
1091 | register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET; | |
1092 | register unsigned char *oldlittle; | |
1093 | ||
1094 | if (littlelen > bigend - big) | |
1095 | return Nullch; | |
1096 | --littlelen; /* Last char found by table lookup */ | |
1097 | ||
1098 | s = big + littlelen; | |
1099 | little += littlelen; /* last char */ | |
1100 | oldlittle = little; | |
1101 | if (s < bigend) { | |
1102 | register I32 tmp; | |
1103 | ||
1104 | top2: | |
1105 | /*SUPPRESS 560*/ | |
1106 | if (tmp = table[*s]) { | |
62b28dd9 | 1107 | #ifdef POINTERRIGOR |
cf93c79d IZ |
1108 | if (bigend - s > tmp) { |
1109 | s += tmp; | |
1110 | goto top2; | |
1111 | } | |
bbce6d69 | 1112 | s += tmp; |
62b28dd9 | 1113 | #else |
cf93c79d | 1114 | if ((s += tmp) < bigend) |
62b28dd9 | 1115 | goto top2; |
cf93c79d IZ |
1116 | #endif |
1117 | goto check_end; | |
1118 | } | |
1119 | else { /* less expensive than calling strncmp() */ | |
1120 | register unsigned char *olds = s; | |
1121 | ||
1122 | tmp = littlelen; | |
1123 | ||
1124 | while (tmp--) { | |
1125 | if (*--s == *--little) | |
1126 | continue; | |
1127 | differ: | |
1128 | s = olds + 1; /* here we pay the price for failure */ | |
1129 | little = oldlittle; | |
1130 | if (s < bigend) /* fake up continue to outer loop */ | |
1131 | goto top2; | |
1132 | goto check_end; | |
1133 | } | |
1134 | return (char *)s; | |
a687059c | 1135 | } |
378cc40b | 1136 | } |
cf93c79d IZ |
1137 | check_end: |
1138 | if ( s == bigend && (table[-1] & FBMcf_TAIL) | |
1139 | && memEQ(bigend - littlelen, oldlittle - littlelen, littlelen) ) | |
1140 | return (char*)bigend - littlelen; | |
1141 | return Nullch; | |
378cc40b | 1142 | } |
378cc40b LW |
1143 | } |
1144 | ||
c277df42 IZ |
1145 | /* start_shift, end_shift are positive quantities which give offsets |
1146 | of ends of some substring of bigstr. | |
1147 | If `last' we want the last occurence. | |
1148 | old_posp is the way of communication between consequent calls if | |
1149 | the next call needs to find the . | |
1150 | The initial *old_posp should be -1. | |
cf93c79d IZ |
1151 | |
1152 | Note that we take into account SvTAIL, so one can get extra | |
1153 | optimizations if _ALL flag is set. | |
c277df42 IZ |
1154 | */ |
1155 | ||
cf93c79d IZ |
1156 | /* If SvTAIL is actually due to \Z or \z, this gives false positives |
1157 | if PL_multiline. In fact if !PL_multiline the autoritative answer | |
1158 | is not supported yet. */ | |
1159 | ||
378cc40b | 1160 | char * |
864dbfa3 | 1161 | Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last) |
378cc40b | 1162 | { |
5c0ca799 | 1163 | dTHR; |
a687059c LW |
1164 | register unsigned char *s, *x; |
1165 | register unsigned char *big; | |
79072805 LW |
1166 | register I32 pos; |
1167 | register I32 previous; | |
1168 | register I32 first; | |
a687059c | 1169 | register unsigned char *little; |
c277df42 | 1170 | register I32 stop_pos; |
a687059c | 1171 | register unsigned char *littleend; |
c277df42 | 1172 | I32 found = 0; |
378cc40b | 1173 | |
c277df42 | 1174 | if (*old_posp == -1 |
3280af22 | 1175 | ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0 |
cf93c79d IZ |
1176 | : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) { |
1177 | cant_find: | |
1178 | if ( BmRARE(littlestr) == '\n' | |
1179 | && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) { | |
1180 | little = (unsigned char *)(SvPVX(littlestr)); | |
1181 | littleend = little + SvCUR(littlestr); | |
1182 | first = *little++; | |
1183 | goto check_tail; | |
1184 | } | |
378cc40b | 1185 | return Nullch; |
cf93c79d IZ |
1186 | } |
1187 | ||
463ee0b2 | 1188 | little = (unsigned char *)(SvPVX(littlestr)); |
79072805 | 1189 | littleend = little + SvCUR(littlestr); |
378cc40b | 1190 | first = *little++; |
c277df42 | 1191 | /* The value of pos we can start at: */ |
79072805 | 1192 | previous = BmPREVIOUS(littlestr); |
463ee0b2 | 1193 | big = (unsigned char *)(SvPVX(bigstr)); |
c277df42 IZ |
1194 | /* The value of pos we can stop at: */ |
1195 | stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous); | |
cf93c79d IZ |
1196 | if (previous + start_shift > stop_pos) { |
1197 | if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */ | |
1198 | goto check_tail; | |
1199 | return Nullch; | |
1200 | } | |
c277df42 | 1201 | while (pos < previous + start_shift) { |
3280af22 | 1202 | if (!(pos += PL_screamnext[pos])) |
cf93c79d | 1203 | goto cant_find; |
378cc40b | 1204 | } |
de3bb511 | 1205 | #ifdef POINTERRIGOR |
bbce6d69 | 1206 | do { |
ef64f398 | 1207 | if (pos >= stop_pos) break; |
bbce6d69 | 1208 | if (big[pos-previous] != first) |
1209 | continue; | |
1210 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { | |
bbce6d69 | 1211 | if (*s++ != *x++) { |
1212 | s--; | |
1213 | break; | |
de3bb511 | 1214 | } |
bbce6d69 | 1215 | } |
c277df42 IZ |
1216 | if (s == littleend) { |
1217 | *old_posp = pos; | |
1218 | if (!last) return (char *)(big+pos-previous); | |
1219 | found = 1; | |
1220 | } | |
6b88bc9c | 1221 | } while ( pos += PL_screamnext[pos] ); |
c277df42 | 1222 | return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch; |
de3bb511 LW |
1223 | #else /* !POINTERRIGOR */ |
1224 | big -= previous; | |
bbce6d69 | 1225 | do { |
ef64f398 | 1226 | if (pos >= stop_pos) break; |
bbce6d69 | 1227 | if (big[pos] != first) |
1228 | continue; | |
1229 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { | |
bbce6d69 | 1230 | if (*s++ != *x++) { |
1231 | s--; | |
1232 | break; | |
378cc40b | 1233 | } |
bbce6d69 | 1234 | } |
c277df42 IZ |
1235 | if (s == littleend) { |
1236 | *old_posp = pos; | |
1237 | if (!last) return (char *)(big+pos); | |
1238 | found = 1; | |
1239 | } | |
3280af22 | 1240 | } while ( pos += PL_screamnext[pos] ); |
cf93c79d IZ |
1241 | if (last && found) |
1242 | return (char *)(big+(*old_posp)); | |
de3bb511 | 1243 | #endif /* POINTERRIGOR */ |
cf93c79d IZ |
1244 | check_tail: |
1245 | if (!SvTAIL(littlestr) || (end_shift > 0)) | |
1246 | return Nullch; | |
1247 | /* Ignore the trailing "\n". This code is not microoptimized */ | |
1248 | big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr)); | |
1249 | stop_pos = littleend - little; /* Actual littlestr len */ | |
1250 | if (stop_pos == 0) | |
1251 | return (char*)big; | |
1252 | big -= stop_pos; | |
1253 | if (*big == first | |
1254 | && ((stop_pos == 1) || memEQ(big + 1, little, stop_pos - 1))) | |
1255 | return (char*)big; | |
1256 | return Nullch; | |
8d063cd8 LW |
1257 | } |
1258 | ||
79072805 | 1259 | I32 |
864dbfa3 | 1260 | Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len) |
79072805 | 1261 | { |
bbce6d69 | 1262 | register U8 *a = (U8 *)s1; |
1263 | register U8 *b = (U8 *)s2; | |
79072805 | 1264 | while (len--) { |
22c35a8c | 1265 | if (*a != *b && *a != PL_fold[*b]) |
bbce6d69 | 1266 | return 1; |
1267 | a++,b++; | |
1268 | } | |
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | I32 | |
864dbfa3 | 1273 | Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len) |
bbce6d69 | 1274 | { |
1275 | register U8 *a = (U8 *)s1; | |
1276 | register U8 *b = (U8 *)s2; | |
1277 | while (len--) { | |
22c35a8c | 1278 | if (*a != *b && *a != PL_fold_locale[*b]) |
bbce6d69 | 1279 | return 1; |
1280 | a++,b++; | |
79072805 LW |
1281 | } |
1282 | return 0; | |
1283 | } | |
1284 | ||
8d063cd8 LW |
1285 | /* copy a string to a safe spot */ |
1286 | ||
1287 | char * | |
864dbfa3 | 1288 | Perl_savepv(pTHX_ const char *sv) |
8d063cd8 | 1289 | { |
a687059c | 1290 | register char *newaddr; |
8d063cd8 | 1291 | |
79072805 LW |
1292 | New(902,newaddr,strlen(sv)+1,char); |
1293 | (void)strcpy(newaddr,sv); | |
8d063cd8 LW |
1294 | return newaddr; |
1295 | } | |
1296 | ||
a687059c LW |
1297 | /* same thing but with a known length */ |
1298 | ||
1299 | char * | |
864dbfa3 | 1300 | Perl_savepvn(pTHX_ const char *sv, register I32 len) |
a687059c LW |
1301 | { |
1302 | register char *newaddr; | |
1303 | ||
1304 | New(903,newaddr,len+1,char); | |
79072805 | 1305 | Copy(sv,newaddr,len,char); /* might not be null terminated */ |
a687059c LW |
1306 | newaddr[len] = '\0'; /* is now */ |
1307 | return newaddr; | |
1308 | } | |
1309 | ||
fc36a67e | 1310 | /* the SV for form() and mess() is not kept in an arena */ |
1311 | ||
76e3520e | 1312 | STATIC SV * |
864dbfa3 | 1313 | mess_alloc(pTHX) |
fc36a67e | 1314 | { |
e72dc28c | 1315 | dTHR; |
fc36a67e | 1316 | SV *sv; |
1317 | XPVMG *any; | |
1318 | ||
e72dc28c GS |
1319 | if (!PL_dirty) |
1320 | return sv_2mortal(newSVpvn("",0)); | |
1321 | ||
0372dbb6 GS |
1322 | if (PL_mess_sv) |
1323 | return PL_mess_sv; | |
1324 | ||
fc36a67e | 1325 | /* Create as PVMG now, to avoid any upgrading later */ |
1326 | New(905, sv, 1, SV); | |
1327 | Newz(905, any, 1, XPVMG); | |
1328 | SvFLAGS(sv) = SVt_PVMG; | |
1329 | SvANY(sv) = (void*)any; | |
1330 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ | |
e72dc28c | 1331 | PL_mess_sv = sv; |
fc36a67e | 1332 | return sv; |
1333 | } | |
1334 | ||
8990e307 | 1335 | char * |
864dbfa3 | 1336 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1337 | { |
e72dc28c | 1338 | SV *sv = mess_alloc(); |
46fc3d4c | 1339 | va_list args; |
46fc3d4c | 1340 | va_start(args, pat); |
e72dc28c | 1341 | sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*)); |
46fc3d4c | 1342 | va_end(args); |
e72dc28c | 1343 | return SvPVX(sv); |
46fc3d4c | 1344 | } |
a687059c | 1345 | |
06bf62c7 | 1346 | SV * |
864dbfa3 | 1347 | Perl_mess(pTHX_ const char *pat, va_list *args) |
46fc3d4c | 1348 | { |
e72dc28c | 1349 | SV *sv = mess_alloc(); |
46fc3d4c | 1350 | static char dgd[] = " during global destruction.\n"; |
1351 | ||
fc36a67e | 1352 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); |
46fc3d4c | 1353 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
e858de61 | 1354 | dTHR; |
515f54a1 GS |
1355 | if (PL_curcop->cop_line) |
1356 | sv_catpvf(sv, " at %_ line %ld", | |
1357 | GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line); | |
1358 | if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) { | |
1359 | bool line_mode = (RsSIMPLE(PL_rs) && | |
7c1e0849 | 1360 | SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n'); |
515f54a1 GS |
1361 | sv_catpvf(sv, ", <%s> %s %ld", |
1362 | PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv), | |
1363 | line_mode ? "line" : "chunk", | |
1364 | (long)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1365 | } |
515f54a1 | 1366 | sv_catpv(sv, PL_dirty ? dgd : ".\n"); |
a687059c | 1367 | } |
06bf62c7 | 1368 | return sv; |
a687059c LW |
1369 | } |
1370 | ||
36477c24 | 1371 | OP * |
864dbfa3 | 1372 | Perl_die(pTHX_ const char* pat, ...) |
36477c24 | 1373 | { |
5dc0d613 | 1374 | dTHR; |
36477c24 | 1375 | va_list args; |
1376 | char *message; | |
3280af22 | 1377 | int was_in_eval = PL_in_eval; |
36477c24 | 1378 | HV *stash; |
1379 | GV *gv; | |
1380 | CV *cv; | |
06bf62c7 GS |
1381 | SV *msv; |
1382 | STRLEN msglen; | |
36477c24 | 1383 | |
8b73bbec | 1384 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
199100c8 | 1385 | "%p: die: curstack = %p, mainstack = %p\n", |
533c011a | 1386 | thr, PL_curstack, PL_mainstack)); |
36477c24 | 1387 | |
36477c24 | 1388 | va_start(args, pat); |
06bf62c7 GS |
1389 | if (pat) { |
1390 | msv = mess(pat, &args); | |
1391 | message = SvPV(msv,msglen); | |
1392 | } | |
1393 | else { | |
1394 | message = Nullch; | |
1395 | } | |
36477c24 | 1396 | va_end(args); |
1397 | ||
8b73bbec | 1398 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
199100c8 | 1399 | "%p: die: message = %s\ndiehook = %p\n", |
533c011a | 1400 | thr, message, PL_diehook)); |
3280af22 | 1401 | if (PL_diehook) { |
1738f5c4 | 1402 | /* sv_2cv might call croak() */ |
3280af22 | 1403 | SV *olddiehook = PL_diehook; |
1738f5c4 | 1404 | ENTER; |
3280af22 NIS |
1405 | SAVESPTR(PL_diehook); |
1406 | PL_diehook = Nullsv; | |
1738f5c4 CS |
1407 | cv = sv_2cv(olddiehook, &stash, &gv, 0); |
1408 | LEAVE; | |
1409 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1410 | dSP; | |
774d564b | 1411 | SV *msg; |
1412 | ||
1413 | ENTER; | |
79cb57f6 | 1414 | if (message) { |
06bf62c7 | 1415 | msg = newSVpvn(message, msglen); |
4e6ea2c3 GS |
1416 | SvREADONLY_on(msg); |
1417 | SAVEFREESV(msg); | |
1418 | } | |
1419 | else { | |
1420 | msg = ERRSV; | |
1421 | } | |
1738f5c4 | 1422 | |
e788e7d3 | 1423 | PUSHSTACKi(PERLSI_DIEHOOK); |
924508f0 | 1424 | PUSHMARK(SP); |
1738f5c4 CS |
1425 | XPUSHs(msg); |
1426 | PUTBACK; | |
864dbfa3 | 1427 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1428 | POPSTACK; |
774d564b | 1429 | LEAVE; |
1738f5c4 | 1430 | } |
36477c24 | 1431 | } |
1432 | ||
06bf62c7 | 1433 | PL_restartop = die_where(message, msglen); |
8b73bbec | 1434 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
7c06b590 | 1435 | "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n", |
533c011a | 1436 | thr, PL_restartop, was_in_eval, PL_top_env)); |
3280af22 | 1437 | if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev) |
6224f72b | 1438 | JMPENV_JUMP(3); |
3280af22 | 1439 | return PL_restartop; |
36477c24 | 1440 | } |
1441 | ||
79072805 | 1442 | void |
864dbfa3 | 1443 | Perl_croak(pTHX_ const char* pat, ...) |
a687059c | 1444 | { |
11343788 | 1445 | dTHR; |
a687059c | 1446 | va_list args; |
de3bb511 | 1447 | char *message; |
748a9306 LW |
1448 | HV *stash; |
1449 | GV *gv; | |
1450 | CV *cv; | |
06bf62c7 GS |
1451 | SV *msv; |
1452 | STRLEN msglen; | |
a687059c | 1453 | |
8990e307 | 1454 | va_start(args, pat); |
06bf62c7 GS |
1455 | msv = mess(pat, &args); |
1456 | message = SvPV(msv,msglen); | |
a687059c | 1457 | va_end(args); |
8b73bbec | 1458 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message)); |
3280af22 | 1459 | if (PL_diehook) { |
1738f5c4 | 1460 | /* sv_2cv might call croak() */ |
3280af22 | 1461 | SV *olddiehook = PL_diehook; |
1738f5c4 | 1462 | ENTER; |
3280af22 NIS |
1463 | SAVESPTR(PL_diehook); |
1464 | PL_diehook = Nullsv; | |
20cec16a | 1465 | cv = sv_2cv(olddiehook, &stash, &gv, 0); |
1738f5c4 CS |
1466 | LEAVE; |
1467 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1468 | dSP; |
774d564b | 1469 | SV *msg; |
1470 | ||
1471 | ENTER; | |
06bf62c7 | 1472 | msg = newSVpvn(message, msglen); |
774d564b | 1473 | SvREADONLY_on(msg); |
1474 | SAVEFREESV(msg); | |
20cec16a | 1475 | |
e788e7d3 | 1476 | PUSHSTACKi(PERLSI_DIEHOOK); |
924508f0 | 1477 | PUSHMARK(SP); |
1738f5c4 | 1478 | XPUSHs(msg); |
20cec16a | 1479 | PUTBACK; |
864dbfa3 | 1480 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1481 | POPSTACK; |
774d564b | 1482 | LEAVE; |
20cec16a | 1483 | } |
748a9306 | 1484 | } |
3280af22 | 1485 | if (PL_in_eval) { |
06bf62c7 | 1486 | PL_restartop = die_where(message, msglen); |
6224f72b | 1487 | JMPENV_JUMP(3); |
a0d0e21e | 1488 | } |
d175a3f0 GS |
1489 | { |
1490 | #ifdef USE_SFIO | |
1491 | /* SFIO can really mess with your errno */ | |
1492 | int e = errno; | |
1493 | #endif | |
06bf62c7 | 1494 | PerlIO_write(PerlIO_stderr(), message, msglen); |
d175a3f0 GS |
1495 | (void)PerlIO_flush(PerlIO_stderr()); |
1496 | #ifdef USE_SFIO | |
1497 | errno = e; | |
1498 | #endif | |
1499 | } | |
f86702cc | 1500 | my_failure_exit(); |
a687059c LW |
1501 | } |
1502 | ||
8990e307 | 1503 | void |
864dbfa3 | 1504 | Perl_warn(pTHX_ const char* pat,...) |
a687059c LW |
1505 | { |
1506 | va_list args; | |
de3bb511 | 1507 | char *message; |
748a9306 LW |
1508 | HV *stash; |
1509 | GV *gv; | |
1510 | CV *cv; | |
06bf62c7 GS |
1511 | SV *msv; |
1512 | STRLEN msglen; | |
a687059c | 1513 | |
8990e307 | 1514 | va_start(args, pat); |
06bf62c7 GS |
1515 | msv = mess(pat, &args); |
1516 | message = SvPV(msv, msglen); | |
a687059c LW |
1517 | va_end(args); |
1518 | ||
3280af22 | 1519 | if (PL_warnhook) { |
1738f5c4 | 1520 | /* sv_2cv might call warn() */ |
11343788 | 1521 | dTHR; |
3280af22 | 1522 | SV *oldwarnhook = PL_warnhook; |
1738f5c4 | 1523 | ENTER; |
3280af22 NIS |
1524 | SAVESPTR(PL_warnhook); |
1525 | PL_warnhook = Nullsv; | |
20cec16a | 1526 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); |
1738f5c4 CS |
1527 | LEAVE; |
1528 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1529 | dSP; |
774d564b | 1530 | SV *msg; |
1531 | ||
1532 | ENTER; | |
06bf62c7 | 1533 | msg = newSVpvn(message, msglen); |
774d564b | 1534 | SvREADONLY_on(msg); |
1535 | SAVEFREESV(msg); | |
1536 | ||
e788e7d3 | 1537 | PUSHSTACKi(PERLSI_WARNHOOK); |
924508f0 | 1538 | PUSHMARK(SP); |
774d564b | 1539 | XPUSHs(msg); |
20cec16a | 1540 | PUTBACK; |
864dbfa3 | 1541 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1542 | POPSTACK; |
774d564b | 1543 | LEAVE; |
20cec16a | 1544 | return; |
1545 | } | |
748a9306 | 1546 | } |
06bf62c7 | 1547 | PerlIO_write(PerlIO_stderr(), message, msglen); |
a687059c | 1548 | #ifdef LEAKTEST |
8c52afec IZ |
1549 | DEBUG_L(*message == '!' |
1550 | ? (xstat(message[1]=='!' | |
1551 | ? (message[2]=='!' ? 2 : 1) | |
1552 | : 0) | |
1553 | , 0) | |
1554 | : 0); | |
a687059c | 1555 | #endif |
20cec16a | 1556 | (void)PerlIO_flush(PerlIO_stderr()); |
a687059c | 1557 | } |
8d063cd8 | 1558 | |
599cee73 | 1559 | void |
864dbfa3 | 1560 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 | 1561 | { |
d008e5eb | 1562 | dTHR; |
599cee73 PM |
1563 | va_list args; |
1564 | char *message; | |
1565 | HV *stash; | |
1566 | GV *gv; | |
1567 | CV *cv; | |
06bf62c7 GS |
1568 | SV *msv; |
1569 | STRLEN msglen; | |
599cee73 PM |
1570 | |
1571 | va_start(args, pat); | |
06bf62c7 GS |
1572 | msv = mess(pat, &args); |
1573 | message = SvPV(msv, msglen); | |
599cee73 PM |
1574 | va_end(args); |
1575 | ||
1576 | if (ckDEAD(err)) { | |
1577 | #ifdef USE_THREADS | |
d008e5eb | 1578 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message)); |
599cee73 PM |
1579 | #endif /* USE_THREADS */ |
1580 | if (PL_diehook) { | |
1581 | /* sv_2cv might call croak() */ | |
1582 | SV *olddiehook = PL_diehook; | |
1583 | ENTER; | |
1584 | SAVESPTR(PL_diehook); | |
1585 | PL_diehook = Nullsv; | |
1586 | cv = sv_2cv(olddiehook, &stash, &gv, 0); | |
1587 | LEAVE; | |
1588 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1589 | dSP; | |
1590 | SV *msg; | |
1591 | ||
1592 | ENTER; | |
06bf62c7 | 1593 | msg = newSVpvn(message, msglen); |
599cee73 PM |
1594 | SvREADONLY_on(msg); |
1595 | SAVEFREESV(msg); | |
1596 | ||
1597 | PUSHMARK(sp); | |
1598 | XPUSHs(msg); | |
1599 | PUTBACK; | |
864dbfa3 | 1600 | call_sv((SV*)cv, G_DISCARD); |
599cee73 PM |
1601 | |
1602 | LEAVE; | |
1603 | } | |
1604 | } | |
1605 | if (PL_in_eval) { | |
06bf62c7 | 1606 | PL_restartop = die_where(message, msglen); |
599cee73 PM |
1607 | JMPENV_JUMP(3); |
1608 | } | |
06bf62c7 | 1609 | PerlIO_write(PerlIO_stderr(), message, msglen); |
599cee73 PM |
1610 | (void)PerlIO_flush(PerlIO_stderr()); |
1611 | my_failure_exit(); | |
1612 | ||
1613 | } | |
1614 | else { | |
1615 | if (PL_warnhook) { | |
1616 | /* sv_2cv might call warn() */ | |
1617 | dTHR; | |
1618 | SV *oldwarnhook = PL_warnhook; | |
1619 | ENTER; | |
1620 | SAVESPTR(PL_warnhook); | |
1621 | PL_warnhook = Nullsv; | |
1622 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); | |
1623 | LEAVE; | |
1624 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1625 | dSP; | |
1626 | SV *msg; | |
1627 | ||
1628 | ENTER; | |
06bf62c7 | 1629 | msg = newSVpvn(message, msglen); |
599cee73 PM |
1630 | SvREADONLY_on(msg); |
1631 | SAVEFREESV(msg); | |
1632 | ||
1633 | PUSHMARK(sp); | |
1634 | XPUSHs(msg); | |
1635 | PUTBACK; | |
864dbfa3 | 1636 | call_sv((SV*)cv, G_DISCARD); |
599cee73 PM |
1637 | |
1638 | LEAVE; | |
1639 | return; | |
1640 | } | |
1641 | } | |
06bf62c7 | 1642 | PerlIO_write(PerlIO_stderr(), message, msglen); |
599cee73 PM |
1643 | #ifdef LEAKTEST |
1644 | DEBUG_L(xstat()); | |
1645 | #endif | |
1646 | (void)PerlIO_flush(PerlIO_stderr()); | |
1647 | } | |
1648 | } | |
1649 | ||
a0d0e21e | 1650 | #ifndef VMS /* VMS' my_setenv() is in VMS.c */ |
f89d6eaa | 1651 | #if !defined(WIN32) && !defined(CYGWIN32) |
8d063cd8 | 1652 | void |
864dbfa3 | 1653 | Perl_my_setenv(pTHX_ char *nam, char *val) |
8d063cd8 | 1654 | { |
f2517201 GS |
1655 | #ifndef PERL_USE_SAFE_PUTENV |
1656 | /* most putenv()s leak, so we manipulate environ directly */ | |
79072805 | 1657 | register I32 i=setenv_getix(nam); /* where does it go? */ |
8d063cd8 | 1658 | |
3280af22 | 1659 | if (environ == PL_origenviron) { /* need we copy environment? */ |
79072805 LW |
1660 | I32 j; |
1661 | I32 max; | |
fe14fcc3 LW |
1662 | char **tmpenv; |
1663 | ||
de3bb511 | 1664 | /*SUPPRESS 530*/ |
fe14fcc3 | 1665 | for (max = i; environ[max]; max++) ; |
f2517201 GS |
1666 | tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); |
1667 | for (j=0; j<max; j++) { /* copy environment */ | |
1668 | tmpenv[j] = (char*)safesysmalloc((strlen(environ[j])+1)*sizeof(char)); | |
1669 | strcpy(tmpenv[j], environ[j]); | |
1670 | } | |
fe14fcc3 LW |
1671 | tmpenv[max] = Nullch; |
1672 | environ = tmpenv; /* tell exec where it is now */ | |
1673 | } | |
a687059c | 1674 | if (!val) { |
f2517201 | 1675 | safesysfree(environ[i]); |
a687059c LW |
1676 | while (environ[i]) { |
1677 | environ[i] = environ[i+1]; | |
1678 | i++; | |
1679 | } | |
1680 | return; | |
1681 | } | |
8d063cd8 | 1682 | if (!environ[i]) { /* does not exist yet */ |
f2517201 | 1683 | environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); |
8d063cd8 LW |
1684 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
1685 | } | |
fe14fcc3 | 1686 | else |
f2517201 GS |
1687 | safesysfree(environ[i]); |
1688 | environ[i] = (char*)safesysmalloc((strlen(nam)+strlen(val)+2) * sizeof(char)); | |
1689 | ||
62b28dd9 | 1690 | #ifndef MSDOS |
a687059c | 1691 | (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
62b28dd9 LW |
1692 | #else |
1693 | /* MS-DOS requires environment variable names to be in uppercase */ | |
fe14fcc3 LW |
1694 | /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but |
1695 | * some utilities and applications may break because they only look | |
1696 | * for upper case strings. (Fixed strupr() bug here.)] | |
1697 | */ | |
1698 | strcpy(environ[i],nam); strupr(environ[i]); | |
62b28dd9 LW |
1699 | (void)sprintf(environ[i] + strlen(nam),"=%s",val); |
1700 | #endif /* MSDOS */ | |
f2517201 GS |
1701 | |
1702 | #else /* PERL_USE_SAFE_PUTENV */ | |
1703 | char *new_env; | |
1704 | ||
1705 | new_env = (char*)safesysmalloc((strlen(nam) + strlen(val) + 2) * sizeof(char)); | |
1706 | #ifndef MSDOS | |
1707 | (void)sprintf(new_env,"%s=%s",nam,val);/* all that work just for this */ | |
1708 | #else | |
1709 | strcpy(new_env,nam); strupr(new_env); | |
1710 | (void)sprintf(new_env + strlen(nam),"=%s",val); | |
1711 | #endif | |
1712 | (void)putenv(new_env); | |
1713 | #endif /* PERL_USE_SAFE_PUTENV */ | |
8d063cd8 LW |
1714 | } |
1715 | ||
3e3baf6d | 1716 | #else /* if WIN32 */ |
68dc0745 | 1717 | |
1718 | void | |
864dbfa3 | 1719 | Perl_my_setenv(pTHX_ char *nam,char *val) |
68dc0745 | 1720 | { |
3e3baf6d TB |
1721 | |
1722 | #ifdef USE_WIN32_RTL_ENV | |
1723 | ||
68dc0745 | 1724 | register char *envstr; |
1725 | STRLEN namlen = strlen(nam); | |
3e3baf6d TB |
1726 | STRLEN vallen; |
1727 | char *oldstr = environ[setenv_getix(nam)]; | |
1728 | ||
1729 | /* putenv() has totally broken semantics in both the Borland | |
1730 | * and Microsoft CRTLs. They either store the passed pointer in | |
1731 | * the environment without making a copy, or make a copy and don't | |
1732 | * free it. And on top of that, they dont free() old entries that | |
1733 | * are being replaced/deleted. This means the caller must | |
1734 | * free any old entries somehow, or we end up with a memory | |
1735 | * leak every time my_setenv() is called. One might think | |
1736 | * one could directly manipulate environ[], like the UNIX code | |
1737 | * above, but direct changes to environ are not allowed when | |
1738 | * calling putenv(), since the RTLs maintain an internal | |
1739 | * *copy* of environ[]. Bad, bad, *bad* stink. | |
1740 | * GSAR 97-06-07 | |
1741 | */ | |
68dc0745 | 1742 | |
3e3baf6d TB |
1743 | if (!val) { |
1744 | if (!oldstr) | |
1745 | return; | |
1746 | val = ""; | |
1747 | vallen = 0; | |
1748 | } | |
1749 | else | |
1750 | vallen = strlen(val); | |
f2517201 | 1751 | envstr = (char*)safesysmalloc((namlen + vallen + 3) * sizeof(char)); |
68dc0745 | 1752 | (void)sprintf(envstr,"%s=%s",nam,val); |
76e3520e | 1753 | (void)PerlEnv_putenv(envstr); |
3e3baf6d | 1754 | if (oldstr) |
f2517201 | 1755 | safesysfree(oldstr); |
3e3baf6d | 1756 | #ifdef _MSC_VER |
f2517201 | 1757 | safesysfree(envstr); /* MSVCRT leaks without this */ |
3e3baf6d TB |
1758 | #endif |
1759 | ||
1760 | #else /* !USE_WIN32_RTL_ENV */ | |
1761 | ||
ac5c734f GS |
1762 | register char *envstr; |
1763 | STRLEN len = strlen(nam) + 3; | |
1764 | if (!val) { | |
1765 | val = ""; | |
1766 | } | |
1767 | len += strlen(val); | |
1768 | New(904, envstr, len, char); | |
1769 | (void)sprintf(envstr,"%s=%s",nam,val); | |
1770 | (void)PerlEnv_putenv(envstr); | |
1771 | Safefree(envstr); | |
3e3baf6d TB |
1772 | |
1773 | #endif | |
1774 | } | |
1775 | ||
1776 | #endif /* WIN32 */ | |
1777 | ||
1778 | I32 | |
864dbfa3 | 1779 | Perl_setenv_getix(pTHX_ char *nam) |
3e3baf6d TB |
1780 | { |
1781 | register I32 i, len = strlen(nam); | |
1782 | ||
1783 | for (i = 0; environ[i]; i++) { | |
1784 | if ( | |
1785 | #ifdef WIN32 | |
1786 | strnicmp(environ[i],nam,len) == 0 | |
1787 | #else | |
1788 | strnEQ(environ[i],nam,len) | |
1789 | #endif | |
1790 | && environ[i][len] == '=') | |
1791 | break; /* strnEQ must come first to avoid */ | |
1792 | } /* potential SEGV's */ | |
1793 | return i; | |
68dc0745 | 1794 | } |
1795 | ||
a0d0e21e | 1796 | #endif /* !VMS */ |
378cc40b | 1797 | |
16d20bd9 | 1798 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 1799 | I32 |
864dbfa3 | 1800 | Perl_unlnk(pTHX_ char *f) /* unlink all versions of a file */ |
378cc40b | 1801 | { |
79072805 | 1802 | I32 i; |
378cc40b | 1803 | |
6ad3d225 | 1804 | for (i = 0; PerlLIO_unlink(f) >= 0; i++) ; |
378cc40b LW |
1805 | return i ? 0 : -1; |
1806 | } | |
1807 | #endif | |
1808 | ||
85e6fe83 | 1809 | #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY) |
378cc40b | 1810 | char * |
864dbfa3 | 1811 | Perl_my_bcopy(pTHX_ register const char *from,register char *to,register I32 len) |
378cc40b LW |
1812 | { |
1813 | char *retval = to; | |
1814 | ||
7c0587c8 LW |
1815 | if (from - to >= 0) { |
1816 | while (len--) | |
1817 | *to++ = *from++; | |
1818 | } | |
1819 | else { | |
1820 | to += len; | |
1821 | from += len; | |
1822 | while (len--) | |
faf8582f | 1823 | *(--to) = *(--from); |
7c0587c8 | 1824 | } |
378cc40b LW |
1825 | return retval; |
1826 | } | |
ffed7fef | 1827 | #endif |
378cc40b | 1828 | |
fc36a67e | 1829 | #ifndef HAS_MEMSET |
1830 | void * | |
864dbfa3 | 1831 | Perl_my_memset(pTHX_ register char *loc, register I32 ch, register I32 len) |
fc36a67e | 1832 | { |
1833 | char *retval = loc; | |
1834 | ||
1835 | while (len--) | |
1836 | *loc++ = ch; | |
1837 | return retval; | |
1838 | } | |
1839 | #endif | |
1840 | ||
7c0587c8 | 1841 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 1842 | char * |
864dbfa3 | 1843 | Perl_my_bzero(pTHX_ register char *loc, register I32 len) |
378cc40b LW |
1844 | { |
1845 | char *retval = loc; | |
1846 | ||
1847 | while (len--) | |
1848 | *loc++ = 0; | |
1849 | return retval; | |
1850 | } | |
1851 | #endif | |
7c0587c8 | 1852 | |
36477c24 | 1853 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 1854 | I32 |
864dbfa3 | 1855 | Perl_my_memcmp(pTHX_ const char *s1, const char *s2, register I32 len) |
7c0587c8 | 1856 | { |
36477c24 | 1857 | register U8 *a = (U8 *)s1; |
1858 | register U8 *b = (U8 *)s2; | |
79072805 | 1859 | register I32 tmp; |
7c0587c8 LW |
1860 | |
1861 | while (len--) { | |
36477c24 | 1862 | if (tmp = *a++ - *b++) |
7c0587c8 LW |
1863 | return tmp; |
1864 | } | |
1865 | return 0; | |
1866 | } | |
36477c24 | 1867 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 1868 | |
fe14fcc3 | 1869 | #ifndef HAS_VPRINTF |
a687059c | 1870 | |
85e6fe83 | 1871 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1872 | char * |
1873 | #else | |
1874 | int | |
1875 | #endif | |
08105a92 | 1876 | vsprintf(char *dest, const char *pat, char *args) |
a687059c LW |
1877 | { |
1878 | FILE fakebuf; | |
1879 | ||
1880 | fakebuf._ptr = dest; | |
1881 | fakebuf._cnt = 32767; | |
35c8bce7 LW |
1882 | #ifndef _IOSTRG |
1883 | #define _IOSTRG 0 | |
1884 | #endif | |
a687059c LW |
1885 | fakebuf._flag = _IOWRT|_IOSTRG; |
1886 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
1887 | (void)putc('\0', &fakebuf); | |
85e6fe83 | 1888 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
1889 | return(dest); |
1890 | #else | |
1891 | return 0; /* perl doesn't use return value */ | |
1892 | #endif | |
1893 | } | |
1894 | ||
fe14fcc3 | 1895 | #endif /* HAS_VPRINTF */ |
a687059c LW |
1896 | |
1897 | #ifdef MYSWAP | |
ffed7fef | 1898 | #if BYTEORDER != 0x4321 |
a687059c | 1899 | short |
864dbfa3 | 1900 | Perl_my_swap(pTHX_ short s) |
a687059c LW |
1901 | { |
1902 | #if (BYTEORDER & 1) == 0 | |
1903 | short result; | |
1904 | ||
1905 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
1906 | return result; | |
1907 | #else | |
1908 | return s; | |
1909 | #endif | |
1910 | } | |
1911 | ||
1912 | long | |
864dbfa3 | 1913 | Perl_my_htonl(pTHX_ long l) |
a687059c LW |
1914 | { |
1915 | union { | |
1916 | long result; | |
ffed7fef | 1917 | char c[sizeof(long)]; |
a687059c LW |
1918 | } u; |
1919 | ||
ffed7fef | 1920 | #if BYTEORDER == 0x1234 |
a687059c LW |
1921 | u.c[0] = (l >> 24) & 255; |
1922 | u.c[1] = (l >> 16) & 255; | |
1923 | u.c[2] = (l >> 8) & 255; | |
1924 | u.c[3] = l & 255; | |
1925 | return u.result; | |
1926 | #else | |
ffed7fef | 1927 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1928 | croak("Unknown BYTEORDER\n"); |
a687059c | 1929 | #else |
79072805 LW |
1930 | register I32 o; |
1931 | register I32 s; | |
a687059c | 1932 | |
ffed7fef LW |
1933 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1934 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
1935 | } |
1936 | return u.result; | |
1937 | #endif | |
1938 | #endif | |
1939 | } | |
1940 | ||
1941 | long | |
864dbfa3 | 1942 | Perl_my_ntohl(pTHX_ long l) |
a687059c LW |
1943 | { |
1944 | union { | |
1945 | long l; | |
ffed7fef | 1946 | char c[sizeof(long)]; |
a687059c LW |
1947 | } u; |
1948 | ||
ffed7fef | 1949 | #if BYTEORDER == 0x1234 |
a687059c LW |
1950 | u.c[0] = (l >> 24) & 255; |
1951 | u.c[1] = (l >> 16) & 255; | |
1952 | u.c[2] = (l >> 8) & 255; | |
1953 | u.c[3] = l & 255; | |
1954 | return u.l; | |
1955 | #else | |
ffed7fef | 1956 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
463ee0b2 | 1957 | croak("Unknown BYTEORDER\n"); |
a687059c | 1958 | #else |
79072805 LW |
1959 | register I32 o; |
1960 | register I32 s; | |
a687059c LW |
1961 | |
1962 | u.l = l; | |
1963 | l = 0; | |
ffed7fef LW |
1964 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
1965 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
1966 | } |
1967 | return l; | |
1968 | #endif | |
1969 | #endif | |
1970 | } | |
1971 | ||
ffed7fef | 1972 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
1973 | #endif /* MYSWAP */ |
1974 | ||
1975 | /* | |
1976 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
1977 | * If these functions are defined, | |
1978 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
1979 | * However, this is not assumed. | |
1980 | * -DWS | |
1981 | */ | |
1982 | ||
1983 | #define HTOV(name,type) \ | |
1984 | type \ | |
ba106d47 | 1985 | name (register type n) \ |
988174c1 LW |
1986 | { \ |
1987 | union { \ | |
1988 | type value; \ | |
1989 | char c[sizeof(type)]; \ | |
1990 | } u; \ | |
79072805 LW |
1991 | register I32 i; \ |
1992 | register I32 s; \ | |
988174c1 LW |
1993 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
1994 | u.c[i] = (n >> s) & 0xFF; \ | |
1995 | } \ | |
1996 | return u.value; \ | |
1997 | } | |
1998 | ||
1999 | #define VTOH(name,type) \ | |
2000 | type \ | |
ba106d47 | 2001 | name (register type n) \ |
988174c1 LW |
2002 | { \ |
2003 | union { \ | |
2004 | type value; \ | |
2005 | char c[sizeof(type)]; \ | |
2006 | } u; \ | |
79072805 LW |
2007 | register I32 i; \ |
2008 | register I32 s; \ | |
988174c1 LW |
2009 | u.value = n; \ |
2010 | n = 0; \ | |
2011 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ | |
2012 | n += (u.c[i] & 0xFF) << s; \ | |
2013 | } \ | |
2014 | return n; \ | |
2015 | } | |
2016 | ||
2017 | #if defined(HAS_HTOVS) && !defined(htovs) | |
2018 | HTOV(htovs,short) | |
2019 | #endif | |
2020 | #if defined(HAS_HTOVL) && !defined(htovl) | |
2021 | HTOV(htovl,long) | |
2022 | #endif | |
2023 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
2024 | VTOH(vtohs,short) | |
2025 | #endif | |
2026 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
2027 | VTOH(vtohl,long) | |
2028 | #endif | |
a687059c | 2029 | |
5f05dabc | 2030 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
092bebab | 2031 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) |
760ac839 | 2032 | PerlIO * |
864dbfa3 | 2033 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
a687059c LW |
2034 | { |
2035 | int p[2]; | |
8ac85365 | 2036 | register I32 This, that; |
79072805 LW |
2037 | register I32 pid; |
2038 | SV *sv; | |
1738f5c4 | 2039 | I32 doexec = strNE(cmd,"-"); |
e446cec8 IZ |
2040 | I32 did_pipes = 0; |
2041 | int pp[2]; | |
a687059c | 2042 | |
45bc9206 | 2043 | PERL_FLUSHALL_FOR_CHILD; |
ddcf38b7 IZ |
2044 | #ifdef OS2 |
2045 | if (doexec) { | |
2046 | return my_syspopen(cmd,mode); | |
2047 | } | |
2048 | #endif | |
8ac85365 NIS |
2049 | This = (*mode == 'w'); |
2050 | that = !This; | |
3280af22 | 2051 | if (doexec && PL_tainting) { |
bbce6d69 | 2052 | taint_env(); |
2053 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 2054 | } |
c2267164 IZ |
2055 | if (PerlProc_pipe(p) < 0) |
2056 | return Nullfp; | |
e446cec8 IZ |
2057 | if (doexec && PerlProc_pipe(pp) >= 0) |
2058 | did_pipes = 1; | |
a687059c LW |
2059 | while ((pid = (doexec?vfork():fork())) < 0) { |
2060 | if (errno != EAGAIN) { | |
6ad3d225 | 2061 | PerlLIO_close(p[This]); |
e446cec8 IZ |
2062 | if (did_pipes) { |
2063 | PerlLIO_close(pp[0]); | |
2064 | PerlLIO_close(pp[1]); | |
2065 | } | |
a687059c | 2066 | if (!doexec) |
463ee0b2 | 2067 | croak("Can't fork"); |
a687059c LW |
2068 | return Nullfp; |
2069 | } | |
2070 | sleep(5); | |
2071 | } | |
2072 | if (pid == 0) { | |
79072805 LW |
2073 | GV* tmpgv; |
2074 | ||
30ac6d9b GS |
2075 | #undef THIS |
2076 | #undef THAT | |
a687059c | 2077 | #define THIS that |
8ac85365 | 2078 | #define THAT This |
6ad3d225 | 2079 | PerlLIO_close(p[THAT]); |
e446cec8 IZ |
2080 | if (did_pipes) { |
2081 | PerlLIO_close(pp[0]); | |
2082 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2083 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2084 | #endif | |
2085 | } | |
a687059c | 2086 | if (p[THIS] != (*mode == 'r')) { |
6ad3d225 GS |
2087 | PerlLIO_dup2(p[THIS], *mode == 'r'); |
2088 | PerlLIO_close(p[THIS]); | |
a687059c | 2089 | } |
4435c477 | 2090 | #ifndef OS2 |
a687059c | 2091 | if (doexec) { |
a0d0e21e | 2092 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
2093 | int fd; |
2094 | ||
2095 | #ifndef NOFILE | |
2096 | #define NOFILE 20 | |
2097 | #endif | |
6b88bc9c | 2098 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) |
e446cec8 IZ |
2099 | if (fd != pp[1]) |
2100 | PerlLIO_close(fd); | |
ae986130 | 2101 | #endif |
e446cec8 | 2102 | do_exec3(cmd,pp[1],did_pipes); /* may or may not use the shell */ |
6ad3d225 | 2103 | PerlProc__exit(1); |
a687059c | 2104 | } |
4435c477 | 2105 | #endif /* defined OS2 */ |
de3bb511 | 2106 | /*SUPPRESS 560*/ |
85e6fe83 | 2107 | if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV)) |
1e422769 | 2108 | sv_setiv(GvSV(tmpgv), (IV)getpid()); |
3280af22 NIS |
2109 | PL_forkprocess = 0; |
2110 | hv_clear(PL_pidstatus); /* we have no children */ | |
a687059c LW |
2111 | return Nullfp; |
2112 | #undef THIS | |
2113 | #undef THAT | |
2114 | } | |
62b28dd9 | 2115 | do_execfree(); /* free any memory malloced by child on vfork */ |
6ad3d225 | 2116 | PerlLIO_close(p[that]); |
e446cec8 IZ |
2117 | if (did_pipes) |
2118 | PerlLIO_close(pp[1]); | |
8ac85365 | 2119 | if (p[that] < p[This]) { |
6ad3d225 GS |
2120 | PerlLIO_dup2(p[This], p[that]); |
2121 | PerlLIO_close(p[This]); | |
8ac85365 | 2122 | p[This] = p[that]; |
62b28dd9 | 2123 | } |
3280af22 | 2124 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
a0d0e21e | 2125 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 2126 | SvIVX(sv) = pid; |
3280af22 | 2127 | PL_forkprocess = pid; |
e446cec8 IZ |
2128 | if (did_pipes && pid > 0) { |
2129 | int errkid; | |
2130 | int n = 0, n1; | |
2131 | ||
2132 | while (n < sizeof(int)) { | |
2133 | n1 = PerlLIO_read(pp[0], | |
2134 | (void*)(((char*)&errkid)+n), | |
2135 | (sizeof(int)) - n); | |
2136 | if (n1 <= 0) | |
2137 | break; | |
2138 | n += n1; | |
2139 | } | |
2140 | if (n) { /* Error */ | |
2141 | if (n != sizeof(int)) | |
2142 | croak("panic: kid popen errno read"); | |
2143 | PerlLIO_close(pp[0]); | |
2144 | errno = errkid; /* Propagate errno from kid */ | |
2145 | return Nullfp; | |
2146 | } | |
2147 | } | |
2148 | if (did_pipes) | |
2149 | PerlLIO_close(pp[0]); | |
8ac85365 | 2150 | return PerlIO_fdopen(p[This], mode); |
a687059c | 2151 | } |
7c0587c8 | 2152 | #else |
55497cff | 2153 | #if defined(atarist) || defined(DJGPP) |
7c0587c8 | 2154 | FILE *popen(); |
760ac839 | 2155 | PerlIO * |
864dbfa3 | 2156 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
7c0587c8 | 2157 | { |
760ac839 | 2158 | /* Needs work for PerlIO ! */ |
55497cff | 2159 | /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */ |
45bc9206 | 2160 | PERL_FLUSHALL_FOR_CHILD; |
55497cff | 2161 | return popen(PerlIO_exportFILE(cmd, 0), mode); |
7c0587c8 LW |
2162 | } |
2163 | #endif | |
2164 | ||
2165 | #endif /* !DOSISH */ | |
a687059c | 2166 | |
748a9306 | 2167 | #ifdef DUMP_FDS |
35ff7856 | 2168 | void |
864dbfa3 | 2169 | Perl_dump_fds(pTHX_ char *s) |
ae986130 LW |
2170 | { |
2171 | int fd; | |
2172 | struct stat tmpstatbuf; | |
2173 | ||
760ac839 | 2174 | PerlIO_printf(PerlIO_stderr(),"%s", s); |
ae986130 | 2175 | for (fd = 0; fd < 32; fd++) { |
6ad3d225 | 2176 | if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0) |
760ac839 | 2177 | PerlIO_printf(PerlIO_stderr()," %d",fd); |
ae986130 | 2178 | } |
760ac839 | 2179 | PerlIO_printf(PerlIO_stderr(),"\n"); |
ae986130 | 2180 | } |
35ff7856 | 2181 | #endif /* DUMP_FDS */ |
ae986130 | 2182 | |
fe14fcc3 | 2183 | #ifndef HAS_DUP2 |
fec02dd3 | 2184 | int |
ba106d47 | 2185 | dup2(int oldfd, int newfd) |
a687059c | 2186 | { |
a0d0e21e | 2187 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
2188 | if (oldfd == newfd) |
2189 | return oldfd; | |
6ad3d225 | 2190 | PerlLIO_close(newfd); |
fec02dd3 | 2191 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 2192 | #else |
fc36a67e | 2193 | #define DUP2_MAX_FDS 256 |
2194 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 2195 | I32 fdx = 0; |
ae986130 LW |
2196 | int fd; |
2197 | ||
fe14fcc3 | 2198 | if (oldfd == newfd) |
fec02dd3 | 2199 | return oldfd; |
6ad3d225 | 2200 | PerlLIO_close(newfd); |
fc36a67e | 2201 | /* good enough for low fd's... */ |
6ad3d225 | 2202 | while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
fc36a67e | 2203 | if (fdx >= DUP2_MAX_FDS) { |
6ad3d225 | 2204 | PerlLIO_close(fd); |
fc36a67e | 2205 | fd = -1; |
2206 | break; | |
2207 | } | |
ae986130 | 2208 | fdtmp[fdx++] = fd; |
fc36a67e | 2209 | } |
ae986130 | 2210 | while (fdx > 0) |
6ad3d225 | 2211 | PerlLIO_close(fdtmp[--fdx]); |
fec02dd3 | 2212 | return fd; |
62b28dd9 | 2213 | #endif |
a687059c LW |
2214 | } |
2215 | #endif | |
2216 | ||
ff68c719 | 2217 | |
2218 | #ifdef HAS_SIGACTION | |
2219 | ||
2220 | Sighandler_t | |
864dbfa3 | 2221 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2222 | { |
2223 | struct sigaction act, oact; | |
2224 | ||
2225 | act.sa_handler = handler; | |
2226 | sigemptyset(&act.sa_mask); | |
2227 | act.sa_flags = 0; | |
2228 | #ifdef SA_RESTART | |
2229 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
2230 | #endif | |
85264bed CS |
2231 | #ifdef SA_NOCLDWAIT |
2232 | if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) | |
2233 | act.sa_flags |= SA_NOCLDWAIT; | |
2234 | #endif | |
ff68c719 | 2235 | if (sigaction(signo, &act, &oact) == -1) |
36477c24 | 2236 | return SIG_ERR; |
ff68c719 | 2237 | else |
36477c24 | 2238 | return oact.sa_handler; |
ff68c719 | 2239 | } |
2240 | ||
2241 | Sighandler_t | |
864dbfa3 | 2242 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2243 | { |
2244 | struct sigaction oact; | |
2245 | ||
2246 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
2247 | return SIG_ERR; | |
2248 | else | |
2249 | return oact.sa_handler; | |
2250 | } | |
2251 | ||
2252 | int | |
864dbfa3 | 2253 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2254 | { |
2255 | struct sigaction act; | |
2256 | ||
2257 | act.sa_handler = handler; | |
2258 | sigemptyset(&act.sa_mask); | |
2259 | act.sa_flags = 0; | |
2260 | #ifdef SA_RESTART | |
2261 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
2262 | #endif | |
85264bed CS |
2263 | #ifdef SA_NOCLDWAIT |
2264 | if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) | |
2265 | act.sa_flags |= SA_NOCLDWAIT; | |
2266 | #endif | |
ff68c719 | 2267 | return sigaction(signo, &act, save); |
2268 | } | |
2269 | ||
2270 | int | |
864dbfa3 | 2271 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2272 | { |
2273 | return sigaction(signo, save, (struct sigaction *)NULL); | |
2274 | } | |
2275 | ||
2276 | #else /* !HAS_SIGACTION */ | |
2277 | ||
2278 | Sighandler_t | |
864dbfa3 | 2279 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2280 | { |
6ad3d225 | 2281 | return PerlProc_signal(signo, handler); |
ff68c719 | 2282 | } |
2283 | ||
2284 | static int sig_trapped; | |
2285 | ||
2286 | static | |
2287 | Signal_t | |
4e35701f | 2288 | sig_trap(int signo) |
ff68c719 | 2289 | { |
2290 | sig_trapped++; | |
2291 | } | |
2292 | ||
2293 | Sighandler_t | |
864dbfa3 | 2294 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2295 | { |
2296 | Sighandler_t oldsig; | |
2297 | ||
2298 | sig_trapped = 0; | |
6ad3d225 GS |
2299 | oldsig = PerlProc_signal(signo, sig_trap); |
2300 | PerlProc_signal(signo, oldsig); | |
ff68c719 | 2301 | if (sig_trapped) |
6ad3d225 | 2302 | PerlProc_kill(getpid(), signo); |
ff68c719 | 2303 | return oldsig; |
2304 | } | |
2305 | ||
2306 | int | |
864dbfa3 | 2307 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2308 | { |
6ad3d225 | 2309 | *save = PerlProc_signal(signo, handler); |
ff68c719 | 2310 | return (*save == SIG_ERR) ? -1 : 0; |
2311 | } | |
2312 | ||
2313 | int | |
864dbfa3 | 2314 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2315 | { |
6ad3d225 | 2316 | return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0; |
ff68c719 | 2317 | } |
2318 | ||
2319 | #endif /* !HAS_SIGACTION */ | |
2320 | ||
5f05dabc | 2321 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
092bebab | 2322 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) |
79072805 | 2323 | I32 |
864dbfa3 | 2324 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a687059c | 2325 | { |
ff68c719 | 2326 | Sigsave_t hstat, istat, qstat; |
a687059c | 2327 | int status; |
a0d0e21e | 2328 | SV **svp; |
20188a90 | 2329 | int pid; |
1d3434b8 | 2330 | int pid2; |
03136e13 CS |
2331 | bool close_failed; |
2332 | int saved_errno; | |
2333 | #ifdef VMS | |
2334 | int saved_vaxc_errno; | |
2335 | #endif | |
22fae026 TM |
2336 | #ifdef WIN32 |
2337 | int saved_win32_errno; | |
2338 | #endif | |
a687059c | 2339 | |
3280af22 | 2340 | svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE); |
748a9306 | 2341 | pid = (int)SvIVX(*svp); |
a0d0e21e | 2342 | SvREFCNT_dec(*svp); |
3280af22 | 2343 | *svp = &PL_sv_undef; |
ddcf38b7 IZ |
2344 | #ifdef OS2 |
2345 | if (pid == -1) { /* Opened by popen. */ | |
2346 | return my_syspclose(ptr); | |
2347 | } | |
2348 | #endif | |
03136e13 CS |
2349 | if ((close_failed = (PerlIO_close(ptr) == EOF))) { |
2350 | saved_errno = errno; | |
2351 | #ifdef VMS | |
2352 | saved_vaxc_errno = vaxc$errno; | |
2353 | #endif | |
22fae026 TM |
2354 | #ifdef WIN32 |
2355 | saved_win32_errno = GetLastError(); | |
2356 | #endif | |
03136e13 | 2357 | } |
7c0587c8 | 2358 | #ifdef UTS |
6ad3d225 | 2359 | if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ |
7c0587c8 | 2360 | #endif |
ff68c719 | 2361 | rsignal_save(SIGHUP, SIG_IGN, &hstat); |
2362 | rsignal_save(SIGINT, SIG_IGN, &istat); | |
2363 | rsignal_save(SIGQUIT, SIG_IGN, &qstat); | |
748a9306 | 2364 | do { |
1d3434b8 GS |
2365 | pid2 = wait4pid(pid, &status, 0); |
2366 | } while (pid2 == -1 && errno == EINTR); | |
ff68c719 | 2367 | rsignal_restore(SIGHUP, &hstat); |
2368 | rsignal_restore(SIGINT, &istat); | |
2369 | rsignal_restore(SIGQUIT, &qstat); | |
03136e13 CS |
2370 | if (close_failed) { |
2371 | SETERRNO(saved_errno, saved_vaxc_errno); | |
2372 | return -1; | |
2373 | } | |
1d3434b8 | 2374 | return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)); |
20188a90 | 2375 | } |
4633a7c4 LW |
2376 | #endif /* !DOSISH */ |
2377 | ||
8736538c | 2378 | #if !defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(CYGWIN32) |
79072805 | 2379 | I32 |
864dbfa3 | 2380 | Perl_wait4pid(pTHX_ int pid, int *statusp, int flags) |
20188a90 | 2381 | { |
79072805 LW |
2382 | SV *sv; |
2383 | SV** svp; | |
fc36a67e | 2384 | char spid[TYPE_CHARS(int)]; |
20188a90 LW |
2385 | |
2386 | if (!pid) | |
2387 | return -1; | |
20188a90 LW |
2388 | if (pid > 0) { |
2389 | sprintf(spid, "%d", pid); | |
3280af22 NIS |
2390 | svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE); |
2391 | if (svp && *svp != &PL_sv_undef) { | |
463ee0b2 | 2392 | *statusp = SvIVX(*svp); |
3280af22 | 2393 | (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2394 | return pid; |
2395 | } | |
2396 | } | |
2397 | else { | |
79072805 | 2398 | HE *entry; |
20188a90 | 2399 | |
3280af22 NIS |
2400 | hv_iterinit(PL_pidstatus); |
2401 | if (entry = hv_iternext(PL_pidstatus)) { | |
a0d0e21e | 2402 | pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
3280af22 | 2403 | sv = hv_iterval(PL_pidstatus,entry); |
463ee0b2 | 2404 | *statusp = SvIVX(sv); |
20188a90 | 2405 | sprintf(spid, "%d", pid); |
3280af22 | 2406 | (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2407 | return pid; |
2408 | } | |
2409 | } | |
79072805 | 2410 | #ifdef HAS_WAITPID |
367f3c24 IZ |
2411 | # ifdef HAS_WAITPID_RUNTIME |
2412 | if (!HAS_WAITPID_RUNTIME) | |
2413 | goto hard_way; | |
2414 | # endif | |
f55ee38a | 2415 | return PerlProc_waitpid(pid,statusp,flags); |
367f3c24 IZ |
2416 | #endif |
2417 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
a0d0e21e | 2418 | return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); |
367f3c24 IZ |
2419 | #endif |
2420 | #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME) | |
2421 | hard_way: | |
a0d0e21e LW |
2422 | { |
2423 | I32 result; | |
2424 | if (flags) | |
2425 | croak("Can't do waitpid with flags"); | |
2426 | else { | |
76e3520e | 2427 | while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
a0d0e21e LW |
2428 | pidgone(result,*statusp); |
2429 | if (result < 0) | |
2430 | *statusp = -1; | |
2431 | } | |
2432 | return result; | |
a687059c LW |
2433 | } |
2434 | #endif | |
a687059c | 2435 | } |
2d7a9237 | 2436 | #endif /* !DOSISH || OS2 || WIN32 */ |
a687059c | 2437 | |
7c0587c8 | 2438 | void |
de3bb511 | 2439 | /*SUPPRESS 590*/ |
864dbfa3 | 2440 | Perl_pidgone(pTHX_ int pid, int status) |
a687059c | 2441 | { |
79072805 | 2442 | register SV *sv; |
fc36a67e | 2443 | char spid[TYPE_CHARS(int)]; |
a687059c | 2444 | |
20188a90 | 2445 | sprintf(spid, "%d", pid); |
3280af22 | 2446 | sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE); |
a0d0e21e | 2447 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 2448 | SvIVX(sv) = status; |
20188a90 | 2449 | return; |
a687059c LW |
2450 | } |
2451 | ||
55497cff | 2452 | #if defined(atarist) || defined(OS2) || defined(DJGPP) |
7c0587c8 | 2453 | int pclose(); |
ddcf38b7 IZ |
2454 | #ifdef HAS_FORK |
2455 | int /* Cannot prototype with I32 | |
2456 | in os2ish.h. */ | |
ba106d47 | 2457 | my_syspclose(PerlIO *ptr) |
ddcf38b7 | 2458 | #else |
79072805 | 2459 | I32 |
864dbfa3 | 2460 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
ddcf38b7 | 2461 | #endif |
a687059c | 2462 | { |
760ac839 LW |
2463 | /* Needs work for PerlIO ! */ |
2464 | FILE *f = PerlIO_findFILE(ptr); | |
2465 | I32 result = pclose(f); | |
2466 | PerlIO_releaseFILE(ptr,f); | |
2467 | return result; | |
a687059c | 2468 | } |
7c0587c8 | 2469 | #endif |
9f68db38 LW |
2470 | |
2471 | void | |
864dbfa3 | 2472 | Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count) |
9f68db38 | 2473 | { |
79072805 | 2474 | register I32 todo; |
08105a92 | 2475 | register const char *frombase = from; |
9f68db38 LW |
2476 | |
2477 | if (len == 1) { | |
08105a92 | 2478 | register const char c = *from; |
9f68db38 | 2479 | while (count-- > 0) |
5926133d | 2480 | *to++ = c; |
9f68db38 LW |
2481 | return; |
2482 | } | |
2483 | while (count-- > 0) { | |
2484 | for (todo = len; todo > 0; todo--) { | |
2485 | *to++ = *from++; | |
2486 | } | |
2487 | from = frombase; | |
2488 | } | |
2489 | } | |
0f85fab0 | 2490 | |
463ee0b2 | 2491 | U32 |
864dbfa3 | 2492 | Perl_cast_ulong(pTHX_ double f) |
0f85fab0 LW |
2493 | { |
2494 | long along; | |
2495 | ||
27e2fb84 | 2496 | #if CASTFLAGS & 2 |
34de22dd LW |
2497 | # define BIGDOUBLE 2147483648.0 |
2498 | if (f >= BIGDOUBLE) | |
2499 | return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000; | |
2500 | #endif | |
0f85fab0 LW |
2501 | if (f >= 0.0) |
2502 | return (unsigned long)f; | |
2503 | along = (long)f; | |
2504 | return (unsigned long)along; | |
2505 | } | |
ed6116ce | 2506 | # undef BIGDOUBLE |
5d94fbed | 2507 | |
5d94fbed AD |
2508 | /* Unfortunately, on some systems the cast_uv() function doesn't |
2509 | work with the system-supplied definition of ULONG_MAX. The | |
2510 | comparison (f >= ULONG_MAX) always comes out true. It must be a | |
2511 | problem with the compiler constant folding. | |
2512 | ||
2513 | In any case, this workaround should be fine on any two's complement | |
2514 | system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your | |
2515 | ccflags. | |
2516 | --Andy Dougherty <doughera@lafcol.lafayette.edu> | |
2517 | */ | |
1eb770ff | 2518 | |
2519 | /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead | |
2520 | of LONG_(MIN/MAX). | |
2521 | -- Kenneth Albanowski <kjahds@kjahds.com> | |
2522 | */ | |
2523 | ||
20cec16a | 2524 | #ifndef MY_UV_MAX |
2525 | # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1) | |
5d94fbed AD |
2526 | #endif |
2527 | ||
ed6116ce | 2528 | I32 |
864dbfa3 | 2529 | Perl_cast_i32(pTHX_ double f) |
ed6116ce | 2530 | { |
20cec16a | 2531 | if (f >= I32_MAX) |
2532 | return (I32) I32_MAX; | |
2533 | if (f <= I32_MIN) | |
2534 | return (I32) I32_MIN; | |
ed6116ce LW |
2535 | return (I32) f; |
2536 | } | |
a0d0e21e LW |
2537 | |
2538 | IV | |
864dbfa3 | 2539 | Perl_cast_iv(pTHX_ double f) |
a0d0e21e | 2540 | { |
25da4f38 IZ |
2541 | if (f >= IV_MAX) { |
2542 | UV uv; | |
2543 | ||
2544 | if (f >= (double)UV_MAX) | |
2545 | return (IV) UV_MAX; | |
2546 | uv = (UV) f; | |
2547 | return (IV)uv; | |
2548 | } | |
20cec16a | 2549 | if (f <= IV_MIN) |
2550 | return (IV) IV_MIN; | |
a0d0e21e LW |
2551 | return (IV) f; |
2552 | } | |
5d94fbed AD |
2553 | |
2554 | UV | |
864dbfa3 | 2555 | Perl_cast_uv(pTHX_ double f) |
5d94fbed | 2556 | { |
20cec16a | 2557 | if (f >= MY_UV_MAX) |
2558 | return (UV) MY_UV_MAX; | |
25da4f38 IZ |
2559 | if (f < 0) { |
2560 | IV iv; | |
2561 | ||
2562 | if (f < IV_MIN) | |
2563 | return (UV)IV_MIN; | |
2564 | iv = (IV) f; | |
2565 | return (UV) iv; | |
2566 | } | |
5d94fbed AD |
2567 | return (UV) f; |
2568 | } | |
2569 | ||
fe14fcc3 | 2570 | #ifndef HAS_RENAME |
79072805 | 2571 | I32 |
864dbfa3 | 2572 | Perl_same_dirent(pTHX_ char *a, char *b) |
62b28dd9 | 2573 | { |
93a17b20 LW |
2574 | char *fa = strrchr(a,'/'); |
2575 | char *fb = strrchr(b,'/'); | |
62b28dd9 LW |
2576 | struct stat tmpstatbuf1; |
2577 | struct stat tmpstatbuf2; | |
46fc3d4c | 2578 | SV *tmpsv = sv_newmortal(); |
62b28dd9 LW |
2579 | |
2580 | if (fa) | |
2581 | fa++; | |
2582 | else | |
2583 | fa = a; | |
2584 | if (fb) | |
2585 | fb++; | |
2586 | else | |
2587 | fb = b; | |
2588 | if (strNE(a,b)) | |
2589 | return FALSE; | |
2590 | if (fa == a) | |
46fc3d4c | 2591 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2592 | else |
46fc3d4c | 2593 | sv_setpvn(tmpsv, a, fa - a); |
c6ed36e1 | 2594 | if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0) |
62b28dd9 LW |
2595 | return FALSE; |
2596 | if (fb == b) | |
46fc3d4c | 2597 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2598 | else |
46fc3d4c | 2599 | sv_setpvn(tmpsv, b, fb - b); |
c6ed36e1 | 2600 | if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0) |
62b28dd9 LW |
2601 | return FALSE; |
2602 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
2603 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
2604 | } | |
fe14fcc3 LW |
2605 | #endif /* !HAS_RENAME */ |
2606 | ||
55497cff | 2607 | UV |
864dbfa3 | 2608 | Perl_scan_bin(pTHX_ char *start, I32 len, I32 *retlen) |
4f19785b WSI |
2609 | { |
2610 | register char *s = start; | |
2611 | register UV retval = 0; | |
2612 | bool overflowed = FALSE; | |
2613 | while (len && *s >= '0' && *s <= '1') { | |
2614 | register UV n = retval << 1; | |
2615 | if (!overflowed && (n >> 1) != retval) { | |
2616 | warn("Integer overflow in binary number"); | |
2617 | overflowed = TRUE; | |
2618 | } | |
2619 | retval = n | (*s++ - '0'); | |
2620 | len--; | |
2621 | } | |
6529e8bd | 2622 | if (len && (*s >= '2' && *s <= '9')) { |
4f19785b WSI |
2623 | dTHR; |
2624 | if (ckWARN(WARN_UNSAFE)) | |
399388f4 | 2625 | warner(WARN_UNSAFE, "Illegal binary digit '%c' ignored", *s); |
4f19785b WSI |
2626 | } |
2627 | *retlen = s - start; | |
2628 | return retval; | |
2629 | } | |
2630 | UV | |
864dbfa3 | 2631 | Perl_scan_oct(pTHX_ char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2632 | { |
2633 | register char *s = start; | |
55497cff | 2634 | register UV retval = 0; |
2635 | bool overflowed = FALSE; | |
fe14fcc3 | 2636 | |
748a9306 | 2637 | while (len && *s >= '0' && *s <= '7') { |
55497cff | 2638 | register UV n = retval << 3; |
2639 | if (!overflowed && (n >> 3) != retval) { | |
2640 | warn("Integer overflow in octal number"); | |
2641 | overflowed = TRUE; | |
2642 | } | |
2643 | retval = n | (*s++ - '0'); | |
748a9306 | 2644 | len--; |
fe14fcc3 | 2645 | } |
d008e5eb GS |
2646 | if (len && (*s == '8' || *s == '9')) { |
2647 | dTHR; | |
2648 | if (ckWARN(WARN_OCTAL)) | |
399388f4 | 2649 | warner(WARN_OCTAL, "Illegal octal digit '%c' ignored", *s); |
d008e5eb | 2650 | } |
fe14fcc3 LW |
2651 | *retlen = s - start; |
2652 | return retval; | |
2653 | } | |
2654 | ||
71be2cbc | 2655 | UV |
864dbfa3 | 2656 | Perl_scan_hex(pTHX_ char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2657 | { |
2658 | register char *s = start; | |
55497cff | 2659 | register UV retval = 0; |
2660 | bool overflowed = FALSE; | |
6ff81951 | 2661 | char *tmp = s; |
a0ed51b3 | 2662 | register UV n; |
fe14fcc3 | 2663 | |
a0ed51b3 LW |
2664 | while (len-- && *s) { |
2665 | tmp = strchr((char *) PL_hexdigit, *s++); | |
2666 | if (!tmp) { | |
e3fdf988 | 2667 | if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0)) |
a0ed51b3 LW |
2668 | continue; |
2669 | else { | |
d008e5eb | 2670 | dTHR; |
a0ed51b3 | 2671 | --s; |
599cee73 | 2672 | if (ckWARN(WARN_UNSAFE)) |
399388f4 | 2673 | warner(WARN_UNSAFE,"Illegal hex digit '%c' ignored", *s); |
a0ed51b3 LW |
2674 | break; |
2675 | } | |
2676 | } | |
2677 | n = retval << 4; | |
55497cff | 2678 | if (!overflowed && (n >> 4) != retval) { |
2679 | warn("Integer overflow in hex number"); | |
2680 | overflowed = TRUE; | |
2681 | } | |
3280af22 | 2682 | retval = n | ((tmp - PL_hexdigit) & 15); |
6ff81951 | 2683 | } |
fe14fcc3 LW |
2684 | *retlen = s - start; |
2685 | return retval; | |
2686 | } | |
760ac839 | 2687 | |
491527d0 | 2688 | char* |
864dbfa3 | 2689 | Perl_find_script(pTHX_ char *scriptname, bool dosearch, char **search_ext, I32 flags) |
491527d0 GS |
2690 | { |
2691 | dTHR; | |
2692 | char *xfound = Nullch; | |
2693 | char *xfailed = Nullch; | |
0f31cffe | 2694 | char tmpbuf[MAXPATHLEN]; |
491527d0 GS |
2695 | register char *s; |
2696 | I32 len; | |
2697 | int retval; | |
2698 | #if defined(DOSISH) && !defined(OS2) && !defined(atarist) | |
2699 | # define SEARCH_EXTS ".bat", ".cmd", NULL | |
2700 | # define MAX_EXT_LEN 4 | |
2701 | #endif | |
2702 | #ifdef OS2 | |
2703 | # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL | |
2704 | # define MAX_EXT_LEN 4 | |
2705 | #endif | |
2706 | #ifdef VMS | |
2707 | # define SEARCH_EXTS ".pl", ".com", NULL | |
2708 | # define MAX_EXT_LEN 4 | |
2709 | #endif | |
2710 | /* additional extensions to try in each dir if scriptname not found */ | |
2711 | #ifdef SEARCH_EXTS | |
2712 | char *exts[] = { SEARCH_EXTS }; | |
2713 | char **ext = search_ext ? search_ext : exts; | |
2714 | int extidx = 0, i = 0; | |
2715 | char *curext = Nullch; | |
2716 | #else | |
2717 | # define MAX_EXT_LEN 0 | |
2718 | #endif | |
2719 | ||
2720 | /* | |
2721 | * If dosearch is true and if scriptname does not contain path | |
2722 | * delimiters, search the PATH for scriptname. | |
2723 | * | |
2724 | * If SEARCH_EXTS is also defined, will look for each | |
2725 | * scriptname{SEARCH_EXTS} whenever scriptname is not found | |
2726 | * while searching the PATH. | |
2727 | * | |
2728 | * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search | |
2729 | * proceeds as follows: | |
2730 | * If DOSISH or VMSISH: | |
2731 | * + look for ./scriptname{,.foo,.bar} | |
2732 | * + search the PATH for scriptname{,.foo,.bar} | |
2733 | * | |
2734 | * If !DOSISH: | |
2735 | * + look *only* in the PATH for scriptname{,.foo,.bar} (note | |
2736 | * this will not look in '.' if it's not in the PATH) | |
2737 | */ | |
84486fc6 | 2738 | tmpbuf[0] = '\0'; |
491527d0 GS |
2739 | |
2740 | #ifdef VMS | |
2741 | # ifdef ALWAYS_DEFTYPES | |
2742 | len = strlen(scriptname); | |
2743 | if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { | |
2744 | int hasdir, idx = 0, deftypes = 1; | |
2745 | bool seen_dot = 1; | |
2746 | ||
2747 | hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ; | |
2748 | # else | |
2749 | if (dosearch) { | |
2750 | int hasdir, idx = 0, deftypes = 1; | |
2751 | bool seen_dot = 1; | |
2752 | ||
2753 | hasdir = (strpbrk(scriptname,":[</") != Nullch) ; | |
2754 | # endif | |
2755 | /* The first time through, just add SEARCH_EXTS to whatever we | |
2756 | * already have, so we can check for default file types. */ | |
2757 | while (deftypes || | |
84486fc6 | 2758 | (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
491527d0 GS |
2759 | { |
2760 | if (deftypes) { | |
2761 | deftypes = 0; | |
84486fc6 | 2762 | *tmpbuf = '\0'; |
491527d0 | 2763 | } |
84486fc6 GS |
2764 | if ((strlen(tmpbuf) + strlen(scriptname) |
2765 | + MAX_EXT_LEN) >= sizeof tmpbuf) | |
491527d0 | 2766 | continue; /* don't search dir with too-long name */ |
84486fc6 | 2767 | strcat(tmpbuf, scriptname); |
491527d0 GS |
2768 | #else /* !VMS */ |
2769 | ||
2770 | #ifdef DOSISH | |
2771 | if (strEQ(scriptname, "-")) | |
2772 | dosearch = 0; | |
2773 | if (dosearch) { /* Look in '.' first. */ | |
2774 | char *cur = scriptname; | |
2775 | #ifdef SEARCH_EXTS | |
2776 | if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ | |
2777 | while (ext[i]) | |
2778 | if (strEQ(ext[i++],curext)) { | |
2779 | extidx = -1; /* already has an ext */ | |
2780 | break; | |
2781 | } | |
2782 | do { | |
2783 | #endif | |
2784 | DEBUG_p(PerlIO_printf(Perl_debug_log, | |
2785 | "Looking for %s\n",cur)); | |
017f25f1 IZ |
2786 | if (PerlLIO_stat(cur,&PL_statbuf) >= 0 |
2787 | && !S_ISDIR(PL_statbuf.st_mode)) { | |
491527d0 GS |
2788 | dosearch = 0; |
2789 | scriptname = cur; | |
2790 | #ifdef SEARCH_EXTS | |
2791 | break; | |
2792 | #endif | |
2793 | } | |
2794 | #ifdef SEARCH_EXTS | |
2795 | if (cur == scriptname) { | |
2796 | len = strlen(scriptname); | |
84486fc6 | 2797 | if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
491527d0 | 2798 | break; |
84486fc6 | 2799 | cur = strcpy(tmpbuf, scriptname); |
491527d0 GS |
2800 | } |
2801 | } while (extidx >= 0 && ext[extidx] /* try an extension? */ | |
84486fc6 | 2802 | && strcpy(tmpbuf+len, ext[extidx++])); |
491527d0 GS |
2803 | #endif |
2804 | } | |
2805 | #endif | |
2806 | ||
2807 | if (dosearch && !strchr(scriptname, '/') | |
2808 | #ifdef DOSISH | |
2809 | && !strchr(scriptname, '\\') | |
2810 | #endif | |
2811 | && (s = PerlEnv_getenv("PATH"))) { | |
2812 | bool seen_dot = 0; | |
2813 | ||
3280af22 NIS |
2814 | PL_bufend = s + strlen(s); |
2815 | while (s < PL_bufend) { | |
491527d0 GS |
2816 | #if defined(atarist) || defined(DOSISH) |
2817 | for (len = 0; *s | |
2818 | # ifdef atarist | |
2819 | && *s != ',' | |
2820 | # endif | |
2821 | && *s != ';'; len++, s++) { | |
84486fc6 GS |
2822 | if (len < sizeof tmpbuf) |
2823 | tmpbuf[len] = *s; | |
491527d0 | 2824 | } |
84486fc6 GS |
2825 | if (len < sizeof tmpbuf) |
2826 | tmpbuf[len] = '\0'; | |
491527d0 | 2827 | #else /* ! (atarist || DOSISH) */ |
3280af22 | 2828 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend, |
491527d0 GS |
2829 | ':', |
2830 | &len); | |
2831 | #endif /* ! (atarist || DOSISH) */ | |
3280af22 | 2832 | if (s < PL_bufend) |
491527d0 | 2833 | s++; |
84486fc6 | 2834 | if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
491527d0 GS |
2835 | continue; /* don't search dir with too-long name */ |
2836 | if (len | |
61ae2fbf | 2837 | #if defined(atarist) || defined(__MINT__) || defined(DOSISH) |
84486fc6 GS |
2838 | && tmpbuf[len - 1] != '/' |
2839 | && tmpbuf[len - 1] != '\\' | |
491527d0 GS |
2840 | #endif |
2841 | ) | |
84486fc6 GS |
2842 | tmpbuf[len++] = '/'; |
2843 | if (len == 2 && tmpbuf[0] == '.') | |
491527d0 | 2844 | seen_dot = 1; |
84486fc6 | 2845 | (void)strcpy(tmpbuf + len, scriptname); |
491527d0 GS |
2846 | #endif /* !VMS */ |
2847 | ||
2848 | #ifdef SEARCH_EXTS | |
84486fc6 | 2849 | len = strlen(tmpbuf); |
491527d0 GS |
2850 | if (extidx > 0) /* reset after previous loop */ |
2851 | extidx = 0; | |
2852 | do { | |
2853 | #endif | |
84486fc6 | 2854 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
3280af22 | 2855 | retval = PerlLIO_stat(tmpbuf,&PL_statbuf); |
017f25f1 IZ |
2856 | if (S_ISDIR(PL_statbuf.st_mode)) { |
2857 | retval = -1; | |
2858 | } | |
491527d0 GS |
2859 | #ifdef SEARCH_EXTS |
2860 | } while ( retval < 0 /* not there */ | |
2861 | && extidx>=0 && ext[extidx] /* try an extension? */ | |
84486fc6 | 2862 | && strcpy(tmpbuf+len, ext[extidx++]) |
491527d0 GS |
2863 | ); |
2864 | #endif | |
2865 | if (retval < 0) | |
2866 | continue; | |
3280af22 NIS |
2867 | if (S_ISREG(PL_statbuf.st_mode) |
2868 | && cando(S_IRUSR,TRUE,&PL_statbuf) | |
491527d0 | 2869 | #ifndef DOSISH |
3280af22 | 2870 | && cando(S_IXUSR,TRUE,&PL_statbuf) |
491527d0 GS |
2871 | #endif |
2872 | ) | |
2873 | { | |
84486fc6 | 2874 | xfound = tmpbuf; /* bingo! */ |
491527d0 GS |
2875 | break; |
2876 | } | |
2877 | if (!xfailed) | |
84486fc6 | 2878 | xfailed = savepv(tmpbuf); |
491527d0 GS |
2879 | } |
2880 | #ifndef DOSISH | |
017f25f1 IZ |
2881 | if (!xfound && !seen_dot && !xfailed && |
2882 | (PerlLIO_stat(scriptname,&PL_statbuf) < 0 | |
2883 | || S_ISDIR(PL_statbuf.st_mode))) | |
491527d0 GS |
2884 | #endif |
2885 | seen_dot = 1; /* Disable message. */ | |
9ccb31f9 GS |
2886 | if (!xfound) { |
2887 | if (flags & 1) { /* do or die? */ | |
2888 | croak("Can't %s %s%s%s", | |
2889 | (xfailed ? "execute" : "find"), | |
2890 | (xfailed ? xfailed : scriptname), | |
2891 | (xfailed ? "" : " on PATH"), | |
2892 | (xfailed || seen_dot) ? "" : ", '.' not in PATH"); | |
2893 | } | |
2894 | scriptname = Nullch; | |
2895 | } | |
491527d0 GS |
2896 | if (xfailed) |
2897 | Safefree(xfailed); | |
2898 | scriptname = xfound; | |
2899 | } | |
9ccb31f9 | 2900 | return (scriptname ? savepv(scriptname) : Nullch); |
491527d0 GS |
2901 | } |
2902 | ||
2903 | ||
11343788 | 2904 | #ifdef USE_THREADS |
12ca11f6 MB |
2905 | #ifdef FAKE_THREADS |
2906 | /* Very simplistic scheduler for now */ | |
2907 | void | |
2908 | schedule(void) | |
2909 | { | |
c7848ba1 | 2910 | thr = thr->i.next_run; |
12ca11f6 MB |
2911 | } |
2912 | ||
2913 | void | |
864dbfa3 | 2914 | Perl_cond_init(pTHX_ perl_cond *cp) |
12ca11f6 MB |
2915 | { |
2916 | *cp = 0; | |
2917 | } | |
2918 | ||
2919 | void | |
864dbfa3 | 2920 | Perl_cond_signal(pTHX_ perl_cond *cp) |
12ca11f6 | 2921 | { |
51dd5992 | 2922 | perl_os_thread t; |
12ca11f6 MB |
2923 | perl_cond cond = *cp; |
2924 | ||
2925 | if (!cond) | |
2926 | return; | |
2927 | t = cond->thread; | |
2928 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
2929 | t->i.next_run = thr->i.next_run; |
2930 | thr->i.next_run->i.prev_run = t; | |
2931 | t->i.prev_run = thr; | |
2932 | thr->i.next_run = t; | |
2933 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
2934 | /* Remove from the wait queue */ |
2935 | *cp = cond->next; | |
2936 | Safefree(cond); | |
2937 | } | |
2938 | ||
2939 | void | |
864dbfa3 | 2940 | Perl_cond_broadcast(pTHX_ perl_cond *cp) |
12ca11f6 | 2941 | { |
51dd5992 | 2942 | perl_os_thread t; |
12ca11f6 MB |
2943 | perl_cond cond, cond_next; |
2944 | ||
2945 | for (cond = *cp; cond; cond = cond_next) { | |
2946 | t = cond->thread; | |
2947 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
2948 | t->i.next_run = thr->i.next_run; |
2949 | thr->i.next_run->i.prev_run = t; | |
2950 | t->i.prev_run = thr; | |
2951 | thr->i.next_run = t; | |
2952 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
2953 | /* Remove from the wait queue */ |
2954 | cond_next = cond->next; | |
2955 | Safefree(cond); | |
2956 | } | |
2957 | *cp = 0; | |
2958 | } | |
2959 | ||
2960 | void | |
864dbfa3 | 2961 | Perl_cond_wait(pTHX_ perl_cond *cp) |
12ca11f6 MB |
2962 | { |
2963 | perl_cond cond; | |
2964 | ||
c7848ba1 | 2965 | if (thr->i.next_run == thr) |
12ca11f6 MB |
2966 | croak("panic: perl_cond_wait called by last runnable thread"); |
2967 | ||
0f15f207 | 2968 | New(666, cond, 1, struct perl_wait_queue); |
12ca11f6 MB |
2969 | cond->thread = thr; |
2970 | cond->next = *cp; | |
2971 | *cp = cond; | |
c7848ba1 | 2972 | thr->i.wait_queue = cond; |
12ca11f6 | 2973 | /* Remove ourselves from runnable queue */ |
c7848ba1 MB |
2974 | thr->i.next_run->i.prev_run = thr->i.prev_run; |
2975 | thr->i.prev_run->i.next_run = thr->i.next_run; | |
12ca11f6 MB |
2976 | } |
2977 | #endif /* FAKE_THREADS */ | |
2978 | ||
0d85d877 | 2979 | #ifdef PTHREAD_GETSPECIFIC_INT |
52e1cb5e | 2980 | struct perl_thread * |
864dbfa3 | 2981 | Perl_getTHR(pTHX) |
11343788 MB |
2982 | { |
2983 | pthread_addr_t t; | |
2984 | ||
6b88bc9c | 2985 | if (pthread_getspecific(PL_thr_key, &t)) |
11343788 | 2986 | croak("panic: pthread_getspecific"); |
52e1cb5e | 2987 | return (struct perl_thread *) t; |
11343788 | 2988 | } |
0d85d877 | 2989 | #endif |
f93b4edd MB |
2990 | |
2991 | MAGIC * | |
864dbfa3 | 2992 | Perl_condpair_magic(pTHX_ SV *sv) |
f93b4edd MB |
2993 | { |
2994 | MAGIC *mg; | |
2995 | ||
2996 | SvUPGRADE(sv, SVt_PVMG); | |
2997 | mg = mg_find(sv, 'm'); | |
2998 | if (!mg) { | |
2999 | condpair_t *cp; | |
3000 | ||
3001 | New(53, cp, 1, condpair_t); | |
3002 | MUTEX_INIT(&cp->mutex); | |
3003 | COND_INIT(&cp->owner_cond); | |
3004 | COND_INIT(&cp->cond); | |
3005 | cp->owner = 0; | |
28a8573f | 3006 | MUTEX_LOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
f93b4edd MB |
3007 | mg = mg_find(sv, 'm'); |
3008 | if (mg) { | |
3009 | /* someone else beat us to initialising it */ | |
28a8573f | 3010 | MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
f93b4edd MB |
3011 | MUTEX_DESTROY(&cp->mutex); |
3012 | COND_DESTROY(&cp->owner_cond); | |
3013 | COND_DESTROY(&cp->cond); | |
3014 | Safefree(cp); | |
3015 | } | |
3016 | else { | |
3017 | sv_magic(sv, Nullsv, 'm', 0, 0); | |
3018 | mg = SvMAGIC(sv); | |
3019 | mg->mg_ptr = (char *)cp; | |
565764a8 | 3020 | mg->mg_len = sizeof(cp); |
28a8573f | 3021 | MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
8b73bbec | 3022 | DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(), |
c7848ba1 | 3023 | "%p: condpair_magic %p\n", thr, sv));) |
f93b4edd MB |
3024 | } |
3025 | } | |
3026 | return mg; | |
3027 | } | |
a863c7d1 MB |
3028 | |
3029 | /* | |
199100c8 MB |
3030 | * Make a new perl thread structure using t as a prototype. Some of the |
3031 | * fields for the new thread are copied from the prototype thread, t, | |
3032 | * so t should not be running in perl at the time this function is | |
3033 | * called. The use by ext/Thread/Thread.xs in core perl (where t is the | |
3034 | * thread calling new_struct_thread) clearly satisfies this constraint. | |
a863c7d1 | 3035 | */ |
52e1cb5e | 3036 | struct perl_thread * |
864dbfa3 | 3037 | Perl_new_struct_thread(pTHX_ struct perl_thread *t) |
a863c7d1 | 3038 | { |
52e1cb5e | 3039 | struct perl_thread *thr; |
a863c7d1 | 3040 | SV *sv; |
199100c8 MB |
3041 | SV **svp; |
3042 | I32 i; | |
3043 | ||
79cb57f6 | 3044 | sv = newSVpvn("", 0); |
52e1cb5e JH |
3045 | SvGROW(sv, sizeof(struct perl_thread) + 1); |
3046 | SvCUR_set(sv, sizeof(struct perl_thread)); | |
199100c8 | 3047 | thr = (Thread) SvPVX(sv); |
949ced2d | 3048 | #ifdef DEBUGGING |
52e1cb5e | 3049 | memset(thr, 0xab, sizeof(struct perl_thread)); |
533c011a NIS |
3050 | PL_markstack = 0; |
3051 | PL_scopestack = 0; | |
3052 | PL_savestack = 0; | |
3053 | PL_retstack = 0; | |
3054 | PL_dirty = 0; | |
3055 | PL_localizing = 0; | |
949ced2d GS |
3056 | Zero(&PL_hv_fetch_ent_mh, 1, HE); |
3057 | #else | |
3058 | Zero(thr, 1, struct perl_thread); | |
3059 | #endif | |
199100c8 | 3060 | |
312caa8e CS |
3061 | PL_protect = FUNC_NAME_TO_PTR(default_protect); |
3062 | ||
199100c8 | 3063 | thr->oursv = sv; |
d55594ae | 3064 | init_stacks(ARGS); |
a863c7d1 | 3065 | |
533c011a | 3066 | PL_curcop = &PL_compiling; |
199100c8 | 3067 | thr->cvcache = newHV(); |
54b9620d | 3068 | thr->threadsv = newAV(); |
a863c7d1 | 3069 | thr->specific = newAV(); |
79cb57f6 | 3070 | thr->errsv = newSVpvn("", 0); |
38a03e6e | 3071 | thr->errhv = newHV(); |
a863c7d1 MB |
3072 | thr->flags = THRf_R_JOINABLE; |
3073 | MUTEX_INIT(&thr->mutex); | |
199100c8 | 3074 | |
d55594ae GS |
3075 | /* top_env needs to be non-zero. It points to an area |
3076 | in which longjmp() stuff is stored, as C callstack | |
3077 | info there at least is thread specific this has to | |
3078 | be per-thread. Otherwise a 'die' in a thread gives | |
3079 | that thread the C stack of last thread to do an eval {}! | |
3080 | See comments in scope.h | |
3081 | Initialize top entry (as in perl.c for main thread) | |
3082 | */ | |
533c011a NIS |
3083 | PL_start_env.je_prev = NULL; |
3084 | PL_start_env.je_ret = -1; | |
3085 | PL_start_env.je_mustcatch = TRUE; | |
3086 | PL_top_env = &PL_start_env; | |
3087 | ||
faef0170 | 3088 | PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR) */ |
533c011a NIS |
3089 | PL_restartop = 0; |
3090 | ||
b099ddc0 GS |
3091 | PL_statname = NEWSV(66,0); |
3092 | PL_maxscream = -1; | |
3093 | PL_regcompp = FUNC_NAME_TO_PTR(pregcomp); | |
3094 | PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags); | |
3095 | PL_regindent = 0; | |
3096 | PL_reginterp_cnt = 0; | |
3097 | PL_lastscream = Nullsv; | |
3098 | PL_screamfirst = 0; | |
3099 | PL_screamnext = 0; | |
3100 | PL_reg_start_tmp = 0; | |
3101 | PL_reg_start_tmpl = 0; | |
3102 | ||
3103 | /* parent thread's data needs to be locked while we make copy */ | |
3104 | MUTEX_LOCK(&t->mutex); | |
3105 | ||
312caa8e CS |
3106 | PL_protect = t->Tprotect; |
3107 | ||
b099ddc0 GS |
3108 | PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */ |
3109 | PL_defstash = t->Tdefstash; /* XXX maybe these should */ | |
3110 | PL_curstash = t->Tcurstash; /* always be set to main? */ | |
3111 | ||
6b88bc9c | 3112 | PL_tainted = t->Ttainted; |
84fee439 NIS |
3113 | PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */ |
3114 | PL_nrs = newSVsv(t->Tnrs); | |
3115 | PL_rs = SvREFCNT_inc(PL_nrs); | |
3116 | PL_last_in_gv = Nullgv; | |
3117 | PL_ofslen = t->Tofslen; | |
3118 | PL_ofs = savepvn(t->Tofs, PL_ofslen); | |
3119 | PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv); | |
3120 | PL_chopset = t->Tchopset; | |
3121 | PL_formtarget = newSVsv(t->Tformtarget); | |
3122 | PL_bodytarget = newSVsv(t->Tbodytarget); | |
3123 | PL_toptarget = newSVsv(t->Ttoptarget); | |
533c011a | 3124 | |
54b9620d MB |
3125 | /* Initialise all per-thread SVs that the template thread used */ |
3126 | svp = AvARRAY(t->threadsv); | |
93965878 | 3127 | for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) { |
533c011a | 3128 | if (*svp && *svp != &PL_sv_undef) { |
199100c8 | 3129 | SV *sv = newSVsv(*svp); |
54b9620d | 3130 | av_store(thr->threadsv, i, sv); |
533c011a | 3131 | sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1); |
8b73bbec | 3132 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
54b9620d | 3133 | "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr)); |
199100c8 MB |
3134 | } |
3135 | } | |
940cb80d | 3136 | thr->threadsvp = AvARRAY(thr->threadsv); |
199100c8 | 3137 | |
533c011a NIS |
3138 | MUTEX_LOCK(&PL_threads_mutex); |
3139 | PL_nthreads++; | |
3140 | thr->tid = ++PL_threadnum; | |
199100c8 MB |
3141 | thr->next = t->next; |
3142 | thr->prev = t; | |
3143 | t->next = thr; | |
3144 | thr->next->prev = thr; | |
533c011a | 3145 | MUTEX_UNLOCK(&PL_threads_mutex); |
a863c7d1 | 3146 | |
b099ddc0 GS |
3147 | /* done copying parent's state */ |
3148 | MUTEX_UNLOCK(&t->mutex); | |
3149 | ||
a863c7d1 MB |
3150 | #ifdef HAVE_THREAD_INTERN |
3151 | init_thread_intern(thr); | |
a863c7d1 | 3152 | #endif /* HAVE_THREAD_INTERN */ |
a863c7d1 MB |
3153 | return thr; |
3154 | } | |
11343788 | 3155 | #endif /* USE_THREADS */ |
760ac839 LW |
3156 | |
3157 | #ifdef HUGE_VAL | |
3158 | /* | |
3159 | * This hack is to force load of "huge" support from libm.a | |
3160 | * So it is in perl for (say) POSIX to use. | |
3161 | * Needed for SunOS with Sun's 'acc' for example. | |
3162 | */ | |
3163 | double | |
8ac85365 | 3164 | Perl_huge(void) |
760ac839 LW |
3165 | { |
3166 | return HUGE_VAL; | |
3167 | } | |
3168 | #endif | |
4e35701f | 3169 | |
22239a37 NIS |
3170 | #ifdef PERL_GLOBAL_STRUCT |
3171 | struct perl_vars * | |
864dbfa3 | 3172 | Perl_GetVars(pTHX) |
22239a37 | 3173 | { |
533c011a | 3174 | return &PL_Vars; |
22239a37 | 3175 | } |
31fb1209 NIS |
3176 | #endif |
3177 | ||
3178 | char ** | |
864dbfa3 | 3179 | Perl_get_op_names(pTHX) |
31fb1209 | 3180 | { |
22c35a8c | 3181 | return PL_op_name; |
31fb1209 NIS |
3182 | } |
3183 | ||
3184 | char ** | |
864dbfa3 | 3185 | Perl_get_op_descs(pTHX) |
31fb1209 | 3186 | { |
22c35a8c | 3187 | return PL_op_desc; |
31fb1209 | 3188 | } |
9e6b2b00 GS |
3189 | |
3190 | char * | |
864dbfa3 | 3191 | Perl_get_no_modify(pTHX) |
9e6b2b00 | 3192 | { |
22c35a8c | 3193 | return (char*)PL_no_modify; |
9e6b2b00 GS |
3194 | } |
3195 | ||
3196 | U32 * | |
864dbfa3 | 3197 | Perl_get_opargs(pTHX) |
9e6b2b00 | 3198 | { |
22c35a8c | 3199 | return PL_opargs; |
9e6b2b00 | 3200 | } |
51aa15f3 | 3201 | |
a6c40364 GS |
3202 | #ifndef HAS_GETENV_LEN |
3203 | char * | |
864dbfa3 | 3204 | Perl_getenv_len(pTHX_ char *env_elem, unsigned long *len) |
a6c40364 GS |
3205 | { |
3206 | char *env_trans = PerlEnv_getenv(env_elem); | |
3207 | if (env_trans) | |
3208 | *len = strlen(env_trans); | |
3209 | return env_trans; | |
f675dbe5 CB |
3210 | } |
3211 | #endif | |
3212 | ||
dc9e4912 GS |
3213 | |
3214 | MGVTBL* | |
864dbfa3 | 3215 | Perl_get_vtbl(pTHX_ int vtbl_id) |
dc9e4912 GS |
3216 | { |
3217 | MGVTBL* result = Null(MGVTBL*); | |
3218 | ||
3219 | switch(vtbl_id) { | |
3220 | case want_vtbl_sv: | |
3221 | result = &PL_vtbl_sv; | |
3222 | break; | |
3223 | case want_vtbl_env: | |
3224 | result = &PL_vtbl_env; | |
3225 | break; | |
3226 | case want_vtbl_envelem: | |
3227 | result = &PL_vtbl_envelem; | |
3228 | break; | |
3229 | case want_vtbl_sig: | |
3230 | result = &PL_vtbl_sig; | |
3231 | break; | |
3232 | case want_vtbl_sigelem: | |
3233 | result = &PL_vtbl_sigelem; | |
3234 | break; | |
3235 | case want_vtbl_pack: | |
3236 | result = &PL_vtbl_pack; | |
3237 | break; | |
3238 | case want_vtbl_packelem: | |
3239 | result = &PL_vtbl_packelem; | |
3240 | break; | |
3241 | case want_vtbl_dbline: | |
3242 | result = &PL_vtbl_dbline; | |
3243 | break; | |
3244 | case want_vtbl_isa: | |
3245 | result = &PL_vtbl_isa; | |
3246 | break; | |
3247 | case want_vtbl_isaelem: | |
3248 | result = &PL_vtbl_isaelem; | |
3249 | break; | |
3250 | case want_vtbl_arylen: | |
3251 | result = &PL_vtbl_arylen; | |
3252 | break; | |
3253 | case want_vtbl_glob: | |
3254 | result = &PL_vtbl_glob; | |
3255 | break; | |
3256 | case want_vtbl_mglob: | |
3257 | result = &PL_vtbl_mglob; | |
3258 | break; | |
3259 | case want_vtbl_nkeys: | |
3260 | result = &PL_vtbl_nkeys; | |
3261 | break; | |
3262 | case want_vtbl_taint: | |
3263 | result = &PL_vtbl_taint; | |
3264 | break; | |
3265 | case want_vtbl_substr: | |
3266 | result = &PL_vtbl_substr; | |
3267 | break; | |
3268 | case want_vtbl_vec: | |
3269 | result = &PL_vtbl_vec; | |
3270 | break; | |
3271 | case want_vtbl_pos: | |
3272 | result = &PL_vtbl_pos; | |
3273 | break; | |
3274 | case want_vtbl_bm: | |
3275 | result = &PL_vtbl_bm; | |
3276 | break; | |
3277 | case want_vtbl_fm: | |
3278 | result = &PL_vtbl_fm; | |
3279 | break; | |
3280 | case want_vtbl_uvar: | |
3281 | result = &PL_vtbl_uvar; | |
3282 | break; | |
3283 | #ifdef USE_THREADS | |
3284 | case want_vtbl_mutex: | |
3285 | result = &PL_vtbl_mutex; | |
3286 | break; | |
3287 | #endif | |
3288 | case want_vtbl_defelem: | |
3289 | result = &PL_vtbl_defelem; | |
3290 | break; | |
3291 | case want_vtbl_regexp: | |
3292 | result = &PL_vtbl_regexp; | |
3293 | break; | |
3294 | case want_vtbl_regdata: | |
3295 | result = &PL_vtbl_regdata; | |
3296 | break; | |
3297 | case want_vtbl_regdatum: | |
3298 | result = &PL_vtbl_regdatum; | |
3299 | break; | |
3c90161d | 3300 | #ifdef USE_LOCALE_COLLATE |
dc9e4912 GS |
3301 | case want_vtbl_collxfrm: |
3302 | result = &PL_vtbl_collxfrm; | |
3303 | break; | |
3c90161d | 3304 | #endif |
dc9e4912 GS |
3305 | case want_vtbl_amagic: |
3306 | result = &PL_vtbl_amagic; | |
3307 | break; | |
3308 | case want_vtbl_amagicelem: | |
3309 | result = &PL_vtbl_amagicelem; | |
3310 | break; | |
810b8aa5 GS |
3311 | case want_vtbl_backref: |
3312 | result = &PL_vtbl_backref; | |
3313 | break; | |
dc9e4912 GS |
3314 | } |
3315 | return result; | |
3316 | } | |
3317 | ||
767df6a1 | 3318 | I32 |
864dbfa3 | 3319 | Perl_my_fflush_all(pTHX) |
767df6a1 JH |
3320 | { |
3321 | #ifdef FFLUSH_NULL | |
ce720889 | 3322 | return PerlIO_flush(NULL); |
767df6a1 JH |
3323 | #else |
3324 | long open_max = -1; | |
ed39a0f2 | 3325 | # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY) |
d2201af2 AD |
3326 | # ifdef PERL_FFLUSH_ALL_FOPEN_MAX |
3327 | open_max = PERL_FFLUSH_ALL_FOPEN_MAX; | |
3328 | # else | |
767df6a1 JH |
3329 | # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX) |
3330 | open_max = sysconf(_SC_OPEN_MAX); | |
3331 | # else | |
3332 | # ifdef FOPEN_MAX | |
d2201af2 | 3333 | open_max = FOPEN_MAX; |
767df6a1 JH |
3334 | # else |
3335 | # ifdef OPEN_MAX | |
d2201af2 | 3336 | open_max = OPEN_MAX; |
767df6a1 JH |
3337 | # else |
3338 | # ifdef _NFILE | |
d2201af2 | 3339 | open_max = _NFILE; |
767df6a1 JH |
3340 | # endif |
3341 | # endif | |
3342 | # endif | |
3343 | # endif | |
d2201af2 | 3344 | # endif |
767df6a1 JH |
3345 | if (open_max > 0) { |
3346 | long i; | |
3347 | for (i = 0; i < open_max; i++) | |
d2201af2 AD |
3348 | if (STDIO_STREAM_ARRAY[i]._file >= 0 && |
3349 | STDIO_STREAM_ARRAY[i]._file < open_max && | |
3350 | STDIO_STREAM_ARRAY[i]._flag) | |
3351 | PerlIO_flush(&STDIO_STREAM_ARRAY[i]); | |
767df6a1 JH |
3352 | return 0; |
3353 | } | |
3354 | # endif | |
3355 | SETERRNO(EBADF,RMS$_IFI); | |
3356 | return EOF; | |
3357 | #endif | |
3358 | } |