Integrate with Sarathy.
[p5sagit/p5-mst-13.2.git] / util.c
diff --git a/util.c b/util.c
index 2c897a4..64580f6 100644 (file)
--- a/util.c
+++ b/util.c
 #  include <sys/wait.h>
 #endif
 
+#ifdef I_LOCALE
+#  include <locale.h>
+#endif
+
 #define FLUSH
 
 #ifdef LEAKTEST
@@ -62,6 +66,10 @@ long lastxycount[MAXXCOUNT][MAXYCOUNT];
 
 #endif
 
+#if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC)
+#  define FD_CLOEXEC 1                 /* NeXT needs this */
+#endif
+
 /* paranoid version of system's malloc() */
 
 /* NOTE:  Do not call the next three routines directly.  Use the macros
@@ -71,18 +79,19 @@ long lastxycount[MAXXCOUNT][MAXYCOUNT];
  */
 
 Malloc_t
-Perl_safesysmalloc(pTHX_ MEM_SIZE size)
+Perl_safesysmalloc(MEM_SIZE size)
 {
     Malloc_t ptr;
 #ifdef HAS_64K_LIMIT
        if (size > 0xffff) {
-               PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
-               my_exit(1);
+           PerlIO_printf(PerlIO_stderr(),
+                         "Allocation too large: %lx\n", size) FLUSH;
+           WITH_THX(my_exit(1));
        }
 #endif /* HAS_64K_LIMIT */
 #ifdef DEBUGGING
     if ((long)size < 0)
-       Perl_croak(aTHX_ "panic: malloc");
+       Perl_croak_nocontext("panic: malloc");
 #endif
     ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
 #if !(defined(I286) || defined(atarist))
@@ -96,7 +105,7 @@ Perl_safesysmalloc(pTHX_ MEM_SIZE size)
        return Nullch;
     else {
        PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
-       my_exit(1);
+       WITH_THX(my_exit(1));
         return Nullch;
     }
     /*NOTREACHED*/
@@ -105,7 +114,7 @@ Perl_safesysmalloc(pTHX_ MEM_SIZE size)
 /* paranoid version of system's realloc() */
 
 Malloc_t
-Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size)
+Perl_safesysrealloc(Malloc_t where,MEM_SIZE size)
 {
     Malloc_t ptr;
 #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
@@ -116,7 +125,7 @@ Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size)
     if (size > 0xffff) {
        PerlIO_printf(PerlIO_stderr(),
                      "Reallocation too large: %lx\n", size) FLUSH;
-       my_exit(1);
+       WITH_THX(my_exit(1));
     }
 #endif /* HAS_64K_LIMIT */
     if (!size) {
@@ -128,7 +137,7 @@ Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size)
        return safesysmalloc(size);
 #ifdef DEBUGGING
     if ((long)size < 0)
-       Perl_croak(aTHX_ "panic: realloc");
+       Perl_croak_nocontext("panic: realloc");
 #endif
     ptr = PerlMem_realloc(where,size);
 
@@ -150,7 +159,7 @@ Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size)
        return Nullch;
     else {
        PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
-       my_exit(1);
+       WITH_THX(my_exit(1));
        return Nullch;
     }
     /*NOTREACHED*/
@@ -159,7 +168,7 @@ Perl_safesysrealloc(pTHX_ Malloc_t where,MEM_SIZE size)
 /* safe version of system's free() */
 
 Free_t
-Perl_safesysfree(pTHX_ Malloc_t where)
+Perl_safesysfree(Malloc_t where)
 {
 #if !(defined(I286) || defined(atarist))
     DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
@@ -175,7 +184,7 @@ Perl_safesysfree(pTHX_ Malloc_t where)
 /* safe version of system's calloc() */
 
 Malloc_t
-Perl_safesyscalloc(pTHX_ MEM_SIZE count, MEM_SIZE size)
+Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size)
 {
     Malloc_t ptr;
 
@@ -183,12 +192,12 @@ Perl_safesyscalloc(pTHX_ MEM_SIZE count, MEM_SIZE size)
     if (size * count > 0xffff) {
        PerlIO_printf(PerlIO_stderr(),
                      "Allocation too large: %lx\n", size * count) FLUSH;
-       my_exit(1);
+       WITH_THX(my_exit(1));
     }
 #endif /* HAS_64K_LIMIT */
 #ifdef DEBUGGING
     if ((long)size < 0 || (long)count < 0)
-       Perl_croak(aTHX_ "panic: calloc");
+       Perl_croak_nocontext("panic: calloc");
 #endif
     size *= count;
     ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
@@ -205,7 +214,7 @@ Perl_safesyscalloc(pTHX_ MEM_SIZE count, MEM_SIZE size)
        return Nullch;
     else {
        PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
-       my_exit(1);
+       WITH_THX(my_exit(1));
        return Nullch;
     }
     /*NOTREACHED*/
@@ -235,7 +244,7 @@ struct mem_test_strut {
                              : ((size) - 1)/4))
 
 Malloc_t
