-
Notifications
You must be signed in to change notification settings - Fork 3
/
luachild_lua_5_3.c
50 lines (41 loc) · 1.11 KB
/
luachild_lua_5_3.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
#include "luachild.h"
#ifdef USE_LUAPUC
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "lualib.h"
#include "lauxlib.h"
int lua_report_type_error(lua_State *L, int narg, const char * tname) {
// TODO : proper implementation !-
printf("bad argument %d to function %s\n", narg, tname);
return 0;
}
size_t lua_value_length(lua_State *L, int index) {
return lua_rawlen(L, index);
}
static int file_close(lua_State *L) {
int result = 1;
FILE **p = (FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE);
if (!fclose(*p)) {
lua_pushboolean(L, 1);
} else {
int en = errno; /* calls to Lua API may change this value */
lua_pushnil(L);
lua_pushfstring(L, "%s", strerror(en));
lua_pushinteger(L, en);
result = 3;
}
*p = NULL;
return result;
}
int file_handler_creator(lua_State *L, const char * file_path, int get_path_from_env){
return 1;
}
void lua_pushcfile(lua_State *L, FILE * f){
luaL_Stream *lf = lua_newuserdata(L, sizeof(luaL_Stream));
luaL_getmetatable(L, LUA_FILEHANDLE);
lua_setmetatable(L, -2);
lf->f = f;
lf->closef = &file_close;
}
#endif // USE_LUAPUC