remove stray K&R-isms
[p5sagit/p5-mst-13.2.git] / util.c
diff --git a/util.c b/util.c
index e4f408d..1fcc82b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,6 +1,6 @@
 /*    util.c
  *
- *    Copyright (c) 1991-1997, Larry Wall
+ *    Copyright (c) 1991-1999, Larry Wall
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -14,7 +14,6 @@
 
 #include "EXTERN.h"
 #include "perl.h"
-#include "perlmem.h"
 
 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
 #include <signal.h>
@@ -63,9 +62,7 @@ long lastxycount[MAXXCOUNT][MAXYCOUNT];
 
 #endif
 
-#ifndef MYMALLOC
-
-/* paranoid version of malloc */
+/* paranoid version of system's malloc() */
 
 /* NOTE:  Do not call the next three routines directly.  Use the macros
  * in handy.h, so that we can easily redefine everything to do tracking of
@@ -74,7 +71,7 @@ long lastxycount[MAXXCOUNT][MAXYCOUNT];
  */
 
 Malloc_t
-safemalloc(MEM_SIZE size)
+safesysmalloc(MEM_SIZE size)
 {
     Malloc_t ptr;
 #ifdef HAS_64K_LIMIT
@@ -89,26 +86,26 @@ safemalloc(MEM_SIZE size)
 #endif
     ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
 #if !(defined(I286) || defined(atarist))
-    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
+    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
 #else
-    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size));
+    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
 #endif
     if (ptr != Nullch)
        return ptr;
-    else if (nomemok)
+    else if (PL_nomemok)
        return Nullch;
     else {
-       PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
+       PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
        my_exit(1);
         return Nullch;
     }
     /*NOTREACHED*/
 }
 
-/* paranoid version of realloc */
+/* paranoid version of system's realloc() */
 
 Malloc_t
-saferealloc(Malloc_t where,MEM_SIZE size)
+safesysrealloc(Malloc_t where,MEM_SIZE size)
 {
     Malloc_t ptr;
 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
@@ -122,47 +119,52 @@ saferealloc(Malloc_t where,MEM_SIZE size)
        my_exit(1);
     }
 #endif /* HAS_64K_LIMIT */
+    if (!size) {
+       safesysfree(where);
+       return NULL;
+    }
+
     if (!where)
-       croak("Null realloc");
+       return safesysmalloc(size);
 #ifdef DEBUGGING
     if ((long)size < 0)
        croak("panic: realloc");
 #endif
-    ptr = PerlMem_realloc(where,size?size:1);  /* realloc(0) is NASTY on our system */
+    ptr = PerlMem_realloc(where,size);
 
 #if !(defined(I286) || defined(atarist))
     DEBUG_m( {
-       PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,an++);
-       PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
+       PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++);
+       PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
     } )
 #else
     DEBUG_m( {
-       PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,an++);
-       PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
+       PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++);
+       PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
     } )
 #endif
 
     if (ptr != Nullch)
        return ptr;
-    else if (nomemok)
+    else if (PL_nomemok)
        return Nullch;
     else {
-       PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
+       PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
        my_exit(1);
        return Nullch;
     }
     /*NOTREACHED*/
 }
 
-/* safe version of free */
+/* safe version of system's free() */
 
 Free_t
-safefree(Malloc_t where)
+safesysfree(Malloc_t where)
 {
 #if !(defined(I286) || defined(atarist))
-    DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,an++));
+    DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
 #else
-    DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,an++));
+    DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++));
 #endif
     if (where) {
        /*SUPPRESS 701*/
@@ -170,10 +172,10 @@ safefree(Malloc_t where)
     }
 }
 
-/* safe version of calloc */
+/* safe version of system's calloc() */
 
 Malloc_t
-safecalloc(MEM_SIZE count, MEM_SIZE size)
+safesyscalloc(MEM_SIZE count, MEM_SIZE size)
 {
     Malloc_t ptr;
 
@@ -191,26 +193,24 @@ safecalloc(MEM_SIZE count, MEM_SIZE size)
     size *= count;
     ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
 #if !(defined(I286) || defined(atarist))
-    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld  x %ld bytes\n",ptr,an++,(long)count,(long)size));
+    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld  x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
 #else
-    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,an++,(long)count,(long)size));
+    DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
 #endif
     if (ptr != Nullch) {
        memset((void*)ptr, 0, size);
        return ptr;
     }
-    else if (nomemok)
+    else if (PL_nomemok)
        return Nullch;
     else {
-       PerlIO_puts(PerlIO_stderr(),no_mem) FLUSH;
+       PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
        my_exit(1);
        return Nullch;
     }
     /*NOTREACHED*/
 }
 
-#endif /* !MYMALLOC */
-
 #ifdef LEAKTEST
 
 struct mem_test_strut {
@@ -385,16 +385,16 @@ delimcpy(register char *to, register char *toend, register char *from, register
 /* This routine was donated by Corey Satten. */
 
 char *
-instr(register char *big, register char *little)
+instr(register const char *big, register const char *little)
 {
-    register char *s, *x;
+    register const char *s, *x;
     register I32 first;
 
     if (!little)
-       return big;
+       return (char*)big;
     first = *little++;
     if (!first)
-       return big;
+       return (char*)big;
     while (*big) {
        if (*big++ != first)
            continue;
@@ -407,7 +407,7 @@ instr(register char *big, register char *little)
            }
        }
        if (!*s)
-           return big-1;
+           return (char*)(big-1);
     }
     return Nullch;
 }
@@ -415,14 +415,14 @@ instr(register char *big, register char *little)
 /* same as instr but allow embedded nulls */
 
 char *
-ninstr(register char *big, register char *bigend, char *little, char *lend)
+ninstr(register const char *big, register const char *bigend, const char *little, const char *lend)
 {
-    register char *s, *x;
+    register const char *s, *x;
     register I32 first = *little;
-    register char *littleend = lend;
+    register const char *littleend = lend;
 
     if (!first && little >= littleend)
-       return big;
+       return (char*)big;
     if (bigend - big < littleend - little)
        return Nullch;
     bigend -= littleend - little++;
@@ -436,7 +436,7 @@ ninstr(register char *big, register char *bigend, char *little, char *lend)
            }
        }
        if (s >= littleend)
-           return big-1;
+           return (char*)(big-1);
     }
     return Nullch;
 }
@@ -444,15 +444,15 @@ ninstr(register char *big, register char *bigend, char *little, char *lend)
 /* reverse of the above--find last substring */
 
 char *
-rninstr(register char *big, char *bigend, char *little, char *lend)
+rninstr(register const char *big, const char *bigend, const char *little, const char *lend)
 {
-    register char *bigbeg;
-    register char *s, *x;
+    register const char *bigbeg;
+    register const char *s, *x;
     register I32 first = *little;
-    register char *littleend = lend;
+    register const char *littleend = lend;
 
     if (!first && little >= littleend)
-       return bigend;
+       return (char*)bigend;
     bigbeg = big;
     big = bigend - (littleend - little++);
     while (big >= bigbeg) {
@@ -465,7 +465,7 @@ rninstr(register char *big, char *bigend, char *little, char *lend)
            }
        }
        if (s >= littleend)
-           return big+1;
+           return (char*)(big+1);
     }
     return Nullch;
 }
@@ -474,7 +474,7 @@ rninstr(register char *big, char *bigend, char *little, char *lend)
  * Set up for a new ctype locale.
  */
 void
