From 206f0731815280387a2001e09550aee48a1cba5c Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 23 Jun 2022 04:05:51 -0700 Subject: [PATCH] Add stat mode macros to redbean unix api --- third_party/lua/lunix.c | 57 +++++++++++++++++++++++++++++++++++++++++ tool/net/help.txt | 16 ++++++------ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/third_party/lua/lunix.c b/third_party/lua/lunix.c index a558627b7..3ca21490c 100644 --- a/third_party/lua/lunix.c +++ b/third_party/lua/lunix.c @@ -88,6 +88,7 @@ #include "third_party/lua/lgc.h" #include "third_party/lua/lua.h" #include "third_party/lua/luaconf.h" +#include "libc/sysv/consts/s.h" #include "tool/net/luacheck.h" /** @@ -1756,6 +1757,55 @@ static int LuaUnixMinor(lua_State *L) { return ReturnInteger(L, minor(luaL_checkinteger(L, 1))); } +// unix.S_ISDIR(mode:int) +// └─→ bool +static int LuaUnixSisdir(lua_State *L) { + lua_pushboolean(L, S_ISDIR(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISCHR(mode:int) +// └─→ bool +static int LuaUnixSischr(lua_State *L) { + lua_pushboolean(L, S_ISCHR(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISBLK(mode:int) +// └─→ bool +static int LuaUnixSisblk(lua_State *L) { + lua_pushboolean(L, S_ISBLK(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISREG(mode:int) +// └─→ bool +static int LuaUnixSisreg(lua_State *L) { + lua_pushboolean(L, S_ISREG(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISFIFO(mode:int) +// └─→ bool +static int LuaUnixSisfifo(lua_State *L) { + lua_pushboolean(L, S_ISFIFO(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISLNK(mode:int) +// └─→ bool +static int LuaUnixSislnk(lua_State *L) { + lua_pushboolean(L, S_ISLNK(luaL_checkinteger(L, 1))); + return 1; +} + +// unix.S_ISSOCK(mode:int) +// └─→ bool +static int LuaUnixSissock(lua_State *L) { + lua_pushboolean(L, S_ISSOCK(luaL_checkinteger(L, 1))); + return 1; +} + //////////////////////////////////////////////////////////////////////////////// // unix.Stat object @@ -2408,6 +2458,13 @@ static void LuaUnixDirObj(lua_State *L) { // UNIX module static const luaL_Reg kLuaUnix[] = { + {"S_ISBLK", LuaUnixSisblk}, // is st:mode() a block device? + {"S_ISCHR", LuaUnixSischr}, // is st:mode() a character device? + {"S_ISDIR", LuaUnixSisdir}, // is st:mode() a directory? + {"S_ISFIFO", LuaUnixSisfifo}, // is st:mode() a fifo? + {"S_ISLNK", LuaUnixSislnk}, // is st:mode() a symbolic link? + {"S_ISREG", LuaUnixSisreg}, // is st:mode() a regular file? + {"S_ISSOCK", LuaUnixSissock}, // is st:mode() a socket? {"Sigset", LuaUnixSigset}, // creates signal bitmask {"WEXITSTATUS", LuaUnixWexitstatus}, // gets exit status from wait status {"WIFEXITED", LuaUnixWifexited}, // gets exit code from wait status diff --git a/tool/net/help.txt b/tool/net/help.txt index 4e36f81f6..51719019d 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -838,7 +838,7 @@ FUNCTIONS request message is not preserved. Whether or not the same key can repeat depends on whether or not it's a standard header, and if so, if it's one of the ones that the RFCs define as repeatable. - See khttprepeatable.c. Those headers will not be folded. Standard +v See khttprepeatable.c. Those headers will not be folded. Standard headers which aren't on that list, will be overwritten with the last-occurring one during parsing. Extended headers are always passed through exactly as they're received. Please consider using @@ -3527,13 +3527,13 @@ UNIX MODULE To determine the file type: - - `(st:mode() & 0170000) == 0010000` means fifo or pipe - - `(st:mode() & 0170000) == 0020000` means character device - - `(st:mode() & 0170000) == 0040000` means directory - - `(st:mode() & 0170000) == 0060000` means block device - - `(st:mode() & 0170000) == 0100000` means regular file - - `(st:mode() & 0170000) == 0120000` means symbolic link - - `(st:mode() & 0170000) == 0140000` means socket + - `unix.S_ISREG(st:mode())` means regular file + - `unix.S_ISDIR(st:mode())` means directory + - `unix.S_ISLNK(st:mode())` means symbolic link + - `unix.S_ISCHR(st:mode())` means character device + - `unix.S_ISBLK(st:mode())` means block device + - `unix.S_ISFIFO(st:mode())` means fifo or pipe + - `unix.S_ISSOCK(st:mode())` means socket unix.Stat:uid() └─→ uid:int