-Perl_safexmalloc(pTHX_ I32 x, MEM_SIZE size)
+Perl_safexmalloc(I32 x, MEM_SIZE size)
 {
     register char* where = (char*)safemalloc(size + ALIGN);
 
@@ -247,7 +256,7 @@ Perl_safexmalloc(pTHX_ I32 x, MEM_SIZE size)
 }
 
 Malloc_t
-Perl_safexrealloc(pTHX_ Malloc_t wh, MEM_SIZE size)
+Perl_safexrealloc(Malloc_t wh, MEM_SIZE size)
 {
     char *where = (char*)wh;
 
@@ -268,7 +277,7 @@ Perl_safexrealloc(pTHX_ Malloc_t wh, MEM_SIZE size)
 }
 
 void
-Perl_safexfree(pTHX_ Malloc_t wh)
+Perl_safexfree(Malloc_t wh)
 {
     I32 x;
     char *where = (char*)wh;
@@ -285,7 +294,7 @@ Perl_safexfree(pTHX_ Malloc_t wh)
 }
 
 Malloc_t
-Perl_safexcalloc(pTHX_ I32 x,MEM_SIZE count, MEM_SIZE size)
+Perl_safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
 {
     register char * where = (char*)safexmalloc(x, size * count + ALIGN);
     xcount[x] += size;
@@ -536,6 +545,27 @@ Perl_new_collate(pTHX_ const char *newcoll)
 #endif /* USE_LOCALE_COLLATE */
 }
 
+void
+Perl_set_numeric_radix(pTHX)
+{
+#ifdef USE_LOCALE_NUMERIC
+# ifdef HAS_LOCALECONV
+    struct lconv* lc;
+
+    lc = localeconv();
+    if (lc && lc->decimal_point)
+       /* We assume that decimal separator aka the radix
+        * character is always a single character.  If it
+        * ever is a string, this needs to be rethunk. */
+       PL_numeric_radix = *lc->decimal_point;
+    else
+       PL_numeric_radix = 0;
+# endif /* HAS_LOCALECONV */
+#else
+    PL_numeric_radix = 0;
+#endif /* USE_LOCALE_NUMERIC */
+}
+
 /*
  * Set up for a new numeric locale.
  */
@@ -559,6 +589,7 @@ Perl_new_numeric(pTHX_ const char *newnum)
        PL_numeric_name = savepv(newnum);
        PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
        PL_numeric_local = TRUE;
+       set_numeric_radix();
     }
 
 #endif /* USE_LOCALE_NUMERIC */
@@ -587,12 +618,12 @@ Perl_set_numeric_local(pTHX)
        setlocale(LC_NUMERIC, PL_numeric_name);
        PL_numeric_standard = FALSE;
        PL_numeric_local = TRUE;
+       set_numeric_radix();
     }
 
 #endif /* USE_LOCALE_NUMERIC */
 }
 
-
 /*
  * Initialize locale awareness.
  */
@@ -1382,8 +1413,8 @@ Perl_mess(pTHX_ const char *pat, va_list *args)
     return sv;
 }
 
-static OP *
-do_die(pTHX_ const char* pat, va_list *args)
+STATIC OP *
+S_do_die(pTHX_ const char* pat, va_list *args)
 {
     dTHR;
     char *message;
@@ -1458,7 +1489,7 @@ Perl_die_nocontext(const char* pat, ...)
     OP *o;
     va_list args;
     va_start(args, pat);
-    o = do_die(aTHX_ pat, &args);
+    o = do_die(pat, &args);
     va_end(args);
     return o;
 }
