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