-perl_new_ctype(char *newctype)
+perl_new_ctype(const char *newctype)
 {
 #ifdef USE_LOCALE_CTYPE
 
@@ -482,11 +482,11 @@ perl_new_ctype(char *newctype)
 
     for (i = 0; i < 256; i++) {
        if (isUPPER_LC(i))
-           fold_locale[i] = toLOWER_LC(i);
+           PL_fold_locale[i] = toLOWER_LC(i);
        else if (isLOWER_LC(i))
-           fold_locale[i] = toUPPER_LC(i);
+           PL_fold_locale[i] = toUPPER_LC(i);
        else
-           fold_locale[i] = i;
+           PL_fold_locale[i] = i;
     }
 
 #endif /* USE_LOCALE_CTYPE */
@@ -496,27 +496,27 @@ perl_new_ctype(char *newctype)
  * Set up for a new collation locale.
  */
 void
-perl_new_collate(char *newcoll)
+perl_new_collate(const char *newcoll)
 {
 #ifdef USE_LOCALE_COLLATE
 
     if (! newcoll) {
-       if (collation_name) {
-           ++collation_ix;
-           Safefree(collation_name);
-           collation_name = NULL;
-           collation_standard = TRUE;
-           collxfrm_base = 0;
-           collxfrm_mult = 2;
+       if (PL_collation_name) {
+           ++PL_collation_ix;
+           Safefree(PL_collation_name);
+           PL_collation_name = NULL;
+           PL_collation_standard = TRUE;
+           PL_collxfrm_base = 0;
+           PL_collxfrm_mult = 2;
        }
        return;
     }
 
-    if (! collation_name || strNE(collation_name, newcoll)) {
-       ++collation_ix;
-       Safefree(collation_name);
-       collation_name = savepv(newcoll);
-       collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
+    if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
+       ++PL_collation_ix;
+       Safefree(PL_collation_name);
+       PL_collation_name = savepv(newcoll);
+       PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
 
        {
          /*  2: at most so many chars ('a', 'b'). */
@@ -528,8 +528,8 @@ perl_new_collate(char *newcoll)
          SSize_t mult = fb - fa;
          if (mult < 1)
              croak("strxfrm() gets absurd");
-         collxfrm_base = (fa > mult) ? (fa - mult) : 0;
-         collxfrm_mult = mult;
+         PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
+         PL_collxfrm_mult = mult;
        }
     }
 
@@ -540,25 +540,25 @@ perl_new_collate(char *newcoll)
  * Set up for a new numeric locale.
  */
 void
-perl_new_numeric(char *newnum)
+perl_new_numeric(const char *newnum)
 {
 #ifdef USE_LOCALE_NUMERIC
 
     if (! newnum) {
-       if (numeric_name) {
-           Safefree(numeric_name);
-           numeric_name = NULL;
-           numeric_standard = TRUE;
-           numeric_local = TRUE;
+       if (PL_numeric_name) {
+           Safefree(PL_numeric_name);
+           PL_numeric_name = NULL;
+           PL_numeric_standard = TRUE;
+           PL_numeric_local = TRUE;
        }
        return;
     }
 
-    if (! numeric_name || strNE(numeric_name, newnum)) {
-       Safefree(numeric_name);
-       numeric_name = savepv(newnum);
-       numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
-       numeric_local = TRUE;
+    if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
+       Safefree(PL_numeric_name);
+       PL_numeric_name = savepv(newnum);
+       PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
+       PL_numeric_local = TRUE;
     }
 
 #endif /* USE_LOCALE_NUMERIC */
@@ -569,10 +569,10 @@ perl_set_numeric_standard(void)
 {
 #ifdef USE_LOCALE_NUMERIC
 
-    if (! numeric_standard) {
+    if (! PL_numeric_standard) {
        setlocale(LC_NUMERIC, "C");
-       numeric_standard = TRUE;
-       numeric_local = FALSE;
+       PL_numeric_standard = TRUE;
+       PL_numeric_local = FALSE;
     }
 
 #endif /* USE_LOCALE_NUMERIC */
@@ -583,10 +583,10 @@ perl_set_numeric_local(void)
 {
 #ifdef USE_LOCALE_NUMERIC
 
-    if (! numeric_local) {
-       setlocale(LC_NUMERIC, numeric_name);
-       numeric_standard = FALSE;
-       numeric_local = TRUE;
+    if (! PL_numeric_local) {
+       setlocale(LC_NUMERIC, PL_numeric_name);
+       PL_numeric_standard = FALSE;
+       PL_numeric_local = TRUE;
     }
 
 #endif /* USE_LOCALE_NUMERIC */
@@ -617,6 +617,9 @@ perl_init_i18nl10n(int printwarn)
 #ifdef USE_LOCALE_NUMERIC
     char *curnum     = NULL;
 #endif /* USE_LOCALE_NUMERIC */
+#ifdef __GLIBC__
+    char *language   = PerlEnv_getenv("LANGUAGE");
+#endif
     char *lc_all     = PerlEnv_getenv("LC_ALL");
     char *lang       = PerlEnv_getenv("LANG");
     bool setlocale_failure = FALSE;
@@ -637,65 +640,53 @@ perl_init_i18nl10n(int printwarn)
        else
            setlocale_failure = TRUE;
     }
-    if (!setlocale_failure)
-#endif /* LC_ALL */
-    {
+    if (!setlocale_failure) {
 #ifdef USE_LOCALE_CTYPE
-       if (! (curctype = setlocale(LC_CTYPE,
-                                   (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
+       if (! (curctype =
+              setlocale(LC_CTYPE,
+                        (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
                                    ? "" : Nullch)))
            setlocale_failure = TRUE;
 #endif /* USE_LOCALE_CTYPE */
 #ifdef USE_LOCALE_COLLATE
-       if (! (curcoll = setlocale(LC_COLLATE,
-                                  (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
+       if (! (curcoll =
+              setlocale(LC_COLLATE,
+                        (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
                                   ? "" : Nullch)))
            setlocale_failure = TRUE;
 #endif /* USE_LOCALE_COLLATE */
 #ifdef USE_LOCALE_NUMERIC
-       if (! (curnum = setlocale(LC_NUMERIC,
-                                 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
+       if (! (curnum =
+              setlocale(LC_NUMERIC,
+                        (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
                                  ? "" : Nullch)))
            setlocale_failure = TRUE;
 #endif /* USE_LOCALE_NUMERIC */
     }
 
-#else /* !LOCALE_ENVIRON_REQUIRED */
+#endif /* LC_ALL */
 
-#ifdef LC_ALL
+#endif /* !LOCALE_ENVIRON_REQUIRED */
 
+#ifdef LC_ALL
     if (! setlocale(LC_ALL, ""))
        setlocale_failure = TRUE;
-    else {
-#ifdef USE_LOCALE_CTYPE
-       curctype = setlocale(LC_CTYPE, Nullch);
-#endif /* USE_LOCALE_CTYPE */
-#ifdef USE_LOCALE_COLLATE
-       curcoll = setlocale(LC_COLLATE, Nullch);
-#endif /* USE_LOCALE_COLLATE */
-#ifdef USE_LOCALE_NUMERIC
-       curnum = setlocale(LC_NUMERIC, Nullch);
-#endif /* USE_LOCALE_NUMERIC */
-    }
-
-#else /* !LC_ALL */
+#endif /* LC_ALL */
 
+    if (!setlocale_failure) {
 #ifdef USE_LOCALE_CTYPE
-    if (! (curctype = setlocale(LC_CTYPE, "")))
-       setlocale_failure = TRUE;
+       if (! (curctype = setlocale(LC_CTYPE, "")))
+           setlocale_failure = TRUE;
 #endif /* USE_LOCALE_CTYPE */
 #ifdef USE_LOCALE_COLLATE
-    if (! (curcoll = setlocale(LC_COLLATE, "")))
-       setlocale_failure = TRUE;
+       if (! (curcoll = setlocale(LC_COLLATE, "")))
+           setlocale_failure = TRUE;
 #endif /* USE_LOCALE_COLLATE */
 #ifdef USE_LOCALE_NUMERIC
-    if (! (curnum = setlocale(LC_NUMERIC, "")))
-       setlocale_failure = TRUE;
+       if (! (curnum = setlocale(LC_NUMERIC, "")))
+           setlocale_failure = TRUE;
 #endif /* USE_LOCALE_NUMERIC */
-
-#endif /* LC_ALL */
-
-#endif /* !LOCALE_ENVIRON_REQUIRED */
+    }
 
     if (setlocale_failure) {
        char *p;
@@ -732,6 +723,14 @@ perl_init_i18nl10n(int printwarn)
            PerlIO_printf(PerlIO_stderr(),
                "perl: warning: Please check that your locale settings:\n");
 
+#ifdef __GLIBC__
+           PerlIO_printf(PerlIO_stderr(),
+                         "\tLANGUAGE = %c%s%c,\n",
+                         language ? '"' : '(',
+                         language ? language : "unset",
+                         language ? '"' : ')');
+#endif
+
            PerlIO_printf(PerlIO_stderr(),
                          "\tLC_ALL = %c%s%c,\n",
                          lc_all ? '"' : '(',
@@ -844,29 +843,29 @@ char *
 mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
 {
     char *xbuf;
-    STRLEN xalloc, xin, xout;
+    STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
 
     /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
     /* the +1 is for the terminating NUL. */
 
-    xalloc = sizeof(collation_ix) + collxfrm_base + (collxfrm_mult * len) + 1;
-    New(171, xbuf, xalloc, char);
+    xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
+    New(171, xbuf, xAlloc, char);
     if (! xbuf)
        goto bad;
 
-    *(U32*)xbuf = collation_ix;
-    xout = sizeof(collation_ix);
+    *(U32*)xbuf = PL_collation_ix;
+    xout = sizeof(PL_collation_ix);
     for (xin = 0; xin < len; ) {
        SSize_t xused;
 
        for (;;) {
-           xused = strxfrm(xbuf + xout, s + xin, xalloc - xout);
+           xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
            if (xused == -1)
                goto bad;
-           if (xused < xalloc - xout)
+           if (xused < xAlloc - xout)
                break;
-           xalloc = (2 * xalloc) + 1;
-           Renew(xbuf, xalloc, char);
+           xAlloc = (2 * xAlloc) + 1;
+           Renew(xbuf, xAlloc, char);
            if (! xbuf)
                goto bad;
        }
@@ -879,7 +878,7 @@ mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
     }
 
     xbuf[xout] = '\0';
-    *xlen = xout - sizeof(collation_ix);
+    *xlen = xout - sizeof(PL_collation_ix);
     return xbuf;
 
   bad:
@@ -890,159 +889,257 @@ mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
 
 #endif /* USE_LOCALE_COLLATE */
 
+#define FBM_TABLE_OFFSET 2     /* Number of bytes between EOS and table*/
+
+/* As a space optimization, we do not compile tables for strings of length
+   0 and 1, and for strings of length 2 unless FBMcf_TAIL.  These are
+   special-cased in fbm_instr().
+
+   If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
+
 void
 fbm_compile(SV *sv, U32 flags /* not used yet */)
 {
-    register unsigned char *s;
-    register unsigned char *table;
+    register U8 *s;
+    register U8 *table;
     register U32 i;
-    register U32 len = SvCUR(sv);
+    STRLEN len;
     I32 rarest = 0;
     U32 frequency = 256;
 
-    sv_upgrade(sv, SVt_PVBM);
-    if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
-       return;                 /* can't have offsets that big */
+    if (flags & FBMcf_TAIL)
+       sv_catpvn(sv, "\n", 1);         /* Taken into account in fbm_instr() */
+    s = (U8*)SvPV_force(sv, len);
+    (void)SvUPGRADE(sv, SVt_PVBM);
+    if (len == 0)              /* TAIL might be on on a zero-length string. */
+       return;
     if (len > 2) {
-       Sv_Grow(sv,len + 258);
-       table = (unsigned char*)(SvPVX(sv) + len + 1);
-       s = table - 2;
+       I32 mlen = len;
+       unsigned char *sb;
+
+       if (mlen > 255)
+           mlen = 255;
+       Sv_Grow(sv,len + 256 + FBM_TABLE_OFFSET);
+       table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET);
+       s = table - 1 - FBM_TABLE_OFFSET; /* Last char */
        for (i = 0; i < 256; i++) {
-           table[i] = len;
+           table[i] = mlen;
        }
+       table[-1] = flags;              /* Not used yet */
        i = 0;
-       while (s >= (unsigned char*)(SvPVX(sv)))
-           {
-               if (table[*s] == len)
-                   table[*s] = i;
-               s--,i++;
-           }
+       sb = s - mlen;
+       while (s >= sb) {
+           if (table[*s] == mlen)
+               table[*s] = i;
+           s--, i++;
+       }
     }
     sv_magic(sv, Nullsv, 'B', Nullch, 0);      /* deep magic */
     SvVALID_on(sv);
 
     s = (unsigned char*)(SvPVX(sv));           /* deeper magic */
     for (i = 0; i < len; i++) {
-       if (freq[s[i]] < frequency) {
+       if (PL_freq[s[i]] < frequency) {
            rarest = i;
-           frequency = freq[s[i]];
+           frequency = PL_freq[s[i]];
        }
     }
     BmRARE(sv) = s[rarest];
     BmPREVIOUS(sv) = rarest;
+    BmUSEFUL(sv) = 100;                        /* Initial value */
+    if (flags & FBMcf_TAIL)
+       SvTAIL_on(sv);
     DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
 }
 
+/* If SvTAIL(littlestr), it has a fake '\n' at end. */
+/* If SvTAIL is actually due to \Z or \z, this gives false positives
+   if multiline */
+
 char *
-fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
+fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
 {
     register unsigned char *s;
-    register I32 tmp;
-    register I32 littlelen;
-    register unsigned char *little;
-    register unsigned char *table;
-    register unsigned char *olds;
-    register unsigned char *oldlittle;
+    STRLEN l;
+    register unsigned char *little = (unsigned char *)SvPV(littlestr,l);
+    register STRLEN littlelen = l;
+    register I32 multiline = flags & FBMrf_MULTILINE;
+
+    if (bigend - big < littlelen) {
+      check_tail:
+       if ( SvTAIL(littlestr) 
+            && (bigend - big == littlelen - 1)
+            && (littlelen == 1 
+                || *big == *little && memEQ(big, little, littlelen - 1)))
+           return (char*)big;
+       return Nullch;
+    }
 
-    if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
-       STRLEN len;
-       char *l = SvPV(littlestr,len);
-       if (!len) {
-           if (SvTAIL(littlestr)) {    /* Can be only 0-len constant
-                                          substr => we can ignore SvVALID */
-               if (multiline) {
-                   char *t = "\n";
-                   if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
-                                                   t, t + len))) {
-                       return (char*)s;
+    if (littlelen <= 2) {              /* Special-cased */
+       register char c;
+
+       if (littlelen == 1) {
+           if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
+               /* Know that bigend != big.  */
+               if (bigend[-1] == '\n')
+                   return (char *)(bigend - 1);
+               return (char *) bigend;
+           }
+           s = big;
+           while (s < bigend) {
+               if (*s == *little)
+                   return (char *)s;
+               s++;
+           }
+           if (SvTAIL(littlestr))
+               return (char *) bigend;
+           return Nullch;
+       }
+       if (!littlelen)
+           return (char*)big;          /* Cannot be SvTAIL! */
+
+       /* littlelen is 2 */
+       if (SvTAIL(littlestr) && !multiline) {
+           if (bigend[-1] == '\n' && bigend[-2] == *little)
+               return (char*)bigend - 2;
+           if (bigend[-1] == *little)
+               return (char*)bigend - 1;
+           return Nullch;
+       }
+       {
+           /* This should be better than FBM if c1 == c2, and almost
+              as good otherwise: maybe better since we do less indirection.
+              And we save a lot of memory by caching no table. */
+           register unsigned char c1 = little[0];
+           register unsigned char c2 = little[1];
+
+           s = big + 1;
+           bigend--;
+           if (c1 != c2) {
+               while (s <= bigend) {
+                   if (s[0] == c2) {
+                       if (s[-1] == c1)
+                           return (char*)s - 1;
+                       s += 2;
+                       continue;
                    }
+                 next_chars:
+                   if (s[0] == c1) {
+                       if (s == bigend)
+                           goto check_1char_anchor;
+                       if (s[1] == c2)
+                           return (char*)s;
+                       else {
+                           s++;
+                           goto next_chars;
+                       }
+                   }
+                   else
+                       s += 2;
+               }
+               goto check_1char_anchor;
+           }
+           /* Now c1 == c2 */
+           while (s <= bigend) {
+               if (s[0] == c1) {
+                   if (s[-1] == c1)
+                       return (char*)s - 1;
+                   if (s == bigend)
+                       goto check_1char_anchor;
+                   if (s[1] == c1)
+                       return (char*)s;
+                   s += 3;
                }
-               if (bigend > big && bigend[-1] == '\n')
-                   return (char *)(bigend - 1);
                else
-                   return (char *) bigend;
+                   s += 2;
            }
-           return (char*)big;
        }
-       return ninstr((char*)big,(char*)bigend, l, l + len);
+      check_1char_anchor:              /* One char and anchor! */
+       if (SvTAIL(littlestr) && (*bigend == *little))
+           return (char *)bigend;      /* bigend is already decremented. */
+       return Nullch;
     }
-
-    littlelen = SvCUR(littlestr);
     if (SvTAIL(littlestr) && !multiline) {     /* tail anchored? */
-       if (littlelen > bigend - big)
-           return Nullch;
-       little = (unsigned char*)SvPVX(littlestr);
        s = bigend - littlelen;
-       if (s > big
+       if (s >= big
            && bigend[-1] == '\n' 
-           && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
-           return (char*)s - 1;        /* how sweet it is */
-       else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
+           && *s == *little 
+           /* Automatically of length > 2 */
+           && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
            return (char*)s;            /* how sweet it is */
+       if (s[1] == *little && memEQ((char*)s + 2,(char*)little + 1,
+                                    littlelen - 2))
+           return (char*)s + 1;        /* how sweet it is */
        return Nullch;
     }
-    if (littlelen <= 2) {
-       unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
-       unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
-       /* This may do extra comparisons if littlelen == 2, but this
-          should be hidden in the noise since we do less indirection. */
-       
-       s = big;
-       bigend -= littlelen;
-       while (s <= bigend) {
-           if (s[0] == c1 
-               && (littlelen == 1 || s[1] == c2)
-               && (!SvTAIL(littlestr)
-                   || s == bigend
-                   || s[littlelen] == '\n')) /* Automatically multiline */
-           {
+    if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
+       char *b = ninstr((char*)big,(char*)bigend,
+                        (char*)little, (char*)little + littlelen);
+
+       if (!b && SvTAIL(littlestr)) {  /* Automatically multiline!  */
+           /* Chop \n from littlestr: */
+           s = bigend - littlelen + 1;
+           if (*s == *little && memEQ((char*)s + 1, (char*)little + 1,
+                                      littlelen - 2))
                return (char*)s;
-           }
-           s++;
+           return Nullch;
        }
-       return Nullch;
+       return b;
     }
-    table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
-    if (--littlelen >= bigend - big)
-       return Nullch;
-    s = big + littlelen;
-    oldlittle = little = table - 2;
-    if (s < bigend) {
-      top2:
-       /*SUPPRESS 560*/
-       if (tmp = table[*s]) {
+    
+    {  /* Do actual FBM.  */
+       register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET;
+       register unsigned char *oldlittle;
+
+       if (littlelen > bigend - big)
+           return Nullch;
+       --littlelen;                    /* Last char found by table lookup */
+
+       s = big + littlelen;
+       little += littlelen;            /* last char */
+       oldlittle = little;
+       if (s < bigend) {
+           register I32 tmp;
+
+         top2:
+           /*SUPPRESS 560*/
+           if (tmp = table[*s]) {
 #ifdef POINTERRIGOR
-           if (bigend - s > tmp) {
+               if (bigend - s > tmp) {
+                   s += tmp;
+                   goto top2;
+               }
                s += tmp;
-               goto top2;
-           }
 #else
-           if ((s += tmp) < bigend)
-               goto top2;
-#endif
-           return Nullch;
-       }
-       else {
-           tmp = littlelen;    /* less expensive than calling strncmp() */
-           olds = s;
-           while (tmp--) {
-               if (*--s == *--little)
-                   continue;
-             differ:
-               s = olds + 1;   /* here we pay the price for failure */
-               little = oldlittle;
-               if (s < bigend) /* fake up continue to outer loop */
+               if ((s += tmp) < bigend)
                    goto top2;
-               return Nullch;
+#endif
+               goto check_end;
+           }
+           else {              /* less expensive than calling strncmp() */
+               register unsigned char *olds = s;
+
+               tmp = littlelen;
+
+               while (tmp--) {
+                   if (*--s == *--little)
+                       continue;
+                 differ:
+                   s = olds + 1;       /* here we pay the price for failure */
+                   little = oldlittle;
+                   if (s < bigend)     /* fake up continue to outer loop */
+                       goto top2;
+                   goto check_end;
+               }
+               return (char *)s;
            }
-           if (SvTAIL(littlestr)       /* automatically multiline */
-               && olds + 1 != bigend
-               && olds[1] != '\n') 
-               goto differ;
-           return (char *)s;
        }
+      check_end:
+       if ( s == bigend && (table[-1] & FBMcf_TAIL)
+            && memEQ(bigend - littlelen, oldlittle - littlelen, littlelen) )
+           return (char*)bigend - littlelen;
+       return Nullch;
     }
-    return Nullch;
 }
 
 /* start_shift, end_shift are positive quantities which give offsets
@@ -1051,13 +1148,19 @@ fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr)
    old_posp is the way of communication between consequent calls if
    the next call needs to find the . 
    The initial *old_posp should be -1.
-   Note that we do not take into account SvTAIL, so it may give wrong
-   positives if _ALL flag is set.
+
+   Note that we take into account SvTAIL, so one can get extra
+   optimizations if _ALL flag is set.
  */
 
+/* If SvTAIL is actually due to \Z or \z, this gives false positives
+   if PL_multiline.  In fact if !PL_multiline the autoritative answer
+   is not supported yet. */
+
 char *
 screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
 {
+    dTHR;
     register unsigned char *s, *x;
     register unsigned char *big;
     register I32 pos;
@@ -1069,9 +1172,19 @@ screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_
     I32 found = 0;
 
     if (*old_posp == -1
-       ? (pos = screamfirst[BmRARE(littlestr)]) < 0
-       : (((pos = *old_posp), pos += screamnext[pos]) == 0))
+       ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
+       : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) {
+      cant_find:
+       if ( BmRARE(littlestr) == '\n' 
+            && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) {
+           little = (unsigned char *)(SvPVX(littlestr));
+           littleend = little + SvCUR(littlestr);
+           first = *little++;
+           goto check_tail;
+       }
        return Nullch;
+    }
+
     little = (unsigned char *)(SvPVX(littlestr));
     littleend = little + SvCUR(littlestr);
     first = *little++;
@@ -1080,14 +1193,18 @@ screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_
     big = (unsigned char *)(SvPVX(bigstr));
     /* The value of pos we can stop at: */
     stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
-    if (previous + start_shift > stop_pos) return Nullch;
+    if (previous + start_shift > stop_pos) {
+       if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */
+           goto check_tail;
+       return Nullch;
+    }
     while (pos < previous + start_shift) {
-       if (!(pos += screamnext[pos]))
-           return Nullch;
+       if (!(pos += PL_screamnext[pos]))
+           goto cant_find;
     }
 #ifdef POINTERRIGOR
     do {
-       if (pos >= stop_pos) return Nullch;
+       if (pos >= stop_pos) break;
        if (big[pos-previous] != first)
            continue;
        for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
@@ -1101,12 +1218,12 @@ screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_
            if (!last) return (char *)(big+pos-previous);
            found = 1;
        }
-    } while ( pos += screamnext[pos] );
+    } while ( pos += PL_screamnext[pos] );
     return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
 #else /* !POINTERRIGOR */
     big -= previous;
     do {
-       if (pos >= stop_pos) return Nullch;
+       if (pos >= stop_pos) break;
        if (big[pos] != first)
            continue;
        for (x=big+pos+1,s=little; s < littleend; /**/ ) {
@@ -1120,18 +1237,32 @@ screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_
            if (!last) return (char *)(big+pos);
            found = 1;
        }
-    } while ( pos += screamnext[pos] );
-    return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
+    } while ( pos += PL_screamnext[pos] );
+    if (last && found) 
+       return (char *)(big+(*old_posp));
 #endif /* POINTERRIGOR */
+  check_tail:
+    if (!SvTAIL(littlestr) || (end_shift > 0))
+       return Nullch;
+    /* Ignore the trailing "\n".  This code is not microoptimized */
+    big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr));
+    stop_pos = littleend - little;     /* Actual littlestr len */
+    if (stop_pos == 0)
+       return (char*)big;
+    big -= stop_pos;
+    if (*big == first
+       && ((stop_pos == 1) || memEQ(big + 1, little, stop_pos - 1)))
+       return (char*)big;
+    return Nullch;
 }
 
 I32
-ibcmp(char *s1, char *s2, register I32 len)
+ibcmp(const char *s1, const char *s2, register I32 len)
 {
     register U8 *a = (U8 *)s1;
     register U8 *b = (U8 *)s2;
     while (len--) {
-       if (*a != *b && *a != fold[*b])
+       if (*a != *b && *a != PL_fold[*b])
            return 1;
        a++,b++;
     }
@@ -1139,12 +1270,12 @@ ibcmp(char *s1, char *s2, register I32 len)
 }
 
 I32
-ibcmp_locale(char *s1, char *s2, register I32 len)
+ibcmp_locale(const char *s1, const char *s2, register I32 len)
 {
     register U8 *a = (U8 *)s1;
     register U8 *b = (U8 *)s2;
     while (len--) {
-       if (*a != *b && *a != fold_locale[*b])
+       if (*a != *b && *a != PL_fold_locale[*b])
            return 1;
        a++,b++;
     }
@@ -1154,7 +1285,7 @@ ibcmp_locale(char *s1, char *s2, register I32 len)
 /* copy a string to a safe spot */
 
 char *
-savepv(char *sv)
+savepv(const char *sv)
 {
     register char *newaddr;
 
@@ -1166,7 +1297,7 @@ savepv(char *sv)
 /* same thing but with a known length */
 
 char *
-savepvn(char *sv, register I32 len)
+savepvn(const char *sv, register I32 len)
 {
     register char *newaddr;
 
@@ -1178,121 +1309,101 @@ savepvn(char *sv, register I32 len)
 
 /* the SV for form() and mess() is not kept in an arena */
 
-static SV *
+STATIC SV *
 mess_alloc(void)
 {
+    dTHR;
     SV *sv;
     XPVMG *any;
 
+    if (!PL_dirty)
+       return sv_2mortal(newSVpvn("",0));
+
+    if (PL_mess_sv)
+       return PL_mess_sv;
+
     /* Create as PVMG now, to avoid any upgrading later */
     New(905, sv, 1, SV);
     Newz(905, any, 1, XPVMG);
     SvFLAGS(sv) = SVt_PVMG;
     SvANY(sv) = (void*)any;
     SvREFCNT(sv) = 1 << 30; /* practically infinite */
+    PL_mess_sv = sv;
     return sv;
 }
 
-#ifdef I_STDARG
 char *
 form(const char* pat, ...)
-#else
-/*VARARGS0*/
-char *
-form(pat, va_alist)
-    const char *pat;
-    va_dcl
-#endif
 {
+    SV *sv = mess_alloc();
     va_list args;
-#ifdef I_STDARG
     va_start(args, pat);
-#else
-    va_start(args);
-#endif
-    if (!mess_sv)
-       mess_sv = mess_alloc();
-    sv_vsetpvfn(mess_sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
+    sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
     va_end(args);
-    return SvPVX(mess_sv);
+    return SvPVX(sv);
 }
 
-char *
+SV *
 mess(const char *pat, va_list *args)
 {
-    SV *sv;
+    SV *sv = mess_alloc();
     static char dgd[] = " during global destruction.\n";
 
-    if (!mess_sv)
-       mess_sv = mess_alloc();
-    sv = mess_sv;
     sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
     if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
        dTHR;
-       if (dirty)
-           sv_catpv(sv, dgd);
-       else {
-           if (curcop->cop_line)
-               sv_catpvf(sv, " at %_ line %ld",
-                         GvSV(curcop->cop_filegv), (long)curcop->cop_line);
-           if (GvIO(last_in_gv) && IoLINES(GvIOp(last_in_gv))) {
-               bool line_mode = (RsSIMPLE(rs) &&
-                                 SvLEN(rs) == 1 && *SvPVX(rs) == '\n');
-               sv_catpvf(sv, ", <%s> %s %ld",
-                         last_in_gv == argvgv ? "" : GvNAME(last_in_gv),
-                         line_mode ? "line" : "chunk", 
-                         (long)IoLINES(GvIOp(last_in_gv)));
-           }
-           sv_catpv(sv, ".\n");
+       if (PL_curcop->cop_line)
+           sv_catpvf(sv, " at %_ line %ld",
+                     GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line);
+       if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
+           bool line_mode = (RsSIMPLE(PL_rs) &&
+                             SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
+           sv_catpvf(sv, ", <%s> %s %ld",
+                     PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
+                     line_mode ? "line" : "chunk", 
+                     (long)IoLINES(GvIOp(PL_last_in_gv)));
        }
+       sv_catpv(sv, PL_dirty ? dgd : ".\n");
     }
-    return SvPVX(sv);
+    return sv;
 }
 
-#ifdef I_STDARG
 OP *
 die(const char* pat, ...)
-#else
-/*VARARGS0*/
-OP *
-die(pat, va_alist)
-    const char *pat;
-    va_dcl
-#endif
 {
     dTHR;
     va_list args;
     char *message;
-    int was_in_eval = in_eval;
+    int was_in_eval = PL_in_eval;
     HV *stash;
     GV *gv;
     CV *cv;
+    SV *msv;
+    STRLEN msglen;
 
-#ifdef USE_THREADS
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+    DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                          "%p: die: curstack = %p, mainstack = %p\n",
-                         thr, curstack, mainstack));
-#endif /* USE_THREADS */
+                         thr, PL_curstack, PL_mainstack));
 
-#ifdef I_STDARG
     va_start(args, pat);
-#else
-    va_start(args);
-#endif
-    message = pat ? mess(pat, &args) : Nullch;
+    if (pat) {
+       msv = mess(pat, &args);
+       message = SvPV(msv,msglen);
+    }
+    else {
+       message = Nullch;
+    }
     va_end(args);
 
-#ifdef USE_THREADS
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+    DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                          "%p: die: message = %s\ndiehook = %p\n",
-                         thr, message, diehook));
-#endif /* USE_THREADS */
-    if (diehook) {
+                         thr, message, PL_diehook));
+    if (PL_diehook) {
        /* sv_2cv might call croak() */
-       SV *olddiehook = diehook;
+       SV *olddiehook = PL_diehook;
        ENTER;
-       SAVESPTR(diehook);
-       diehook = Nullsv;
+       SAVESPTR(PL_diehook);
+       PL_diehook = Nullsv;
        cv = sv_2cv(olddiehook, &stash, &gv, 0);
        LEAVE;
        if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
@@ -1300,8 +1411,8 @@ die(pat, va_alist)
            SV *msg;
 
            ENTER;
-           if(message) {
-               msg = newSVpv(message, 0);
+           if (message) {
+               msg = newSVpvn(message, msglen);
                SvREADONLY_on(msg);
                SAVEFREESV(msg);
            }
@@ -1309,37 +1420,27 @@ die(pat, va_alist)
                msg = ERRSV;
            }
 
-           PUSHSTACK(SI_DIEHOOK);
+           PUSHSTACKi(PERLSI_DIEHOOK);
            PUSHMARK(SP);
            XPUSHs(msg);
            PUTBACK;
            perl_call_sv((SV*)cv, G_DISCARD);
-           POPSTACK();
+           POPSTACK;
            LEAVE;
        }
     }
 
-    restartop = die_where(message);
-#ifdef USE_THREADS
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+    PL_restartop = die_where(message, msglen);
+    DEBUG_S(PerlIO_printf(PerlIO_stderr(),
          "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
-         thr, restartop, was_in_eval, top_env));
-#endif /* USE_THREADS */
-    if ((!restartop && was_in_eval) || top_env->je_prev)
+         thr, PL_restartop, was_in_eval, PL_top_env));
+    if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
        JMPENV_JUMP(3);
-    return restartop;
+    return PL_restartop;
 }
 
-#ifdef I_STDARG
 void
 croak(const char* pat, ...)
-#else
-/*VARARGS0*/
-void
-croak(pat, va_alist)
-    char *pat;
-    va_dcl
-#endif
 {
     dTHR;
     va_list args;
@@ -1347,23 +1448,20 @@ croak(pat, va_alist)
     HV *stash;
     GV *gv;
     CV *cv;
+    SV *msv;
+    STRLEN msglen;
 
-#ifdef I_STDARG
     va_start(args, pat);
-#else
-    va_start(args);
-#endif
-    message = mess(pat, &args);
+    msv = mess(pat, &args);
+    message = SvPV(msv,msglen);
     va_end(args);
-#ifdef USE_THREADS
-    DEBUG_L(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
-#endif /* USE_THREADS */
-    if (diehook) {
+    DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
+    if (PL_diehook) {
        /* sv_2cv might call croak() */
-       SV *olddiehook = diehook;
+       SV *olddiehook = PL_diehook;
        ENTER;
-       SAVESPTR(diehook);
-       diehook = Nullsv;
+       SAVESPTR(PL_diehook);
+       PL_diehook = Nullsv;
        cv = sv_2cv(olddiehook, &stash, &gv, 0);
        LEAVE;
        if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
@@ -1371,59 +1469,60 @@ croak(pat, va_alist)
            SV *msg;
 
            ENTER;
-           msg = newSVpv(message, 0);
+           msg = newSVpvn(message, msglen);
            SvREADONLY_on(msg);
            SAVEFREESV(msg);
 
-           PUSHSTACK(SI_DIEHOOK);
+           PUSHSTACKi(PERLSI_DIEHOOK);
            PUSHMARK(SP);
            XPUSHs(msg);
            PUTBACK;
            perl_call_sv((SV*)cv, G_DISCARD);
-           POPSTACK();
+           POPSTACK;
            LEAVE;
        }
     }
-    if (in_eval) {
-       restartop = die_where(message);
+    if (PL_in_eval) {
+       PL_restartop = die_where(message, msglen);
        JMPENV_JUMP(3);
     }
-    PerlIO_puts(PerlIO_stderr(),message);
-    (void)PerlIO_flush(PerlIO_stderr());
+    {
+#ifdef USE_SFIO
+       /* SFIO can really mess with your errno */
+       int e = errno;
+#endif
+       PerlIO_write(PerlIO_stderr(), message, msglen);
+       (void)PerlIO_flush(PerlIO_stderr());
+#ifdef USE_SFIO
+       errno = e;
+#endif
+    }
     my_failure_exit();
 }
 
 void
-#ifdef I_STDARG
 warn(const char* pat,...)
-#else
-/*VARARGS0*/
-warn(pat,va_alist)
-    const char *pat;
-    va_dcl
-#endif
 {
     va_list args;
     char *message;
     HV *stash;
     GV *gv;
     CV *cv;
+    SV *msv;
+    STRLEN msglen;
 
-#ifdef I_STDARG
     va_start(args, pat);
-#else
-    va_start(args);
-#endif
-    message = mess(pat, &args);
+    msv = mess(pat, &args);
+    message = SvPV(msv, msglen);
     va_end(args);
 
-    if (warnhook) {
+    if (PL_warnhook) {
        /* sv_2cv might call warn() */
        dTHR;
-       SV *oldwarnhook = warnhook;
+       SV *oldwarnhook = PL_warnhook;
        ENTER;
-       SAVESPTR(warnhook);
-       warnhook = Nullsv;
+       SAVESPTR(PL_warnhook);
+       PL_warnhook = Nullsv;
        cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
        LEAVE;
        if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
@@ -1431,21 +1530,21 @@ warn(pat,va_alist)
            SV *msg;
 
            ENTER;
-           msg = newSVpv(message, 0);
+           msg = newSVpvn(message, msglen);
            SvREADONLY_on(msg);
            SAVEFREESV(msg);
 
-           PUSHSTACK(SI_WARNHOOK);
+           PUSHSTACKi(PERLSI_WARNHOOK);
            PUSHMARK(SP);
            XPUSHs(msg);
            PUTBACK;
            perl_call_sv((SV*)cv, G_DISCARD);
-           POPSTACK();
+           POPSTACK;
            LEAVE;
            return;
        }
     }
-    PerlIO_puts(PerlIO_stderr(),message);
+    PerlIO_write(PerlIO_stderr(), message, msglen);
 #ifdef LEAKTEST
     DEBUG_L(*message == '!' 
            ? (xstat(message[1]=='!'
@@ -1457,28 +1556,123 @@ warn(pat,va_alist)
     (void)PerlIO_flush(PerlIO_stderr());
 }
 
+void
+warner(U32  err, const char* pat,...)
+{
+    dTHR;
+    va_list args;
+    char *message;
+    HV *stash;
+    GV *gv;
+    CV *cv;
+    SV *msv;
+    STRLEN msglen;
+
+    va_start(args, pat);
+    msv = mess(pat, &args);
+    message = SvPV(msv, msglen);
+    va_end(args);
+
+    if (ckDEAD(err)) {
+#ifdef USE_THREADS
+        DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
+#endif /* USE_THREADS */
+        if (PL_diehook) {
+            /* sv_2cv might call croak() */
+            SV *olddiehook = PL_diehook;
+            ENTER;
+            SAVESPTR(PL_diehook);
+            PL_diehook = Nullsv;
+            cv = sv_2cv(olddiehook, &stash, &gv, 0);
+            LEAVE;
+            if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
+                dSP;
+                SV *msg;
+                ENTER;
+                msg = newSVpvn(message, msglen);
+                SvREADONLY_on(msg);
+                SAVEFREESV(msg);
+                PUSHMARK(sp);
+                XPUSHs(msg);
+                PUTBACK;
+                perl_call_sv((SV*)cv, G_DISCARD);
+                LEAVE;
+            }
+        }
+        if (PL_in_eval) {
+            PL_restartop = die_where(message, msglen);
+            JMPENV_JUMP(3);
+        }
+        PerlIO_write(PerlIO_stderr(), message, msglen);
+        (void)PerlIO_flush(PerlIO_stderr());
+        my_failure_exit();
+
+    }
+    else {
+        if (PL_warnhook) {
+            /* sv_2cv might call warn() */
+            dTHR;
+            SV *oldwarnhook = PL_warnhook;
+            ENTER;
+            SAVESPTR(PL_warnhook);
+            PL_warnhook = Nullsv;
+            cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
+                LEAVE;
+            if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
+                dSP;
+                SV *msg;
+                ENTER;
+                msg = newSVpvn(message, msglen);
+                SvREADONLY_on(msg);
+                SAVEFREESV(msg);
+                PUSHMARK(sp);
+                XPUSHs(msg);
+                PUTBACK;
+                perl_call_sv((SV*)cv, G_DISCARD);
+                LEAVE;
+                return;
+            }
+        }
+        PerlIO_write(PerlIO_stderr(), message, msglen);
+#ifdef LEAKTEST
+        DEBUG_L(xstat());
+#endif
+        (void)PerlIO_flush(PerlIO_stderr());
+    }
+}
+
 #ifndef VMS  /* VMS' my_setenv() is in VMS.c */
-#ifndef WIN32
+#if !defined(WIN32) && !defined(CYGWIN32)
 void
 my_setenv(char *nam, char *val)
 {
+#ifndef PERL_USE_SAFE_PUTENV
+    /* most putenv()s leak, so we manipulate environ directly */
     register I32 i=setenv_getix(nam);          /* where does it go? */
 
-    if (environ == origenviron) {      /* need we copy environment? */
+    if (environ == PL_origenviron) {   /* need we copy environment? */
        I32 j;
        I32 max;
        char **tmpenv;
 
        /*SUPPRESS 530*/
        for (max = i; environ[max]; max++) ;
-       New(901,tmpenv, max+2, char*);
-       for (j=0; j<max; j++)           /* copy environment */
-           tmpenv[j] = savepv(environ[j]);
+       tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
+       for (j=0; j<max; j++) {         /* copy environment */
+           tmpenv[j] = (char*)safesysmalloc((strlen(environ[j])+1)*sizeof(char));
+           strcpy(tmpenv[j], environ[j]);
+       }
        tmpenv[max] = Nullch;
        environ = tmpenv;               /* tell exec where it is now */
     }
     if (!val) {
-       Safefree(environ[i]);
+       safesysfree(environ[i]);
        while (environ[i]) {
            environ[i] = environ[i+1];
            i++;
@@ -1486,12 +1680,13 @@ my_setenv(char *nam, char *val)
        return;
     }
     if (!environ[i]) {                 /* does not exist yet */
-       Renew(environ, i+2, char*);     /* just expand it a bit */
+       environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
        environ[i+1] = Nullch;  /* make sure it's null terminated */
     }
     else
-       Safefree(environ[i]);
-    New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
+       safesysfree(environ[i]);
+    environ[i] = (char*)safesysmalloc((strlen(nam)+strlen(val)+2) * sizeof(char));
+
 #ifndef MSDOS
     (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
 #else
@@ -1503,6 +1698,19 @@ my_setenv(char *nam, char *val)
     strcpy(environ[i],nam); strupr(environ[i]);
     (void)sprintf(environ[i] + strlen(nam),"=%s",val);
 #endif /* MSDOS */
+
+#else   /* PERL_USE_SAFE_PUTENV */
+    char *new_env;
+
+    new_env = (char*)safesysmalloc((strlen(nam) + strlen(val) + 2) * sizeof(char));
+#ifndef MSDOS
+    (void)sprintf(new_env,"%s=%s",nam,val);/* all that work just for this */
+#else
+    strcpy(new_env,nam); strupr(new_env);
+    (void)sprintf(new_env + strlen(nam),"=%s",val);
+#endif
+    (void)putenv(new_env);
+#endif  /* PERL_USE_SAFE_PUTENV */
 }
 
 #else /* if WIN32 */
@@ -1540,32 +1748,27 @@ my_setenv(char *nam,char *val)
     }
     else
        vallen = strlen(val);
-    New(904, envstr, namlen + vallen + 3, char);
+    envstr = (char*)safesysmalloc((namlen + vallen + 3) * sizeof(char));
     (void)sprintf(envstr,"%s=%s",nam,val);
     (void)PerlEnv_putenv(envstr);
     if (oldstr)
-       Safefree(oldstr);
+       safesysfree(oldstr);
 #ifdef _MSC_VER
-    Safefree(envstr);          /* MSVCRT leaks without this */
+    safesysfree(envstr);       /* MSVCRT leaks without this */
 #endif
 
 #else /* !USE_WIN32_RTL_ENV */
 
-    /* The sane way to deal with the environment.
-     * Has these advantages over putenv() & co.:
-     *  * enables us to store a truly empty value in the
-     *    environment (like in UNIX).
-     *  * we don't have to deal with RTL globals, bugs and leaks.
-     *  * Much faster.
-     * Why you may want to enable USE_WIN32_RTL_ENV:
-     *  * environ[] and RTL functions will not reflect changes,
-     *    which might be an issue if extensions want to access
-     *    the env. via RTL.  This cuts both ways, since RTL will
-     *    not see changes made by extensions that call the Win32
-     *    functions directly, either.
-     * GSAR 97-06-07
-     */
-    SetEnvironmentVariable(nam,val);
+    register char *envstr;
+    STRLEN len = strlen(nam) + 3;
+    if (!val) {
+       val = "";
+    }
+    len += strlen(val);
+    New(904, envstr, len, char);
+    (void)sprintf(envstr,"%s=%s",nam,val);
+    (void)PerlEnv_putenv(envstr);
+    Safefree(envstr);
 
 #endif
 }
@@ -1594,8 +1797,7 @@ setenv_getix(char *nam)
 
 #ifdef UNLINK_ALL_VERSIONS
 I32
-unlnk(f)       /* unlink all versions of a file */
-char *f;
+unlnk(char *f) /* unlink all versions of a file */
 {
     I32 i;
 
@@ -1606,7 +1808,7 @@ char *f;
 
 #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
 char *
-my_bcopy(register char *from,register char *to,register I32 len)
+my_bcopy(register const char *from,register char *to,register I32 len)
 {
     char *retval = to;
 
@@ -1626,10 +1828,7 @@ my_bcopy(register char *from,register char *to,register I32 len)
 
 #ifndef HAS_MEMSET
 void *
-my_memset(loc,ch,len)
-register char *loc;
-register I32 ch;
-register I32 len;
+my_memset(register char *loc, register I32 ch, register I32 len)
 {
     char *retval = loc;
 
@@ -1641,9 +1840,7 @@ register I32 len;
 
 #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
 char *
-my_bzero(loc,len)
-register char *loc;
-register I32 len;
+my_bzero(register char *loc, register I32 len)
 {
     char *retval = loc;
 
@@ -1655,10 +1852,7 @@ register I32 len;
 
 #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
 I32
-my_memcmp(s1,s2,len)
-char *s1;
-char *s2;
-register I32 len;
+my_memcmp(const char *s1, const char *s2, register I32 len)
 {
     register U8 *a = (U8 *)s1;
     register U8 *b = (U8 *)s2;
@@ -1672,7 +1866,6 @@ register I32 len;
 }
 #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
 
-#if defined(I_STDARG) || defined(I_VARARGS)
 #ifndef HAS_VPRINTF
 
 #ifdef USE_CHAR_VSPRINTF
@@ -1680,10 +1873,7 @@ char *
 #else
 int
 #endif
-vsprintf(dest, pat, args)
-char *dest;
-const char *pat;
-char *args;
+vsprintf(char *dest, const char *pat, char *args)
 {
     FILE fakebuf;
 
@@ -1703,7 +1893,6 @@ char *args;
 }
 
 #endif /* HAS_VPRINTF */
-#endif /* I_VARARGS || I_STDARGS */
 
 #ifdef MYSWAP
 #if BYTEORDER != 0x4321
@@ -1793,8 +1982,7 @@ my_ntohl(long l)
 
 #define HTOV(name,type)                                                \
        type                                                    \
-       name (n)                                                \
-       register type n;                                        \
+       name (register type n)                                  \
        {                                                       \
            union {                                             \
                type value;                                     \
@@ -1810,8 +1998,7 @@ my_ntohl(long l)
 
 #define VTOH(name,type)                                                \
        type                                                    \
-       name (n)                                                \
-       register type n;                                        \
+       name (register type n)                                  \
        {                                                       \
            union {                                             \
                type value;                                     \
@@ -1841,7 +2028,7 @@ VTOH(vtohl,long)
 #endif
 
     /* VMS' my_popen() is in VMS.c, same with OS/2. */
-#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
+#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
 PerlIO *
 my_popen(char *cmd, char *mode)
 {
@@ -1850,7 +2037,10 @@ my_popen(char *cmd, char *mode)
     register I32 pid;
     SV *sv;
     I32 doexec = strNE(cmd,"-");
+    I32 did_pipes = 0;
+    int pp[2];
 
+    PERL_FLUSHALL_FOR_CHILD;
 #ifdef OS2
     if (doexec) {
        return my_syspopen(cmd,mode);
@@ -1858,15 +2048,21 @@ my_popen(char *cmd, char *mode)
 #endif 
     This = (*mode == 'w');
     that = !This;
-    if (doexec && tainting) {
+    if (doexec && PL_tainting) {
        taint_env();
        taint_proper("Insecure %s%s", "EXEC");
     }
     if (PerlProc_pipe(p) < 0)
        return Nullfp;
+    if (doexec && PerlProc_pipe(pp) >= 0)
+       did_pipes = 1;
     while ((pid = (doexec?vfork():fork())) < 0) {
        if (errno != EAGAIN) {
            PerlLIO_close(p[This]);
+           if (did_pipes) {
+               PerlLIO_close(pp[0]);
+               PerlLIO_close(pp[1]);
+           }
            if (!doexec)
                croak("Can't fork");
            return Nullfp;
@@ -1876,9 +2072,17 @@ my_popen(char *cmd, char *mode)
     if (pid == 0) {
        GV* tmpgv;
 
+#undef THIS
+#undef THAT
 #define THIS that
 #define THAT This
        PerlLIO_close(p[THAT]);
+       if (did_pipes) {
+           PerlLIO_close(pp[0]);
+#if defined(HAS_FCNTL) && defined(F_SETFD)
+           fcntl(pp[1], F_SETFD, FD_CLOEXEC);
+#endif
+       }
        if (p[THIS] != (*mode == 'r')) {
            PerlLIO_dup2(p[THIS], *mode == 'r');
            PerlLIO_close(p[THIS]);
@@ -1890,44 +2094,68 @@ my_popen(char *cmd, char *mode)
 #ifndef NOFILE
 #define NOFILE 20
 #endif
-           for (fd = maxsysfd + 1; fd < NOFILE; fd++)
-               PerlLIO_close(fd);
+           for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
+               if (fd != pp[1])
+                   PerlLIO_close(fd);
 #endif
-           do_exec(cmd);       /* may or may not use the shell */
+           do_exec3(cmd,pp[1],did_pipes);      /* may or may not use the shell */
            PerlProc__exit(1);
        }
        /*SUPPRESS 560*/
        if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
            sv_setiv(GvSV(tmpgv), (IV)getpid());
-       forkprocess = 0;
-       hv_clear(pidstatus);    /* we have no children */
+       PL_forkprocess = 0;
+       hv_clear(PL_pidstatus); /* we have no children */
        return Nullfp;
 #undef THIS
 #undef THAT
     }
     do_execfree();     /* free any memory malloced by child on vfork */
     PerlLIO_close(p[that]);
+    if (did_pipes)
+       PerlLIO_close(pp[1]);
     if (p[that] < p[This]) {
        PerlLIO_dup2(p[This], p[that]);
        PerlLIO_close(p[This]);
        p[This] = p[that];
     }
-    sv = *av_fetch(fdpid,p[This],TRUE);
+    sv = *av_fetch(PL_fdpid,p[This],TRUE);
     (void)SvUPGRADE(sv,SVt_IV);
     SvIVX(sv) = pid;
-    forkprocess = pid;
+    PL_forkprocess = pid;
+    if (did_pipes && pid > 0) {
+       int errkid;
+       int n = 0, n1;
+
+       while (n < sizeof(int)) {
+           n1 = PerlLIO_read(pp[0],
+                             (void*)(((char*)&errkid)+n),
+                             (sizeof(int)) - n);
+           if (n1 <= 0)
+               break;
+           n += n1;
+       }
+       if (n) {                        /* Error */
+           if (n != sizeof(int))
+               croak("panic: kid popen errno read");
+           PerlLIO_close(pp[0]);
+           errno = errkid;             /* Propagate errno from kid */
+           return Nullfp;
+       }
+    }
+    if (did_pipes)
+        PerlLIO_close(pp[0]);
     return PerlIO_fdopen(p[This], mode);
 }
 #else
 #if defined(atarist) || defined(DJGPP)
 FILE *popen();
 PerlIO *
-my_popen(cmd,mode)
-char   *cmd;
-char   *mode;
+my_popen(char *cmd, char *mode)
 {
     /* Needs work for PerlIO ! */
     /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
+    PERL_FLUSHALL_FOR_CHILD;
     return popen(PerlIO_exportFILE(cmd, 0), mode);
 }
 #endif
@@ -1935,8 +2163,8 @@ char      *mode;
 #endif /* !DOSISH */
 
 #ifdef DUMP_FDS
-dump_fds(s)
-char *s;
+void
+dump_fds(char *s)
 {
     int fd;
     struct stat tmpstatbuf;
@@ -1948,13 +2176,11 @@ char *s;
     }
     PerlIO_printf(PerlIO_stderr(),"\n");
 }
-#endif
+#endif /* DUMP_FDS */
 
 #ifndef HAS_DUP2
 int
-dup2(oldfd,newfd)
-int oldfd;
-int newfd;
+dup2(int oldfd, int newfd)
 {
 #if defined(HAS_FCNTL) && defined(F_DUPFD)
     if (oldfd == newfd)
@@ -2000,6 +2226,10 @@ rsignal(int signo, Sighandler_t handler)
 #ifdef SA_RESTART
     act.sa_flags |= SA_RESTART;        /* SVR4, 4.3+BSD */
 #endif
+#ifdef SA_NOCLDWAIT
+    if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
+       act.sa_flags |= SA_NOCLDWAIT;
+#endif
     if (sigaction(signo, &act, &oact) == -1)
        return SIG_ERR;
     else
@@ -2028,6 +2258,10 @@ rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
 #ifdef SA_RESTART
     act.sa_flags |= SA_RESTART;        /* SVR4, 4.3+BSD */
 #endif
+#ifdef SA_NOCLDWAIT
+    if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
+       act.sa_flags |= SA_NOCLDWAIT;
+#endif
     return sigaction(signo, &act, save);
 }
 
@@ -2083,7 +2317,7 @@ rsignal_restore(int signo, Sigsave_t *save)
 #endif /* !HAS_SIGACTION */
 
     /* VMS' my_pclose() is in VMS.c; same with OS/2 */
-#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS)
+#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
 I32
 my_pclose(PerlIO *ptr)
 {
@@ -2101,10 +2335,10 @@ my_pclose(PerlIO *ptr)
     int saved_win32_errno;
 #endif
 
-    svp = av_fetch(fdpid,PerlIO_fileno(ptr),TRUE);
+    svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
     pid = (int)SvIVX(*svp);
     SvREFCNT_dec(*svp);
-    *svp = &sv_undef;
+    *svp = &PL_sv_undef;
 #ifdef OS2
     if (pid == -1) {                   /* Opened by popen. */
        return my_syspclose(ptr);
@@ -2139,7 +2373,7 @@ my_pclose(PerlIO *ptr)
 }
 #endif /* !DOSISH */
 
-#if  !defined(DOSISH) || defined(OS2) || defined(WIN32)
+#if  !defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(CYGWIN32)
 I32
 wait4pid(int pid, int *statusp, int flags)
 {
@@ -2151,23 +2385,23 @@ wait4pid(int pid, int *statusp, int flags)
        return -1;
     if (pid > 0) {
        sprintf(spid, "%d", pid);
-       svp = hv_fetch(pidstatus,spid,strlen(spid),FALSE);
-       if (svp && *svp != &sv_undef) {
+       svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
+       if (svp && *svp != &PL_sv_undef) {
            *statusp = SvIVX(*svp);
-           (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
+           (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
            return pid;
        }
     }
     else {
        HE *entry;
 
-       hv_iterinit(pidstatus);
-       if (entry = hv_iternext(pidstatus)) {
+       hv_iterinit(PL_pidstatus);
+       if (entry = hv_iternext(PL_pidstatus)) {
            pid = atoi(hv_iterkey(entry,(I32*)statusp));
-           sv = hv_iterval(pidstatus,entry);
+           sv = hv_iterval(PL_pidstatus,entry);
            *statusp = SvIVX(sv);
            sprintf(spid, "%d", pid);
-           (void)hv_delete(pidstatus,spid,strlen(spid),G_DISCARD);
+           (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
            return pid;
        }
     }
@@ -2176,7 +2410,7 @@ wait4pid(int pid, int *statusp, int flags)
     if (!HAS_WAITPID_RUNTIME)
        goto hard_way;
 #  endif
-    return waitpid(pid,statusp,flags);
+    return PerlProc_waitpid(pid,statusp,flags);
 #endif
 #if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
     return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
@@ -2188,7 +2422,7 @@ wait4pid(int pid, int *statusp, int flags)
        if (flags)
            croak("Can't do waitpid with flags");
        else {
-           while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
+           while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
                pidgone(result,*statusp);
            if (result < 0)
                *statusp = -1;
@@ -2207,7 +2441,7 @@ pidgone(int pid, int status)
     char spid[TYPE_CHARS(int)];
 
     sprintf(spid, "%d", pid);
-    sv = *hv_fetch(pidstatus,spid,strlen(spid),TRUE);
+    sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
     (void)SvUPGRADE(sv,SVt_IV);
     SvIVX(sv) = status;
     return;
@@ -2218,12 +2452,11 @@ int pclose();
 #ifdef HAS_FORK
 int                                    /* Cannot prototype with I32
                                           in os2ish.h. */
-my_syspclose(ptr)
+my_syspclose(PerlIO *ptr)
 #else
 I32
-my_pclose(ptr)
+my_pclose(PerlIO *ptr)
 #endif 
-PerlIO *ptr;
 {
     /* Needs work for PerlIO ! */
     FILE *f = PerlIO_findFILE(ptr);
@@ -2234,15 +2467,15 @@ PerlIO *ptr;
 #endif
 
 void
-repeatcpy(register char *to, register char *from, I32 len, register I32 count)
+repeatcpy(register char *to, register const char *from, I32 len, register I32 count)
 {
     register I32 todo;
-    register char *frombase = from;
+    register const char *frombase = from;
 
     if (len == 1) {
-       todo = *from;
+       register const char c = *from;
        while (count-- > 0)
-           *to++ = todo;
+           *to++ = c;
        return;
     }
     while (count-- > 0) {
@@ -2253,10 +2486,8 @@ repeatcpy(register char *to, register char *from, I32 len, register I32 count)
     }
 }
 
-#ifndef CASTNEGFLOAT
 U32
-cast_ulong(f)
-double f;
+cast_ulong(double f)
 {
     long along;
 
@@ -2271,9 +2502,6 @@ double f;
     return (unsigned long)along;
 }
 # undef BIGDOUBLE
-#endif
-
-#ifndef CASTI32
 
 /* Unfortunately, on some systems the cast_uv() function doesn't
    work with the system-supplied definition of ULONG_MAX.  The
@@ -2296,8 +2524,7 @@ double f;
 #endif
 
 I32
-cast_i32(f)
-double f;
+cast_i32(double f)
 {
     if (f >= I32_MAX)
        return (I32) I32_MAX;
@@ -2307,32 +2534,40 @@ double f;
 }
 
 IV
-cast_iv(f)
-double f;
+cast_iv(double f)
 {
-    if (f >= IV_MAX)
-       return (IV) IV_MAX;
+    if (f >= IV_MAX) {
+       UV uv;
+       
+       if (f >= (double)UV_MAX)
+           return (IV) UV_MAX; 
+       uv = (UV) f;
+       return (IV)uv;
+    }
     if (f <= IV_MIN)
        return (IV) IV_MIN;
     return (IV) f;
 }
 
 UV
-cast_uv(f)
-double f;
+cast_uv(double f)
 {
     if (f >= MY_UV_MAX)
        return (UV) MY_UV_MAX;
+    if (f < 0) {
+       IV iv;
+       
+       if (f < IV_MIN)
+           return (UV)IV_MIN;
+       iv = (IV) f;
+       return (UV) iv;
+    }
     return (UV) f;
 }
 
-#endif
-
 #ifndef HAS_RENAME
 I32
-same_dirent(a,b)
-char *a;
-char *b;
+same_dirent(char *a, char *b)
 {
     char *fa = strrchr(a,'/');
     char *fb = strrchr(b,'/');
@@ -2368,6 +2603,29 @@ char *b;
 #endif /* !HAS_RENAME */
 
 UV
+scan_bin(char *start, I32 len, I32 *retlen)
+{
+    register char *s = start;
+    register UV retval = 0;
+    bool overflowed = FALSE;
+    while (len && *s >= '0' && *s <= '1') {
+      register UV n = retval << 1;
+      if (!overflowed && (n >> 1) != retval) {
+          warn("Integer overflow in binary number");
+          overflowed = TRUE;
+      }
+      retval = n | (*s++ - '0');
+      len--;
+    }
+    if (len && (*s >= '2' && *s <= '9')) {
+      dTHR;
+      if (ckWARN(WARN_UNSAFE))
+          warner(WARN_UNSAFE, "Illegal binary digit '%c' ignored", *s);
+    }
+    *retlen = s - start;
+    return retval;
+}
+UV
 scan_oct(char *start, I32 len, I32 *retlen)
 {
     register char *s = start;
@@ -2383,8 +2641,11 @@ scan_oct(char *start, I32 len, I32 *retlen)
        retval = n | (*s++ - '0');
        len--;
     }
-    if (dowarn && len && (*s == '8' || *s == '9'))
-       warn("Illegal octal digit ignored");
+    if (len && (*s == '8' || *s == '9')) {
+       dTHR;
+       if (ckWARN(WARN_OCTAL))
+           warner(WARN_OCTAL, "Illegal octal digit '%c' ignored", *s);
+    }
     *retlen = s - start;
     return retval;
 }
@@ -2396,18 +2657,27 @@ scan_hex(char *start, I32 len, I32 *retlen)
     register UV retval = 0;
     bool overflowed = FALSE;
     char *tmp = s;
+    register UV n;
 
-    while (len-- && *s && (tmp = strchr((char *) hexdigit, *s))) {
-       register UV n = retval << 4;
+    while (len-- && *s) {
+       tmp = strchr((char *) PL_hexdigit, *s++);
+       if (!tmp) {
+           if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0))
+               continue;
+           else {
+               dTHR;
+               --s;
+               if (ckWARN(WARN_UNSAFE))
+                   warner(WARN_UNSAFE,"Illegal hex digit '%c' ignored", *s);
+               break;
+           }
+       }
+       n = retval << 4;
        if (!overflowed && (n >> 4) != retval) {
            warn("Integer overflow in hex number");
            overflowed = TRUE;
        }
-       retval = n | ((tmp - hexdigit) & 15);
-       s++;
-    }
-    if (dowarn && !tmp) {
-       warn("Illegal hex digit ignored");
+       retval = n | ((tmp - PL_hexdigit) & 15);
     }
     *retlen = s - start;
     return retval;
@@ -2419,6 +2689,7 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
     dTHR;
     char *xfound = Nullch;
     char *xfailed = Nullch;
+    char tmpbuf[MAXPATHLEN];
     register char *s;
     I32 len;
     int retval;
@@ -2462,6 +2733,7 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
      *     + look *only* in the PATH for scriptname{,.foo,.bar} (note
      *       this will not look in '.' if it's not in the PATH)
      */
+    tmpbuf[0] = '\0';
 
 #ifdef VMS
 #  ifdef ALWAYS_DEFTYPES
@@ -2481,16 +2753,16 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
        /* The first time through, just add SEARCH_EXTS to whatever we
         * already have, so we can check for default file types. */
        while (deftypes ||
-              (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) )
+              (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
        {
            if (deftypes) {
                deftypes = 0;
-               *tokenbuf = '\0';
+               *tmpbuf = '\0';
            }
-           if ((strlen(tokenbuf) + strlen(scriptname)
-                + MAX_EXT_LEN) >= sizeof tokenbuf)
+           if ((strlen(tmpbuf) + strlen(scriptname)
+                + MAX_EXT_LEN) >= sizeof tmpbuf)
                continue;       /* don't search dir with too-long name */
-           strcat(tokenbuf, scriptname);
+           strcat(tmpbuf, scriptname);
 #else  /* !VMS */
 
 #ifdef DOSISH
@@ -2509,7 +2781,8 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
 #endif
            DEBUG_p(PerlIO_printf(Perl_debug_log,
                                  "Looking for %s\n",cur));
-           if (PerlLIO_stat(cur,&statbuf) >= 0) {
+           if (PerlLIO_stat(cur,&PL_statbuf) >= 0
+               && !S_ISDIR(PL_statbuf.st_mode)) {
                dosearch = 0;
                scriptname = cur;
 #ifdef SEARCH_EXTS
@@ -2519,12 +2792,12 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
 #ifdef SEARCH_EXTS
            if (cur == scriptname) {
                len = strlen(scriptname);
-               if (len+MAX_EXT_LEN+1 >= sizeof(tokenbuf))
+               if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
                    break;
-               cur = strcpy(tokenbuf, scriptname);
+               cur = strcpy(tmpbuf, scriptname);
            }
        } while (extidx >= 0 && ext[extidx]     /* try an extension? */
-                && strcpy(tokenbuf+len, ext[extidx++]));
+                && strcpy(tmpbuf+len, ext[extidx++]));
 #endif
     }
 #endif
@@ -2536,85 +2809,93 @@ find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
                 && (s = PerlEnv_getenv("PATH"))) {
        bool seen_dot = 0;
        
-       bufend = s + strlen(s);
-       while (s < bufend) {
+       PL_bufend = s + strlen(s);
+       while (s < PL_bufend) {
 #if defined(atarist) || defined(DOSISH)
            for (len = 0; *s
 #  ifdef atarist
                    && *s != ','
 #  endif
                    && *s != ';'; len++, s++) {
-               if (len < sizeof tokenbuf)
-                   tokenbuf[len] = *s;
+               if (len < sizeof tmpbuf)
+                   tmpbuf[len] = *s;
            }
-           if (len < sizeof tokenbuf)
-               tokenbuf[len] = '\0';
+           if (len < sizeof tmpbuf)
+               tmpbuf[len] = '\0';
 #else  /* ! (atarist || DOSISH) */
-           s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf, s, bufend,
+           s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
                        ':',
                        &len);
 #endif /* ! (atarist || DOSISH) */
-           if (s < bufend)
+           if (s < PL_bufend)
                s++;
-           if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tokenbuf)
+           if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
                continue;       /* don't search dir with too-long name */
            if (len
-#if defined(atarist) || defined(DOSISH)
-               && tokenbuf[len - 1] != '/'
-               && tokenbuf[len - 1] != '\\'
+#if defined(atarist) || defined(__MINT__) || defined(DOSISH)
+               && tmpbuf[len - 1] != '/'
+               && tmpbuf[len - 1] != '\\'
 #endif
               )
-               tokenbuf[len++] = '/';
-           if (len == 2 && tokenbuf[0] == '.')
+               tmpbuf[len++] = '/';
+           if (len == 2 && tmpbuf[0] == '.')
                seen_dot = 1;
-           (void)strcpy(tokenbuf + len, scriptname);
+           (void)strcpy(tmpbuf + len, scriptname);
 #endif  /* !VMS */
 
 #ifdef SEARCH_EXTS
-           len = strlen(tokenbuf);
+           len = strlen(tmpbuf);
            if (extidx > 0)     /* reset after previous loop */
                extidx = 0;
            do {
 #endif
-               DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
-               retval = PerlLIO_stat(tokenbuf,&statbuf);
+               DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
+               retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
+               if (S_ISDIR(PL_statbuf.st_mode)) {
+                   retval = -1;
+               }
 #ifdef SEARCH_EXTS
            } while (  retval < 0               /* not there */
                    && extidx>=0 && ext[extidx] /* try an extension? */
-                   && strcpy(tokenbuf+len, ext[extidx++])
+                   && strcpy(tmpbuf+len, ext[extidx++])
                );
 #endif
            if (retval < 0)
                continue;
-           if (S_ISREG(statbuf.st_mode)
-               && cando(S_IRUSR,TRUE,&statbuf)
+           if (S_ISREG(PL_statbuf.st_mode)
+               && cando(S_IRUSR,TRUE,&PL_statbuf)
 #ifndef DOSISH
-               && cando(S_IXUSR,TRUE,&statbuf)
+               && cando(S_IXUSR,TRUE,&PL_statbuf)
 #endif
                )
            {
-               xfound = tokenbuf;              /* bingo! */
+               xfound = tmpbuf;              /* bingo! */
                break;
            }
            if (!xfailed)
-               xfailed = savepv(tokenbuf);
+               xfailed = savepv(tmpbuf);
        }
 #ifndef DOSISH
-       if (!xfound && !seen_dot && !xfailed && (PerlLIO_stat(scriptname,&statbuf) < 0))
+       if (!xfound && !seen_dot && !xfailed &&
+           (PerlLIO_stat(scriptname,&PL_statbuf) < 0 
+            || S_ISDIR(PL_statbuf.st_mode)))
 #endif
            seen_dot = 1;                       /* Disable message. */
-       if (!xfound) 
-           scriptname = NULL;
-/*         croak("Can't %s %s%s%s",
-                 (xfailed ? "execute" : "find"),
-                 (xfailed ? xfailed : scriptname),
-                 (xfailed ? "" : " on PATH"),
-                 (xfailed || seen_dot) ? "" : ", '.' not in PATH"); */
+       if (!xfound) {
+           if (flags & 1) {                    /* do or die? */
+               croak("Can't %s %s%s%s",
+                     (xfailed ? "execute" : "find"),
+                     (xfailed ? xfailed : scriptname),
+                     (xfailed ? "" : " on PATH"),
+                     (xfailed || seen_dot) ? "" : ", '.' not in PATH");
+           }
+           scriptname = Nullch;
+       }
        if (xfailed)
            Safefree(xfailed);
        scriptname = xfound;
     }
-    return scriptname;
+    return (scriptname ? savepv(scriptname) : Nullch);
 }
 
 
@@ -2628,15 +2909,13 @@ schedule(void)
 }
 
 void
-perl_cond_init(cp)
-perl_cond *cp;
+perl_cond_init(perl_cond *cp)
 {
     *cp = 0;
 }
 
 void
-perl_cond_signal(cp)
-perl_cond *cp;
+perl_cond_signal(perl_cond *cp)
 {
     perl_os_thread t;
     perl_cond cond = *cp;
@@ -2656,8 +2935,7 @@ perl_cond *cp;
 }
 
 void
-perl_cond_broadcast(cp)
-perl_cond *cp;
+perl_cond_broadcast(perl_cond *cp)
 {
     perl_os_thread t;
     perl_cond cond, cond_next;
@@ -2678,8 +2956,7 @@ perl_cond *cp;
 }
 
 void
-perl_cond_wait(cp)
-perl_cond *cp;
+perl_cond_wait(perl_cond *cp)
 {
     perl_cond cond;
 
@@ -2697,17 +2974,17 @@ perl_cond *cp;
 }
 #endif /* FAKE_THREADS */
 
-#ifdef OLD_PTHREADS_API
+#ifdef PTHREAD_GETSPECIFIC_INT
 struct perl_thread *
 getTHR _((void))
 {
     pthread_addr_t t;
 
-    if (pthread_getspecific(thr_key, &t))
+    if (pthread_getspecific(PL_thr_key, &t))
        croak("panic: pthread_getspecific");
     return (struct perl_thread *) t;
 }
-#endif /* OLD_PTHREADS_API */
+#endif
 
 MAGIC *
 condpair_magic(SV *sv)
@@ -2724,11 +3001,11 @@ condpair_magic(SV *sv)
        COND_INIT(&cp->owner_cond);
        COND_INIT(&cp->cond);
        cp->owner = 0;
-       LOCK_SV_MUTEX;
+       MUTEX_LOCK(&PL_cred_mutex);             /* XXX need separate mutex? */
        mg = mg_find(sv, 'm');
        if (mg) {
            /* someone else beat us to initialising it */
-           UNLOCK_SV_MUTEX;
+           MUTEX_UNLOCK(&PL_cred_mutex);       /* XXX need separate mutex? */
            MUTEX_DESTROY(&cp->mutex);
            COND_DESTROY(&cp->owner_cond);
            COND_DESTROY(&cp->cond);
@@ -2739,8 +3016,8 @@ condpair_magic(SV *sv)
            mg = SvMAGIC(sv);
            mg->mg_ptr = (char *)cp;
            mg->mg_len = sizeof(cp);
-           UNLOCK_SV_MUTEX;
-           DEBUG_L(WITH_THR(PerlIO_printf(PerlIO_stderr(),
+           MUTEX_UNLOCK(&PL_cred_mutex);       /* XXX need separate mutex? */
+           DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
                                           "%p: condpair_magic %p\n", thr, sv));)
        }
     }
@@ -2762,37 +3039,37 @@ new_struct_thread(struct perl_thread *t)
     SV **svp;
     I32 i;
 
-    sv = newSVpv("", 0);
+    sv = newSVpvn("", 0);
     SvGROW(sv, sizeof(struct perl_thread) + 1);
     SvCUR_set(sv, sizeof(struct perl_thread));
     thr = (Thread) SvPVX(sv);
-    /* debug */
+#ifdef DEBUGGING
     memset(thr, 0xab, sizeof(struct perl_thread));
-    markstack = 0;
-    scopestack = 0;
-    savestack = 0;
-    retstack = 0;
-    dirty = 0;
-    localizing = 0;
-    /* end debug */
+    PL_markstack = 0;
+    PL_scopestack = 0;
+    PL_savestack = 0;
+    PL_retstack = 0;
+    PL_dirty = 0;
+    PL_localizing = 0;
+    Zero(&PL_hv_fetch_ent_mh, 1, HE);
+#else
+    Zero(thr, 1, struct perl_thread);
+#endif
+
+    PL_protect = FUNC_NAME_TO_PTR(default_protect);
 
     thr->oursv = sv;
     init_stacks(ARGS);
 
-    curcop = &compiling;
+    PL_curcop = &PL_compiling;
     thr->cvcache = newHV();
     thr->threadsv = newAV();
     thr->specific = newAV();
-    thr->errsv = newSVpv("", 0);
+    thr->errsv = newSVpvn("", 0);
     thr->errhv = newHV();
     thr->flags = THRf_R_JOINABLE;
     MUTEX_INIT(&thr->mutex);
 
-    curcop = t->Tcurcop;       /* XXX As good a guess as any? */
-    defstash = t->Tdefstash;   /* XXX maybe these should */
-    curstash = t->Tcurstash;   /* always be set to main? */
-
-
     /* top_env needs to be non-zero. It points to an area
        in which longjmp() stuff is stored, as C callstack
        info there at least is thread specific this has to
@@ -2801,48 +3078,72 @@ new_struct_thread(struct perl_thread *t)
        See comments in scope.h    
        Initialize top entry (as in perl.c for main thread)
      */
-    start_env.je_prev = NULL;
-    start_env.je_ret = -1;
-    start_env.je_mustcatch = TRUE;
-    top_env  = &start_env;
-
-    in_eval = FALSE;
-    restartop = 0;
-
-    tainted = t->Ttainted;
-    curpm = t->Tcurpm;         /* XXX No PMOP ref count */
-    nrs = newSVsv(t->Tnrs);
-    rs = newSVsv(t->Trs);
-    last_in_gv = (GV*)SvREFCNT_inc(t->Tlast_in_gv);
-    ofslen = t->Tofslen;
-    ofs = savepvn(t->Tofs, ofslen);
-    defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
-    chopset = t->Tchopset;
-    formtarget = newSVsv(t->Tformtarget);
-    bodytarget = newSVsv(t->Tbodytarget);
-    toptarget = newSVsv(t->Ttoptarget);
-    
+    PL_start_env.je_prev = NULL;
+    PL_start_env.je_ret = -1;
+    PL_start_env.je_mustcatch = TRUE;
+    PL_top_env  = &PL_start_env;
+
+    PL_in_eval = EVAL_NULL;    /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR) */
+    PL_restartop = 0;
+
+    PL_statname = NEWSV(66,0);
+    PL_maxscream = -1;
+    PL_regcompp = FUNC_NAME_TO_PTR(pregcomp);
+    PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags);
+    PL_regindent = 0;
+    PL_reginterp_cnt = 0;
+    PL_lastscream = Nullsv;
+    PL_screamfirst = 0;
+    PL_screamnext = 0;
+    PL_reg_start_tmp = 0;
+    PL_reg_start_tmpl = 0;
+
+    /* parent thread's data needs to be locked while we make copy */
+    MUTEX_LOCK(&t->mutex);
+
+    PL_protect = t->Tprotect;
+
+    PL_curcop = t->Tcurcop;       /* XXX As good a guess as any? */
+    PL_defstash = t->Tdefstash;   /* XXX maybe these should */
+    PL_curstash = t->Tcurstash;   /* always be set to main? */
+
+    PL_tainted = t->Ttainted;
+    PL_curpm = t->Tcurpm;         /* XXX No PMOP ref count */
+    PL_nrs = newSVsv(t->Tnrs);
+    PL_rs = SvREFCNT_inc(PL_nrs);
+    PL_last_in_gv = Nullgv;
+    PL_ofslen = t->Tofslen;
+    PL_ofs = savepvn(t->Tofs, PL_ofslen);
+    PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
+    PL_chopset = t->Tchopset;
+    PL_formtarget = newSVsv(t->Tformtarget);
+    PL_bodytarget = newSVsv(t->Tbodytarget);
+    PL_toptarget = newSVsv(t->Ttoptarget);
+
     /* Initialise all per-thread SVs that the template thread used */
     svp = AvARRAY(t->threadsv);
     for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
-       if (*svp && *svp != &sv_undef) {
+       if (*svp && *svp != &PL_sv_undef) {
            SV *sv = newSVsv(*svp);
            av_store(thr->threadsv, i, sv);
-           sv_magic(sv, 0, 0, &threadsv_names[i], 1);
-           DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+           sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1);
+           DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
        }
     } 
     thr->threadsvp = AvARRAY(thr->threadsv);
 
-    MUTEX_LOCK(&threads_mutex);
-    nthreads++;
-    thr->tid = ++threadnum;
+    MUTEX_LOCK(&PL_threads_mutex);
+    PL_nthreads++;
+    thr->tid = ++PL_threadnum;
     thr->next = t->next;
     thr->prev = t;
     t->next = thr;
     thr->next->prev = thr;
-    MUTEX_UNLOCK(&threads_mutex);
+    MUTEX_UNLOCK(&PL_threads_mutex);
+
+    /* done copying parent's state */
+    MUTEX_UNLOCK(&t->mutex);
 
 #ifdef HAVE_THREAD_INTERN
     init_thread_intern(thr);
@@ -2868,18 +3169,187 @@ Perl_huge(void)
 struct perl_vars *
 Perl_GetVars(void)
 {
- return &Perl_Vars;
+ return &PL_Vars;
 }
 #endif
 
 char **
 get_op_names(void)
 {
- return op_name;
+ return PL_op_name;
 }
 
 char **
 get_op_descs(void)
 {
- return op_desc;
+ return PL_op_desc;
+}
+
+char *
+get_no_modify(void)
+{
+ return (char*)PL_no_modify;
+}
+
+U32 *
+get_opargs(void)
+{
+ return PL_opargs;
+}
+
+SV **
+get_specialsv_list(void)
+{
+ return PL_specialsv_list;
+}
+
+#ifndef HAS_GETENV_LEN
+char *
+getenv_len(char *env_elem, unsigned long *len)
+{
+    char *env_trans = PerlEnv_getenv(env_elem);
+    if (env_trans)
+       *len = strlen(env_trans);
+    return env_trans;
+}
+#endif
+
+
+MGVTBL*
+get_vtbl(int vtbl_id)
+{
+    MGVTBL* result = Null(MGVTBL*);
+
+    switch(vtbl_id) {
+    case want_vtbl_sv:
+       result = &PL_vtbl_sv;
+       break;
+    case want_vtbl_env:
+       result = &PL_vtbl_env;
+       break;
+    case want_vtbl_envelem:
+       result = &PL_vtbl_envelem;
+       break;
+    case want_vtbl_sig:
+       result = &PL_vtbl_sig;
+       break;
+    case want_vtbl_sigelem:
+       result = &PL_vtbl_sigelem;
+       break;
+    case want_vtbl_pack:
+       result = &PL_vtbl_pack;
+       break;
+    case want_vtbl_packelem:
+       result = &PL_vtbl_packelem;
+       break;
+    case want_vtbl_dbline:
+       result = &PL_vtbl_dbline;
+       break;
+    case want_vtbl_isa:
+       result = &PL_vtbl_isa;
+       break;
+    case want_vtbl_isaelem:
+       result = &PL_vtbl_isaelem;
+       break;
+    case want_vtbl_arylen:
+       result = &PL_vtbl_arylen;
+       break;
+    case want_vtbl_glob:
+       result = &PL_vtbl_glob;
+       break;
+    case want_vtbl_mglob:
+       result = &PL_vtbl_mglob;
+       break;
+    case want_vtbl_nkeys:
+       result = &PL_vtbl_nkeys;
+       break;
+    case want_vtbl_taint:
+       result = &PL_vtbl_taint;
+       break;
+    case want_vtbl_substr:
+       result = &PL_vtbl_substr;
+       break;
+    case want_vtbl_vec:
+       result = &PL_vtbl_vec;
+       break;
+    case want_vtbl_pos:
+       result = &PL_vtbl_pos;
+       break;
+    case want_vtbl_bm:
+       result = &PL_vtbl_bm;
+       break;
+    case want_vtbl_fm:
+       result = &PL_vtbl_fm;
+       break;
+    case want_vtbl_uvar:
+       result = &PL_vtbl_uvar;
+       break;
+#ifdef USE_THREADS
+    case want_vtbl_mutex:
+       result = &PL_vtbl_mutex;
+       break;
+#endif
+    case want_vtbl_defelem:
+       result = &PL_vtbl_defelem;
+       break;
+    case want_vtbl_regexp:
+       result = &PL_vtbl_regexp;
+       break;
+    case want_vtbl_regdata:
+       result = &PL_vtbl_regdata;
+       break;
+    case want_vtbl_regdatum:
+       result = &PL_vtbl_regdatum;
+       break;
+#ifdef USE_LOCALE_COLLATE
+    case want_vtbl_collxfrm:
+       result = &PL_vtbl_collxfrm;
+       break;
+#endif
+    case want_vtbl_amagic:
+       result = &PL_vtbl_amagic;
+       break;
+    case want_vtbl_amagicelem:
+       result = &PL_vtbl_amagicelem;
+       break;
+    case want_vtbl_backref:
+       result = &PL_vtbl_backref;
+       break;
+    }
+    return result;
+}
+
+I32
+my_fflush_all(void)
+{
+#ifdef FFLUSH_NULL
+    return fflush(NULL);
+#else
+    long open_max = -1;
+# if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
+#  if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
+    open_max = sysconf(_SC_OPEN_MAX);
+#  else
+#   ifdef FOPEN_MAX
+#   open_max = FOPEN_MAX;
+#   else
+#    ifdef OPEN_MAX
+#   open_max = OPEN_MAX;
+#    else
+#     ifdef _NFILE
+#   open_max = _NFILE;
+#     endif
+#    endif
+#   endif
+#  endif
+    if (open_max > 0) {
+      long i;
+      for (i = 0; i < open_max; i++)
+         fflush(&STDIO_STREAM_ARRAY[i]);
+      return 0;
+    }
+# endif
+    SETERRNO(EBADF,RMS$_IFI);
+    return EOF;
+#endif
 }