15 #include "mod_cml_funcs.h"
27 typedef unsigned char HASH
[HASHLEN
];
29 typedef char HASHHEX
[HASHHEXLEN
+1];
31 int f_crypto_md5(lua_State
*L
) {
35 int n
= lua_gettop(L
);
40 lua_pushstring(L
, "md5: expected one argument");
44 if (!lua_isstring(L
, 1)) {
45 lua_pushstring(L
, "md5: argument has to be a string");
49 s
= lua_tolstring(L
, 1, &s_len
);
52 li_MD5_Update(&Md5Ctx
, (unsigned char *) s
, (unsigned int) s_len
);
53 li_MD5_Final(HA1
, &Md5Ctx
);
55 li_tohex(hex
, sizeof(hex
), (const char*) HA1
, 16);
57 lua_pushstring(L
, hex
);
63 int f_file_mtime(lua_State
*L
) {
65 int n
= lua_gettop(L
);
68 lua_pushstring(L
, "file_mtime: expected one argument");
72 if (!lua_isstring(L
, 1)) {
73 lua_pushstring(L
, "file_mtime: argument has to be a string");
77 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
82 lua_pushinteger(L
, st
.st_mtime
);
87 static int f_dir_files_iter(lua_State
*L
) {
91 d
= lua_touserdata(L
, lua_upvalueindex(1));
93 if (NULL
== (de
= readdir(d
))) {
99 lua_pushstring(L
, de
->d_name
);
104 int f_dir_files(lua_State
*L
) {
106 int n
= lua_gettop(L
);
109 lua_pushstring(L
, "dir_files: expected one argument");
113 if (!lua_isstring(L
, 1)) {
114 lua_pushstring(L
, "dir_files: argument has to be a string");
118 /* check if there is a valid DIR handle on the stack */
119 if (NULL
== (d
= opendir(lua_tostring(L
, 1)))) {
124 /* push d into userdata */
125 lua_pushlightuserdata(L
, d
);
126 lua_pushcclosure(L
, f_dir_files_iter
, 1);
131 int f_file_isreg(lua_State
*L
) {
133 int n
= lua_gettop(L
);
136 lua_pushstring(L
, "file_isreg: expected one argument");
140 if (!lua_isstring(L
, 1)) {
141 lua_pushstring(L
, "file_isreg: argument has to be a string");
145 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
150 lua_pushinteger(L
, S_ISREG(st
.st_mode
));
155 int f_file_isdir(lua_State
*L
) {
157 int n
= lua_gettop(L
);
160 lua_pushstring(L
, "file_isreg: expected one argument");
164 if (!lua_isstring(L
, 1)) {
165 lua_pushstring(L
, "file_isreg: argument has to be a string");
169 if (-1 == stat(lua_tostring(L
, 1), &st
)) {
174 lua_pushinteger(L
, S_ISDIR(st
.st_mode
));
182 int f_memcache_exists(lua_State
*L
) {
187 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
188 lua_pushstring(L
, "where is my userdata ?");
192 memc
= lua_touserdata(L
, lua_upvalueindex(1));
194 if (1 != lua_gettop(L
)) {
195 lua_pushstring(L
, "expected one argument");
199 key
= luaL_checklstring(L
, 1, &key_len
);
200 lua_pushboolean(L
, (MEMCACHED_SUCCESS
== memcached_exist(memc
, key
, key_len
)));
204 int f_memcache_get_string(lua_State
*L
) {
205 size_t key_len
, value_len
;
210 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
211 lua_pushstring(L
, "where is my userdata ?");
215 memc
= lua_touserdata(L
, lua_upvalueindex(1));
217 if (1 != lua_gettop(L
)) {
218 lua_pushstring(L
, "expected one argument");
222 key
= luaL_checklstring(L
, 1, &key_len
);
223 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
228 lua_pushlstring(L
, value
, value_len
);
235 int f_memcache_get_long(lua_State
*L
) {
236 size_t key_len
, value_len
;
243 if (!lua_islightuserdata(L
, lua_upvalueindex(1))) {
244 lua_pushstring(L
, "where is my userdata ?");
248 memc
= lua_touserdata(L
, lua_upvalueindex(1));
250 if (1 != lua_gettop(L
)) {
251 lua_pushstring(L
, "expected one argument");
255 key
= luaL_checklstring(L
, 1, &key_len
);
256 if (NULL
== (value
= memcached_get(memc
, key
, key_len
, &value_len
, NULL
, NULL
))) {
262 v
= strtol(value
, &endptr
, 10);
263 if (0 == errno
&& *endptr
== '\0') {
264 lua_pushinteger(L
, v
);