This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] Various win32 fixes
[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";
3fe6f2dc
MB
874 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
875 t, t + len))) {
876 return (char*)s;
877 }
02128f11 878 }
c277df42 879 if (bigend > big && bigend[-1] == '\n')
161b471a 880 return (char *)(bigend - 1);
c277df42 881 else
161b471a 882 return (char *) bigend;
c277df42 883 }
d48672a2 884 return (char*)big;
c277df42 885 }
a0d0e21e 886 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 887 }
378cc40b 888
79072805
LW
889 littlelen = SvCUR(littlestr);
890 if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */
0f85fab0
LW
891 if (littlelen > bigend - big)
892 return Nullch;
463ee0b2 893 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 894 s = bigend - littlelen;
02128f11
IZ
895 if (s > big
896 && bigend[-1] == '\n'
897 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
898 return (char*)s - 1; /* how sweet it is */
899 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 900 return (char*)s; /* how sweet it is */
02128f11
IZ
901 return Nullch;
902 }
903 if (littlelen <= 2) {
904 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
905 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
906 /* This may do extra comparisons if littlelen == 2, but this
907 should be hidden in the noise since we do less indirection. */
908
909 s = big;
910 bigend -= littlelen;
911 while (s <= bigend) {
912 if (s[0] == c1
913 && (littlelen == 1 || s[1] == c2)
914 && (!SvTAIL(littlestr)
915 || s == bigend
916 || s[littlelen] == '\n')) /* Automatically multiline */
3fe6f2dc
MB
917 {
918 return (char*)s;
919 }
02128f11 920 s++;
a687059c 921 }
bbce6d69 922 return Nullch;
a687059c 923 }
463ee0b2 924 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9
LW
925 if (--littlelen >= bigend - big)
926 return Nullch;
927 s = big + littlelen;
a687059c 928 oldlittle = little = table - 2;
bbce6d69 929 if (s < bigend) {
930 top2:
931 /*SUPPRESS 560*/
932 if (tmp = table[*s]) {
62b28dd9 933#ifdef POINTERRIGOR
bbce6d69 934 if (bigend - s > tmp) {
935 s += tmp;
936 goto top2;
937 }
62b28dd9 938#else
bbce6d69 939 if ((s += tmp) < bigend)
940 goto top2;
62b28dd9 941#endif
bbce6d69 942 return Nullch;
a687059c 943 }
bbce6d69 944 else {
945 tmp = littlelen; /* less expensive than calling strncmp() */
946 olds = s;
947 while (tmp--) {
948 if (*--s == *--little)
949 continue;
c277df42 950 differ:
bbce6d69 951 s = olds + 1; /* here we pay the price for failure */
952 little = oldlittle;
953 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 954 goto top2;
62b28dd9 955 return Nullch;
a687059c 956 }
c277df42
IZ
957 if (SvTAIL(littlestr) /* automatically multiline */
958 && olds + 1 != bigend
959 && olds[1] != '\n')
960 goto differ;
bbce6d69 961 return (char *)s;
378cc40b
LW
962 }
963 }
964 return Nullch;
965}
966
c277df42
IZ
967/* start_shift, end_shift are positive quantities which give offsets
968 of ends of some substring of bigstr.
969 If `last' we want the last occurence.
970 old_posp is the way of communication between consequent calls if
971 the next call needs to find the .
972 The initial *old_posp should be -1.
973 Note that we do not take into account SvTAIL, so it may give wrong
974 positives if _ALL flag is set.
975 */
976
378cc40b 977char *
c277df42 978screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 979{
a687059c
LW
980 register unsigned char *s, *x;
981 register unsigned char *big;
79072805
LW
982 register I32 pos;
983 register I32 previous;
984 register I32 first;
a687059c 985 register unsigned char *little;
c277df42 986 register I32 stop_pos;
a687059c 987 register unsigned char *littleend;
c277df42 988 I32 found = 0;
378cc40b 989
c277df42
IZ
990 if (*old_posp == -1
991 ? (pos = screamfirst[BmRARE(littlestr)]) < 0
992 : (((pos = *old_posp), pos += screamnext[pos]) == 0))
378cc40b 993 return Nullch;
463ee0b2 994 little = (unsigned char *)(SvPVX(littlestr));
79072805 995 littleend = little + SvCUR(littlestr);
378cc40b 996 first = *little++;
c277df42 997 /* The value of pos we can start at: */
79072805 998 previous = BmPREVIOUS(littlestr);
463ee0b2 999 big = (unsigned char *)(SvPVX(bigstr));
c277df42
IZ
1000 /* The value of pos we can stop at: */
1001 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1002 if (previous + start_shift > stop_pos) return Nullch;
1003 while (pos < previous + start_shift) {
378cc40b
LW
1004 if (!(pos += screamnext[pos]))
1005 return Nullch;
1006 }
de3bb511 1007#ifdef POINTERRIGOR
bbce6d69 1008 do {
c277df42 1009 if (pos >= stop_pos) return Nullch;
bbce6d69 1010 if (big[pos-previous] != first)
1011 continue;
1012 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
bbce6d69 1013 if (*s++ != *x++) {
1014 s--;
1015 break;
de3bb511 1016 }
bbce6d69 1017 }
c277df42
IZ
1018 if (s == littleend) {
1019 *old_posp = pos;
1020 if (!last) return (char *)(big+pos-previous);
1021 found = 1;
1022 }
bbce6d69 1023 } while ( pos += screamnext[pos] );
c277df42 1024 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
de3bb511
LW
1025#else /* !POINTERRIGOR */
1026 big -= previous;
bbce6d69 1027 do {
c277df42 1028 if (pos >= stop_pos) return Nullch;
bbce6d69 1029 if (big[pos] != first)
1030 continue;
1031 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69 1032 if (*s++ != *x++) {
1033 s--;
1034 break;
378cc40b 1035 }
bbce6d69 1036 }
c277df42
IZ
1037 if (s == littleend) {
1038 *old_posp = pos;
1039 if (!last) return (char *)(big+pos);
1040 found = 1;
1041 }
bbce6d69 1042 } while ( pos += screamnext[pos] );
c277df42 1043 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
de3bb511 1044#endif /* POINTERRIGOR */
8d063cd8
LW
1045}
1046
79072805 1047I32
8ac85365 1048ibcmp(char *s1, char *s2, register I32 len)
79072805 1049{
bbce6d69 1050 register U8 *a = (U8 *)s1;
1051 register U8 *b = (U8 *)s2;
79072805 1052 while (len--) {
bbce6d69 1053 if (*a != *b && *a != fold[*b])
1054 return 1;
1055 a++,b++;
1056 }
1057 return 0;
1058}
1059
1060I32
8ac85365 1061ibcmp_locale(char *s1, char *s2, register I32 len)
bbce6d69 1062{
1063 register U8 *a = (U8 *)s1;
1064 register U8 *b = (U8 *)s2;
1065 while (len--) {
1066 if (*a != *b && *a != fold_locale[*b])
1067 return 1;
1068 a++,b++;
79072805
LW
1069 }
1070 return 0;
1071}
1072
8d063cd8
LW
1073/* copy a string to a safe spot */
1074
1075char *
8ac85365 1076savepv(char *sv)
8d063cd8 1077{
a687059c 1078 register char *newaddr;
8d063cd8 1079
79072805
LW
1080 New(902,newaddr,strlen(sv)+1,char);
1081 (void)strcpy(newaddr,sv);
8d063cd8
LW
1082 return newaddr;
1083}
1084
a687059c
LW
1085/* same thing but with a known length */
1086
1087char *
8ac85365 1088savepvn(char *sv, register I32 len)
a687059c
LW
1089{
1090 register char *newaddr;
1091
1092 New(903,newaddr,len+1,char);
79072805 1093 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c
LW
1094 newaddr[len] = '\0'; /* is now */
1095 return newaddr;
1096}
1097
fc36a67e 1098/* the SV for form() and mess() is not kept in an arena */
1099
1100static SV *
8ac85365 1101mess_alloc(void)
fc36a67e 1102{
1103 SV *sv;
1104 XPVMG *any;
1105
1106 /* Create as PVMG now, to avoid any upgrading later */
1107 New(905, sv, 1, SV);
1108 Newz(905, any, 1, XPVMG);
1109 SvFLAGS(sv) = SVt_PVMG;
1110 SvANY(sv) = (void*)any;
1111 SvREFCNT(sv) = 1 << 30; /* practically infinite */
1112 return sv;
1113}
1114
a0d0e21e 1115#ifdef I_STDARG
8990e307 1116char *
46fc3d4c 1117form(const char* pat, ...)
a687059c
LW
1118#else
1119/*VARARGS0*/
de3bb511 1120char *
46fc3d4c 1121form(pat, va_alist)
71be2cbc 1122 const char *pat;
46fc3d4c 1123 va_dcl
8990e307
LW
1124#endif
1125{
46fc3d4c 1126 va_list args;
1127#ifdef I_STDARG
1128 va_start(args, pat);
a687059c 1129#else
46fc3d4c 1130 va_start(args);
d48672a2 1131#endif
fc36a67e 1132 if (!mess_sv)
1133 mess_sv = mess_alloc();
1134 sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1135 va_end(args);
fc36a67e 1136 return SvPVX(mess_sv);
46fc3d4c 1137}
a687059c 1138
46fc3d4c 1139char *
8ac85365 1140mess(const char *pat, va_list *args)
46fc3d4c 1141{
1142 SV *sv;
1143 static char dgd[] = " during global destruction.\n";
1144
46fc3d4c 1145 if (!mess_sv)
fc36a67e 1146 mess_sv = mess_alloc();
46fc3d4c 1147 sv = mess_sv;
fc36a67e 1148 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1149 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1150 dTHR;
2304df62 1151 if (dirty)
46fc3d4c 1152 sv_catpv(sv, dgd);
2304df62 1153 else {
46fc3d4c 1154 if (curcop->cop_line)
fc36a67e 1155 sv_catpvf(sv, " at %_ line %ld",
46fc3d4c 1156 GvSV(curcop->cop_filegv), (long)curcop->cop_line);
c07a80fd 1157 if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
1158 bool line_mode = (RsSIMPLE(rs) &&
1159 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
46fc3d4c 1160 sv_catpvf(sv, ", <%s> %s %ld",
1161 last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
1162 line_mode ? "line" : "chunk",
1163 (long)IoLINES(GvIOp(last_in_gv)));
2304df62 1164 }
46fc3d4c 1165 sv_catpv(sv, ".\n");
a687059c 1166 }
a687059c 1167 }
46fc3d4c 1168 return SvPVX(sv);
a687059c
LW
1169}
1170
ecfc5424 1171#ifdef I_STDARG
36477c24 1172OP *
71be2cbc 1173die(const char* pat, ...)
36477c24 1174#else
1175/*VARARGS0*/
1176OP *
1177die(pat, va_alist)
71be2cbc 1178 const char *pat;
36477c24 1179 va_dcl
1180#endif
1181{
5dc0d613 1182 dTHR;
36477c24 1183 va_list args;
1184 char *message;
36477c24 1185 int was_in_eval = in_eval;
1186 HV *stash;
1187 GV *gv;
1188 CV *cv;
1189
57d3b86d 1190#ifdef USE_THREADS
199100c8
MB
1191 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1192 "%p: die: curstack = %p, mainstack = %p\n",
1193 thr, curstack, mainstack));
57d3b86d 1194#endif /* USE_THREADS */
36477c24 1195 /* We have to switch back to mainstack or die_where may try to pop
1196 * the eval block from the wrong stack if die is being called from a
1197 * signal handler. - dkindred@cs.cmu.edu */
1198 if (curstack != mainstack) {
1199 dSP;
1200 SWITCHSTACK(curstack, mainstack);
1201 }
1202
1203#ifdef I_STDARG
1204 va_start(args, pat);
1205#else
1206 va_start(args);
1207#endif
1208 message = mess(pat, &args);
1209 va_end(args);
1210
57d3b86d 1211#ifdef USE_THREADS
199100c8
MB
1212 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
1213 "%p: die: message = %s\ndiehook = %p\n",
1214 thr, message, diehook));
57d3b86d 1215#endif /* USE_THREADS */
1738f5c4
CS
1216 if (diehook) {
1217 /* sv_2cv might call croak() */
1218 SV *olddiehook = diehook;
1219 ENTER;
1220 SAVESPTR(diehook);
1221 diehook = Nullsv;
1222 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1223 LEAVE;
1224 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1225 dSP;
774d564b 1226 SV *msg;
1227
1228 ENTER;
1229 msg = newSVpv(message, 0);
1230 SvREADONLY_on(msg);
1231 SAVEFREESV(msg);
1738f5c4
CS
1232
1233 PUSHMARK(sp);
1234 XPUSHs(msg);
1235 PUTBACK;
1236 perl_call_sv((SV*)cv, G_DISCARD);
1237
774d564b 1238 LEAVE;
1738f5c4 1239 }
36477c24 1240 }
1241
1242 restartop = die_where(message);
57d3b86d 1243#ifdef USE_THREADS
d9f997d7 1244 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
7c06b590
GS
1245 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
1246 thr, restartop, was_in_eval, top_env));
57d3b86d 1247#endif /* USE_THREADS */
7c06b590 1248 if ((!restartop && was_in_eval) || top_env->je_prev)
54310121 1249 JMPENV_JUMP(3);
36477c24 1250 return restartop;
1251}
1252
1253#ifdef I_STDARG
79072805 1254void
71be2cbc 1255croak(const char* pat, ...)
463ee0b2 1256#else
8990e307
LW
1257/*VARARGS0*/
1258void
1259croak(pat, va_alist)
1260 char *pat;
1261 va_dcl
463ee0b2 1262#endif
a687059c 1263{
11343788 1264 dTHR;
a687059c 1265 va_list args;
de3bb511 1266 char *message;
748a9306
LW
1267 HV *stash;
1268 GV *gv;
1269 CV *cv;
a687059c 1270
a0d0e21e 1271#ifdef I_STDARG
8990e307
LW
1272 va_start(args, pat);
1273#else
a687059c 1274 va_start(args);
8990e307 1275#endif
2304df62 1276 message = mess(pat, &args);
a687059c 1277 va_end(args);
11343788 1278#ifdef USE_THREADS
d9f997d7 1279 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
11343788 1280#endif /* USE_THREADS */
20cec16a 1281 if (diehook) {
1738f5c4 1282 /* sv_2cv might call croak() */
20cec16a 1283 SV *olddiehook = diehook;
1738f5c4
CS
1284 ENTER;
1285 SAVESPTR(diehook);
1286 diehook = Nullsv;
20cec16a 1287 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4
CS
1288 LEAVE;
1289 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1290 dSP;
774d564b 1291 SV *msg;
1292
1293 ENTER;
1294 msg = newSVpv(message, 0);
1295 SvREADONLY_on(msg);
1296 SAVEFREESV(msg);
20cec16a 1297
1298 PUSHMARK(sp);
1738f5c4 1299 XPUSHs(msg);
20cec16a 1300 PUTBACK;
1301 perl_call_sv((SV*)cv, G_DISCARD);
36477c24 1302
774d564b 1303 LEAVE;
20cec16a 1304 }
748a9306 1305 }
a0d0e21e
LW
1306 if (in_eval) {
1307 restartop = die_where(message);
54310121 1308 JMPENV_JUMP(3);
a0d0e21e 1309 }
760ac839
LW
1310 PerlIO_puts(PerlIO_stderr(),message);
1311 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1312 my_failure_exit();
a687059c
LW
1313}
1314
8990e307 1315void
ecfc5424 1316#ifdef I_STDARG
71be2cbc 1317warn(const char* pat,...)
463ee0b2 1318#else
8990e307
LW
1319/*VARARGS0*/
1320warn(pat,va_alist)
71be2cbc 1321 const char *pat;
8990e307 1322 va_dcl
463ee0b2 1323#endif
a687059c
LW
1324{
1325 va_list args;
de3bb511 1326 char *message;
748a9306
LW
1327 HV *stash;
1328 GV *gv;
1329 CV *cv;
a687059c 1330
a0d0e21e 1331#ifdef I_STDARG
8990e307
LW
1332 va_start(args, pat);
1333#else
a687059c 1334 va_start(args);
8990e307 1335#endif
2304df62 1336 message = mess(pat, &args);
a687059c
LW
1337 va_end(args);
1338
20cec16a 1339 if (warnhook) {
1738f5c4 1340 /* sv_2cv might call warn() */
11343788 1341 dTHR;
20cec16a 1342 SV *oldwarnhook = warnhook;
1738f5c4
CS
1343 ENTER;
1344 SAVESPTR(warnhook);
1345 warnhook = Nullsv;
20cec16a 1346 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4
CS
1347 LEAVE;
1348 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1349 dSP;
774d564b 1350 SV *msg;
1351
1352 ENTER;
1353 msg = newSVpv(message, 0);
1354 SvREADONLY_on(msg);
1355 SAVEFREESV(msg);
1356
20cec16a 1357 PUSHMARK(sp);
774d564b 1358 XPUSHs(msg);
20cec16a 1359 PUTBACK;
1360 perl_call_sv((SV*)cv, G_DISCARD);
774d564b 1361
1362 LEAVE;
20cec16a 1363 return;
1364 }
748a9306 1365 }
20cec16a 1366 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1367#ifdef LEAKTEST
20cec16a 1368 DEBUG_L(xstat());
a687059c 1369#endif
20cec16a 1370 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1371}
8d063cd8 1372
a0d0e21e 1373#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1374#ifndef WIN32
8d063cd8 1375void
8ac85365 1376my_setenv(char *nam, char *val)
8d063cd8 1377{
79072805 1378 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1379
fe14fcc3 1380 if (environ == origenviron) { /* need we copy environment? */
79072805
LW
1381 I32 j;
1382 I32 max;
fe14fcc3
LW
1383 char **tmpenv;
1384
de3bb511 1385 /*SUPPRESS 530*/
fe14fcc3
LW
1386 for (max = i; environ[max]; max++) ;
1387 New(901,tmpenv, max+2, char*);
1388 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1389 tmpenv[j] = savepv(environ[j]);
fe14fcc3
LW
1390 tmpenv[max] = Nullch;
1391 environ = tmpenv; /* tell exec where it is now */
1392 }
a687059c 1393 if (!val) {
e5ebf479 1394 Safefree(environ[i]);
a687059c
LW
1395 while (environ[i]) {
1396 environ[i] = environ[i+1];
1397 i++;
1398 }
1399 return;
1400 }
8d063cd8 1401 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1402 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8
LW
1403 environ[i+1] = Nullch; /* make sure it's null terminated */
1404 }
fe14fcc3
LW
1405 else
1406 Safefree(environ[i]);
a687059c 1407 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1408#ifndef MSDOS
a687059c 1409 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9
LW
1410#else
1411 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3
LW
1412 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1413 * some utilities and applications may break because they only look
1414 * for upper case strings. (Fixed strupr() bug here.)]
1415 */
1416 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9
LW
1417 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1418#endif /* MSDOS */
8d063cd8
LW
1419}
1420
3e3baf6d 1421#else /* if WIN32 */
68dc0745 1422
1423void
4e35701f 1424my_setenv(char *nam,char *val)
68dc0745 1425{
3e3baf6d
TB
1426
1427#ifdef USE_WIN32_RTL_ENV
1428
68dc0745 1429 register char *envstr;
1430 STRLEN namlen = strlen(nam);
3e3baf6d
TB
1431 STRLEN vallen;
1432 char *oldstr = environ[setenv_getix(nam)];
1433
1434 /* putenv() has totally broken semantics in both the Borland
1435 * and Microsoft CRTLs. They either store the passed pointer in
1436 * the environment without making a copy, or make a copy and don't
1437 * free it. And on top of that, they dont free() old entries that
1438 * are being replaced/deleted. This means the caller must
1439 * free any old entries somehow, or we end up with a memory
1440 * leak every time my_setenv() is called. One might think
1441 * one could directly manipulate environ[], like the UNIX code
1442 * above, but direct changes to environ are not allowed when
1443 * calling putenv(), since the RTLs maintain an internal
1444 * *copy* of environ[]. Bad, bad, *bad* stink.
1445 * GSAR 97-06-07
1446 */
68dc0745 1447
3e3baf6d
TB
1448 if (!val) {
1449 if (!oldstr)
1450 return;
1451 val = "";
1452 vallen = 0;
1453 }
1454 else
1455 vallen = strlen(val);
fc36a67e 1456 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1457 (void)sprintf(envstr,"%s=%s",nam,val);
3e3baf6d
TB
1458 (void)putenv(envstr);
1459 if (oldstr)
1460 Safefree(oldstr);
1461#ifdef _MSC_VER
1462 Safefree(envstr); /* MSVCRT leaks without this */
1463#endif
1464
1465#else /* !USE_WIN32_RTL_ENV */
1466
1467 /* The sane way to deal with the environment.
1468 * Has these advantages over putenv() & co.:
1469 * * enables us to store a truly empty value in the
1470 * environment (like in UNIX).
1471 * * we don't have to deal with RTL globals, bugs and leaks.
1472 * * Much faster.
1473 * Why you may want to enable USE_WIN32_RTL_ENV:
1474 * * environ[] and RTL functions will not reflect changes,
1475 * which might be an issue if extensions want to access
1476 * the env. via RTL. This cuts both ways, since RTL will
1477 * not see changes made by extensions that call the Win32
1478 * functions directly, either.
1479 * GSAR 97-06-07
1480 */
1481 SetEnvironmentVariable(nam,val);
1482
1483#endif
1484}
1485
1486#endif /* WIN32 */
1487
1488I32
8ac85365 1489setenv_getix(char *nam)
3e3baf6d
TB
1490{
1491 register I32 i, len = strlen(nam);
1492
1493 for (i = 0; environ[i]; i++) {
1494 if (
1495#ifdef WIN32
1496 strnicmp(environ[i],nam,len) == 0
1497#else
1498 strnEQ(environ[i],nam,len)
1499#endif
1500 && environ[i][len] == '=')
1501 break; /* strnEQ must come first to avoid */
1502 } /* potential SEGV's */
1503 return i;
68dc0745 1504}
1505
a0d0e21e 1506#endif /* !VMS */
378cc40b 1507
16d20bd9 1508#ifdef UNLINK_ALL_VERSIONS
79072805 1509I32
378cc40b
LW
1510unlnk(f) /* unlink all versions of a file */
1511char *f;
1512{
79072805 1513 I32 i;
378cc40b
LW
1514
1515 for (i = 0; unlink(f) >= 0; i++) ;
1516 return i ? 0 : -1;
1517}
1518#endif
1519
85e6fe83 1520#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1521char *
4e35701f 1522my_bcopy(register char *from,register char *to,register I32 len)
378cc40b
LW
1523{
1524 char *retval = to;
1525
7c0587c8
LW
1526 if (from - to >= 0) {
1527 while (len--)
1528 *to++ = *from++;
1529 }
1530 else {
1531 to += len;
1532 from += len;
1533 while (len--)
faf8582f 1534 *(--to) = *(--from);
7c0587c8 1535 }
378cc40b
LW
1536 return retval;
1537}
ffed7fef 1538#endif
378cc40b 1539
fc36a67e 1540#ifndef HAS_MEMSET
1541void *
1542my_memset(loc,ch,len)
1543register char *loc;
1544register I32 ch;
1545register I32 len;
1546{
1547 char *retval = loc;
1548
1549 while (len--)
1550 *loc++ = ch;
1551 return retval;
1552}
1553#endif
1554
7c0587c8 1555#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1556char *
7c0587c8 1557my_bzero(loc,len)
378cc40b 1558register char *loc;
79072805 1559register I32 len;
378cc40b
LW
1560{
1561 char *retval = loc;
1562
1563 while (len--)
1564 *loc++ = 0;
1565 return retval;
1566}
1567#endif
7c0587c8 1568
36477c24 1569#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1570I32
7c0587c8 1571my_memcmp(s1,s2,len)
36477c24 1572char *s1;
1573char *s2;
79072805 1574register I32 len;
7c0587c8 1575{
36477c24 1576 register U8 *a = (U8 *)s1;
1577 register U8 *b = (U8 *)s2;
79072805 1578 register I32 tmp;
7c0587c8
LW
1579
1580 while (len--) {
36477c24 1581 if (tmp = *a++ - *b++)
7c0587c8
LW
1582 return tmp;
1583 }
1584 return 0;
1585}
36477c24 1586#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1587
4633a7c4 1588#if defined(I_STDARG) || defined(I_VARARGS)
fe14fcc3 1589#ifndef HAS_VPRINTF
a687059c 1590
85e6fe83 1591#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1592char *
1593#else
1594int
1595#endif
1596vsprintf(dest, pat, args)
71be2cbc 1597char *dest;
1598const char *pat;
1599char *args;
a687059c
LW
1600{
1601 FILE fakebuf;
1602
1603 fakebuf._ptr = dest;
1604 fakebuf._cnt = 32767;
35c8bce7
LW
1605#ifndef _IOSTRG
1606#define _IOSTRG 0
1607#endif
a687059c
LW
1608 fakebuf._flag = _IOWRT|_IOSTRG;
1609 _doprnt(pat, args, &fakebuf); /* what a kludge */
1610 (void)putc('\0', &fakebuf);
85e6fe83 1611#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1612 return(dest);
1613#else
1614 return 0; /* perl doesn't use return value */
1615#endif
1616}
1617
fe14fcc3 1618#endif /* HAS_VPRINTF */
4633a7c4 1619#endif /* I_VARARGS || I_STDARGS */
a687059c
LW
1620
1621#ifdef MYSWAP
ffed7fef 1622#if BYTEORDER != 0x4321
a687059c 1623short
748a9306 1624#ifndef CAN_PROTOTYPE
a687059c
LW
1625my_swap(s)
1626short s;
748a9306
LW
1627#else
1628my_swap(short s)
1629#endif
a687059c
LW
1630{
1631#if (BYTEORDER & 1) == 0
1632 short result;
1633
1634 result = ((s & 255) << 8) + ((s >> 8) & 255);
1635 return result;
1636#else
1637 return s;
1638#endif
1639}
1640
1641long
748a9306
LW
1642#ifndef CAN_PROTOTYPE
1643my_htonl(l)
a687059c 1644register long l;
748a9306
LW
1645#else
1646my_htonl(long l)
1647#endif
a687059c
LW
1648{
1649 union {
1650 long result;
ffed7fef 1651 char c[sizeof(long)];
a687059c
LW
1652 } u;
1653
ffed7fef 1654#if BYTEORDER == 0x1234
a687059c
LW
1655 u.c[0] = (l >> 24) & 255;
1656 u.c[1] = (l >> 16) & 255;
1657 u.c[2] = (l >> 8) & 255;
1658 u.c[3] = l & 255;
1659 return u.result;
1660#else
ffed7fef 1661#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1662 croak("Unknown BYTEORDER\n");
a687059c 1663#else
79072805
LW
1664 register I32 o;
1665 register I32 s;
a687059c 1666
ffed7fef
LW
1667 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1668 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1669 }
1670 return u.result;
1671#endif
1672#endif
1673}
1674
1675long
748a9306
LW
1676#ifndef CAN_PROTOTYPE
1677my_ntohl(l)
a687059c 1678register long l;
748a9306
LW
1679#else
1680my_ntohl(long l)
1681#endif
a687059c
LW
1682{
1683 union {
1684 long l;
ffed7fef 1685 char c[sizeof(long)];
a687059c
LW
1686 } u;
1687
ffed7fef 1688#if BYTEORDER == 0x1234
a687059c
LW
1689 u.c[0] = (l >> 24) & 255;
1690 u.c[1] = (l >> 16) & 255;
1691 u.c[2] = (l >> 8) & 255;
1692 u.c[3] = l & 255;
1693 return u.l;
1694#else
ffed7fef 1695#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1696 croak("Unknown BYTEORDER\n");
a687059c 1697#else
79072805
LW
1698 register I32 o;
1699 register I32 s;
a687059c
LW
1700
1701 u.l = l;
1702 l = 0;
ffed7fef
LW
1703 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1704 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1705 }
1706 return l;
1707#endif
1708#endif
1709}
1710
ffed7fef 1711#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1712#endif /* MYSWAP */
1713
1714/*
1715 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1716 * If these functions are defined,
1717 * the BYTEORDER is neither 0x1234 nor 0x4321.
1718 * However, this is not assumed.
1719 * -DWS
1720 */
1721
1722#define HTOV(name,type) \
1723 type \
1724 name (n) \
1725 register type n; \
1726 { \
1727 union { \
1728 type value; \
1729 char c[sizeof(type)]; \
1730 } u; \
79072805
LW
1731 register I32 i; \
1732 register I32 s; \
988174c1
LW
1733 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1734 u.c[i] = (n >> s) & 0xFF; \
1735 } \
1736 return u.value; \
1737 }
1738
1739#define VTOH(name,type) \
1740 type \
1741 name (n) \
1742 register type n; \
1743 { \
1744 union { \
1745 type value; \
1746 char c[sizeof(type)]; \
1747 } u; \
79072805
LW
1748 register I32 i; \
1749 register I32 s; \
988174c1
LW
1750 u.value = n; \
1751 n = 0; \
1752 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1753 n += (u.c[i] & 0xFF) << s; \
1754 } \
1755 return n; \
1756 }
1757
1758#if defined(HAS_HTOVS) && !defined(htovs)
1759HTOV(htovs,short)
1760#endif
1761#if defined(HAS_HTOVL) && !defined(htovl)
1762HTOV(htovl,long)
1763#endif
1764#if defined(HAS_VTOHS) && !defined(vtohs)
1765VTOH(vtohs,short)
1766#endif
1767#if defined(HAS_VTOHL) && !defined(vtohl)
1768VTOH(vtohl,long)
1769#endif
a687059c 1770
5f05dabc 1771 /* VMS' my_popen() is in VMS.c, same with OS/2. */
1772#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
760ac839 1773PerlIO *
8ac85365 1774my_popen(char *cmd, char *mode)
a687059c
LW
1775{
1776 int p[2];
8ac85365 1777 register I32 This, that;
79072805
LW
1778 register I32 pid;
1779 SV *sv;
1738f5c4 1780 I32 doexec = strNE(cmd,"-");
a687059c 1781
ddcf38b7
IZ
1782#ifdef OS2
1783 if (doexec) {
1784 return my_syspopen(cmd,mode);
1785 }
1786#endif
a687059c
LW
1787 if (pipe(p) < 0)
1788 return Nullfp;
8ac85365
NIS
1789 This = (*mode == 'w');
1790 that = !This;
bbce6d69 1791 if (doexec && tainting) {
1792 taint_env();
1793 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1794 }
a687059c
LW
1795 while ((pid = (doexec?vfork():fork())) < 0) {
1796 if (errno != EAGAIN) {
8ac85365 1797 close(p[This]);
a687059c 1798 if (!doexec)
463ee0b2 1799 croak("Can't fork");
a687059c
LW
1800 return Nullfp;
1801 }
1802 sleep(5);
1803 }
1804 if (pid == 0) {
79072805
LW
1805 GV* tmpgv;
1806
a687059c 1807#define THIS that
8ac85365 1808#define THAT This
a687059c
LW
1809 close(p[THAT]);
1810 if (p[THIS] != (*mode == 'r')) {
1811 dup2(p[THIS], *mode == 'r');
1812 close(p[THIS]);
1813 }
1814 if (doexec) {
a0d0e21e 1815#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
1816 int fd;
1817
1818#ifndef NOFILE
1819#define NOFILE 20
1820#endif
d48672a2 1821 for (fd = maxsysfd + 1; fd < NOFILE; fd++)
ae986130
LW
1822 close(fd);
1823#endif
a687059c
LW
1824 do_exec(cmd); /* may or may not use the shell */
1825 _exit(1);
1826 }
de3bb511 1827 /*SUPPRESS 560*/
85e6fe83 1828 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1829 sv_setiv(GvSV(tmpgv), (IV)getpid());
9f68db38 1830 forkprocess = 0;
463ee0b2 1831 hv_clear(pidstatus); /* we have no children */
a687059c
LW
1832 return Nullfp;
1833#undef THIS
1834#undef THAT
1835 }
62b28dd9 1836 do_execfree(); /* free any memory malloced by child on vfork */
a687059c 1837 close(p[that]);
8ac85365
NIS
1838 if (p[that] < p[This]) {
1839 dup2(p[This], p[that]);
1840 close(p[This]);
1841 p[This] = p[that];
62b28dd9 1842 }
8ac85365 1843 sv = *av_fetch(fdpid,p[This],TRUE);
a0d0e21e 1844 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1845 SvIVX(sv) = pid;
a687059c 1846 forkprocess = pid;
8ac85365 1847 return PerlIO_fdopen(p[This], mode);
a687059c 1848}
7c0587c8 1849#else
55497cff 1850#if defined(atarist) || defined(DJGPP)
7c0587c8 1851FILE *popen();
760ac839 1852PerlIO *
79072805 1853my_popen(cmd,mode)
7c0587c8
LW
1854char *cmd;
1855char *mode;
1856{
760ac839 1857 /* Needs work for PerlIO ! */
55497cff 1858 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1859 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8
LW
1860}
1861#endif
1862
1863#endif /* !DOSISH */
a687059c 1864
748a9306 1865#ifdef DUMP_FDS
79072805 1866dump_fds(s)
ae986130
LW
1867char *s;
1868{
1869 int fd;
1870 struct stat tmpstatbuf;
1871
760ac839 1872 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1873 for (fd = 0; fd < 32; fd++) {
a0d0e21e 1874 if (Fstat(fd,&tmpstatbuf) >= 0)
760ac839 1875 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1876 }
760ac839 1877 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130
LW
1878}
1879#endif
1880
fe14fcc3 1881#ifndef HAS_DUP2
fec02dd3 1882int
a687059c
LW
1883dup2(oldfd,newfd)
1884int oldfd;
1885int newfd;
1886{
a0d0e21e 1887#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
1888 if (oldfd == newfd)
1889 return oldfd;
62b28dd9 1890 close(newfd);
fec02dd3 1891 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 1892#else
fc36a67e 1893#define DUP2_MAX_FDS 256
1894 int fdtmp[DUP2_MAX_FDS];
79072805 1895 I32 fdx = 0;
ae986130
LW
1896 int fd;
1897
fe14fcc3 1898 if (oldfd == newfd)
fec02dd3 1899 return oldfd;
a687059c 1900 close(newfd);
fc36a67e 1901 /* good enough for low fd's... */
1902 while ((fd = dup(oldfd)) != newfd && fd >= 0) {
1903 if (fdx >= DUP2_MAX_FDS) {
1904 close(fd);
1905 fd = -1;
1906 break;
1907 }
ae986130 1908 fdtmp[fdx++] = fd;
fc36a67e 1909 }
ae986130
LW
1910 while (fdx > 0)
1911 close(fdtmp[--fdx]);
fec02dd3 1912 return fd;
62b28dd9 1913#endif
a687059c
LW
1914}
1915#endif
1916
ff68c719 1917
1918#ifdef HAS_SIGACTION
1919
1920Sighandler_t
8ac85365 1921rsignal(int signo, Sighandler_t handler)
ff68c719 1922{
1923 struct sigaction act, oact;
1924
1925 act.sa_handler = handler;
1926 sigemptyset(&act.sa_mask);
1927 act.sa_flags = 0;
1928#ifdef SA_RESTART
1929 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1930#endif
1931 if (sigaction(signo, &act, &oact) == -1)
36477c24 1932 return SIG_ERR;
ff68c719 1933 else
36477c24 1934 return oact.sa_handler;
ff68c719 1935}
1936
1937Sighandler_t
8ac85365 1938rsignal_state(int signo)
ff68c719 1939{
1940 struct sigaction oact;
1941
1942 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
1943 return SIG_ERR;
1944 else
1945 return oact.sa_handler;
1946}
1947
1948int
8ac85365 1949rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 1950{
1951 struct sigaction act;
1952
1953 act.sa_handler = handler;
1954 sigemptyset(&act.sa_mask);
1955 act.sa_flags = 0;
1956#ifdef SA_RESTART
1957 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
1958#endif
1959 return sigaction(signo, &act, save);
1960}
1961
1962int
8ac85365 1963rsignal_restore(int signo, Sigsave_t *save)
ff68c719 1964{
1965 return sigaction(signo, save, (struct sigaction *)NULL);
1966}
1967
1968#else /* !HAS_SIGACTION */
1969
1970Sighandler_t
4e35701f 1971rsignal(int signo, Sighandler_t handler)
ff68c719 1972{
1973 return signal(signo, handler);
1974}
1975
1976static int sig_trapped;
1977
1978static
1979Signal_t
4e35701f 1980sig_trap(int signo)
ff68c719 1981{
1982 sig_trapped++;
1983}
1984
1985Sighandler_t
4e35701f 1986rsignal_state(int signo)
ff68c719 1987{
1988 Sighandler_t oldsig;
1989
1990 sig_trapped = 0;
1991 oldsig = signal(signo, sig_trap);
1992 signal(signo, oldsig);
1993 if (sig_trapped)
1994 kill(getpid(), signo);
1995 return oldsig;
1996}
1997
1998int
4e35701f 1999rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2000{
2001 *save = signal(signo, handler);
2002 return (*save == SIG_ERR) ? -1 : 0;
2003}
2004
2005int
4e35701f 2006rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2007{
2008 return (signal(signo, *save) == SIG_ERR) ? -1 : 0;
2009}
2010
2011#endif /* !HAS_SIGACTION */
2012
5f05dabc 2013 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
2014#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
79072805 2015I32
8ac85365 2016my_pclose(FILE *ptr)
a687059c 2017{
ff68c719 2018 Sigsave_t hstat, istat, qstat;
a687059c 2019 int status;
a0d0e21e 2020 SV **svp;
20188a90 2021 int pid;
03136e13
CS
2022 bool close_failed;
2023 int saved_errno;
2024#ifdef VMS
2025 int saved_vaxc_errno;
2026#endif
a687059c 2027
760ac839 2028 svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 2029 pid = (int)SvIVX(*svp);
a0d0e21e
LW
2030 SvREFCNT_dec(*svp);
2031 *svp = &sv_undef;
ddcf38b7
IZ
2032#ifdef OS2
2033 if (pid == -1) { /* Opened by popen. */
2034 return my_syspclose(ptr);
2035 }
2036#endif
03136e13
CS
2037 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2038 saved_errno = errno;
2039#ifdef VMS
2040 saved_vaxc_errno = vaxc$errno;
2041#endif
2042 }
7c0587c8
LW
2043#ifdef UTS
2044 if(kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
2045#endif
ff68c719 2046 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2047 rsignal_save(SIGINT, SIG_IGN, &istat);
2048 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306
LW
2049 do {
2050 pid = wait4pid(pid, &status, 0);
2051 } while (pid == -1 && errno == EINTR);
ff68c719 2052 rsignal_restore(SIGHUP, &hstat);
2053 rsignal_restore(SIGINT, &istat);
2054 rsignal_restore(SIGQUIT, &qstat);
03136e13
CS
2055 if (close_failed) {
2056 SETERRNO(saved_errno, saved_vaxc_errno);
2057 return -1;
2058 }
2059 return(pid < 0 ? pid : status == 0 ? 0 : (errno = 0, status));
20188a90 2060}
4633a7c4
LW
2061#endif /* !DOSISH */
2062
2d7a9237 2063#if !defined(DOSISH) || defined(OS2) || defined(WIN32)
79072805 2064I32
8ac85365 2065wait4pid(int pid, int *statusp, int flags)
20188a90 2066{
79072805
LW
2067 SV *sv;
2068 SV** svp;
fc36a67e 2069 char spid[TYPE_CHARS(int)];
20188a90
LW
2070
2071 if (!pid)
2072 return -1;
20188a90
LW
2073 if (pid > 0) {
2074 sprintf(spid, "%d", pid);
79072805
LW
2075 svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
2076 if (svp && *svp != &sv_undef) {
463ee0b2 2077 *statusp = SvIVX(*svp);
748a9306 2078 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2079 return pid;
2080 }
2081 }
2082 else {
79072805 2083 HE *entry;
20188a90 2084
79072805
LW
2085 hv_iterinit(pidstatus);
2086 if (entry = hv_iternext(pidstatus)) {
a0d0e21e 2087 pid = atoi(hv_iterkey(entry,(I32*)statusp));
79072805 2088 sv = hv_iterval(pidstatus,entry);
463ee0b2 2089 *statusp = SvIVX(sv);
20188a90 2090 sprintf(spid, "%d", pid);
748a9306 2091 (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2092 return pid;
2093 }
2094 }
79072805 2095#ifdef HAS_WAITPID
367f3c24
IZ
2096# ifdef HAS_WAITPID_RUNTIME
2097 if (!HAS_WAITPID_RUNTIME)
2098 goto hard_way;
2099# endif
79072805 2100 return waitpid(pid,statusp,flags);
367f3c24
IZ
2101#endif
2102#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2103 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24
IZ
2104#endif
2105#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2106 hard_way:
a0d0e21e
LW
2107 {
2108 I32 result;
2109 if (flags)
2110 croak("Can't do waitpid with flags");
2111 else {
2112 while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
2113 pidgone(result,*statusp);
2114 if (result < 0)
2115 *statusp = -1;
2116 }
2117 return result;
a687059c
LW
2118 }
2119#endif
a687059c 2120}
2d7a9237 2121#endif /* !DOSISH || OS2 || WIN32 */
a687059c 2122
7c0587c8 2123void
de3bb511 2124/*SUPPRESS 590*/
8ac85365 2125pidgone(int pid, int status)
a687059c 2126{
79072805 2127 register SV *sv;
fc36a67e 2128 char spid[TYPE_CHARS(int)];
a687059c 2129
20188a90 2130 sprintf(spid, "%d", pid);
79072805 2131 sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2132 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2133 SvIVX(sv) = status;
20188a90 2134 return;
a687059c
LW
2135}
2136
55497cff 2137#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2138int pclose();
ddcf38b7
IZ
2139#ifdef HAS_FORK
2140int /* Cannot prototype with I32
2141 in os2ish.h. */
2142my_syspclose(ptr)
2143#else
79072805
LW
2144I32
2145my_pclose(ptr)
ddcf38b7 2146#endif
760ac839 2147PerlIO *ptr;
a687059c 2148{
760ac839
LW
2149 /* Needs work for PerlIO ! */
2150 FILE *f = PerlIO_findFILE(ptr);
2151 I32 result = pclose(f);
2152 PerlIO_releaseFILE(ptr,f);
2153 return result;
a687059c 2154}
7c0587c8 2155#endif
9f68db38
LW
2156
2157void
8ac85365 2158repeatcpy(register char *to, register char *from, I32 len, register I32 count)
9f68db38 2159{
79072805 2160 register I32 todo;
9f68db38
LW
2161 register char *frombase = from;
2162
2163 if (len == 1) {
2164 todo = *from;
2165 while (count-- > 0)
2166 *to++ = todo;
2167 return;
2168 }
2169 while (count-- > 0) {
2170 for (todo = len; todo > 0; todo--) {
2171 *to++ = *from++;
2172 }
2173 from = frombase;
2174 }
2175}
0f85fab0
LW
2176
2177#ifndef CASTNEGFLOAT
463ee0b2 2178U32
79072805 2179cast_ulong(f)
0f85fab0
LW
2180double f;
2181{
2182 long along;
2183
27e2fb84 2184#if CASTFLAGS & 2
34de22dd
LW
2185# define BIGDOUBLE 2147483648.0
2186 if (f >= BIGDOUBLE)
2187 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2188#endif
0f85fab0
LW
2189 if (f >= 0.0)
2190 return (unsigned long)f;
2191 along = (long)f;
2192 return (unsigned long)along;
2193}
ed6116ce
LW
2194# undef BIGDOUBLE
2195#endif
2196
2197#ifndef CASTI32
5d94fbed 2198
5d94fbed
AD
2199/* Unfortunately, on some systems the cast_uv() function doesn't
2200 work with the system-supplied definition of ULONG_MAX. The
2201 comparison (f >= ULONG_MAX) always comes out true. It must be a
2202 problem with the compiler constant folding.
2203
2204 In any case, this workaround should be fine on any two's complement
2205 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2206 ccflags.
2207 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2208*/
1eb770ff 2209
2210/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2211 of LONG_(MIN/MAX).
2212 -- Kenneth Albanowski <kjahds@kjahds.com>
2213*/
2214
20cec16a 2215#ifndef MY_UV_MAX
2216# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed
AD
2217#endif
2218
ed6116ce
LW
2219I32
2220cast_i32(f)
2221double f;
2222{
20cec16a 2223 if (f >= I32_MAX)
2224 return (I32) I32_MAX;
2225 if (f <= I32_MIN)
2226 return (I32) I32_MIN;
ed6116ce
LW
2227 return (I32) f;
2228}
a0d0e21e
LW
2229
2230IV
2231cast_iv(f)
2232double f;
2233{
20cec16a 2234 if (f >= IV_MAX)
2235 return (IV) IV_MAX;
2236 if (f <= IV_MIN)
2237 return (IV) IV_MIN;
a0d0e21e
LW
2238 return (IV) f;
2239}
5d94fbed
AD
2240
2241UV
2242cast_uv(f)
2243double f;
2244{
20cec16a 2245 if (f >= MY_UV_MAX)
2246 return (UV) MY_UV_MAX;
5d94fbed
AD
2247 return (UV) f;
2248}
2249
0f85fab0 2250#endif
62b28dd9 2251
fe14fcc3 2252#ifndef HAS_RENAME
79072805 2253I32
62b28dd9
LW
2254same_dirent(a,b)
2255char *a;
2256char *b;
2257{
93a17b20
LW
2258 char *fa = strrchr(a,'/');
2259 char *fb = strrchr(b,'/');
62b28dd9
LW
2260 struct stat tmpstatbuf1;
2261 struct stat tmpstatbuf2;
46fc3d4c 2262 SV *tmpsv = sv_newmortal();
62b28dd9
LW
2263
2264 if (fa)
2265 fa++;
2266 else
2267 fa = a;
2268 if (fb)
2269 fb++;
2270 else
2271 fb = b;
2272 if (strNE(a,b))
2273 return FALSE;
2274 if (fa == a)
46fc3d4c 2275 sv_setpv(tmpsv, ".");
62b28dd9 2276 else
46fc3d4c 2277 sv_setpvn(tmpsv, a, fa - a);
2278 if (Stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2279 return FALSE;
2280 if (fb == b)
46fc3d4c 2281 sv_setpv(tmpsv, ".");
62b28dd9 2282 else
46fc3d4c 2283 sv_setpvn(tmpsv, b, fb - b);
2284 if (Stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2285 return FALSE;
2286 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2287 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2288}
fe14fcc3
LW
2289#endif /* !HAS_RENAME */
2290
55497cff 2291UV
8ac85365 2292scan_oct(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2293{
2294 register char *s = start;
55497cff 2295 register UV retval = 0;
2296 bool overflowed = FALSE;
fe14fcc3 2297
748a9306 2298 while (len && *s >= '0' && *s <= '7') {
55497cff 2299 register UV n = retval << 3;
2300 if (!overflowed && (n >> 3) != retval) {
2301 warn("Integer overflow in octal number");
2302 overflowed = TRUE;
2303 }
2304 retval = n | (*s++ - '0');
748a9306 2305 len--;
fe14fcc3 2306 }
748a9306
LW
2307 if (dowarn && len && (*s == '8' || *s == '9'))
2308 warn("Illegal octal digit ignored");
fe14fcc3
LW
2309 *retlen = s - start;
2310 return retval;
2311}
2312
71be2cbc 2313UV
8ac85365 2314scan_hex(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2315{
2316 register char *s = start;
55497cff 2317 register UV retval = 0;
2318 bool overflowed = FALSE;
fe14fcc3
LW
2319 char *tmp;
2320
4e35701f 2321 while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
55497cff 2322 register UV n = retval << 4;
2323 if (!overflowed && (n >> 4) != retval) {
2324 warn("Integer overflow in hex number");
2325 overflowed = TRUE;
2326 }
4e35701f 2327 retval = n | ((tmp - hexdigit) & 15);
fe14fcc3
LW
2328 s++;
2329 }
2330 *retlen = s - start;
2331 return retval;
2332}
760ac839 2333
11343788 2334#ifdef USE_THREADS
12ca11f6
MB
2335#ifdef FAKE_THREADS
2336/* Very simplistic scheduler for now */
2337void
2338schedule(void)
2339{
c7848ba1 2340 thr = thr->i.next_run;
12ca11f6
MB
2341}
2342
2343void
2344perl_cond_init(cp)
2345perl_cond *cp;
2346{
2347 *cp = 0;
2348}
2349
2350void
2351perl_cond_signal(cp)
2352perl_cond *cp;
2353{
51dd5992 2354 perl_os_thread t;
12ca11f6
MB
2355 perl_cond cond = *cp;
2356
2357 if (!cond)
2358 return;
2359 t = cond->thread;
2360 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2361 t->i.next_run = thr->i.next_run;
2362 thr->i.next_run->i.prev_run = t;
2363 t->i.prev_run = thr;
2364 thr->i.next_run = t;
2365 thr->i.wait_queue = 0;
12ca11f6
MB
2366 /* Remove from the wait queue */
2367 *cp = cond->next;
2368 Safefree(cond);
2369}
2370
2371void
2372perl_cond_broadcast(cp)
2373perl_cond *cp;
2374{
51dd5992 2375 perl_os_thread t;
12ca11f6
MB
2376 perl_cond cond, cond_next;
2377
2378 for (cond = *cp; cond; cond = cond_next) {
2379 t = cond->thread;
2380 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2381 t->i.next_run = thr->i.next_run;
2382 thr->i.next_run->i.prev_run = t;
2383 t->i.prev_run = thr;
2384 thr->i.next_run = t;
2385 thr->i.wait_queue = 0;
12ca11f6
MB
2386 /* Remove from the wait queue */
2387 cond_next = cond->next;
2388 Safefree(cond);
2389 }
2390 *cp = 0;
2391}
2392
2393void
2394perl_cond_wait(cp)
2395perl_cond *cp;
2396{
2397 perl_cond cond;
2398
c7848ba1 2399 if (thr->i.next_run == thr)
12ca11f6
MB
2400 croak("panic: perl_cond_wait called by last runnable thread");
2401
0f15f207 2402 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
2403 cond->thread = thr;
2404 cond->next = *cp;
2405 *cp = cond;
c7848ba1 2406 thr->i.wait_queue = cond;
12ca11f6 2407 /* Remove ourselves from runnable queue */
c7848ba1
MB
2408 thr->i.next_run->i.prev_run = thr->i.prev_run;
2409 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
2410}
2411#endif /* FAKE_THREADS */
2412
11343788 2413#ifdef OLD_PTHREADS_API
52e1cb5e 2414struct perl_thread *
11343788
MB
2415getTHR _((void))
2416{
2417 pthread_addr_t t;
2418
2419 if (pthread_getspecific(thr_key, &t))
2420 croak("panic: pthread_getspecific");
52e1cb5e 2421 return (struct perl_thread *) t;
11343788
MB
2422}
2423#endif /* OLD_PTHREADS_API */
f93b4edd
MB
2424
2425MAGIC *
8ac85365 2426condpair_magic(SV *sv)
f93b4edd
MB
2427{
2428 MAGIC *mg;
2429
2430 SvUPGRADE(sv, SVt_PVMG);
2431 mg = mg_find(sv, 'm');
2432 if (!mg) {
2433 condpair_t *cp;
2434
2435 New(53, cp, 1, condpair_t);
2436 MUTEX_INIT(&cp->mutex);
2437 COND_INIT(&cp->owner_cond);
2438 COND_INIT(&cp->cond);
2439 cp->owner = 0;
2440 MUTEX_LOCK(&sv_mutex);
2441 mg = mg_find(sv, 'm');
2442 if (mg) {
2443 /* someone else beat us to initialising it */
2444 MUTEX_UNLOCK(&sv_mutex);
2445 MUTEX_DESTROY(&cp->mutex);
2446 COND_DESTROY(&cp->owner_cond);
2447 COND_DESTROY(&cp->cond);
2448 Safefree(cp);
2449 }
2450 else {
2451 sv_magic(sv, Nullsv, 'm', 0, 0);
2452 mg = SvMAGIC(sv);
2453 mg->mg_ptr = (char *)cp;
2454 mg->mg_len = sizeof(cp);
2455 MUTEX_UNLOCK(&sv_mutex);
bc1f4c86 2456 DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2457 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd
MB
2458 }
2459 }
2460 return mg;
2461}
a863c7d1
MB
2462
2463/*
199100c8
MB
2464 * Make a new perl thread structure using t as a prototype. Some of the
2465 * fields for the new thread are copied from the prototype thread, t,
2466 * so t should not be running in perl at the time this function is
2467 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2468 * thread calling new_struct_thread) clearly satisfies this constraint.
a863c7d1 2469 */
52e1cb5e
JH
2470struct perl_thread *
2471new_struct_thread(struct perl_thread *t)
a863c7d1 2472{
52e1cb5e 2473 struct perl_thread *thr;
a863c7d1 2474 SV *sv;
199100c8
MB
2475 SV **svp;
2476 I32 i;
2477
2478 sv = newSVpv("", 0);
52e1cb5e
JH
2479 SvGROW(sv, sizeof(struct perl_thread) + 1);
2480 SvCUR_set(sv, sizeof(struct perl_thread));
199100c8 2481 thr = (Thread) SvPVX(sv);
199100c8 2482 /* debug */
52e1cb5e 2483 memset(thr, 0xab, sizeof(struct perl_thread));
199100c8
MB
2484 markstack = 0;
2485 scopestack = 0;
2486 savestack = 0;
2487 retstack = 0;
2488 dirty = 0;
2489 localizing = 0;
2490 /* end debug */
2491
2492 thr->oursv = sv;
d55594ae 2493 init_stacks(ARGS);
a863c7d1 2494
a863c7d1 2495 curcop = &compiling;
199100c8 2496 thr->cvcache = newHV();
54b9620d 2497 thr->threadsv = newAV();
a863c7d1 2498 thr->specific = newAV();
38a03e6e
MB
2499 thr->errsv = newSVpv("", 0);
2500 thr->errhv = newHV();
a863c7d1
MB
2501 thr->flags = THRf_R_JOINABLE;
2502 MUTEX_INIT(&thr->mutex);
199100c8
MB
2503
2504 curcop = t->Tcurcop; /* XXX As good a guess as any? */
2505 defstash = t->Tdefstash; /* XXX maybe these should */
2506 curstash = t->Tcurstash; /* always be set to main? */
d55594ae
GS
2507
2508
2509 /* top_env needs to be non-zero. It points to an area
2510 in which longjmp() stuff is stored, as C callstack
2511 info there at least is thread specific this has to
2512 be per-thread. Otherwise a 'die' in a thread gives
2513 that thread the C stack of last thread to do an eval {}!
2514 See comments in scope.h
2515 Initialize top entry (as in perl.c for main thread)
2516 */
2517 start_env.je_prev = NULL;
2518 start_env.je_ret = -1;
2519 start_env.je_mustcatch = TRUE;
2520 top_env = &start_env;
2521
199100c8
MB
2522 in_eval = FALSE;
2523 restartop = 0;
2524
2525 tainted = t->Ttainted;
2526 curpm = t->Tcurpm; /* XXX No PMOP ref count */
2527 nrs = newSVsv(t->Tnrs);
2528 rs = newSVsv(t->Trs);
2529 last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
2530 ofslen = t->Tofslen;
2531 ofs = savepvn(t->Tofs, ofslen);
2532 defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2533 chopset = t->Tchopset;
2534 formtarget = newSVsv(t->Tformtarget);
2535 bodytarget = newSVsv(t->Tbodytarget);
2536 toptarget = newSVsv(t->Ttoptarget);
2537
54b9620d
MB
2538 /* Initialise all per-thread SVs that the template thread used */
2539 svp = AvARRAY(t->threadsv);
2540 for (i = 0; i <= AvFILL(t->threadsv); i++, svp++) {
199100c8
MB
2541 if (*svp && *svp != &sv_undef) {
2542 SV *sv = newSVsv(*svp);
54b9620d
MB
2543 av_store(thr->threadsv, i, sv);
2544 sv_magic(sv, 0, 0, &threadsv_names[i], 1);
199100c8 2545 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
54b9620d 2546 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
199100c8
MB
2547 }
2548 }
2549
a863c7d1
MB
2550 MUTEX_LOCK(&threads_mutex);
2551 nthreads++;
199100c8
MB
2552 thr->tid = ++threadnum;
2553 thr->next = t->next;
2554 thr->prev = t;
2555 t->next = thr;
2556 thr->next->prev = thr;
a863c7d1
MB
2557 MUTEX_UNLOCK(&threads_mutex);
2558
2559#ifdef HAVE_THREAD_INTERN
2560 init_thread_intern(thr);
a863c7d1 2561#endif /* HAVE_THREAD_INTERN */
a863c7d1
MB
2562 return thr;
2563}
11343788 2564#endif /* USE_THREADS */
760ac839
LW
2565
2566#ifdef HUGE_VAL
2567/*
2568 * This hack is to force load of "huge" support from libm.a
2569 * So it is in perl for (say) POSIX to use.
2570 * Needed for SunOS with Sun's 'acc' for example.
2571 */
2572double
8ac85365 2573Perl_huge(void)
760ac839
LW
2574{
2575 return HUGE_VAL;
2576}
2577#endif
4e35701f 2578
22239a37
NIS
2579#ifdef PERL_GLOBAL_STRUCT
2580struct perl_vars *
2581Perl_GetVars(void)
2582{
2583 return &Perl_Vars;
2584}
31fb1209
NIS
2585#endif
2586
2587char **
2588get_op_names(void)
2589{
2590 return op_name;
2591}
2592
2593char **
2594get_op_descs(void)
2595{
2596 return op_desc;
2597}