ref: ce3af9e03b554081f6689a541438f32cdd2c44c5
dir: /sys_posix.c/
#include "flisp.h"
#include "timefuncs.h"
double
sec_realtime(void)
{
struct timespec now;
if(clock_gettime(CLOCK_REALTIME, &now) != 0)
return 0;
return (double)now.tv_sec + (double)now.tv_nsec/1.0e9;
}
uint64_t
nanosec_monotonic(void)
{
static int64_t z;
struct timespec now;
if(clock_gettime(CLOCK_MONOTONIC, &now) != 0)
return 0;
if(z == 0){
z = now.tv_sec*1000000000LL + now.tv_nsec;
return 0;
}
return now.tv_sec*1000000000LL + now.tv_nsec - z;
}
void
timestring(double s, char *buf, int sz)
{
time_t tme = (time_t)s;
struct tm tm;
localtime_r(&tme, &tm);
strftime(buf, sz, "%c", &tm);
}
double
parsetime(const char *s)
{
char *res;
time_t t;
struct tm tm;
res = strptime(s, "%c", &tm);
if(res != nil){
/* Not set by strptime(); tells mktime() to determine
* whether daylight saving time is in effect
*/
tm.tm_isdst = -1;
t = mktime(&tm);
if(t == (time_t)-1)
return -1;
return (double)t;
}
return -1;
}
void
sleep_ms(int ms)
{
if(ms != 0){
struct timeval timeout;
timeout.tv_sec = ms/1000;
timeout.tv_usec = (ms % 1000) * 1000;
select(0, nil, nil, nil, &timeout);
}
}
static const uint8_t boot[] = {
#include "flisp.boot.h"
};
#if defined(__linux__)
char linux_kernel_version[64];
#include <sys/utsname.h>
#endif
int
main(int argc, char **argv)
{
setlocale(LC_NUMERIC, "C");
#if defined(__linux__)
struct utsname u;
linux_kernel_version[0] = 0;
if(uname(&u) == 0)
snprintf(linux_kernel_version, sizeof(linux_kernel_version), "%s", u.release);
#endif
flmain(boot, sizeof(boot), argc, argv);
}