diff -urN lua-5.0/config lua_new/config --- lua-5.0/config 2003年04月11日 10:00:41.000000000 -0400 +++ lua_new/config 2003年05月14日 14:15:15.000000000 -0400 @@ -103,6 +103,8 @@ #USERCONF=-DLUA_USERCONFIG='"$(LUA)/etc/saconfig.c"' -DUSE_READLINE #EXTRA_LIBS= -lm -ldl -lreadline # -lhistory -lcurses -lncurses +USERCONF= -DLUA_USERCONFIG='"$(LUA)/include/dirlib.h"' + # ------------------------------------------------------------------ C compiler # You need an ANSI C compiler. gcc is a popular one. We do not use -ansi in diff -urN lua-5.0/etc/saconfig.c lua_new/etc/saconfig.c --- lua-5.0/etc/saconfig.c 2003年03月28日 15:34:45.000000000 -0500 +++ lua_new/etc/saconfig.c 2003年05月14日 14:15:15.000000000 -0400 @@ -39,6 +39,8 @@ * */ +#include "dirlib.h" + #ifdef USE_READLINE /* * This section implements of lua_readline and lua_saveline for lua.c using diff -urN lua-5.0/include/dirlib.h lua_new/include/dirlib.h --- lua-5.0/include/dirlib.h 1969年12月31日 19:00:00.000000000 -0500 +++ lua_new/include/dirlib.h 2003年05月14日 14:15:15.000000000 -0400 @@ -0,0 +1,27 @@ + +/* + * add to the config file: + * + * USERCONF= -DLUA_USERCONFIG='"$(LUA)/include/dirlib.h"' + * + */ + +#ifndef dirlib_h +#define dirlib_h + +#include "lua.h" + +#ifndef DIRLIB_API +#define DIRLIB_API LUA_API +#endif + +#ifndef LUA_EXTRALIBS +#define LUA_EXTRALIBS {"dirlib", luaopen_dir}, +#else +#define lua_userinit(L) openstdlibs(L), luaopen_dir(L) +#endif + +DIRLIB_API int luaopen_dir (lua_State *L); + +#endif + diff -urN lua-5.0/src/lib/Makefile lua_new/src/lib/Makefile --- lua-5.0/src/lib/Makefile 2003年03月28日 07:49:56.000000000 -0500 +++ lua_new/src/lib/Makefile 2003年05月14日 14:15:15.000000000 -0400 @@ -8,6 +8,8 @@ OBJS= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o ltablib.o lstrlib.o loadlib.o SRCS= lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c ltablib.c lstrlib.c loadlib.c +OBJS += dirlib.o +SRCS += dirlib.c T= $(LIB)/liblualib.a diff -urN lua-5.0/src/lib/dirlib.c lua_new/src/lib/dirlib.c --- lua-5.0/src/lib/dirlib.c 1969年12月31日 19:00:00.000000000 -0500 +++ lua_new/src/lib/dirlib.c 2003年05月14日 14:53:34.000000000 -0400 @@ -0,0 +1,176 @@ + +#include +#include +#include +#include + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "dirlib.h" + +static void setfield (lua_State *L, const char *key, int value) { + lua_pushstring(L, key); + lua_pushnumber(L, value); + lua_rawset(L, -3); +} + +static void setboolfield (lua_State *L, const char *key, int value) { + lua_pushstring(L, key); + lua_pushboolean(L, value); + lua_rawset(L, -3); +} + +static int os_stat (lua_State *L) +{ + const char *filename = luaL_checkstring(L, 1); + struct stat st[1]; + if (stat(filename, st) == 0) { + lua_newtable(L); + setboolfield(L, "dir", S_ISDIR(st->st_mode)); + setboolfield(L, "reg", S_ISREG(st->st_mode)); + setfield(L, "size", st->st_size); + setfield(L, "atime", st->st_atime); + setfield(L, "ctime", st->st_ctime); + setfield(L, "mtime", st->st_mtime); + return 1; + } else { + lua_pushnil(L); + lua_pushfstring(L, "failed to stat file %s", filename); + return 2; + } +} + +static int os_chdir (lua_State *L) +{ + const char *dirname = luaL_checkstring(L, 1); + + if (chdir(dirname) == 0) { + lua_pushboolean(L, 1); + return 1; + } else { + lua_pushnil(L); + lua_pushfstring(L, "failed to change to directory %s", dirname); + return 2; + } +} + + +#define DIRHANDLE "DIR*" + +static DIR *toDir (lua_State *L, int dindex) +{ + DIR **pd = (DIR **)luaL_checkudata(L, dindex, DIRHANDLE); + if (pd == 0) luaL_typerror(L, dindex, DIRHANDLE); + return *pd; +} + +static DIR *checkDir(lua_State *L, int dindex) +{ + DIR *d; + luaL_checktype(L, dindex, LUA_TUSERDATA); + d = toDir(L, dindex); + if (!d) + luaL_error(L, "attempt to use a closed directory"); + return d; +} + +static DIR **pushDir (lua_State *L, DIR *d) +{ + DIR **pd = (DIR **)lua_newuserdata(L, sizeof(DIR *)); + *pd = d; + luaL_getmetatable(L, DIRHANDLE); + lua_setmetatable(L, -2); + return pd; +} + + +static int Dir_read (lua_State *L) +{ + DIR *d = checkDir(L, 1); + struct dirent *de = readdir(d); + if (de) { + lua_pushstring(L, de->d_name); + } else + lua_pushnil(L); + return 1; +} + +static int Dir_close (lua_State *L) +{ + DIR *d = checkDir(L, 1); + int rc = closedir(d); + if (!rc) { + *(DIR **)lua_touserdata(L, 1) = 0; /* mark directory as closed */ + lua_pushboolean(L, 1); + return 1; + } else { + lua_pushnil(L); + lua_pushfstring(L, "closedir error %d", rc); + return 2; + } +} + +static int Dir_gc (lua_State *L) +{ + DIR *d = toDir(L, 1); + if (d) closedir(d); + return 0; +} + +static int Dir_tostring (lua_State *L) +{ + char buff[32]; + DIR *d = toDir(L, 1); + if (d) sprintf(buff, "%p", lua_touserdata(L, 1)); + else strcpy(buff, "closed"); + lua_pushfstring(L, "dir (%s)", buff); + return 1; +} + +static const luaL_reg Dir_meta[] = { + {"read", Dir_read}, + {"close", Dir_close}, + {"__gc", Dir_gc}, + {"__tostring", Dir_tostring}, + {0, 0} +}; + +static int os_opendir (lua_State *L) +{ + const char *dirname = luaL_checkstring(L, 1); + DIR **pd = pushDir(L, 0); + *pd = opendir(dirname); + if (*pd) + return 1; + else { + lua_pushnil(L); + lua_pushfstring(L, "failed to open directory %s", dirname); + return 2; + } +} + +static const luaL_reg Dir_methods[] = { + {"stat", os_stat}, + {"chdir", os_chdir}, + {"opendir", os_opendir}, + {0, 0} +}; + +DIRLIB_API int luaopen_dir (lua_State *L) +{ + luaL_openlib(L, LUA_OSLIBNAME, Dir_methods, 0); /* add methods to os table */ + luaL_newmetatable(L, DIRHANDLE); /* create metatable for directory handle, + add it to the Lua registry */ + luaL_openlib(L, 0, Dir_meta, 0); /* fill metatable */ + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); /* dup metatable */ + lua_rawset(L, -3); /* metatable.__index = metatable */ + lua_pushliteral(L, "__metatable"); + lua_pushvalue(L, -2); /* dup metatable */ + lua_rawset(L, -3); /* hide metatable: + metatable.__metatable = methods */ + lua_pop(L, 1); /* drop metatable */ + return 1; /* return os table on the stack */ +} + diff -urN lua-5.0/test/dirlib.lua lua_new/test/dirlib.lua --- lua-5.0/test/dirlib.lua 1969年12月31日 19:00:00.000000000 -0500 +++ lua_new/test/dirlib.lua 2003年05月14日 14:15:15.000000000 -0400 @@ -0,0 +1,38 @@ + +local root = os.getenv'ROOT' or [[.]] + +local pathsep = [[/]] +if os.execute'uname' ~= 0 then pathsep = [[\]] end + +os.chdir(root) + +local function files(dirname) + local dh = assert(os.opendir(dirname)) + return function() + return dh:read() or assert(dh:close()) and nil + end +end + +local function showfiles(dirname) + local dirs = {} + for fn in files(dirname) do + if fn ~= '.' and fn ~= '..' then + local filename = dirname .. pathsep .. fn + local info = assert(os.stat(filename)) + if info.dir then + table.insert(dirs, filename) + elseif info.reg + and ( string.find(fn, '%.exe$') or string.find(fn, '%.lua$') ) then + local date = os.date('%b %d %Y', info.ctime) + print(fn, dirname, info.size, date) + -- io.write(table.concat({fn, dirname, info.size, date}, ', '), '\n') + end + end + end + for _,dn in ipairs(dirs) do + showfiles(dn) + end +end + +showfiles(root) +

AltStyle によって変換されたページ (->オリジナル) /