@@ -1470,7 +1501,7 @@ Perl_die(pTHX_ const char* pat, ...)
     OP *o;
     va_list args;
     va_start(args, pat);
-    o = do_die(aTHX_ pat, &args);
+    o = do_die(pat, &args);
     va_end(args);
     return o;
 }
@@ -1787,6 +1818,41 @@ Perl_my_setenv(pTHX_ char *nam, char *val)
 #endif  /* PERL_USE_SAFE_PUTENV */
 }
 
+#else /* WIN32 || CYGWIN32 */
+#if defined(CYGWIN32)
+/*
+ * Save environ of perl.exe, currently Cygwin links in separate environ's
+ * for each exe/dll.  Probably should be a member of impure_ptr.
+ */
+static char ***Perl_main_environ;
+
+EXTERN_C void
+Perl_my_setenv_init(char ***penviron)
+{
+    Perl_main_environ = penviron;
+}
+
+void
+my_setenv(char *nam, char *val)
+{
+    /* You can not directly manipulate the environ[] array because
+     * the routines do some additional work that syncs the Cygwin
+     * environment with the Windows environment.
+     */
+    char *oldstr = environ[setenv_getix(nam)];
+
+    if (!val) {
+       if (!oldstr)
+           return;
+       unsetenv(nam);
+       Safefree(oldstr);
+       return;
+    }
+    setenv(nam, val, 1);
+    environ = *Perl_main_environ; /* environ realloc can occur in setenv */
+    if(oldstr && environ[setenv_getix(nam)] != oldstr)
+       Safefree(oldstr);
+}
 #else /* if WIN32 */
 
 void
@@ -1848,6 +1914,7 @@ Perl_my_setenv(pTHX_ char *nam,char *val)
 }
 
 #endif /* WIN32 */
+#endif
 
 I32
 Perl_setenv_getix(pTHX_ char *nam)
