10 #include "mod_cml_funcs.h"
25 typedef unsigned char HASH
[HASHLEN
];
27 typedef char HASHHEX
[HASHHEXLEN
+1];
37 int f_crypto_md5(lua_State
*L
) {
41 int n
= lua_gettop(L
);
46 lua_pushstring(L
, "md5: expected one argument");
50 if (!lua_isstring(L
, 1)) {
51 lua_pushstring(L
, "md5: argument has to be a string");
55 s
= lua_tolstring(L
, 1, &s_len
);
58 li_MD5_Update(&Md5Ctx
, (unsigned char *) s
, (unsigned int) s_len
);
59 li_MD5_Final(HA1
, &Md5Ctx
);
61 li_tohex(hex
, sizeof(hex
), (const char*) HA1
, 16);
63 lua_pushstring(L
, hex
);
69 int f_file_mtime(lua_State
*L
) {
71 int n
= lua_gettop(L
);
74 lua_pushstring(L
, "file_mtime: expected one argument");
78 if (!lua_isstring(L
, 1)) {
79 lua_pushstring(L
, "file_mtime: argument has to be a string");
83 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
88 lua_pushinteger(L
, st
.st_mtime
);
93 static int f_dir_files_iter(lua_State
*L
) {
97 d
= lua_touserdata(L
, lua_upvalueindex(1));
99 if (NULL
== (de
= readdir(d
))) {
105 lua_pushstring(L
, de
->d_name
);
110 int f_dir_files(lua_State
*L
) {
112 int n
= lua_gettop(L
);
115 lua_pushstring(L
, "dir_files: expected one argument");
119 if (!lua_isstring(L
, 1)) {
120 lua_pushstring(L
, "dir_files: argument has to be a string");
124 /* check if there is a valid DIR handle on the stack */
125 if (NULL
== (d
= opendir(lua_tostring(L
, 1)))) {
130 /* push d into userdata */
131 lua_pushlightuserdata(L
, d
);
132 lua_pushcclosure(L
, f_dir_files_iter
, 1);
137 int f_file_isreg(lua_State
*L
) {
139 int n
= lua_gettop(L
);
142 lua_pushstring(L
, "file_isreg: expected one argument");
146 if (!lua_isstring(L
, 1)) {
147 lua_pushstring(L
, "file_isreg: argument has to be a string");
151 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
156 lua_pushinteger(L
, S_ISREG(st
.st_mode
));
161 int f_file_isdir(lua_State
*L
) {
163 int n
= lua_gettop(L
);
166 lua_pushstring(L
, "file_isreg: expected one argument");
170 if (!lua_isstring(L
, 1)) {
171 lua_pushstring(L
, "file_isreg: argument has to be a string");
175 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
180 lua_pushinteger(L
, S_ISDIR(st
.st_mode
));
188 int f_memcache_exists(lua_State
*L
) {
193 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
194 lua_pushstring(L
, "where is my userdata ?");
198 memc
= lua_touserdata(L
, lua_upvalueindex(1));
200 if (1 != lua_gettop(L
)) {
201 lua_pushstring(L
, "expected one argument");
205 key
= luaL_checklstring(L
, 1, &key_len
);
206 lua_pushboolean(L
, (MEMCACHED_SUCCESS
== memcached_exist(memc
, key
, key_len
)));
210 int f_memcache_get_string(lua_State
*L
) {
211 size_t key_len
, value_len
;
216 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
217 lua_pushstring(L
, "where is my userdata ?");
221 memc
= lua_touserdata(L
, lua_upvalueindex(1));
223 if (1 != lua_gettop(L
)) {
224 lua_pushstring(L
, "expected one argument");
228 key
= luaL_checklstring(L
, 1, &key_len
);
229 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
234 lua_pushlstring(L
, value
, value_len
);
241 int f_memcache_get_long(lua_State
*L
) {
242 size_t key_len
, value_len
;
249 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
250 lua_pushstring(L
, "where is my userdata ?");
254 memc
= lua_touserdata(L
, lua_upvalueindex(1));
256 if (1 != lua_gettop(L
)) {
257 lua_pushstring(L
, "expected one argument");
261 key
= luaL_checklstring(L
, 1, &key_len
);
262 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
268 v
= strtol(value
, &endptr
, 10);
269 if (0 == errno
&& *endptr
== '\0') {
270 lua_pushinteger(L
, v
);