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