]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
main/utils: allow checking for command in $PATH
authorPhilip Prindeville <philipp@redfish-solutions.com>
Tue, 26 Jul 2022 19:38:10 +0000 (13:38 -0600)
committerGeorge Joseph <gjoseph@digium.com>
Mon, 12 Sep 2022 14:49:58 +0000 (09:49 -0500)
ASTERISK-30037

Change-Id: I4b6f7264c8c737c476c798d2352f3232b263bbdf

include/asterisk/utils.h
main/utils.c

index 72deaa3952ecb7639cb90041a00451b08ec8ac57..3c06e834eba2ea440d95a9a17e7d0d7708d138aa 100644 (file)
@@ -1105,4 +1105,14 @@ int ast_thread_user_interface_set(int is_user_interface);
  */
 int ast_thread_is_user_interface(void);
 
+/*!
+ * \brief Test for the presence of an executable command in $PATH
+ *
+ * \param cmd Name of command to locate.
+ *
+ * \retval True (non-zero) if command is in $PATH.
+ * \retval False (zero) command not found.
+ */
+int ast_check_command_in_path(const char *cmd);
+
 #endif /* _ASTERISK_UTILS_H */
index 7d1d6bd7b7ef9408232fd3312eedda35d32ef97c..3ab6dc1e704a28982792268808ea28af218310f4 100644 (file)
@@ -3216,3 +3216,38 @@ int ast_thread_is_user_interface(void)
 
        return *thread_user_interface;
 }
+
+int ast_check_command_in_path(const char *cmd)
+{
+       char *token, *saveptr, *path = getenv("PATH");
+       char filename[PATH_MAX];
+       int len;
+
+       if (path == NULL) {
+               return 0;
+       }
+
+       path = ast_strdup(path);
+       if (path == NULL) {
+               return 0;
+       }
+
+       token = strtok_r(path, ":", &saveptr);
+       while (token != NULL) {
+               len = snprintf(filename, sizeof(filename), "%s/%s", token, cmd);
+               if (len < 0 || len >= sizeof(filename)) {
+                       ast_log(LOG_WARNING, "Path constructed with '%s' too long; skipping\n", token);
+                       continue;
+               }
+
+               if (access(filename, X_OK) == 0) {
+                       ast_free(path);
+                       return 1;
+               }
+
+               token = strtok_r(NULL, ":", &saveptr);
+       }
+       ast_free(path);
+       return 0;
+}
+