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