factor/native/misc.c

83 lines
1.3 KiB
C
Raw Normal View History

2004-08-04 03:12:55 -04:00
#include "factor.h"
void primitive_exit(void)
{
exit(to_fixnum(dpop()));
2004-08-04 03:12:55 -04:00
}
void primitive_os_env(void)
{
char *name, *value;
maybe_garbage_collection();
name = unbox_c_string();
value = getenv(name);
2004-08-04 03:12:55 -04:00
if(value == NULL)
2004-09-19 17:39:28 -04:00
dpush(F);
2004-08-04 03:12:55 -04:00
else
2004-09-19 17:39:28 -04:00
box_c_string(getenv(name));
2004-08-04 03:12:55 -04:00
}
void primitive_eq(void)
{
2004-11-08 22:36:51 -05:00
box_boolean(dpop() == dpop());
2004-08-04 03:12:55 -04:00
}
2004-12-10 21:39:45 -05:00
#ifdef WIN32
int64_t current_millis(void)
{
FILETIME t;
GetSystemTimeAsFileTime(&t);
2004-12-11 15:02:34 -05:00
return (((int64_t)t.dwLowDateTime | (int64_t)t.dwHighDateTime<<32) - EPOCH_OFFSET)
/ 10000;
2004-12-10 21:39:45 -05:00
}
#else
int64_t current_millis(void)
2004-08-04 03:12:55 -04:00
{
struct timeval t;
gettimeofday(&t,NULL);
2004-12-10 21:39:45 -05:00
return (int64_t)t.tv_sec * 1000 + t.tv_usec/1000;
2004-11-22 19:15:14 -05:00
}
2004-12-10 21:39:45 -05:00
#endif
2004-11-22 19:15:14 -05:00
void primitive_millis(void)
{
maybe_garbage_collection();
2004-11-22 19:15:14 -05:00
dpush(tag_object(s48_long_long_to_bignum(current_millis())));
2004-08-04 03:12:55 -04:00
}
void primitive_init_random(void)
{
2004-12-10 21:39:45 -05:00
srand((unsigned)time(NULL));
}
void primitive_random_int(void)
{
maybe_garbage_collection();
2004-12-10 21:39:45 -05:00
dpush(tag_object(s48_long_to_bignum(rand())));
}
2004-12-17 12:22:16 -05:00
#ifdef WIN32
F_STRING *last_error()
{
char *buffer;
F_STRING *error;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &buffer,
0, NULL);
error = from_c_string(buffer);
LocalFree(buffer);
return error;
}
#endif