Use PERL=../miniperl
[p5sagit/p5-mst-13.2.git] / util.c
diff --git a/util.c b/util.c
index f2bf077..9ffb431 100644 (file)
--- a/util.c
+++ b/util.c
 #  include <vfork.h>
 #endif
 
+#ifdef I_LIMITS  /* Needed for cast_xxx() functions below. */
+#  include <limits.h>
+#endif
+
 /* Put this after #includes because fork and vfork prototypes may
    conflict.
 */
@@ -1027,7 +1031,7 @@ char *nam;
 }
 #endif /* !VMS */
 
-#ifdef EUNICE
+#ifdef UNLINK_ALL_VERSIONS
 I32
 unlnk(f)       /* unlink all versions of a file */
 char *f;
@@ -1093,7 +1097,7 @@ register I32 len;
 }
 #endif /* HAS_MEMCMP */
 
-#ifdef I_VARARGS
+#if defined(I_STDARG) || defined(I_VARARGS)
 #ifndef HAS_VPRINTF
 
 #ifdef USE_CHAR_VSPRINTF
@@ -1130,7 +1134,7 @@ char *pat, *args;
     return 0;          /* wrong, but perl doesn't use the return value */
 }
 #endif /* HAS_VPRINTF */
-#endif /* I_VARARGS */
+#endif /* I_VARARGS || I_STDARGS */
 
 #ifdef MYSWAP
 #if BYTEORDER != 0x4321
@@ -1359,7 +1363,7 @@ char      *mode;
     return fdopen(p[this], mode);
 }
 #else
-#ifdef atarist
+#if defined(atarist) || defined(OS2)
 FILE *popen();
 FILE *
 my_popen(cmd,mode)
@@ -1416,8 +1420,7 @@ int newfd;
 }
 #endif
 
-#ifndef DOSISH
-#ifndef VMS /* VMS' my_pclose() is in VMS.c */
+#if  !defined(DOSISH) && !defined(VMS)  /* VMS' my_popen() is in VMS.c */
 I32
 my_pclose(ptr)
 FILE *ptr;
@@ -1446,7 +1449,9 @@ FILE *ptr;
     signal(SIGQUIT, qstat);
     return(pid < 0 ? pid : status);
 }
-#endif /* !VMS */
+#endif /* !DOSISH */
+
+#if  !defined(DOSISH) || defined(OS2)
 I32
 wait4pid(pid,statusp,flags)
 int pid;
@@ -1520,7 +1525,7 @@ int status;
     return;
 }
 
-#ifdef atarist
+#if defined(atarist) || defined(OS2)
 int pclose();
 I32
 my_pclose(ptr)
@@ -1575,36 +1580,75 @@ double f;
 #endif
 
 #ifndef CASTI32
+
+/* Look for MAX and MIN integral values.  If we can't find them,
+   we'll use 32-bit two's complement defaults.
+*/
+#ifndef LONG_MAX
+#  ifdef MAXLONG    /* Often used in <values.h> */
+#    define LONG_MAX MAXLONG
+#  else
+#    define LONG_MAX        2147483647L
+#  endif
+#endif
+
+#ifndef LONG_MIN
+#    define LONG_MIN        (-LONG_MAX - 1)
+#endif
+
+#ifndef ULONG_MAX
+#  ifdef MAXULONG 
+#    define LONG_MAX MAXULONG
+#  else
+#    define ULONG_MAX       4294967295L
+#  endif
+#endif
+
+/* Unfortunately, on some systems the cast_uv() function doesn't
+   work with the system-supplied definition of ULONG_MAX.  The
+   comparison  (f >= ULONG_MAX) always comes out true.  It must be a
+   problem with the compiler constant folding.
+
+   In any case, this workaround should be fine on any two's complement
+   system.  If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
+   ccflags.
+              --Andy Dougherty      <doughera@lafcol.lafayette.edu>
+*/
+#ifndef MY_ULONG_MAX
+#  define MY_ULONG_MAX ((UV)LONG_MAX * (UV)2 + (UV)1)
+#endif
+
 I32
 cast_i32(f)
 double f;
 {
-#   define BIGDOUBLE 2147483648.0        /* Assume 32 bit int's ! */
-#   define BIGNEGDOUBLE (-2147483648.0)
-    if (f >= BIGDOUBLE)
-       return (I32)fmod(f, BIGDOUBLE);
-    if (f <= BIGNEGDOUBLE)
-       return (I32)fmod(f, BIGNEGDOUBLE);
+    if (f >= LONG_MAX)
+       return (I32) LONG_MAX;
+    if (f <= LONG_MIN)
+       return (I32) LONG_MIN;
     return (I32) f;
 }
-# undef BIGDOUBLE
-# undef BIGNEGDOUBLE
 
 IV
 cast_iv(f)
 double f;
 {
-    /* XXX  This should be fixed.  It assumes 32 bit IV's. */
-#   define BIGDOUBLE 2147483648.0        /* Assume 32 bit IV's ! */
-#   define BIGNEGDOUBLE (-2147483648.0)
-    if (f >= BIGDOUBLE)
-       return (IV)fmod(f, BIGDOUBLE);
-    if (f <= BIGNEGDOUBLE)
-       return (IV)fmod(f, BIGNEGDOUBLE);
+    if (f >= LONG_MAX)
+       return (IV) LONG_MAX;
+    if (f <= LONG_MIN)
+       return (IV) LONG_MIN;
     return (IV) f;
 }
-# undef BIGDOUBLE
-# undef BIGNEGDOUBLE
+
+UV
+cast_uv(f)
+double f;
+{
+    if (f >= MY_ULONG_MAX)
+       return (UV) MY_ULONG_MAX;
+    return (UV) f;
+}
+
 #endif
 
 #ifndef HAS_RENAME
@@ -1687,28 +1731,3 @@ I32 *retlen;
     *retlen = s - start;
     return retval;
 }
-
-/* Amazingly enough, some systems (e.g. Dynix 3) don't have fmod.
-   This is a slow, stupid, but working emulation.  (AD)
-*/
-#ifdef USE_MY_FMOD
-double
-my_fmod(x, y)
-double x, y;
-{
-    double i = 0.0;   /* Can't use int because it can overflow */
-    if ((x == 0) || (y == 0))
-       return 0;
-    /* The sign of fmod is the same as the sign of x.  */
-    if ( (x < 0 && y > 0) || (x > 0 && y < 0) )
-       y = -y;
-    if (x > 0) {
-       while (x - i*y > y)
-           i++;
-    } else {
-       while (x - i*y < y)
-           i++;
-    }
-    return x - i * y;
-}
-#endif