From cfca6cf8800f8a834771c78336d622014300baf7 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Wed, 20 Mar 2024 15:57:54 +1300 Subject: [PATCH] fs: include mode constants in the library table --- lfs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lfs.c b/lfs.c index c6f16f8..b964c84 100644 --- a/lfs.c +++ b/lfs.c @@ -26,6 +26,41 @@ #include "util.h" +/* clang-format off */ +struct { + mode_t mode; + char name[8]; +} modes[] = { + /* file types */ + {S_IFMT, "IFMT"}, + {S_IFBLK, "IFBLK"}, + {S_IFCHR, "IFCHR"}, + {S_IFIFO, "IFIFO"}, + {S_IFREG, "IFREG"}, + {S_IFDIR, "IFDIR"}, + {S_IFLNK, "IFLNK"}, + {S_IFSOCK, "IFSOCK"}, + + /* file permissions */ + {S_IRWXU, "IRWXU"}, + {S_IRUSR, "IRUSR"}, + {S_IWUSR, "IWUSR"}, + {S_IXUSR, "IXUSR"}, + {S_IRWXG, "IRWXG"}, + {S_IRGRP, "IRGRP"}, + {S_IWGRP, "IWGRP"}, + {S_IXGRP, "IXGRP"}, + {S_IRWXO, "IRWXO"}, + {S_IROTH, "IROTH"}, + {S_IWOTH, "IWOTH"}, + {S_IXOTH, "IXOTH"}, + {S_ISUID, "ISUID"}, + {S_ISGID, "ISGID"}, + {S_ISVTX, "ISVTX"}, + {0, {0}} +}; +/* clang-format on */ + /*** * Returns the last component of the given path, * removing any trailing '/' characters. If the given @@ -555,6 +590,14 @@ static const luaL_Reg fslib[] = { int luaopen_fs(lua_State *L) { + int i; + luaL_newlib(L, fslib); + + for (i = 0; modes[i].name[0] != 0; i++) { + lua_pushinteger(L, modes[i].mode); + lua_setfield(L, -2, modes[i].name); + } + return 1; }