static int Ftimes(lua_State *L, int i, const void *data) { const struct mytimes *t=data; switch (i) { case 0: pushtime(L, t->t.tms_utime); break; case 1: pushtime(L, t->t.tms_stime); break; case 2: pushtime(L, t->t.tms_cutime); break; case 3: pushtime(L, t->t.tms_cstime); break; case 4: pushtime(L, t->elapsed); break; } return 1; }
static void Ftimes(lua_State *L, int i, const void *data) { static long clk_tck = 0; const struct mytimes *t=data; if( !clk_tck){ clk_tck= sysconf(_SC_CLK_TCK);} switch (i) { case 0: pushtime(L, t->t.tms_utime); break; case 1: pushtime(L, t->t.tms_stime); break; case 2: pushtime(L, t->t.tms_cutime); break; case 3: pushtime(L, t->t.tms_cstime); break; case 4: pushtime(L, t->elapsed); break; } }
static int l_stat( lua_State* L ) { static const struct { unsigned flag; const char* name; } modes[] = { //{ S_IFSOCK, "sock" }, //{ S_IFLNK, "link" }, { S_IFREG, "file" }, { S_IFBLK, "block" }, { S_IFDIR, "dir" }, { S_IFCHR, "char" }, { S_IFIFO, "fifo" }, }; const char* name = luaL_checkstring( L, 1 ); struct stat buf; int i; if ( stat( name, &buf ) == 0 ) { lua_createtable( L, 0, 0 ); lua_pushinteger( L, buf.st_size ); lua_setfield( L, -2, "size" ); pushtime( L, &buf.st_atime ); lua_setfield( L, -2, "atime" ); pushtime( L, &buf.st_mtime ); lua_setfield( L, -2, "mtime" ); pushtime( L, &buf.st_ctime ); lua_setfield( L, -2, "ctime" ); for ( i = 0; i < sizeof( modes ) / sizeof( modes[ 0 ] ); i++ ) { lua_pushboolean( L, ( buf.st_mode & S_IFMT ) == modes[ i ].flag ); lua_setfield( L, -2, modes[ i ].name ); } return 1; } lua_pushstring( L, strerror( errno ) ); return lua_error( L ); }