[Asterisk-code-review] main/utils: allow checking for command in $PATH (asterisk[16])

Friendly Automation asteriskteam at digium.com
Mon Sep 12 10:45:02 CDT 2022


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/19104 )

Change subject: main/utils: allow checking for command in $PATH
......................................................................

main/utils: allow checking for command in $PATH

ASTERISK-30037

Change-Id: I4b6f7264c8c737c476c798d2352f3232b263bbdf
---
M include/asterisk/utils.h
M main/utils.c
2 files changed, 56 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit




diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 72deaa3..3c06e83 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -1105,4 +1105,14 @@
  */
 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 */
diff --git a/main/utils.c b/main/utils.c
index 7d1d6bd..3ab6dc1 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -3216,3 +3216,38 @@
 
 	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;
+}
+

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/19104
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I4b6f7264c8c737c476c798d2352f3232b263bbdf
Gerrit-Change-Number: 19104
Gerrit-PatchSet: 2
Gerrit-Owner: Philip Prindeville <philipp at redfish-solutions.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220912/11a82360/attachment.html>


More information about the asterisk-code-review mailing list