@@ -2102,7 +2169,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) && !defined(__OPEN_VM)
+#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC)
 PerlIO *
 Perl_my_popen(pTHX_ char *cmd, char *mode)
 {
@@ -2393,7 +2460,7 @@ Perl_rsignal_restore(pTHX_ 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) && !defined(__OPEN_VM)
+#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC)
 I32
 Perl_my_pclose(pTHX_ PerlIO *ptr)
 {
@@ -2563,7 +2630,7 @@ Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, regi
 }
 
 U32
-Perl_cast_ulong(pTHX_ double f)
+Perl_cast_ulong(pTHX_ NV f)
 {
     long along;
 
@@ -2600,7 +2667,7 @@ Perl_cast_ulong(pTHX_ double f)
 #endif
 
 I32
-Perl_cast_i32(pTHX_ double f)
+Perl_cast_i32(pTHX_ NV f)
 {
     if (f >= I32_MAX)
        return (I32) I32_MAX;
@@ -2610,12 +2677,12 @@ Perl_cast_i32(pTHX_ double f)
 }
 
 IV
-Perl_cast_iv(pTHX_ double f)
+Perl_cast_iv(pTHX_ NV f)
 {
     if (f >= IV_MAX) {
        UV uv;
        
-       if (f >= (double)UV_MAX)
+       if (f >= (NV)UV_MAX)
            return (IV) UV_MAX; 
        uv = (UV) f;
        return (IV)uv;
@@ -2626,7 +2693,7 @@ Perl_cast_iv(pTHX_ double f)
 }
 
 UV
-Perl_cast_uv(pTHX_ double f)
+Perl_cast_uv(pTHX_ NV f)
 {
     if (f >= MY_UV_MAX)
        return (UV) MY_UV_MAX;
@@ -2685,13 +2752,15 @@ Perl_scan_bin(pTHX_ char *start, I32 len, I32 *retlen)
     register UV retval = 0;
     bool overflowed = FALSE;
     while (len && *s >= '0' && *s <= '1') {
-      register UV n = retval << 1;
-      if (!overflowed && (n >> 1) != retval) {
-          Perl_warn(aTHX_ "Integer overflow in binary number");
-          overflowed = TRUE;
-      }
-      retval = n | (*s++ - '0');
-      len--;
+       register UV n = retval << 1;
+       if (!overflowed && (n >> 1) != retval) {
+           dTHR;
+           if (ckWARN_d(WARN_UNSAFE))
+               Perl_warner(aTHX_ WARN_UNSAFE, "Integer overflow in binary number");
+           overflowed = TRUE;
+       }
+       retval = n | (*s++ - '0');
+       len--;
     }
     if (len && (*s >= '2' && *s <= '9')) {
       dTHR;
@@ -2711,7 +2780,9 @@ Perl_scan_oct(pTHX_ char *start, I32 len, I32 *retlen)
     while (len && *s >= '0' && *s <= '7') {
        register UV n = retval << 3;
        if (!overflowed && (n >> 3) != retval) {
-           Perl_warn(aTHX_ "Integer overflow in octal number");
+           dTHR;
+           if (ckWARN_d(WARN_UNSAFE))
+               Perl_warner(aTHX_ WARN_UNSAFE, "Integer overflow in octal number");
            overflowed = TRUE;
        }
        retval = n | (*s++ - '0');
@@ -2750,7 +2821,9 @@ Perl_scan_hex(pTHX_ char *start, I32 len, I32 *retlen)
        }
        n = retval << 4;
        if (!overflowed && (n >> 4) != retval) {
-           Perl_warn(aTHX_ "Integer overflow in hex number");
+           dTHR;
+           if (ckWARN_d(WARN_UNSAFE))
+               Perl_warner(aTHX_ WARN_UNSAFE, "Integer overflow in hex number");
            overflowed = TRUE;
        }
        retval = n | ((tmp - PL_hexdigit) & 15);
@@ -3168,6 +3241,9 @@ Perl_new_struct_thread(pTHX_ struct perl_thread *t)
     PL_maxscream = -1;
     PL_regcompp = FUNC_NAME_TO_PTR(Perl_pregcomp);
     PL_regexecp = FUNC_NAME_TO_PTR(Perl_regexec_flags);
+    PL_regint_start = FUNC_NAME_TO_PTR(Perl_re_intuit_start);
+    PL_regint_string = FUNC_NAME_TO_PTR(Perl_re_intuit_string);
+    PL_regfree = FUNC_NAME_TO_PTR(Perl_pregfree);
     PL_regindent = 0;
     PL_reginterp_cnt = 0;
     PL_lastscream = Nullsv;
@@ -3224,7 +3300,7 @@ Perl_new_struct_thread(pTHX_ struct perl_thread *t)
     MUTEX_UNLOCK(&t->mutex);
 
 #ifdef HAVE_THREAD_INTERN
-    init_thread_intern(thr);
+    Perl_init_thread_intern(thr);
 #endif /* HAVE_THREAD_INTERN */
     return thr;
 }
@@ -3236,7 +3312,7 @@ Perl_new_struct_thread(pTHX_ struct perl_thread *t)
  * So it is in perl for (say) POSIX to use. 
  * Needed for SunOS with Sun's 'acc' for example.
  */
-double 
+NV 
 Perl_huge(void)
 {
  return HUGE_VAL;
@@ -3275,6 +3351,12 @@ Perl_get_opargs(pTHX)
  return PL_opargs;
 }
 
+PPADDR_t*
+Perl_get_ppaddr(pTHX)
+{
+ return &PL_ppaddr;
+}
+
 #ifndef HAS_GETENV_LEN
 char *
 Perl_getenv_len(pTHX_ char *env_elem, unsigned long *len)
@@ -3432,3 +3514,24 @@ Perl_my_fflush_all(pTHX)
     return EOF;
 #endif
 }
+
+NV
+Perl_my_atof(pTHX_ const char* s) {
+#ifdef USE_LOCALE_NUMERIC
+    if ((PL_hints & HINT_LOCALE) && PL_numeric_local) {
+       NV x, y;
+
+       x = Perl_atof(s);
+       SET_NUMERIC_STANDARD();
+       y = Perl_atof(s);
+       SET_NUMERIC_LOCAL();
+       if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
+           return y;
+       return x;
+    }
+    else
+       return Perl_atof(s);
+#else
+    return Perl_atof(s);
+#endif
+}