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