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