10 #include "mod_cml_funcs.h"
25 typedef unsigned char HASH
[HASHLEN
];
27 typedef char HASHHEX
[HASHHEXLEN
+1];
39 int f_crypto_md5(lua_State
*L
) {
43 int n
= lua_gettop(L
);
48 lua_pushstring(L
, "md5: expected one argument");
52 if (!lua_isstring(L
, 1)) {
53 lua_pushstring(L
, "md5: argument has to be a string");
57 s
= lua_tolstring(L
, 1, &s_len
);
60 li_MD5_Update(&Md5Ctx
, (unsigned char *) s
, (unsigned int) s_len
);
61 li_MD5_Final(HA1
, &Md5Ctx
);
63 li_tohex(hex
, sizeof(hex
), (const char*) HA1
, 16);
65 lua_pushstring(L
, hex
);
71 int f_file_mtime(lua_State
*L
) {
73 int n
= lua_gettop(L
);
76 lua_pushstring(L
, "file_mtime: expected one argument");
80 if (!lua_isstring(L
, 1)) {
81 lua_pushstring(L
, "file_mtime: argument has to be a string");
85 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
90 lua_pushinteger(L
, st
.st_mtime
);
95 static int f_dir_files_iter(lua_State
*L
) {
99 d
= lua_touserdata(L
, lua_upvalueindex(1));
101 if (NULL
== (de
= readdir(d
))) {
107 lua_pushstring(L
, de
->d_name
);
112 int f_dir_files(lua_State
*L
) {
114 int n
= lua_gettop(L
);
117 lua_pushstring(L
, "dir_files: expected one argument");
121 if (!lua_isstring(L
, 1)) {
122 lua_pushstring(L
, "dir_files: argument has to be a string");
126 /* check if there is a valid DIR handle on the stack */
127 if (NULL
== (d
= opendir(lua_tostring(L
, 1)))) {
132 /* push d into userdata */
133 lua_pushlightuserdata(L
, d
);
134 lua_pushcclosure(L
, f_dir_files_iter
, 1);
139 int f_file_isreg(lua_State
*L
) {
141 int n
= lua_gettop(L
);
144 lua_pushstring(L
, "file_isreg: expected one argument");
148 if (!lua_isstring(L
, 1)) {
149 lua_pushstring(L
, "file_isreg: argument has to be a string");
153 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
158 lua_pushinteger(L
, S_ISREG(st
.st_mode
));
163 int f_file_isdir(lua_State
*L
) {
165 int n
= lua_gettop(L
);
168 lua_pushstring(L
, "file_isreg: expected one argument");
172 if (!lua_isstring(L
, 1)) {
173 lua_pushstring(L
, "file_isreg: argument has to be a string");
177 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
182 lua_pushinteger(L
, S_ISDIR(st
.st_mode
));
190 int f_memcache_exists(lua_State
*L
) {
195 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
196 lua_pushstring(L
, "where is my userdata ?");
200 memc
= lua_touserdata(L
, lua_upvalueindex(1));
202 if (1 != lua_gettop(L
)) {
203 lua_pushstring(L
, "expected one argument");
207 key
= luaL_checklstring(L
, 1, &key_len
);
208 lua_pushboolean(L
, (MEMCACHED_SUCCESS
== memcached_exist(memc
, key
, key_len
)));
212 int f_memcache_get_string(lua_State
*L
) {
213 size_t key_len
, value_len
;
218 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
219 lua_pushstring(L
, "where is my userdata ?");
223 memc
= lua_touserdata(L
, lua_upvalueindex(1));
225 if (1 != lua_gettop(L
)) {
226 lua_pushstring(L
, "expected one argument");
230 key
= luaL_checklstring(L
, 1, &key_len
);
231 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
236 lua_pushlstring(L
, value
, value_len
);
243 int f_memcache_get_long(lua_State
*L
) {
244 size_t key_len
, value_len
;
251 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
252 lua_pushstring(L
, "where is my userdata ?");
256 memc
= lua_touserdata(L
, lua_upvalueindex(1));
258 if (1 != lua_gettop(L
)) {
259 lua_pushstring(L
, "expected one argument");
263 key
= luaL_checklstring(L
, 1, &key_len
);
264 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
270 v
= strtol(value
, &endptr
, 10);
271 if (0 == errno
&& *endptr
== '\0') {
272 lua_pushinteger(L
, v
);