diff options
author | 2023-01-16 16:28:02 -0800 | |
---|---|---|
committer | 2023-01-16 16:28:02 -0800 | |
commit | 7dd28bbdd99b31cc88abe4b309ed52aff64be9c2 (patch) | |
tree | ae3b2c0ae4d790995801a59de78fdb3fd5c896af /src/bun.js | |
parent | d54e23ca33cc95199a58d958abf990d9a6e1eb26 (diff) | |
download | bun-7dd28bbdd99b31cc88abe4b309ed52aff64be9c2.tar.gz bun-7dd28bbdd99b31cc88abe4b309ed52aff64be9c2.tar.zst bun-7dd28bbdd99b31cc88abe4b309ed52aff64be9c2.zip |
Fix `which` returning directories sometimes
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/bindings/c-bindings.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/bun.js/bindings/c-bindings.cpp b/src/bun.js/bindings/c-bindings.cpp index aee5f5425..d37dd12cb 100644 --- a/src/bun.js/bindings/c-bindings.cpp +++ b/src/bun.js/bindings/c-bindings.cpp @@ -1,6 +1,9 @@ // when we don't want to use @cInclude, we can just stick wrapper functions here #include <sys/resource.h> #include <cstdint> +#include <unistd.h> +#include <sys/fcntl.h> +#include <sys/stat.h> extern "C" int32_t get_process_priority(uint32_t pid) { @@ -11,3 +14,23 @@ extern "C" int32_t set_process_priority(uint32_t pid, int32_t priority) { return setpriority(PRIO_PROCESS, pid, priority); } + +extern "C" bool is_executable_file(const char* path) +{ + +#ifdef __APPLE__ + // O_EXEC is macOS specific + int fd = open(path, O_EXEC, O_CLOEXEC); + if (fd < 0) + return false; + close(fd); + return true; +#endif + + struct stat st; + if (stat(path, &st) != 0) + return false; + + // regular file and user can execute + return S_ISREG(st.st_mode) && (st.st_mode & S_IXUSR); +} |