This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Start overhauling compiler. It was working at least minimally
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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"
8d063cd8 16#include "perl.h"
62b28dd9 17
6eb13c3b 18#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
a687059c 19#include <signal.h>
62b28dd9 20#endif
a687059c 21
36477c24 22#ifndef SIG_ERR
23# define SIG_ERR ((Sighandler_t) -1)
24#endif
25
bd4080b3 26/* XXX If this causes problems, set i_unistd=undef in the hint file. */
85e6fe83 27#ifdef I_UNISTD
8990e307
LW
28# include <unistd.h>
29#endif
30
a687059c
LW
31#ifdef I_VFORK
32# include <vfork.h>
33#endif
34
94b6baf5
AD
35/* Put this after #includes because fork and vfork prototypes may
36 conflict.
37*/
38#ifndef HAS_VFORK
39# define vfork fork
40#endif
41
fe14fcc3
LW
42#ifdef I_FCNTL
43# include <fcntl.h>
44#endif
45#ifdef I_SYS_FILE
46# include <sys/file.h>
47#endif
48
ff68c719 49#ifdef I_SYS_WAIT
50# include <sys/wait.h>
51#endif
52
8d063cd8 53#define FLUSH
8d063cd8 54
a0d0e21e
LW
55#ifdef LEAKTEST
56static void xstat _((void));
57#endif
58
a863c7d1
MB
59#ifdef USE_THREADS
60static U32 threadnum = 0;
61#endif /* USE_THREADS */
62
55497cff 63#ifndef MYMALLOC
de3bb511 64
8d063cd8
LW
65/* paranoid version of malloc */
66
a687059c
LW
67/* NOTE: Do not call the next three routines directly. Use the macros
68 * in handy.h, so that we can easily redefine everything to do tracking of
69 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 70 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c
LW
71 */
72
bd4080b3 73Malloc_t
f0f333f4 74safemalloc(MEM_SIZE size)
8d063cd8 75{
bd4080b3 76 Malloc_t ptr;
55497cff 77#ifdef HAS_64K_LIMIT
62b28dd9 78 if (size > 0xffff) {
760ac839 79 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
79072805 80 my_exit(1);
62b28dd9 81 }
55497cff 82#endif /* HAS_64K_LIMIT */
34de22dd
LW
83#ifdef DEBUGGING
84 if ((long)size < 0)
463ee0b2 85 croak("panic: malloc");
34de22dd 86#endif
8d063cd8 87 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
79072805 88#if !(defined(I286) || defined(atarist))
760ac839 89 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
79072805 90#else
760ac839 91 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
8d063cd8
LW
92#endif
93 if (ptr != Nullch)
94 return ptr;
7c0587c8
LW
95 else if (nomemok)
96 return Nullch;
8d063cd8 97 else {
760ac839 98 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
79072805 99 my_exit(1);
4e35701f 100 return Nullch;
8d063cd8
LW
101 }
102 /*NOTREACHED*/
103}
104
105/* paranoid version of realloc */
106
bd4080b3 107Malloc_t
f0f333f4 108saferealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 109{
bd4080b3 110 Malloc_t ptr;
ecfc5424 111#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
bd4080b3 112 Malloc_t realloc();
ecfc5424 113#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 114
55497cff 115#ifdef HAS_64K_LIMIT
5f05dabc 116 if (size > 0xffff) {
117 PerlIO_printf(PerlIO_stderr(),
118 "Reallocation too large: %lx\n", size) FLUSH;
119 my_exit(1);
120 }
55497cff 121#endif /* HAS_64K_LIMIT */
378cc40b 122 if (!where)
463ee0b2 123 croak("Null realloc");
34de22dd
LW
124#ifdef DEBUGGING
125 if ((long)size < 0)
463ee0b2 126 croak("panic: realloc");
34de22dd 127#endif
8d063cd8 128 ptr = realloc(where,size?size:1); /* realloc(0) is NASTY on our system */
79072805
LW
129
130#if !(defined(I286) || defined(atarist))
131 DEBUG_m( {
760ac839
LW
132 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
133 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
79072805
LW
134 } )
135#else
136 DEBUG_m( {
760ac839
LW
137 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
138 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
79072805 139 } )
8d063cd8 140#endif
79072805 141
8d063cd8
LW
142 if (ptr != Nullch)
143 return ptr;
7c0587c8
LW
144 else if (nomemok)
145 return Nullch;
8d063cd8 146 else {
760ac839 147 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
79072805 148 my_exit(1);
4e35701f 149 return Nullch;
8d063cd8
LW
150 }
151 /*NOTREACHED*/
152}
153
154/* safe version of free */
155
54310121 156Free_t
f0f333f4 157safefree(Malloc_t where)
8d063cd8 158{
79072805 159#if !(defined(I286) || defined(atarist))
f0f333f4 160 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
79072805 161#else
f0f333f4 162 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
8d063cd8 163#endif
378cc40b 164 if (where) {
de3bb511 165 /*SUPPRESS 701*/
378cc40b
LW
166 free(where);
167 }
8d063cd8
LW
168}
169
1050c9ca 170/* safe version of calloc */
171
bd4080b3 172Malloc_t
f0f333f4 173safecalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 174{
bd4080b3 175 Malloc_t ptr;
1050c9ca 176
55497cff 177#ifdef HAS_64K_LIMIT
5f05dabc 178 if (size * count > 0xffff) {
179 PerlIO_printf(PerlIO_stderr(),
180 "Allocation too large: %lx\n", size * count) FLUSH;
181 my_exit(1);
182 }
55497cff 183#endif /* HAS_64K_LIMIT */
1050c9ca 184#ifdef DEBUGGING
185 if ((long)size < 0 || (long)count < 0)
186 croak("panic: calloc");
187#endif
0b7c1c42
GS
188 size *= count;
189 ptr = malloc(size?size:1); /* malloc(0) is NASTY on our system */
1050c9ca 190#if !(defined(I286) || defined(atarist))
fb73857a 191 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 192#else
fb73857a 193 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
1050c9ca 194#endif
1050c9ca 195 if (ptr != Nullch) {
196 memset((void*)ptr, 0, size);
197 return ptr;
198 }
199 else if (nomemok)
200 return Nullch;
201 else {
760ac839 202 PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
1050c9ca 203 my_exit(1);
4e35701f 204 return Nullch;
1050c9ca 205 }
206 /*NOTREACHED*/
207}
208
55497cff 209#endif /* !MYMALLOC */
de3bb511 210
a687059c
LW
211#ifdef LEAKTEST
212
213#define ALIGN sizeof(long)
8d063cd8 214
bd4080b3 215Malloc_t
f0f333f4 216safexmalloc(I32 x, MEM_SIZE size)
8d063cd8 217{
bd4080b3 218 register Malloc_t where;
8d063cd8 219
a687059c
LW
220 where = safemalloc(size + ALIGN);
221 xcount[x]++;
222 where[0] = x % 100;
223 where[1] = x / 100;
224 return where + ALIGN;
8d063cd8 225}
8d063cd8 226
bd4080b3 227Malloc_t
f0f333f4 228safexrealloc(Malloc_t where, MEM_SIZE size)
a687059c 229{
bd4080b3 230 register Malloc_t new = saferealloc(where - ALIGN, size + ALIGN);
a0d0e21e 231 return new + ALIGN;
a687059c
LW
232}
233
234void
f0f333f4 235safexfree(Malloc_t where)
a687059c 236{
79072805 237 I32 x;
a687059c
LW
238
239 if (!where)
240 return;
241 where -= ALIGN;
242 x = where[0] + 100 * where[1];
243 xcount[x]--;
244 safefree(where);
245}
246
bd4080b3 247Malloc_t
f0f333f4 248safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
1050c9ca 249{
bd4080b3 250 register Malloc_t where;
1050c9ca 251
252 where = safexmalloc(x, size * count + ALIGN);
253 xcount[x]++;
254 memset((void*)where + ALIGN, 0, size * count);
255 where[0] = x % 100;
256 where[1] = x / 100;
257 return where + ALIGN;
258}
259
7c0587c8 260static void
f0f333f4 261xstat(void)
8d063cd8 262{
79072805 263 register I32 i;
8d063cd8 264
a687059c 265 for (i = 0; i < MAXXCOUNT; i++) {
de3bb511 266 if (xcount[i] > lastxcount[i]) {
760ac839 267 PerlIO_printf(PerlIO_stderr(),"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
a687059c 268 lastxcount[i] = xcount[i];
8d063cd8
LW
269 }
270 }
8d063cd8 271}
a687059c
LW
272
273#endif /* LEAKTEST */
8d063cd8
LW
274
275/* copy a string up to some (non-backslashed) delimiter, if any */
276
277char *
8ac85365 278delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
8d063cd8 279{
fc36a67e 280 register I32 tolen;
281 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b
LW
282 if (*from == '\\') {
283 if (from[1] == delim)
284 from++;
fc36a67e 285 else {
286 if (to < toend)
287 *to++ = *from;
288 tolen++;
289 from++;
290 }
378cc40b 291 }
bedebaa5 292 else if (*from == delim)
8d063cd8 293 break;
fc36a67e 294 if (to < toend)
295 *to++ = *from;
8d063cd8 296 }
bedebaa5
CS
297 if (to < toend)
298 *to = '\0';
fc36a67e 299 *retlen = tolen;
8d063cd8
LW
300 return from;
301}
302
303/* return ptr to little string in big string, NULL if not found */
378cc40b 304/* This routine was donated by Corey Satten. */
8d063cd8
LW
305
306char *
8ac85365 307instr(register char *big, register char *little)
378cc40b
LW
308{
309 register char *s, *x;
79072805 310 register I32 first;
378cc40b 311
a687059c
LW
312 if (!little)
313 return big;
314 first = *little++;
378cc40b
LW
315 if (!first)
316 return big;
317 while (*big) {
318 if (*big++ != first)
319 continue;
320 for (x=big,s=little; *s; /**/ ) {
321 if (!*x)
322 return Nullch;
323 if (*s++ != *x++) {
324 s--;
325 break;
326 }
327 }
328 if (!*s)
329 return big-1;
330 }
331 return Nullch;
332}
8d063cd8 333
a687059c
LW
334/* same as instr but allow embedded nulls */
335
336char *
8ac85365 337ninstr(register char *big, register char *bigend, char *little, char *lend)
8d063cd8 338{
a687059c 339 register char *s, *x;
79072805 340 register I32 first = *little;
a687059c 341 register char *littleend = lend;
378cc40b 342
a0d0e21e 343 if (!first && little >= littleend)
a687059c 344 return big;
de3bb511
LW
345 if (bigend - big < littleend - little)
346 return Nullch;
a687059c
LW
347 bigend -= littleend - little++;
348 while (big <= bigend) {
349 if (*big++ != first)
350 continue;
351 for (x=big,s=little; s < littleend; /**/ ) {
352 if (*s++ != *x++) {
353 s--;
354 break;
355 }
356 }
357 if (s >= littleend)
358 return big-1;
378cc40b 359 }
a687059c
LW
360 return Nullch;
361}
362
363/* reverse of the above--find last substring */
364
365char *
8ac85365 366rninstr(register char *big, char *bigend, char *little, char *lend)
a687059c
LW
367{
368 register char *bigbeg;
369 register char *s, *x;
79072805 370 register I32 first = *little;
a687059c
LW
371 register char *littleend = lend;
372
a0d0e21e 373 if (!first && little >= littleend)
a687059c
LW
374 return bigend;
375 bigbeg = big;
376 big = bigend - (littleend - little++);
377 while (big >= bigbeg) {
378 if (*big-- != first)
379 continue;
380 for (x=big+2,s=little; s < littleend; /**/ ) {
381 if (*s++ != *x++) {
382 s--;
383 break;
384 }
385 }
386 if (s >= littleend)
387 return big+1;
378cc40b 388 }
a687059c 389 return Nullch;
378cc40b 390}
a687059c 391
bbce6d69 392/*
393 * Set up for a new ctype locale.
394 */
55497cff 395void
8ac85365 396perl_new_ctype(char *newctype)
ef7eada9 397{
36477c24 398#ifdef USE_LOCALE_CTYPE
399
bbce6d69 400 int i;
ef7eada9 401
bbce6d69 402 for (i = 0; i < 256; i++) {
403 if (isUPPER_LC(i))
404 fold_locale[i] = toLOWER_LC(i);
405 else if (isLOWER_LC(i))
406 fold_locale[i] = toUPPER_LC(i);
407 else
408 fold_locale[i] = i;
409 }
bbce6d69 410
36477c24 411#endif /* USE_LOCALE_CTYPE */
412}
bbce6d69 413
414/*
415 * Set up for a new collation locale.
416 */
417void
8ac85365 418perl_new_collate(char *newcoll)
bbce6d69 419{
36477c24 420#ifdef USE_LOCALE_COLLATE
421
bbce6d69 422 if (! newcoll) {
423 if (collation_name) {
424 ++collation_ix;
425 Safefree(collation_name);
426 collation_name = NULL;
427 collation_standard = TRUE;
bbce6d69 428 collxfrm_base = 0;
429 collxfrm_mult = 2;
bbce6d69 430 }
431 return;
432 }
433
434 if (! collation_name || strNE(collation_name, newcoll)) {
435 ++collation_ix;
436 Safefree(collation_name);
437 collation_name = savepv(newcoll);
ff68c719 438 collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
bbce6d69 439
bbce6d69 440 {
441 /* 2: at most so many chars ('a', 'b'). */
442 /* 50: surely no system expands a char more. */
443#define XFRMBUFSIZE (2 * 50)
444 char xbuf[XFRMBUFSIZE];
445 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
446 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
447 SSize_t mult = fb - fa;
448 if (mult < 1)
449 croak("strxfrm() gets absurd");
450 collxfrm_base = (fa > mult) ? (fa - mult) : 0;
451 collxfrm_mult = mult;
452 }
bbce6d69 453 }
bbce6d69 454
36477c24 455#endif /* USE_LOCALE_COLLATE */
456}
bbce6d69 457
458/*
459 * Set up for a new numeric locale.
460 */
461void
8ac85365 462perl_new_numeric(char *newnum)
bbce6d69 463{
36477c24 464#ifdef USE_LOCALE_NUMERIC
465
bbce6d69 466 if (! newnum) {
467 if (numeric_name) {
468 Safefree(numeric_name);
469 numeric_name = NULL;
470 numeric_standard = TRUE;
471 numeric_local = TRUE;
472 }
473 return;
474 }
475
476 if (! numeric_name || strNE(numeric_name, newnum)) {
477 Safefree(numeric_name);
478 numeric_name = savepv(newnum);
ff68c719 479 numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
bbce6d69 480 numeric_local = TRUE;
481 }
36477c24 482
483#endif /* USE_LOCALE_NUMERIC */
bbce6d69 484}
485
486void
8ac85365 487perl_set_numeric_standard(void)
bbce6d69 488{
5f05dabc 489#ifdef USE_LOCALE_NUMERIC
490
bbce6d69 491 if (! numeric_standard) {
492 setlocale(LC_NUMERIC, "C");
493 numeric_standard = TRUE;
494 numeric_local = FALSE;
495 }
5f05dabc 496
497#endif /* USE_LOCALE_NUMERIC */
bbce6d69 498}
499
500void
8ac85365 501perl_set_numeric_local(void)
bbce6d69 502{
5f05dabc 503#ifdef USE_LOCALE_NUMERIC
504
bbce6d69 505 if (! numeric_local) {
506 setlocale(LC_NUMERIC, numeric_name);
507 numeric_standard = FALSE;
508 numeric_local = TRUE;
509 }
bbce6d69 510
36477c24 511#endif /* USE_LOCALE_NUMERIC */
5f05dabc 512}
36477c24 513
bbce6d69 514
36477c24 515/*
516 * Initialize locale awareness.
517 */
f0c5b223 518int
8ac85365 519perl_init_i18nl10n(int printwarn)
f0c5b223
TB
520{
521 int ok = 1;
522 /* returns
523 * 1 = set ok or not applicable,
524 * 0 = fallback to C locale,
525 * -1 = fallback to C locale failed
526 */
bbce6d69 527
36477c24 528#ifdef USE_LOCALE
bbce6d69 529
36477c24 530#ifdef USE_LOCALE_CTYPE
bbce6d69 531 char *curctype = NULL;
36477c24 532#endif /* USE_LOCALE_CTYPE */
533#ifdef USE_LOCALE_COLLATE
bbce6d69 534 char *curcoll = NULL;
36477c24 535#endif /* USE_LOCALE_COLLATE */
536#ifdef USE_LOCALE_NUMERIC
bbce6d69 537 char *curnum = NULL;
36477c24 538#endif /* USE_LOCALE_NUMERIC */
02b32252 539 char *lc_all = getenv("LC_ALL");
5f05dabc 540 char *lang = getenv("LANG");
bbce6d69 541 bool setlocale_failure = FALSE;
f0c5b223 542
02b32252
CS
543#ifdef LOCALE_ENVIRON_REQUIRED
544
545 /*
546 * Ultrix setlocale(..., "") fails if there are no environment
547 * variables from which to get a locale name.
548 */
549
550 bool done = FALSE;
551
552#ifdef LC_ALL
553 if (lang) {
554 if (setlocale(LC_ALL, ""))
555 done = TRUE;
556 else
557 setlocale_failure = TRUE;
558 }
559 if (!setlocale_failure)
560#endif /* LC_ALL */
561 {
562#ifdef USE_LOCALE_CTYPE
563 if (! (curctype = setlocale(LC_CTYPE,
564 (!done && (lang || getenv("LC_CTYPE")))
565 ? "" : Nullch)))
566 setlocale_failure = TRUE;
567#endif /* USE_LOCALE_CTYPE */
568#ifdef USE_LOCALE_COLLATE
569 if (! (curcoll = setlocale(LC_COLLATE,
570 (!done && (lang || getenv("LC_COLLATE")))
571 ? "" : Nullch)))
572 setlocale_failure = TRUE;
573#endif /* USE_LOCALE_COLLATE */
574#ifdef USE_LOCALE_NUMERIC
575 if (! (curnum = setlocale(LC_NUMERIC,
576 (!done && (lang || getenv("LC_NUMERIC")))
577 ? "" : Nullch)))
578 setlocale_failure = TRUE;
579#endif /* USE_LOCALE_NUMERIC */
580 }
581
582#else /* !LOCALE_ENVIRON_REQUIRED */
583
bbce6d69 584#ifdef LC_ALL
5f05dabc 585
bbce6d69 586 if (! setlocale(LC_ALL, ""))
587 setlocale_failure = TRUE;
5f05dabc 588 else {
589#ifdef USE_LOCALE_CTYPE
590 curctype = setlocale(LC_CTYPE, Nullch);
591#endif /* USE_LOCALE_CTYPE */
592#ifdef USE_LOCALE_COLLATE
593 curcoll = setlocale(LC_COLLATE, Nullch);
594#endif /* USE_LOCALE_COLLATE */
595#ifdef USE_LOCALE_NUMERIC
596 curnum = setlocale(LC_NUMERIC, Nullch);
597#endif /* USE_LOCALE_NUMERIC */
598 }
599
600#else /* !LC_ALL */
bbce6d69 601
36477c24 602#ifdef USE_LOCALE_CTYPE
5f05dabc 603 if (! (curctype = setlocale(LC_CTYPE, "")))
bbce6d69 604 setlocale_failure = TRUE;
36477c24 605#endif /* USE_LOCALE_CTYPE */
606#ifdef USE_LOCALE_COLLATE
5f05dabc 607 if (! (curcoll = setlocale(LC_COLLATE, "")))
bbce6d69 608 setlocale_failure = TRUE;
36477c24 609#endif /* USE_LOCALE_COLLATE */
610#ifdef USE_LOCALE_NUMERIC
5f05dabc 611 if (! (curnum = setlocale(LC_NUMERIC, "")))
bbce6d69 612 setlocale_failure = TRUE;
36477c24 613#endif /* USE_LOCALE_NUMERIC */
bbce6d69 614
5f05dabc 615#endif /* LC_ALL */
616
02b32252
CS
617#endif /* !LOCALE_ENVIRON_REQUIRED */
618
5f05dabc 619 if (setlocale_failure) {
620 char *p;
621 bool locwarn = (printwarn > 1 ||
622 printwarn &&
623 (!(p = getenv("PERL_BADLANG")) || atoi(p)));
20cec16a 624
5f05dabc 625 if (locwarn) {
626#ifdef LC_ALL
627
628 PerlIO_printf(PerlIO_stderr(),
629 "perl: warning: Setting locale failed.\n");
630
631#else /* !LC_ALL */
632
ef7eada9 633 PerlIO_printf(PerlIO_stderr(),
bbce6d69 634 "perl: warning: Setting locale failed for the categories:\n\t");
36477c24 635#ifdef USE_LOCALE_CTYPE
bbce6d69 636 if (! curctype)
5f05dabc 637 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
36477c24 638#endif /* USE_LOCALE_CTYPE */
639#ifdef USE_LOCALE_COLLATE
bbce6d69 640 if (! curcoll)
5f05dabc 641 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
36477c24 642#endif /* USE_LOCALE_COLLATE */
643#ifdef USE_LOCALE_NUMERIC
bbce6d69 644 if (! curnum)
5f05dabc 645 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
36477c24 646#endif /* USE_LOCALE_NUMERIC */
bbce6d69 647 PerlIO_printf(PerlIO_stderr(), "\n");
648
5f05dabc 649#endif /* LC_ALL */
650
760ac839 651 PerlIO_printf(PerlIO_stderr(),
bbce6d69 652 "perl: warning: Please check that your locale settings:\n");
ef7eada9
JH
653
654 PerlIO_printf(PerlIO_stderr(),
bbce6d69 655 "\tLC_ALL = %c%s%c,\n",
656 lc_all ? '"' : '(',
657 lc_all ? lc_all : "unset",
658 lc_all ? '"' : ')');
5f05dabc 659
660 {
661 char **e;
662 for (e = environ; *e; e++) {
663 if (strnEQ(*e, "LC_", 3)
664 && strnNE(*e, "LC_ALL=", 7)
665 && (p = strchr(*e, '=')))
666 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
fb73857a 667 (int)(p - *e), *e, p + 1);
5f05dabc 668 }
669 }
670
ef7eada9 671 PerlIO_printf(PerlIO_stderr(),
bbce6d69 672 "\tLANG = %c%s%c\n",
5f05dabc 673 lang ? '"' : '(',
bbce6d69 674 lang ? lang : "unset",
675 lang ? '"' : ')');
ef7eada9 676
bbce6d69 677 PerlIO_printf(PerlIO_stderr(),
678 " are supported and installed on your system.\n");
5f05dabc 679 }
ef7eada9 680
5f05dabc 681#ifdef LC_ALL
682
683 if (setlocale(LC_ALL, "C")) {
684 if (locwarn)
685 PerlIO_printf(PerlIO_stderr(),
686 "perl: warning: Falling back to the standard locale (\"C\").\n");
bbce6d69 687 ok = 0;
ef7eada9 688 }
5f05dabc 689 else {
690 if (locwarn)
691 PerlIO_printf(PerlIO_stderr(),
692 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
693 ok = -1;
694 }
bbce6d69 695
5f05dabc 696#else /* ! LC_ALL */
697
698 if (0
36477c24 699#ifdef USE_LOCALE_CTYPE
5f05dabc 700 || !(curctype || setlocale(LC_CTYPE, "C"))
36477c24 701#endif /* USE_LOCALE_CTYPE */
702#ifdef USE_LOCALE_COLLATE
5f05dabc 703 || !(curcoll || setlocale(LC_COLLATE, "C"))
36477c24 704#endif /* USE_LOCALE_COLLATE */
705#ifdef USE_LOCALE_NUMERIC
5f05dabc 706 || !(curnum || setlocale(LC_NUMERIC, "C"))
36477c24 707#endif /* USE_LOCALE_NUMERIC */
5f05dabc 708 )
709 {
710 if (locwarn)
bbce6d69 711 PerlIO_printf(PerlIO_stderr(),
5f05dabc 712 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
713 ok = -1;
bbce6d69 714 }
5f05dabc 715
bbce6d69 716#endif /* ! LC_ALL */
5f05dabc 717
718#ifdef USE_LOCALE_CTYPE
719 curctype = setlocale(LC_CTYPE, Nullch);
720#endif /* USE_LOCALE_CTYPE */
721#ifdef USE_LOCALE_COLLATE
722 curcoll = setlocale(LC_COLLATE, Nullch);
723#endif /* USE_LOCALE_COLLATE */
724#ifdef USE_LOCALE_NUMERIC
725 curnum = setlocale(LC_NUMERIC, Nullch);
726#endif /* USE_LOCALE_NUMERIC */
ef7eada9
JH
727 }
728
36477c24 729#ifdef USE_LOCALE_CTYPE
bbce6d69 730 perl_new_ctype(curctype);
36477c24 731#endif /* USE_LOCALE_CTYPE */
bbce6d69 732
36477c24 733#ifdef USE_LOCALE_COLLATE
bbce6d69 734 perl_new_collate(curcoll);
36477c24 735#endif /* USE_LOCALE_COLLATE */
bbce6d69 736
36477c24 737#ifdef USE_LOCALE_NUMERIC
bbce6d69 738 perl_new_numeric(curnum);
36477c24 739#endif /* USE_LOCALE_NUMERIC */
ef7eada9 740
36477c24 741#endif /* USE_LOCALE */
ef7eada9 742
f0c5b223
TB
743 return ok;
744}
745
bbce6d69 746/* Backwards compatibility. */
747int
8ac85365 748perl_init_i18nl14n(int printwarn)
bbce6d69 749{
5f05dabc 750 return perl_init_i18nl10n(printwarn);
bbce6d69 751}
ef7eada9 752
36477c24 753#ifdef USE_LOCALE_COLLATE
ef7eada9 754
bbce6d69 755/*
756 * mem_collxfrm() is a bit like strxfrm() but with two important
757 * differences. First, it handles embedded NULs. Second, it allocates
758 * a bit more memory than needed for the transformed data itself.
759 * The real transformed data begins at offset sizeof(collationix).
760 * Please see sv_collxfrm() to see how this is used.
761 */
762char *
8ac85365 763mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
bbce6d69 764{
765 char *xbuf;
766 STRLEN xalloc, xin, xout;
767
768 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
769 /* the +1 is for the terminating NUL. */
770
771 xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
772 New(171, xbuf, xalloc, char);
773 if (! xbuf)
774 goto bad;
775
776 *(U32*)xbuf = collation_ix;
777 xout = sizeof(collation_ix);
778 for (xin = 0; xin < len; ) {
779 SSize_t xused;
780
781 for (;;) {
782 xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
783 if (xused == -1)
784 goto bad;
785 if (xused < xalloc - xout)
786 break;
787 xalloc = (2 * xalloc) + 1;
788 Renew(xbuf, xalloc, char);
789 if (! xbuf)
790 goto bad;
791 }
ef7eada9 792
bbce6d69 793 xin += strlen(s + xin) + 1;
794 xout += xused;
795
796 /* Embedded NULs are understood but silently skipped
797 * because they make no sense in locale collation. */
798 }
ef7eada9 799
bbce6d69 800 xbuf[xout] = '\0';
801 *xlen = xout - sizeof(collation_ix);
802 return xbuf;
803
804 bad:
805 Safefree(xbuf);
806 *xlen = 0;
807 return NULL;
ef7eada9
JH
808}
809
36477c24 810#endif /* USE_LOCALE_COLLATE */
bbce6d69 811
378cc40b 812void
8ac85365 813fbm_compile(SV *sv)
378cc40b 814{
a687059c
LW
815 register unsigned char *s;
816 register unsigned char *table;
79072805
LW
817 register U32 i;
818 register U32 len = SvCUR(sv);
819 I32 rarest = 0;
820 U32 frequency = 256;
821
c277df42
IZ
822 sv_upgrade(sv, SVt_PVBM);
823 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
748a9306 824 return; /* can't have offsets that big */
02128f11
IZ
825 if (len > 2) {
826 Sv_Grow(sv,len + 258);
827 table = (unsigned char*)(SvPVX(sv) + len + 1);
828 s = table - 2;
829 for (i = 0; i < 256; i++) {
830 table[i] = len;
831 }
832 i = 0;
833 while (s >= (unsigned char*)(SvPVX(sv)))
834 {
835 if (table[*s] == len)
836 table[*s] = i;
837 s--,i++;
838 }
378cc40b 839 }
bbce6d69 840 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
79072805 841 SvVALID_on(sv);
378cc40b 842
463ee0b2 843 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
bbce6d69 844 for (i = 0; i < len; i++) {
845 if (freq[s[i]] < frequency) {
846 rarest = i;
847 frequency = freq[s[i]];
378cc40b
LW
848 }
849 }
79072805
LW
850 BmRARE(sv) = s[rarest];
851 BmPREVIOUS(sv) = rarest;
760ac839 852 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
853}
854
378cc40b 855char *
8ac85365 856fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
378cc40b 857{
a687059c 858 register unsigned char *s;
79072805
LW
859 register I32 tmp;
860 register I32 littlelen;
a687059c
LW
861 register unsigned char *little;
862 register unsigned char *table;
863 register unsigned char *olds;
864 register unsigned char *oldlittle;
378cc40b 865
79072805 866 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
a0d0e21e
LW
867 STRLEN len;
868 char *l = SvPV(littlestr,len);
c277df42 869 if (!len) {
02128f11
IZ
870 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
871 substr => we can ignore SvVALID */
872 if (multiline) {
873 char *t = "\n";
874 if ((s = ninstr((char*)big,(char*)bigend, t, t + len)))
875 return s;
876 }
c277df42 877 if (bigend > big && bigend[-1] == '\n')
161b471a 878 return (char *)(bigend - 1);
c277df42 879 else
161b471a 880 return (char *) bigend;
c277df42 881 }
d48672a2 882 return (char*)big;
c277df42 883 }
a0d0e21e 884 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 885 }
378cc40b 886
79072805
LW
887 littlelen = SvCUR(littlestr);
888 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
0f85fab0
LW
889 if (littlelen > bigend - big)
890 return Nullch;
463ee0b2 891 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 892 s = bigend - littlelen;
02128f11
IZ
893 if (s > big
894 && bigend[-1] == '\n'
895 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
896 return (char*)s - 1; /* how sweet it is */
897 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 898 return (char*)s; /* how sweet it is */
02128f11
IZ
899 return Nullch;
900 }
901 if (littlelen <= 2) {
902 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
903 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
904 /* This may do extra comparisons if littlelen == 2, but this
905 should be hidden in the noise since we do less indirection. */
906
907 s = big;
908 bigend -= littlelen;
909 while (s <= bigend) {
910 if (s[0] == c1
911 && (littlelen == 1 || s[1] == c2)
912 && (!SvTAIL(littlestr)
913 || s == bigend
914 || s[littlelen] == '\n')) /* Automatically multiline */
915 return s;
916 s++;
a687059c 917 }
bbce6d69 918 return Nullch;
a687059c 919 }
463ee0b2 920 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9
LW
921 if (--littlelen >= bigend - big)
922 return Nullch;
923 s = big + littlelen;
a687059c 924 oldlittle = little = table - 2;
bbce6d69 925 if (s < bigend) {
926 top2:
927 /*SUPPRESS 560*/
928 if (tmp = table[*s]) {
62b28dd9 929#ifdef POINTERRIGOR
bbce6d69 930 if (bigend - s > tmp) {
931 s += tmp;
932 goto top2;
933 }
62b28dd9 934#else
bbce6d69 935 if ((s += tmp) < bigend)
936 goto top2;
62b28dd9 937#endif
bbce6d69 938 return Nullch;
a687059c 939 }
bbce6d69 940 else {
941 tmp = littlelen; /* less expensive than calling strncmp() */
942 olds = s;
943 while (tmp--) {
944 if (*--s == *--little)
945 continue;
c277df42 946 differ:
bbce6d69 947 s = olds + 1; /* here we pay the price for failure */
948 little = oldlittle;
949 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 950 goto top2;
62b28dd9 951 return Nullch;
a687059c 952 }
c277df42
IZ
953 if (SvTAIL(littlestr) /* automatically multiline */
954 && olds + 1 != bigend
955 && olds[1] != '\n')
956 goto differ;
bbce6d69 957 return (char *)s;
378cc40b
LW
958 }
959 }
960 return Nullch;
961}
962
c277df42
IZ
963/* start_shift, end_shift are positive quantities which give offsets
964 of ends of some substring of bigstr.
965 If `last' we want the last occurence.
966 old_posp is the way of communication between consequent calls if
967 the next call needs to find the .
968 The initial *old_posp should be -1.
969 Note that we do not take into account SvTAIL, so it may give wrong
970 positives if _ALL flag is set.
971 */
972
378cc40b 973char *
c277df42 974screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 975{
a687059c
LW
976 register unsigned char *s, *x;
977 register unsigned char *big;
79072805
LW
978 register I32 pos;
979 register I32 previous;
980 register I32 first;
a687059c 981 register unsigned char *little;
c277df42 982 register I32 stop_pos;
a687059c 983 register unsigned char *littleend;
c277df42 984 I32 found = 0;
378cc40b 985
c277df42
IZ
986 if (*old_posp == -1
987 ? (pos = screamfirst[BmRARE(littlestr)]) < 0
988 : (((pos = *old_posp), pos += screamnext[pos]) == 0))
378cc40b 989 return Nullch;
463ee0b2 990 little = (unsigned char *)(SvPVX(littlestr));
79072805 991 littleend = little + SvCUR(littlestr);
378cc40b 992 first = *little++;
c277df42 993 /* The value of pos we can start at: */
79072805 994 previous = BmPREVIOUS(littlestr);
463ee0b2 995 big = (unsigned char *)(SvPVX(bigstr));
c277df42
IZ
996 /* The value of pos we can stop at: */
997 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
998 if (previous + start_shift > stop_pos) return Nullch;
999 while (pos < previous + start_shift) {
378cc40b
LW
1000 if (!(pos += screamnext[pos]))
1001 return Nullch;
1002 }
de3bb511 1003#ifdef POINTERRIGOR
bbce6d69 1004 do {
c277df42 1005 if (pos >= stop_pos) return Nullch;
bbce6d69 1006 if (big[pos-previous] != first)
1007 continue;
1008 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
bbce6d69 1009 if (*s++ != *x++) {
1010 s--;
1011 break;
de3bb511 1012 }
bbce6d69 1013 }
c277df42
IZ
1014 if (s == littleend) {
1015 *old_posp = pos;
1016 if (!last) return (char *)(big+pos-previous);
1017 found = 1;
1018 }
bbce6d69 1019 } while ( pos += screamnext[pos] );
c277df42 1020 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
de3bb511
LW
1021#else /* !POINTERRIGOR */
1022 big -= previous;
bbce6d69 1023 do {
c277df42 1024 if (pos >= stop_pos) return Nullch;
bbce6d69 1025 if (big[pos] != first)
1026 continue;
1027 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69 1028 if (*s++ != *x++) {
1029 s--;
1030 break;
378cc40b 1031 }
bbce6d69 1032 }
c277df42
IZ
1033 if (s == littleend) {
1034 *old_posp = pos;
1035 if (!last) return (char *)(big+pos);
1036 found = 1;
1037 }
bbce6d69 1038 } while ( pos += screamnext[pos] );
c277df42 1039 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
de3bb511 1040#endif /* POINTERRIGOR */
8d063cd8
LW
1041}
1042
79072805 1043I32
8ac85365 1044ibcmp(char *s1, char *s2, register I32 len)
79072805 1045{
bbce6d69 1046 register U8 *a = (U8 *)s1;
1047 register U8 *b = (U8 *)s2;
79072805 1048 while (len--) {
bbce6d69 1049 if (*a != *b && *a != fold[*b])
1050 return 1;
1051 a++,b++;
1052 }
1053 return 0;
1054}
1055
1056I32
8ac85365 1057ibcmp_locale(char *s1, char *s2, register I32 len)
bbce6d69 1058{
1059 register U8 *a = (U8 *)s1;
1060 register U8 *b = (U8 *)s2;
1061 while (len--) {
1062 if (*a != *b && *a != fold_locale[*b])
1063 return 1;
1064 a++,b++;
79072805
LW
1065 }
1066 return 0;
1067}
1068
8d063cd8
LW
1069/* copy a string to a safe spot */
1070
1071char *
8ac85365 1072savepv(char *sv)
8d063cd8 1073{
a687059c 1074 register char *newaddr;
8d063cd8 1075
79072805
LW
1076 New(902,newaddr,strlen(sv)+1,char);
1077 (void)strcpy(newaddr,sv);
8d063cd8
LW
1078 return newaddr;
1079}
1080
a687059c
LW
1081/* same thing but with a known length */
1082
1083char *
8ac85365 1084savepvn(char *sv, register I32 len)
a687059c
LW
1085{
1086 register char *newaddr;
1087
1088 New(903,newaddr,len+1,char);
79072805 1089 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c
LW
1090 newaddr[len] = '\0'; /* is now */
1091 return newaddr;
1092}
1093
fc36a67e 1094/* the SV for form() and mess() is not kept in an arena */
1095
1096static SV *
8ac85365 1097mess_alloc(void)
fc36a67e 1098{
1099 SV *sv;
1100 XPVMG *any;
1101
1102 /* Create as PVMG now, to avoid any upgrading later */
1103 New(905, sv, 1, SV);
1104 Newz(905, any, 1, XPVMG);
1105 SvFLAGS(sv) = SVt_PVMG;
1106 SvANY(sv) = (void*)any;
1107 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1108 return sv;
1109}
1110
a0d0e21e 1111#ifdef I_STDARG
8990e307 1112char *
46fc3d4c 1113form(const char* pat, ...)
a687059c
LW
1114#else
1115/*VARARGS0*/
de3bb511 1116char *
46fc3d4c 1117form(pat, va_alist)
71be2cbc 1118 const char *pat;
46fc3d4c 1119 va_dcl
8990e307
LW
1120#endif
1121{
46fc3d4c 1122 va_list args;
1123#ifdef I_STDARG
1124 va_start(args, pat);
a687059c 1125#else
46fc3d4c 1126 va_start(args);
d48672a2 1127#endif
fc36a67e 1128 if (!mess_sv)
1129 mess_sv = mess_alloc();
1130 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1131 va_end(args);
fc36a67e 1132 return SvPVX(mess_sv);
46fc3d4c 1133}
a687059c 1134
46fc3d4c 1135char *
8ac85365 1136mess(const char *pat, va_list *args)
46fc3d4c 1137{
1138 SV *sv;
1139 static char dgd[] = " during global destruction.\n";
1140
46fc3d4c 1141 if (!mess_sv)
fc36a67e 1142 mess_sv = mess_alloc();
46fc3d4c 1143 sv = mess_sv;
fc36a67e 1144 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1145 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1146 dTHR;
2304df62 1147 if (dirty)
46fc3d4c 1148 sv_catpv(sv, dgd);
2304df62 1149 else {
46fc3d4c 1150 if (curcop->cop_line)
fc36a67e 1151 sv_catpvf(sv, " at %_ line %ld",
46fc3d4c 1152 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
c07a80fd 1153 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1154 bool line_mode = (RsSIMPLE(rs) &&
1155 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
46fc3d4c 1156 sv_catpvf(sv, ", <%s> %s %ld",
1157 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1158 line_mode ? "line" : "chunk",
1159 (long)IoLINES(GvIOp(last_in_gv)));
2304df62 1160 }
46fc3d4c 1161 sv_catpv(sv, ".\n");
a687059c 1162 }
a687059c 1163 }
46fc3d4c 1164 return SvPVX(sv);
a687059c
LW
1165}
1166
ecfc5424 1167#ifdef I_STDARG
36477c24 1168OP *
71be2cbc 1169die(const char* pat, ...)
36477c24 1170#else
1171/*VARARGS0*/
1172OP *
1173die(pat, va_alist)
71be2cbc 1174 const char *pat;
36477c24 1175 va_dcl
1176#endif
1177{
5dc0d613 1178 dTHR;
36477c24 1179 va_list args;
1180 char *message;
36477c24 1181 int was_in_eval = in_eval;
1182 HV *stash;
1183 GV *gv;
1184 CV *cv;
1185
57d3b86d 1186#ifdef USE_THREADS
199100c8
MB
1187 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1188 "%p: die: curstack = %p, mainstack = %p\n",
1189 thr, curstack, mainstack));
57d3b86d 1190#endif /* USE_THREADS */
36477c24 1191 /* We have to switch back to mainstack or die_where may try to pop
1192 * the eval block from the wrong stack if die is being called from a
1193 * signal handler. - dkindred@cs.cmu.edu */
1194 if (curstack != mainstack) {
1195 dSP;
1196 SWITCHSTACK(curstack, mainstack);
1197 }
1198
1199#ifdef I_STDARG
1200 va_start(args, pat);
1201#else
1202 va_start(args);
1203#endif
1204 message = mess(pat, &args);
1205 va_end(args);
1206
57d3b86d 1207#ifdef USE_THREADS
199100c8
MB
1208 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1209 "%p: die: message = %s\ndiehook = %p\n",
1210 thr, message, diehook));
57d3b86d 1211#endif /* USE_THREADS */
1738f5c4
CS
1212 if (diehook) {
1213 /* sv_2cv might call croak() */
1214 SV *olddiehook = diehook;
1215 ENTER;
1216 SAVESPTR(diehook);
1217 diehook = Nullsv;
1218 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1219 LEAVE;
1220 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1221 dSP;
774d564b 1222 SV *msg;
1223
1224 ENTER;
1225 msg = newSVpv(message, 0);
1226 SvREADONLY_on(msg);
1227 SAVEFREESV(msg);
1738f5c4
CS
1228
1229 PUSHMARK(sp);
1230 XPUSHs(msg);
1231 PUTBACK;
1232 perl_call_sv((SV*)cv, G_DISCARD);
1233
774d564b 1234 LEAVE;
1738f5c4 1235 }
36477c24 1236 }
1237
1238 restartop = die_where(message);
57d3b86d 1239#ifdef USE_THREADS
d9f997d7 1240 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
7c06b590
GS
1241 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1242 thr, restartop, was_in_eval, top_env));
57d3b86d 1243#endif /* USE_THREADS */
7c06b590 1244 if ((!restartop && was_in_eval) || top_env->je_prev)
54310121 1245 JMPENV_JUMP(3);
36477c24 1246 return restartop;
1247}
1248
1249#ifdef I_STDARG
79072805 1250void
71be2cbc 1251croak(const char* pat, ...)
463ee0b2 1252#else
8990e307
LW
1253/*VARARGS0*/
1254void
1255croak(pat, va_alist)
1256 char *pat;
1257 va_dcl
463ee0b2 1258#endif
a687059c 1259{
11343788 1260 dTHR;
a687059c 1261 va_list args;
de3bb511 1262 char *message;
748a9306
LW
1263 HV *stash;
1264 GV *gv;
1265 CV *cv;
a687059c 1266
a0d0e21e 1267#ifdef I_STDARG
8990e307
LW
1268 va_start(args, pat);
1269#else
a687059c 1270 va_start(args);
8990e307 1271#endif
2304df62 1272 message = mess(pat, &args);
a687059c 1273 va_end(args);
11343788 1274#ifdef USE_THREADS
d9f997d7 1275 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
11343788 1276#endif /* USE_THREADS */
20cec16a 1277 if (diehook) {
1738f5c4 1278 /* sv_2cv might call croak() */
20cec16a 1279 SV *olddiehook = diehook;
1738f5c4
CS
1280 ENTER;
1281 SAVESPTR(diehook);
1282 diehook = Nullsv;
20cec16a 1283 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4
CS
1284 LEAVE;
1285 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1286 dSP;
774d564b 1287 SV *msg;
1288
1289 ENTER;
1290 msg = newSVpv(message, 0);
1291 SvREADONLY_on(msg);
1292 SAVEFREESV(msg);
20cec16a 1293
1294 PUSHMARK(sp);
1738f5c4 1295 XPUSHs(msg);
20cec16a 1296 PUTBACK;
1297 perl_call_sv((SV*)cv, G_DISCARD);
36477c24 1298
774d564b 1299 LEAVE;
20cec16a 1300 }
748a9306 1301 }
a0d0e21e
LW
1302 if (in_eval) {
1303 restartop = die_where(message);
54310121 1304 JMPENV_JUMP(3);
a0d0e21e 1305 }
760ac839
LW
1306 PerlIO_puts(PerlIO_stderr(),message);
1307 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1308 my_failure_exit();
a687059c
LW
1309}
1310
8990e307 1311void
ecfc5424 1312#ifdef I_STDARG
71be2cbc 1313warn(const char* pat,...)
463ee0b2 1314#else
8990e307
LW
1315/*VARARGS0*/
1316warn(pat,va_alist)
71be2cbc 1317 const char *pat;
8990e307 1318 va_dcl
463ee0b2 1319#endif
a687059c
LW
1320{
1321 va_list args;
de3bb511 1322 char *message;
748a9306
LW
1323 HV *stash;
1324 GV *gv;
1325 CV *cv;
a687059c 1326
a0d0e21e 1327#ifdef I_STDARG
8990e307
LW
1328 va_start(args, pat);
1329#else
a687059c 1330 va_start(args);
8990e307 1331#endif
2304df62 1332 message = mess(pat, &args);
a687059c
LW
1333 va_end(args);
1334
20cec16a 1335 if (warnhook) {
1738f5c4 1336 /* sv_2cv might call warn() */
11343788 1337 dTHR;
20cec16a 1338 SV *oldwarnhook = warnhook;
1738f5c4
CS
1339 ENTER;
1340 SAVESPTR(warnhook);
1341 warnhook = Nullsv;
20cec16a 1342 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4
CS
1343 LEAVE;
1344 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1345 dSP;
774d564b 1346 SV *msg;
1347
1348 ENTER;
1349 msg = newSVpv(message, 0);
1350 SvREADONLY_on(msg);
1351 SAVEFREESV(msg);
1352
20cec16a 1353 PUSHMARK(sp);
774d564b 1354 XPUSHs(msg);
20cec16a 1355 PUTBACK;
1356 perl_call_sv((SV*)cv, G_DISCARD);
774d564b 1357
1358 LEAVE;
20cec16a 1359 return;
1360 }
748a9306 1361 }
20cec16a 1362 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1363#ifdef LEAKTEST
20cec16a 1364 DEBUG_L(xstat());
a687059c 1365#endif
20cec16a 1366 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1367}
8d063cd8 1368
a0d0e21e 1369#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1370#ifndef WIN32
8d063cd8 1371void
8ac85365 1372my_setenv(char *nam, char *val)
8d063cd8 1373{
79072805 1374 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1375
fe14fcc3 1376 if (environ == origenviron) { /* need we copy environment? */
79072805
LW
1377 I32 j;
1378 I32 max;
fe14fcc3
LW
1379 char **tmpenv;
1380
de3bb511 1381 /*SUPPRESS 530*/
fe14fcc3
LW
1382 for (max = i; environ[max]; max++) ;
1383 New(901,tmpenv, max+2, char*);
1384 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1385 tmpenv[j] = savepv(environ[j]);
fe14fcc3
LW
1386 tmpenv[max] = Nullch;
1387 environ = tmpenv; /* tell exec where it is now */
1388 }
a687059c 1389 if (!val) {
e5ebf479 1390 Safefree(environ[i]);
a687059c
LW
1391 while (environ[i]) {
1392 environ[i] = environ[i+1];
1393 i++;
1394 }
1395 return;
1396 }
8d063cd8 1397 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1398 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8
LW
1399 environ[i+1] = Nullch; /* make sure it's null terminated */
1400 }
fe14fcc3
LW
1401 else
1402 Safefree(environ[i]);
a687059c 1403 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1404#ifndef MSDOS
a687059c 1405 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9
LW
1406#else
1407 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3
LW
1408 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1409 * some utilities and applications may break because they only look
1410 * for upper case strings. (Fixed strupr() bug here.)]
1411 */
1412 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9
LW
1413 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1414#endif /* MSDOS */
8d063cd8
LW
1415}
1416
3e3baf6d 1417#else /* if WIN32 */
68dc0745 1418
1419void
4e35701f 1420my_setenv(char *nam,char *val)
68dc0745 1421{
3e3baf6d
TB
1422
1423#ifdef USE_WIN32_RTL_ENV
1424
68dc0745 1425 register char *envstr;
1426 STRLEN namlen = strlen(nam);
3e3baf6d
TB
1427 STRLEN vallen;
1428 char *oldstr = environ[setenv_getix(nam)];
1429
1430 /* putenv() has totally broken semantics in both the Borland
1431 * and Microsoft CRTLs. They either store the passed pointer in
1432 * the environment without making a copy, or make a copy and don't
1433 * free it. And on top of that, they dont free() old entries that
1434 * are being replaced/deleted. This means the caller must
1435 * free any old entries somehow, or we end up with a memory
1436 * leak every time my_setenv() is called. One might think
1437 * one could directly manipulate environ[], like the UNIX code
1438 * above, but direct changes to environ are not allowed when
1439 * calling putenv(), since the RTLs maintain an internal
1440 * *copy* of environ[]. Bad, bad, *bad* stink.
1441 * GSAR 97-06-07
1442 */
68dc0745 1443
3e3baf6d
TB
1444 if (!val) {
1445 if (!oldstr)
1446 return;
1447 val = "";
1448 vallen = 0;
1449 }
1450 else
1451 vallen = strlen(val);
fc36a67e 1452 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1453 (void)sprintf(envstr,"%s=%s",nam,val);
3e3baf6d
TB
1454 (void)putenv(envstr);
1455 if (oldstr)
1456 Safefree(oldstr);
1457#ifdef _MSC_VER
1458 Safefree(envstr); /* MSVCRT leaks without this */
1459#endif
1460
1461#else /* !USE_WIN32_RTL_ENV */
1462
1463 /* The sane way to deal with the environment.
1464 * Has these advantages over putenv() & co.:
1465 * * enables us to store a truly empty value in the
1466 * environment (like in UNIX).
1467 * * we don't have to deal with RTL globals, bugs and leaks.
1468 * * Much faster.
1469 * Why you may want to enable USE_WIN32_RTL_ENV:
1470 * * environ[] and RTL functions will not reflect changes,
1471 * which might be an issue if extensions want to access
1472 * the env. via RTL. This cuts both ways, since RTL will
1473 * not see changes made by extensions that call the Win32
1474 * functions directly, either.
1475 * GSAR 97-06-07
1476 */
1477 SetEnvironmentVariable(nam,val);
1478
1479#endif
1480}
1481
1482#endif /* WIN32 */
1483
1484I32
8ac85365 1485setenv_getix(char *nam)
3e3baf6d
TB
1486{
1487 register I32 i, len = strlen(nam);
1488
1489 for (i = 0; environ[i]; i++) {
1490 if (
1491#ifdef WIN32
1492 strnicmp(environ[i],nam,len) == 0
1493#else
1494 strnEQ(environ[i],nam,len)
1495#endif
1496 && environ[i][len] == '=')
1497 break; /* strnEQ must come first to avoid */
1498 } /* potential SEGV's */
1499 return i;
68dc0745 1500}
1501
a0d0e21e 1502#endif /* !VMS */
378cc40b 1503
16d20bd9 1504#ifdef UNLINK_ALL_VERSIONS
79072805 1505I32
378cc40b
LW
1506unlnk(f) /* unlink all versions of a file */
1507char *f;
1508{
79072805 1509 I32 i;
378cc40b
LW
1510
1511 for (i = 0; unlink(f) >= 0; i++) ;
1512 return i ? 0 : -1;
1513}
1514#endif
1515
85e6fe83 1516#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1517char *
4e35701f 1518my_bcopy(register char *from,register char *to,register I32 len)
378cc40b
LW
1519{
1520 char *retval = to;
1521
7c0587c8
LW
1522 if (from - to >= 0) {
1523 while (len--)
1524 *to++ = *from++;
1525 }
1526 else {
1527 to += len;
1528 from += len;
1529 while (len--)
faf8582f 1530 *(--to) = *(--from);
7c0587c8 1531 }
378cc40b
LW
1532 return retval;
1533}
ffed7fef 1534#endif
378cc40b 1535
fc36a67e 1536#ifndef HAS_MEMSET
1537void *
1538my_memset(loc,ch,len)
1539register char *loc;
1540register I32 ch;
1541register I32 len;
1542{
1543 char *retval = loc;
1544
1545 while (len--)
1546 *loc++ = ch;
1547 return retval;
1548}
1549#endif
1550
7c0587c8 1551#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1552char *
7c0587c8 1553my_bzero(loc,len)
378cc40b 1554register char *loc;
79072805 1555register I32 len;
378cc40b
LW
1556{
1557 char *retval = loc;
1558
1559 while (len--)
1560 *loc++ = 0;
1561 return retval;
1562}
1563#endif
7c0587c8 1564
36477c24 1565#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1566I32
7c0587c8 1567my_memcmp(s1,s2,len)
36477c24 1568char *s1;
1569char *s2;
79072805 1570register I32 len;
7c0587c8 1571{
36477c24 1572 register U8 *a = (U8 *)s1;
1573 register U8 *b = (U8 *)s2;
79072805 1574 register I32 tmp;
7c0587c8
LW
1575
1576 while (len--) {
36477c24 1577 if (tmp = *a++ - *b++)
7c0587c8
LW
1578 return tmp;
1579 }
1580 return 0;
1581}
36477c24 1582#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1583
4633a7c4 1584#if defined(I_STDARG) || defined(I_VARARGS)
fe14fcc3 1585#ifndef HAS_VPRINTF
a687059c 1586
85e6fe83 1587#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1588char *
1589#else
1590int
1591#endif
1592vsprintf(dest, pat, args)
71be2cbc 1593char *dest;
1594const char *pat;
1595char *args;
a687059c
LW
1596{
1597 FILE fakebuf;
1598
1599 fakebuf._ptr = dest;
1600 fakebuf._cnt = 32767;
35c8bce7
LW
1601#ifndef _IOSTRG
1602#define _IOSTRG 0
1603#endif
a687059c
LW
1604 fakebuf._flag = _IOWRT|_IOSTRG;
1605 _doprnt(pat, args, &fakebuf); /* what a kludge */
1606 (void)putc('\0', &fakebuf);
85e6fe83 1607#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1608 return(dest);
1609#else
1610 return 0; /* perl doesn't use return value */
1611#endif
1612}
1613
fe14fcc3 1614#endif /* HAS_VPRINTF */
4633a7c4 1615#endif /* I_VARARGS || I_STDARGS */
a687059c
LW
1616
1617#ifdef MYSWAP
ffed7fef 1618#if BYTEORDER != 0x4321
a687059c 1619short
748a9306 1620#ifndef CAN_PROTOTYPE
a687059c
LW
1621my_swap(s)
1622short s;
748a9306
LW
1623#else
1624my_swap(short s)
1625#endif
a687059c
LW
1626{
1627#if (BYTEORDER & 1) == 0
1628 short result;
1629
1630 result = ((s & 255) << 8) + ((s >> 8) & 255);
1631 return result;
1632#else
1633 return s;
1634#endif
1635}
1636
1637long
748a9306
LW
1638#ifndef CAN_PROTOTYPE
1639my_htonl(l)
a687059c 1640register long l;
748a9306
LW
1641#else
1642my_htonl(long l)
1643#endif
a687059c
LW
1644{
1645 union {
1646 long result;
ffed7fef 1647 char c[sizeof(long)];
a687059c
LW
1648 } u;
1649
ffed7fef 1650#if BYTEORDER == 0x1234
a687059c
LW
1651 u.c[0] = (l >> 24) & 255;
1652 u.c[1] = (l >> 16) & 255;
1653 u.c[2] = (l >> 8) & 255;
1654 u.c[3] = l & 255;
1655 return u.result;
1656#else
ffed7fef 1657#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1658 croak("Unknown BYTEORDER\n");
a687059c 1659#else
79072805
LW
1660 register I32 o;
1661 register I32 s;
a687059c 1662
ffed7fef
LW
1663 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1664 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1665 }
1666 return u.result;
1667#endif
1668#endif
1669}
1670
1671long
748a9306
LW
1672#ifndef CAN_PROTOTYPE
1673my_ntohl(l)
a687059c 1674register long l;
748a9306
LW
1675#else
1676my_ntohl(long l)
1677#endif
a687059c
LW
1678{
1679 union {
1680 long l;
ffed7fef 1681 char c[sizeof(long)];
a687059c
LW
1682 } u;
1683
ffed7fef 1684#if BYTEORDER == 0x1234
a687059c
LW
1685 u.c[0] = (l >> 24) & 255;
1686 u.c[1] = (l >> 16) & 255;
1687 u.c[2] = (l >> 8) & 255;
1688 u.c[3] = l & 255;
1689 return u.l;
1690#else
ffed7fef 1691#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1692 croak("Unknown BYTEORDER\n");
a687059c 1693#else
79072805
LW
1694 register I32 o;
1695 register I32 s;
a687059c
LW
1696
1697 u.l = l;
1698 l = 0;
ffed7fef
LW
1699 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1700 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1701 }
1702 return l;
1703#endif
1704#endif
1705}
1706
ffed7fef 1707#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1708#endif /* MYSWAP */
1709
1710/*
1711 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1712 * If these functions are defined,
1713 * the BYTEORDER is neither 0x1234 nor 0x4321.
1714 * However, this is not assumed.
1715 * -DWS
1716 */
1717
1718#define HTOV(name,type) \
1719 type \
1720 name (n) \
1721 register type n; \
1722 { \
1723 union { \
1724 type value; \
1725 char c[sizeof(type)]; \
1726 } u; \
79072805
LW
1727 register I32 i; \
1728 register I32 s; \
988174c1
LW
1729 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1730 u.c[i] = (n >> s) & 0xFF; \
1731 } \
1732 return u.value; \
1733 }
1734
1735#define VTOH(name,type) \
1736 type \
1737 name (n) \
1738 register type n; \
1739 { \
1740 union { \
1741 type value; \
1742 char c[sizeof(type)]; \
1743 } u; \
79072805
LW
1744 register I32 i; \
1745 register I32 s; \
988174c1
LW
1746 u.value = n; \
1747 n = 0; \
1748 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1749 n += (u.c[i] & 0xFF) << s; \
1750 } \
1751 return n; \
1752 }
1753
1754#if defined(HAS_HTOVS) && !defined(htovs)
1755HTOV(htovs,short)
1756#endif
1757#if defined(HAS_HTOVL) && !defined(htovl)
1758HTOV(htovl,long)
1759#endif
1760#if defined(HAS_VTOHS) && !defined(vtohs)
1761VTOH(vtohs,short)
1762#endif
1763#if defined(HAS_VTOHL) && !defined(vtohl)
1764VTOH(vtohl,long)
1765#endif
a687059c 1766
5f05dabc 1767 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1768#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
760ac839 1769PerlIO *
8ac85365 1770my_popen(char *cmd, char *mode)
a687059c
LW
1771{
1772 int p[2];
8ac85365 1773 register I32 This, that;
79072805
LW
1774 register I32 pid;
1775 SV *sv;
1738f5c4 1776 I32 doexec = strNE(cmd,"-");
a687059c 1777
ddcf38b7
IZ
1778#ifdef OS2
1779 if (doexec) {
1780 return my_syspopen(cmd,mode);
1781 }
1782#endif
a687059c
LW
1783 if (pipe(p) < 0)
1784 return Nullfp;
8ac85365
NIS
1785 This = (*mode == 'w');
1786 that = !This;
bbce6d69 1787 if (doexec && tainting) {
1788 taint_env();
1789 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1790 }
a687059c
LW
1791 while ((pid = (doexec?vfork():fork())) < 0) {
1792 if (errno != EAGAIN) {
8ac85365 1793 close(p[This]);
a687059c 1794 if (!doexec)
463ee0b2 1795 croak("Can't fork");
a687059c
LW
1796 return Nullfp;
1797 }
1798 sleep(5);
1799 }
1800 if (pid == 0) {
79072805
LW
1801 GV* tmpgv;
1802
a687059c 1803#define THIS that
8ac85365 1804#define THAT This
a687059c
LW
1805 close(p[THAT]);
1806 if (p[THIS] != (*mode == 'r')) {
1807 dup2(p[THIS], *mode == 'r');
1808 close(p[THIS]);
1809 }
1810 if (doexec) {
a0d0e21e 1811#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
1812 int fd;
1813
1814#ifndef NOFILE
1815#define NOFILE 20
1816#endif
d48672a2 1817 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
ae986130
LW
1818 close(fd);
1819#endif
a687059c
LW
1820 do_exec(cmd); /* may or may not use the shell */
1821 _exit(1);
1822 }
de3bb511 1823 /*SUPPRESS 560*/
85e6fe83 1824 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1825 sv_setiv(GvSV(tmpgv), (IV)getpid());
9f68db38 1826 forkprocess = 0;
463ee0b2 1827 hv_clear(pidstatus); /* we have no children */
a687059c
LW
1828 return Nullfp;
1829#undef THIS
1830#undef THAT
1831 }
62b28dd9 1832 do_execfree(); /* free any memory malloced by child on vfork */
a687059c 1833 close(p[that]);
8ac85365
NIS
1834 if (p[that] < p[This]) {
1835 dup2(p[This], p[that]);
1836 close(p[This]);
1837 p[This] = p[that];
62b28dd9 1838 }
8ac85365 1839 sv = *av_fetch(fdpid,p[This],TRUE);
a0d0e21e 1840 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1841 SvIVX(sv) = pid;
a687059c 1842 forkprocess = pid;
8ac85365 1843 return PerlIO_fdopen(p[This], mode);
a687059c 1844}
7c0587c8 1845#else
55497cff 1846#if defined(atarist) || defined(DJGPP)
7c0587c8 1847FILE *popen();
760ac839 1848PerlIO *
79072805 1849my_popen(cmd,mode)
7c0587c8
LW
1850char *cmd;
1851char *mode;
1852{
760ac839 1853 /* Needs work for PerlIO ! */
55497cff 1854 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1855 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8
LW
1856}
1857#endif
1858
1859#endif /* !DOSISH */
a687059c 1860
748a9306 1861#ifdef DUMP_FDS
79072805 1862dump_fds(s)
ae986130
LW
1863char *s;
1864{
1865 int fd;
1866 struct stat tmpstatbuf;
1867
760ac839 1868 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1869 for (fd = 0; fd < 32; fd++) {
a0d0e21e 1870 if (Fstat(fd,&tmpstatbuf) >= 0)
760ac839 1871 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1872 }
760ac839 1873 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130
LW
1874}
1875#endif
1876
fe14fcc3 1877#ifndef HAS_DUP2
fec02dd3 1878int
a687059c
LW
1879dup2(oldfd,newfd)
1880int oldfd;
1881int newfd;
1882{
a0d0e21e 1883#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
1884 if (oldfd == newfd)
1885 return oldfd;
62b28dd9 1886 close(newfd);
fec02dd3 1887 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 1888#else
fc36a67e 1889#define DUP2_MAX_FDS 256
1890 int fdtmp[DUP2_MAX_FDS];
79072805 1891 I32 fdx = 0;
ae986130
LW
1892 int fd;
1893
fe14fcc3 1894 if (oldfd == newfd)
fec02dd3 1895 return oldfd;
a687059c 1896 close(newfd);
fc36a67e 1897 /* good enough for low fd's... */
1898 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1899 if (fdx >= DUP2_MAX_FDS) {
1900 close(fd);
1901 fd = -1;
1902 break;
1903 }
ae986130 1904 fdtmp[fdx++] = fd;
fc36a67e 1905 }
ae986130
LW
1906 while (fdx > 0)
1907 close(fdtmp[--fdx]);
fec02dd3 1908 return fd;
62b28dd9 1909#endif
a687059c
LW
1910}
1911#endif
1912
ff68c719 1913
1914#ifdef HAS_SIGACTION
1915
1916Sighandler_t
8ac85365 1917rsignal(int signo, Sighandler_t handler)
ff68c719 1918{
1919 struct sigaction act, oact;
1920
1921 act.sa_handler = handler;
1922 sigemptyset(&act.sa_mask);
1923 act.sa_flags = 0;
1924#ifdef SA_RESTART
1925 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1926#endif
1927 if (sigaction(signo, &act, &oact) == -1)
36477c24 1928 return SIG_ERR;
ff68c719 1929 else
36477c24 1930 return oact.sa_handler;
ff68c719 1931}
1932
1933Sighandler_t
8ac85365 1934rsignal_state(int signo)
ff68c719 1935{
1936 struct sigaction oact;
1937
1938 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1939 return SIG_ERR;
1940 else
1941 return oact.sa_handler;
1942}
1943
1944int
8ac85365 1945rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 1946{
1947 struct sigaction act;
1948
1949 act.sa_handler = handler;
1950 sigemptyset(&act.sa_mask);
1951 act.sa_flags = 0;
1952#ifdef SA_RESTART
1953 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1954#endif
1955 return sigaction(signo, &act, save);
1956}
1957
1958int
8ac85365 1959rsignal_restore(int signo, Sigsave_t *save)
ff68c719 1960{
1961 return sigaction(signo, save, (struct sigaction *)NULL);
1962}
1963
1964#else /* !HAS_SIGACTION */
1965
1966Sighandler_t
4e35701f 1967rsignal(int signo, Sighandler_t handler)
ff68c719 1968{
1969 return signal(signo, handler);
1970}
1971
1972static int sig_trapped;
1973
1974static
1975Signal_t
4e35701f 1976sig_trap(int signo)
ff68c719 1977{
1978 sig_trapped++;
1979}
1980
1981Sighandler_t
4e35701f 1982rsignal_state(int signo)
ff68c719 1983{
1984 Sighandler_t oldsig;
1985
1986 sig_trapped = 0;
1987 oldsig = signal(signo, sig_trap);
1988 signal(signo, oldsig);
1989 if (sig_trapped)
1990 kill(getpid(), signo);
1991 return oldsig;
1992}
1993
1994int
4e35701f 1995rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 1996{
1997 *save = signal(signo, handler);
1998 return (*save == SIG_ERR) ? -1 : 0;
1999}
2000
2001int
4e35701f 2002rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2003{
2004 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2005}
2006
2007#endif /* !HAS_SIGACTION */
2008
5f05dabc 2009 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2010#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
79072805 2011I32
8ac85365 2012my_pclose(FILE *ptr)
a687059c 2013{
ff68c719 2014 Sigsave_t hstat, istat, qstat;
a687059c 2015 int status;
a0d0e21e 2016 SV **svp;
20188a90 2017 int pid;
03136e13
CS
2018 bool close_failed;
2019 int saved_errno;
2020#ifdef VMS
2021 int saved_vaxc_errno;
2022#endif
a687059c 2023
760ac839 2024 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 2025 pid = (int)SvIVX(*svp);
a0d0e21e
LW
2026 SvREFCNT_dec(*svp);
2027 *svp = &sv_undef;
ddcf38b7
IZ
2028#ifdef OS2
2029 if (pid == -1) { /* Opened by popen. */
2030 return my_syspclose(ptr);
2031 }
2032#endif
03136e13
CS
2033 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2034 saved_errno = errno;
2035#ifdef VMS
2036 saved_vaxc_errno = vaxc$errno;
2037#endif
2038 }
7c0587c8
LW
2039#ifdef UTS
2040 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2041#endif
ff68c719 2042 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2043 rsignal_save(SIGINT, SIG_IGN, &istat);
2044 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306
LW
2045 do {
2046 pid = wait4pid(pid, &status, 0);
2047 } while (pid == -1 && errno == EINTR);
ff68c719 2048 rsignal_restore(SIGHUP, &hstat);
2049 rsignal_restore(SIGINT, &istat);
2050 rsignal_restore(SIGQUIT, &qstat);
03136e13
CS
2051 if (close_failed) {
2052 SETERRNO(saved_errno, saved_vaxc_errno);
2053 return -1;
2054 }
2055 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
20188a90 2056}
4633a7c4
LW
2057#endif /* !DOSISH */
2058
2059#if !defined(DOSISH) || defined(OS2)
79072805 2060I32
8ac85365 2061wait4pid(int pid, int *statusp, int flags)
20188a90 2062{
79072805
LW
2063 SV *sv;
2064 SV** svp;
fc36a67e 2065 char spid[TYPE_CHARS(int)];
20188a90
LW
2066
2067 if (!pid)
2068 return -1;
20188a90
LW
2069 if (pid > 0) {
2070 sprintf(spid, "%d", pid);
79072805
LW
2071 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2072 if (svp && *svp != &sv_undef) {
463ee0b2 2073 *statusp = SvIVX(*svp);
748a9306 2074 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2075 return pid;
2076 }
2077 }
2078 else {
79072805 2079 HE *entry;
20188a90 2080
79072805
LW
2081 hv_iterinit(pidstatus);
2082 if (entry = hv_iternext(pidstatus)) {
a0d0e21e 2083 pid = atoi(hv_iterkey(entry,(I32*)statusp));
79072805 2084 sv = hv_iterval(pidstatus,entry);
463ee0b2 2085 *statusp = SvIVX(sv);
20188a90 2086 sprintf(spid, "%d", pid);
748a9306 2087 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2088 return pid;
2089 }
2090 }
79072805 2091#ifdef HAS_WAITPID
367f3c24
IZ
2092# ifdef HAS_WAITPID_RUNTIME
2093 if (!HAS_WAITPID_RUNTIME)
2094 goto hard_way;
2095# endif
79072805 2096 return waitpid(pid,statusp,flags);
367f3c24
IZ
2097#endif
2098#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2099 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24
IZ
2100#endif
2101#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2102 hard_way:
a0d0e21e
LW
2103 {
2104 I32 result;
2105 if (flags)
2106 croak("Can't do waitpid with flags");
2107 else {
2108 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2109 pidgone(result,*statusp);
2110 if (result < 0)
2111 *statusp = -1;
2112 }
2113 return result;
a687059c
LW
2114 }
2115#endif
a687059c 2116}
7c0587c8 2117#endif /* !DOSISH */
a687059c 2118
7c0587c8 2119void
de3bb511 2120/*SUPPRESS 590*/
8ac85365 2121pidgone(int pid, int status)
a687059c 2122{
79072805 2123 register SV *sv;
fc36a67e 2124 char spid[TYPE_CHARS(int)];
a687059c 2125
20188a90 2126 sprintf(spid, "%d", pid);
79072805 2127 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2128 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2129 SvIVX(sv) = status;
20188a90 2130 return;
a687059c
LW
2131}
2132
55497cff 2133#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2134int pclose();
ddcf38b7
IZ
2135#ifdef HAS_FORK
2136int /* Cannot prototype with I32
2137 in os2ish.h. */
2138my_syspclose(ptr)
2139#else
79072805
LW
2140I32
2141my_pclose(ptr)
ddcf38b7 2142#endif
760ac839 2143PerlIO *ptr;
a687059c 2144{
760ac839
LW
2145 /* Needs work for PerlIO ! */
2146 FILE *f = PerlIO_findFILE(ptr);
2147 I32 result = pclose(f);
2148 PerlIO_releaseFILE(ptr,f);
2149 return result;
a687059c 2150}
7c0587c8 2151#endif
9f68db38
LW
2152
2153void
8ac85365 2154repeatcpy(register char *to, register char *from, I32 len, register I32 count)
9f68db38 2155{
79072805 2156 register I32 todo;
9f68db38
LW
2157 register char *frombase = from;
2158
2159 if (len == 1) {
2160 todo = *from;
2161 while (count-- > 0)
2162 *to++ = todo;
2163 return;
2164 }
2165 while (count-- > 0) {
2166 for (todo = len; todo > 0; todo--) {
2167 *to++ = *from++;
2168 }
2169 from = frombase;
2170 }
2171}
0f85fab0
LW
2172
2173#ifndef CASTNEGFLOAT
463ee0b2 2174U32
79072805 2175cast_ulong(f)
0f85fab0
LW
2176double f;
2177{
2178 long along;
2179
27e2fb84 2180#if CASTFLAGS & 2
34de22dd
LW
2181# define BIGDOUBLE 2147483648.0
2182 if (f >= BIGDOUBLE)
2183 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2184#endif
0f85fab0
LW
2185 if (f >= 0.0)
2186 return (unsigned long)f;
2187 along = (long)f;
2188 return (unsigned long)along;
2189}
ed6116ce
LW
2190# undef BIGDOUBLE
2191#endif
2192
2193#ifndef CASTI32
5d94fbed 2194
5d94fbed
AD
2195/* Unfortunately, on some systems the cast_uv() function doesn't
2196 work with the system-supplied definition of ULONG_MAX. The
2197 comparison (f >= ULONG_MAX) always comes out true. It must be a
2198 problem with the compiler constant folding.
2199
2200 In any case, this workaround should be fine on any two's complement
2201 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2202 ccflags.
2203 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2204*/
1eb770ff 2205
2206/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2207 of LONG_(MIN/MAX).
2208 -- Kenneth Albanowski <kjahds@kjahds.com>
2209*/
2210
20cec16a 2211#ifndef MY_UV_MAX
2212# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed
AD
2213#endif
2214
ed6116ce
LW
2215I32
2216cast_i32(f)
2217double f;
2218{
20cec16a 2219 if (f >= I32_MAX)
2220 return (I32) I32_MAX;
2221 if (f <= I32_MIN)
2222 return (I32) I32_MIN;
ed6116ce
LW
2223 return (I32) f;
2224}
a0d0e21e
LW
2225
2226IV
2227cast_iv(f)
2228double f;
2229{
20cec16a 2230 if (f >= IV_MAX)
2231 return (IV) IV_MAX;
2232 if (f <= IV_MIN)
2233 return (IV) IV_MIN;
a0d0e21e
LW
2234 return (IV) f;
2235}
5d94fbed
AD
2236
2237UV
2238cast_uv(f)
2239double f;
2240{
20cec16a 2241 if (f >= MY_UV_MAX)
2242 return (UV) MY_UV_MAX;
5d94fbed
AD
2243 return (UV) f;
2244}
2245
0f85fab0 2246#endif
62b28dd9 2247
fe14fcc3 2248#ifndef HAS_RENAME
79072805 2249I32
62b28dd9
LW
2250same_dirent(a,b)
2251char *a;
2252char *b;
2253{
93a17b20
LW
2254 char *fa = strrchr(a,'/');
2255 char *fb = strrchr(b,'/');
62b28dd9
LW
2256 struct stat tmpstatbuf1;
2257 struct stat tmpstatbuf2;
46fc3d4c 2258 SV *tmpsv = sv_newmortal();
62b28dd9
LW
2259
2260 if (fa)
2261 fa++;
2262 else
2263 fa = a;
2264 if (fb)
2265 fb++;
2266 else
2267 fb = b;
2268 if (strNE(a,b))
2269 return FALSE;
2270 if (fa == a)
46fc3d4c 2271 sv_setpv(tmpsv, ".");
62b28dd9 2272 else
46fc3d4c 2273 sv_setpvn(tmpsv, a, fa - a);
2274 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2275 return FALSE;
2276 if (fb == b)
46fc3d4c 2277 sv_setpv(tmpsv, ".");
62b28dd9 2278 else
46fc3d4c 2279 sv_setpvn(tmpsv, b, fb - b);
2280 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2281 return FALSE;
2282 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2283 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2284}
fe14fcc3
LW
2285#endif /* !HAS_RENAME */
2286
55497cff 2287UV
8ac85365 2288scan_oct(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2289{
2290 register char *s = start;
55497cff 2291 register UV retval = 0;
2292 bool overflowed = FALSE;
fe14fcc3 2293
748a9306 2294 while (len && *s >= '0' && *s <= '7') {
55497cff 2295 register UV n = retval << 3;
2296 if (!overflowed && (n >> 3) != retval) {
2297 warn("Integer overflow in octal number");
2298 overflowed = TRUE;
2299 }
2300 retval = n | (*s++ - '0');
748a9306 2301 len--;
fe14fcc3 2302 }
748a9306
LW
2303 if (dowarn && len && (*s == '8' || *s == '9'))
2304 warn("Illegal octal digit ignored");
fe14fcc3
LW
2305 *retlen = s - start;
2306 return retval;
2307}
2308
71be2cbc 2309UV
8ac85365 2310scan_hex(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2311{
2312 register char *s = start;
55497cff 2313 register UV retval = 0;
2314 bool overflowed = FALSE;
fe14fcc3
LW
2315 char *tmp;
2316
4e35701f 2317 while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
55497cff 2318 register UV n = retval << 4;
2319 if (!overflowed && (n >> 4) != retval) {
2320 warn("Integer overflow in hex number");
2321 overflowed = TRUE;
2322 }
4e35701f 2323 retval = n | ((tmp - hexdigit) & 15);
fe14fcc3
LW
2324 s++;
2325 }
2326 *retlen = s - start;
2327 return retval;
2328}
760ac839 2329
11343788 2330#ifdef USE_THREADS
12ca11f6
MB
2331#ifdef FAKE_THREADS
2332/* Very simplistic scheduler for now */
2333void
2334schedule(void)
2335{
c7848ba1 2336 thr = thr->i.next_run;
12ca11f6
MB
2337}
2338
2339void
2340perl_cond_init(cp)
2341perl_cond *cp;
2342{
2343 *cp = 0;
2344}
2345
2346void
2347perl_cond_signal(cp)
2348perl_cond *cp;
2349{
51dd5992 2350 perl_os_thread t;
12ca11f6
MB
2351 perl_cond cond = *cp;
2352
2353 if (!cond)
2354 return;
2355 t = cond->thread;
2356 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2357 t->i.next_run = thr->i.next_run;
2358 thr->i.next_run->i.prev_run = t;
2359 t->i.prev_run = thr;
2360 thr->i.next_run = t;
2361 thr->i.wait_queue = 0;
12ca11f6
MB
2362 /* Remove from the wait queue */
2363 *cp = cond->next;
2364 Safefree(cond);
2365}
2366
2367void
2368perl_cond_broadcast(cp)
2369perl_cond *cp;
2370{
51dd5992 2371 perl_os_thread t;
12ca11f6
MB
2372 perl_cond cond, cond_next;
2373
2374 for (cond = *cp; cond; cond = cond_next) {
2375 t = cond->thread;
2376 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2377 t->i.next_run = thr->i.next_run;
2378 thr->i.next_run->i.prev_run = t;
2379 t->i.prev_run = thr;
2380 thr->i.next_run = t;
2381 thr->i.wait_queue = 0;
12ca11f6
MB
2382 /* Remove from the wait queue */
2383 cond_next = cond->next;
2384 Safefree(cond);
2385 }
2386 *cp = 0;
2387}
2388
2389void
2390perl_cond_wait(cp)
2391perl_cond *cp;
2392{
2393 perl_cond cond;
2394
c7848ba1 2395 if (thr->i.next_run == thr)
12ca11f6
MB
2396 croak("panic: perl_cond_wait called by last runnable thread");
2397
0f15f207 2398 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
2399 cond->thread = thr;
2400 cond->next = *cp;
2401 *cp = cond;
c7848ba1 2402 thr->i.wait_queue = cond;
12ca11f6 2403 /* Remove ourselves from runnable queue */
c7848ba1
MB
2404 thr->i.next_run->i.prev_run = thr->i.prev_run;
2405 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
2406}
2407#endif /* FAKE_THREADS */
2408
11343788 2409#ifdef OLD_PTHREADS_API
52e1cb5e 2410struct perl_thread *
11343788
MB
2411getTHR _((void))
2412{
2413 pthread_addr_t t;
2414
2415 if (pthread_getspecific(thr_key, &t))
2416 croak("panic: pthread_getspecific");
52e1cb5e 2417 return (struct perl_thread *) t;
11343788
MB
2418}
2419#endif /* OLD_PTHREADS_API */
f93b4edd
MB
2420
2421MAGIC *
8ac85365 2422condpair_magic(SV *sv)
f93b4edd
MB
2423{
2424 MAGIC *mg;
2425
2426 SvUPGRADE(sv, SVt_PVMG);
2427 mg = mg_find(sv, 'm');
2428 if (!mg) {
2429 condpair_t *cp;
2430
2431 New(53, cp, 1, condpair_t);
2432 MUTEX_INIT(&cp->mutex);
2433 COND_INIT(&cp->owner_cond);
2434 COND_INIT(&cp->cond);
2435 cp->owner = 0;
2436 MUTEX_LOCK(&sv_mutex);
2437 mg = mg_find(sv, 'm');
2438 if (mg) {
2439 /* someone else beat us to initialising it */
2440 MUTEX_UNLOCK(&sv_mutex);
2441 MUTEX_DESTROY(&cp->mutex);
2442 COND_DESTROY(&cp->owner_cond);
2443 COND_DESTROY(&cp->cond);
2444 Safefree(cp);
2445 }
2446 else {
2447 sv_magic(sv, Nullsv, 'm', 0, 0);
2448 mg = SvMAGIC(sv);
2449 mg->mg_ptr = (char *)cp;
2450 mg->mg_len = sizeof(cp);
2451 MUTEX_UNLOCK(&sv_mutex);
bc1f4c86 2452 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2453 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd
MB
2454 }
2455 }
2456 return mg;
2457}
a863c7d1
MB
2458
2459/*
199100c8
MB
2460 * Make a new perl thread structure using t as a prototype. Some of the
2461 * fields for the new thread are copied from the prototype thread, t,
2462 * so t should not be running in perl at the time this function is
2463 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2464 * thread calling new_struct_thread) clearly satisfies this constraint.
a863c7d1 2465 */
52e1cb5e
JH
2466struct perl_thread *
2467new_struct_thread(struct perl_thread *t)
a863c7d1 2468{
52e1cb5e 2469 struct perl_thread *thr;
a863c7d1 2470 SV *sv;
199100c8
MB
2471 SV **svp;
2472 I32 i;
2473
2474 sv = newSVpv("", 0);
52e1cb5e
JH
2475 SvGROW(sv, sizeof(struct perl_thread) + 1);
2476 SvCUR_set(sv, sizeof(struct perl_thread));
199100c8 2477 thr = (Thread) SvPVX(sv);
199100c8 2478 /* debug */
52e1cb5e 2479 memset(thr, 0xab, sizeof(struct perl_thread));
199100c8
MB
2480 markstack = 0;
2481 scopestack = 0;
2482 savestack = 0;
2483 retstack = 0;
2484 dirty = 0;
2485 localizing = 0;
2486 /* end debug */
2487
2488 thr->oursv = sv;
d55594ae 2489 init_stacks(ARGS);
a863c7d1 2490
a863c7d1 2491 curcop = &compiling;
199100c8 2492 thr->cvcache = newHV();
54b9620d 2493 thr->threadsv = newAV();
a863c7d1 2494 thr->specific = newAV();
38a03e6e
MB
2495 thr->errsv = newSVpv("", 0);
2496 thr->errhv = newHV();
a863c7d1
MB
2497 thr->flags = THRf_R_JOINABLE;
2498 MUTEX_INIT(&thr->mutex);
199100c8
MB
2499
2500 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2501 defstash = t->Tdefstash; /* XXX maybe these should */
2502 curstash = t->Tcurstash; /* always be set to main? */
d55594ae
GS
2503
2504
2505 /* top_env needs to be non-zero. It points to an area
2506 in which longjmp() stuff is stored, as C callstack
2507 info there at least is thread specific this has to
2508 be per-thread. Otherwise a 'die' in a thread gives
2509 that thread the C stack of last thread to do an eval {}!
2510 See comments in scope.h
2511 Initialize top entry (as in perl.c for main thread)
2512 */
2513 start_env.je_prev = NULL;
2514 start_env.je_ret = -1;
2515 start_env.je_mustcatch = TRUE;
2516 top_env = &start_env;
2517
199100c8
MB
2518 in_eval = FALSE;
2519 restartop = 0;
2520
2521 tainted = t->Ttainted;
2522 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2523 nrs = newSVsv(t->Tnrs);
2524 rs = newSVsv(t->Trs);
2525 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2526 ofslen = t->Tofslen;
2527 ofs = savepvn(t->Tofs, ofslen);
2528 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2529 chopset = t->Tchopset;
2530 formtarget = newSVsv(t->Tformtarget);
2531 bodytarget = newSVsv(t->Tbodytarget);
2532 toptarget = newSVsv(t->Ttoptarget);
2533
54b9620d
MB
2534 /* Initialise all per-thread SVs that the template thread used */
2535 svp = AvARRAY(t->threadsv);
2536 for (i = 0; i <= AvFILL(t->threadsv); i++, svp++) {
199100c8
MB
2537 if (*svp && *svp != &sv_undef) {
2538 SV *sv = newSVsv(*svp);
54b9620d
MB
2539 av_store(thr->threadsv, i, sv);
2540 sv_magic(sv, 0, 0, &threadsv_names[i], 1);
199100c8 2541 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
54b9620d 2542 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
199100c8
MB
2543 }
2544 }
2545
a863c7d1
MB
2546 MUTEX_LOCK(&threads_mutex);
2547 nthreads++;
199100c8
MB
2548 thr->tid = ++threadnum;
2549 thr->next = t->next;
2550 thr->prev = t;
2551 t->next = thr;
2552 thr->next->prev = thr;
a863c7d1
MB
2553 MUTEX_UNLOCK(&threads_mutex);
2554
2555#ifdef HAVE_THREAD_INTERN
2556 init_thread_intern(thr);
a863c7d1 2557#endif /* HAVE_THREAD_INTERN */
a863c7d1
MB
2558 return thr;
2559}
11343788 2560#endif /* USE_THREADS */
760ac839
LW
2561
2562#ifdef HUGE_VAL
2563/*
2564 * This hack is to force load of "huge" support from libm.a
2565 * So it is in perl for (say) POSIX to use.
2566 * Needed for SunOS with Sun's 'acc' for example.
2567 */
2568double
8ac85365 2569Perl_huge(void)
760ac839
LW
2570{
2571 return HUGE_VAL;
2572}
2573#endif
4e35701f 2574
22239a37
NIS
2575#ifdef PERL_GLOBAL_STRUCT
2576struct perl_vars *
2577Perl_GetVars(void)
2578{
2579 return &Perl_Vars;
2580}
31fb1209
NIS
2581#endif
2582
2583char **
2584get_op_names(void)
2585{
2586 return op_name;
2587}
2588
2589char **
2590get_op_descs(void)
2591{
2592 return op_desc;
2593}