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