Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access bits + util.is executable #1982

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ffi-cdecl/posix_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ cdecl_func(strcmp)
cdecl_func(strcasecmp)

cdecl_const(F_OK)
cdecl_const(R_OK)
cdecl_const(W_OK)
cdecl_const(W_OK)
benoit-pierre marked this conversation as resolved.
Show resolved Hide resolved
cdecl_func(access)

cdecl_type(FILE)
Expand Down
3 changes: 3 additions & 0 deletions ffi/posix_h.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ int strcoll(const char *, const char *) __attribute__((nothrow, leaf, pure));
int strcmp(const char *, const char *) __attribute__((pure, leaf, nothrow));
int strcasecmp(const char *, const char *) __attribute__((pure, leaf, nothrow));
static const int F_OK = 0;
static const int R_OK = 4;
static const int W_OK = 2;
static const int X_OK = 1;
int access(const char *, int) __attribute__((nothrow, leaf));
typedef struct _IO_FILE FILE;
typedef long long unsigned int dev_t;
Expand Down
9 changes: 9 additions & 0 deletions ffi/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -744,4 +744,13 @@ function util.template(str, ...)
return result
end

-- Check if `path` is an executable file.
function util.isExecutable(path)
local attributes, err = lfs.attributes(path)
if not attributes or err ~= nil then
return false, err
end
return attributes.mode == "file" and C.access(path, C.X_OK + C.R_OK) == 0
end

return util