From eb8f1938dc12086361b91866db2fa9fc9a2bd15d Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 25 Jul 2020 01:58:20 -0600 Subject: [PATCH] libifupdown: add utility function to determine if a path is executable --- libifupdown/execute.c | 15 +++++++++++++++ libifupdown/execute.h | 1 + 2 files changed, 16 insertions(+) diff --git a/libifupdown/execute.c b/libifupdown/execute.c index 04d29e6..b1242d5 100644 --- a/libifupdown/execute.c +++ b/libifupdown/execute.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -58,3 +59,17 @@ lif_execute_fmt(const struct lif_execute_opts *opts, char *const envp[], const c return WIFEXITED(status) && WEXITSTATUS(status) == 0; } + +bool +lif_file_is_executable(const char *path) +{ + struct stat st; + + if (stat(path, &st)) + return false; + + if (!S_ISREG(st.st_mode)) + return false; + + return access(path, X_OK); +} diff --git a/libifupdown/execute.h b/libifupdown/execute.h index b1f2eb5..3fce6ff 100644 --- a/libifupdown/execute.h +++ b/libifupdown/execute.h @@ -25,5 +25,6 @@ struct lif_execute_opts { }; extern bool lif_execute_fmt(const struct lif_execute_opts *opts, char *const envp[], const char *fmt, ...); +extern bool lif_file_is_executable(const char *path); #endif