[project @ 2001-01-05 15:24:28 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / nHandle.c
index 063bc79..ecc5f8f 100644 (file)
@@ -8,6 +8,84 @@
 #include <assert.h>
 #include <malloc.h>
 #include <stdlib.h>
+#include <ctype.h>
+#ifndef _WIN32
+#include <sys/times.h>
+#include <sys/resource.h>
+#include <sys/stat.h>
+#include <time.h>
+#endif
+#include <unistd.h>
+
+#ifndef _WIN32
+double nh_getCPUtime ( void )
+{
+   double usertime;
+   struct rusage usage;
+   getrusage ( RUSAGE_SELF, &usage );
+   usertime = (double)usage.ru_utime.tv_sec +
+              (double)usage.ru_utime.tv_usec / 1000000.0;
+   return usertime;
+}
+
+double nh_getCPUprec ( void )
+{
+   /* or perhaps CLOCKS_PER_SEC ? */
+   return 1.0 / (double)(CLK_TCK);
+}
+#else
+double nh_getCPUtime ( void )
+{
+   return 1;
+}
+
+double nh_getCPUprec ( void )
+{
+   return 1;
+}
+#endif
+
+int nh_getPID ( void )
+{
+#ifndef _WIN32
+   return (int) getpid();
+#else
+   return (int) 0;
+#endif
+}
+
+void nh_exitwith ( int code )
+{
+   exit(code);
+}
+
+int nh_system ( char* cmd )
+{
+   return system ( cmd );
+}
+
+int nh_iseof ( FILE* f )
+{
+   int c;
+   errno = 0;
+   c = fgetc ( f );
+   if (c == EOF) return 1;
+   ungetc ( c, f );
+   return 0;
+}
+
+int nh_filesize ( FILE* f )
+{
+#ifndef _WIN32
+   struct stat buf;
+   errno = 0;
+   fstat ( fileno(f), &buf );
+   return buf.st_size;
+#else
+   errno = EPERM;
+   return 0;
+#endif
+}
 
 int nh_stdin ( void )
 {
@@ -52,7 +130,8 @@ void nh_write ( FILE* f, int c )
 {
    errno = 0;
    fputc(c,f);
-   fflush(f);
+   if (f==stderr) { fflush(f); } 
+   if (f==stdout) { fflush(f); } 
 }
 
 int nh_read ( FILE* f )
@@ -63,13 +142,14 @@ int nh_read ( FILE* f )
 
 int nh_errno ( void )
 {
-   return errno;
+   int t = errno;
+   errno = 0;
+   return t;
 }
 
 int nh_malloc ( int n )
 {
    char* p = malloc(n);
-   assert(p);
    return (int)p;
 }
 
@@ -93,15 +173,3 @@ int nh_getenv ( int p )
    return (int)getenv ( (const char *)p );
 }
 
-extern int prog_argc;
-extern char** prog_argv;
-
-int nh_argc ( void )
-{
-   return prog_argc;
-}
-
-int nh_argvb ( int argno, int offset )
-{
-   return (int)(prog_argv[argno][offset]);
-}