klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

ksystem.posix.c (1233B)


      1 /*
      2 ** ksystem.posix.c
      3 ** Platform dependent functionality - version for POSIX systems.
      4 ** See Copyright Notice in klisp.h
      5 */
      6 
      7 #include <stdio.h>
      8 #include <sys/time.h>
      9 #include "kobject.h"
     10 #include "kstate.h"
     11 #include "kinteger.h"
     12 #include "kport.h"
     13 #include "ksystem.h"
     14 
     15 /* declare implemented functionality */
     16 
     17 #define HAVE_PLATFORM_JIFFIES
     18 #define HAVE_PLATFORM_ISATTY
     19 
     20 /* jiffies */
     21 
     22 TValue ksystem_current_jiffy(klisp_State *K)
     23 {
     24     /* TEMP: use gettimeofday(). clock_gettime(CLOCK_MONOTONIC,...)
     25      * might be more apropriate, but it is reportedly not
     26      * supported on MacOS X. */
     27 
     28     struct timeval tv;
     29     gettimeofday(&tv, NULL);
     30 
     31     TValue res = kbigint_make_simple(K);
     32     krooted_vars_push(K, &res);
     33     mp_int_set_value(K, tv2bigint(res), tv.tv_sec);
     34     mp_int_mul_value(K, tv2bigint(res), 1000000, tv2bigint(res));
     35     mp_int_add_value(K, tv2bigint(res), tv.tv_usec, tv2bigint(res));
     36     krooted_vars_pop(K);
     37 
     38     return kbigint_try_fixint(K, res);
     39 }
     40 
     41 TValue ksystem_jiffies_per_second(klisp_State *K)
     42 {
     43     UNUSED(K);
     44     return i2tv(1000000);
     45 }
     46 
     47 /* isatty */
     48 
     49 bool ksystem_isatty(klisp_State *K, TValue port)
     50 {
     51     return ttisfport(port) && kport_is_open(port)
     52         && isatty(fileno(kfport_file(port)));
     53 }