From 5564cf055458868e01fd8d950ed571194cfb912b Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 11 Apr 2024 23:07:02 +0100 Subject: [PATCH] add nellie.close --- pkgs/nellie/nellie.c | 19 +++++++++++-------- pkgs/nellie/test.lua | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/pkgs/nellie/nellie.c b/pkgs/nellie/nellie.c index 69c3201..761fe19 100644 --- a/pkgs/nellie/nellie.c +++ b/pkgs/nellie/nellie.c @@ -6,17 +6,16 @@ #include #include #include +#include -/* static int l_close_socket(lua_State *L) { */ -/* LStream *p = (LStream *) luaL_checkudata(L, 1, LUA_FILEHANDLE); */ -/* int res = fclose(p->f); */ -/* return luaL_fileresult(L, (res == 0), NULL); */ -/* } */ - +static int l_close_socket(lua_State *L) { + lua_getfield(L, 1, "fileno"); + int fd = (int) lua_tointeger(L, -1); + close(fd); + return 0; +} static int l_read_from_socket(lua_State *L) { - /* struct sockaddr_nl sa; */ - /* memset(&sa, 0, sizeof(sa)); */ int length = 32; if(lua_isnumber(L, 2)) @@ -64,6 +63,10 @@ static int l_open_socket(lua_State *L) { lua_pushcfunction(L, l_read_from_socket); lua_settable(L, 1); + lua_pushliteral(L, "close"); + lua_pushcfunction(L, l_close_socket); + lua_settable(L, 1); + return 1; } else { return 0; diff --git a/pkgs/nellie/test.lua b/pkgs/nellie/test.lua index d964c49..8309d2b 100644 --- a/pkgs/nellie/test.lua +++ b/pkgs/nellie/test.lua @@ -3,3 +3,4 @@ print('dfg') local f = nellie.open(2) print(string.byte(f:read(1000), 0, 60)) +print("CLOSED", f:close())