lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


* On Fri Mar 15 16:19:08 +0100 2013, Coda Highland wrote:
 
> Write a Lua C API function that does the work

I happen to have this lying around. It's trivial but might save you a few minutes work.

static int l_statfs(lua_State *L)
{ 
        struct statfs s; 
        const char *fname; 
        int r = 0; 
 
        fname = luaL_checkstring(L, 1); 
        r = statfs(fname, &s); 

        if(r == -1) {
                lua_pushnil(L); 
                lua_pushstring(L, strerror(errno)); 
                return 2; 
        } 
 
        lua_newtable(L); 
        lua_pushstring(L, "type");    lua_pushnumber(L, s.f_type); lua_settable(L, -3); 
        lua_pushstring(L, "bsize");   lua_pushnumber(L, s.f_bsize); lua_settable(L, -3); 
        lua_pushstring(L, "blocks");  lua_pushnumber(L, s.f_blocks); lua_settable(L, -3); 
        lua_pushstring(L, "bfree");   lua_pushnumber(L, s.f_bfree); lua_settable(L, -3); 
        lua_pushstring(L, "bavail");  lua_pushnumber(L, s.f_bavail); lua_settable(L, -3); 
        lua_pushstring(L, "files");   lua_pushnumber(L, s.f_files); lua_settable(L, -3); 
        lua_pushstring(L, "ffree");   lua_pushnumber(L, s.f_ffree); lua_settable(L, -3); 
        lua_pushstring(L, "namelen"); lua_pushnumber(L, s.f_namelen  ); lua_settable(L, -3); 
                                                           
        return 1;
}


-- 
:wq
^X^Cy^K^X^C^C^C^C