-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.c
62 lines (51 loc) · 1.09 KB
/
sys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <lua.h>
#include <lauxlib.h>
#ifdef LUA_WIN
static int l_clock(lua_State *L) {
printf("warning: sys.clock not implemented on Windows\n");
return 0;
}
static int l_usleep(lua_State *L) {
printf("warning: sys.usleep not implemented on Windows\n");
return 0;
}
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
static int l_clock(lua_State *L) {
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
double precise_time = tv.tv_sec + tv.tv_usec / 1e6;
lua_pushnumber(L,precise_time);
return 1;
}
static int l_usleep(lua_State *L) {
int time = 1;
if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1);
usleep(time);
return 1;
}
#endif
static const struct luaL_Reg routines [] = {
{"clock", l_clock},
{"usleep", l_usleep},
{NULL, NULL}
};
int luaopen_libsys(lua_State *L)
{
lua_newtable(L);
#if LUA_VERSION_NUM == 501
luaL_register(L, NULL, routines);
#else
luaL_setfuncs(L, routines, 0);
#endif
return 1;
}