From d8519cc39f87aac8a16c0614c9dd7893ba975d3e Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Wed, 20 Mar 2024 16:00:46 +1300 Subject: [PATCH] fs: init function "status" fs.status returns a table containing information concerning the status of a file. Currently, the table includes the following information: string path -- the given path to the file; the first argument integer mode -- bitmask of file modes; see chmod(2) integer uid -- owner of the file in UID form integer gid -- owner group of the file in GID form integer accessdate -- date the file was last accessed integer modifydate -- date the file was last modified integer chdate -- date the file's status last changed Dates are given in the form of an integer number representing the number of seconds since Jan 1, 1970 (unixtime). For more information consult the definition of sys/stat.h according to POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html --- lfs.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lfs.c b/lfs.c index b964c84..96cad12 100644 --- a/lfs.c +++ b/lfs.c @@ -523,6 +523,40 @@ fs_rmdir(lua_State *L) return lfail(L); } +static int +fs_status(lua_State *L) +{ + struct stat s; + const char *path; /* parameter 1 (string) */ + + path = luaL_checkstring(L, 1); + + if (lstat(path, &s) == -1) + return lfail(L); + + lua_createtable(L, 0, 7); + + lua_pushvalue(L, 1); + lua_setfield(L, -2, "path"); + + lua_pushinteger(L, s.st_mode); + lua_setfield(L, -2, "mode"); + + lua_pushinteger(L, s.st_uid); + lua_setfield(L, -2, "uid"); + lua_pushinteger(L, s.st_gid); + lua_setfield(L, -2, "gid"); + + lua_pushinteger(L, s.st_atim.tv_sec); + lua_setfield(L, -2, "accessdate"); + lua_pushinteger(L, s.st_mtim.tv_sec); + lua_setfield(L, -2, "modifydate"); + lua_pushinteger(L, s.st_ctim.tv_sec); + lua_setfield(L, -2, "chdate"); + + return 1; +} + /*** * Returns or sets the current working directory. * @@ -583,6 +617,7 @@ static const luaL_Reg fslib[] = { {"move", fs_move}, {"remove", fs_remove}, {"rmdir", fs_rmdir}, + {"status", fs_status}, {"workdir", fs_workdir}, {NULL, NULL} };