diff options
Diffstat (limited to 'src/luasocket.c')
-rw-r--r-- | src/luasocket.c | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/src/luasocket.c b/src/luasocket.c index 578d65c..73583a8 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
@@ -36,8 +36,63 @@ | |||
36 | #include "mime.h" | 36 | #include "mime.h" |
37 | 37 | ||
38 | /*=========================================================================*\ | 38 | /*=========================================================================*\ |
39 | * Exported functions | 39 | * Declarations |
40 | \*=========================================================================*/ | 40 | \*=========================================================================*/ |
41 | static int global_gethostname(lua_State *L); | ||
42 | static int base_open(lua_State *L); | ||
43 | |||
44 | /* functions in library namespace */ | ||
45 | static luaL_reg func[] = { | ||
46 | {"gethostname", global_gethostname}, | ||
47 | {NULL, NULL} | ||
48 | }; | ||
49 | |||
50 | /*-------------------------------------------------------------------------*\ | ||
51 | * Setup basic stuff. | ||
52 | \*-------------------------------------------------------------------------*/ | ||
53 | static int base_open(lua_State *L) | ||
54 | { | ||
55 | /* create namespace table */ | ||
56 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
57 | lua_newtable(L); | ||
58 | #ifdef LUASOCKET_DEBUG | ||
59 | lua_pushstring(L, "debug"); | ||
60 | lua_pushnumber(L, 1); | ||
61 | lua_rawset(L, -3); | ||
62 | #endif | ||
63 | /* make version string available so scripts */ | ||
64 | lua_pushstring(L, "version"); | ||
65 | lua_pushstring(L, LUASOCKET_VERSION); | ||
66 | lua_rawset(L, -3); | ||
67 | /* store namespace as global */ | ||
68 | lua_settable(L, LUA_GLOBALSINDEX); | ||
69 | /* make sure modules know what is our namespace */ | ||
70 | lua_pushstring(L, "LUASOCKET_LIBNAME"); | ||
71 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
72 | lua_settable(L, LUA_GLOBALSINDEX); | ||
73 | /* define library functions */ | ||
74 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | ||
75 | lua_pop(L, 1); | ||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | /*-------------------------------------------------------------------------*\ | ||
80 | * Gets the host name | ||
81 | \*-------------------------------------------------------------------------*/ | ||
82 | static int global_gethostname(lua_State *L) | ||
83 | { | ||
84 | char name[257]; | ||
85 | name[256] = '\0'; | ||
86 | if (gethostname(name, 256) < 0) { | ||
87 | lua_pushnil(L); | ||
88 | lua_pushstring(L, "gethostname failed"); | ||
89 | return 2; | ||
90 | } else { | ||
91 | lua_pushstring(L, name); | ||
92 | return 1; | ||
93 | } | ||
94 | } | ||
95 | |||
41 | /*-------------------------------------------------------------------------*\ | 96 | /*-------------------------------------------------------------------------*\ |
42 | * Initializes all library modules. | 97 | * Initializes all library modules. |
43 | \*-------------------------------------------------------------------------*/ | 98 | \*-------------------------------------------------------------------------*/ |
@@ -45,6 +100,7 @@ LUASOCKET_API int luaopen_socket(lua_State *L) | |||
45 | { | 100 | { |
46 | if (!sock_open()) return 0; | 101 | if (!sock_open()) return 0; |
47 | /* initialize all modules */ | 102 | /* initialize all modules */ |
103 | base_open(L); | ||
48 | aux_open(L); | 104 | aux_open(L); |
49 | tm_open(L); | 105 | tm_open(L); |
50 | buf_open(L); | 106 | buf_open(L); |