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