summaryrefslogtreecommitdiffstats
path: root/filescan-utils.c
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-07-12 18:18:43 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-07-17 00:51:53 +0200
commit4f99d163cb8a993f176f305566dbd8a93c79144c (patch)
tree829a0012c7e8bc030aaafa2cbd38a4bac2483579 /filescan-utils.c
parentf46a0dca64c56d78d08b5c4f84d011f8beaa55d3 (diff)
Retrieve directory list from environment variables
This adds a function able to retrieve from a prefix and suffix an environment variable. This is meant to search config or plugin files from potentially several sources, environment and source code. This tries to get 2 environments variables: - prefix+binderName+suffix (eg: AFT_LOWCAN_CONFIG_PATH) - prefix+suffix (eg: AFT_CONFIG_PATH) Then it returns the one found or both if they exist with the most accurate one (with the binder name) first followed by the other. Change-Id: Ic448ff017e6158bec05895d63688b8968b5c6434 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'filescan-utils.c')
-rw-r--r--filescan-utils.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/filescan-utils.c b/filescan-utils.c
index 6b3cf65..6c443ea 100644
--- a/filescan-utils.c
+++ b/filescan-utils.c
@@ -16,6 +16,7 @@
*/
#define _GNU_SOURCE
+#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
@@ -169,3 +170,80 @@ char* GetBindingDirPath(struct afb_dynapi* dynapi)
return strndup(retdir, sizeof(retdir));
}
+
+
+/**
+ * @brief Takes an input string and makes it upper case. The output buffer
+ * should be able to contains the whole input string else it will be truncated
+ * at the output_size.
+ *
+ * @param input the input string to transform in an upper case version
+ * @param output the upper case version of the input string
+ * @param output_size the output buffer size, if size < to strlen of input then
+ * the output will be truncated.
+ *
+ * @return size_t the string length of the resulting output variable, which should
+ * be equal to the size of input string length.
+ */
+static size_t toUpperString(const char *input, char *output, size_t output_size) {
+ int i = 0;
+
+ if(input && output) {
+ while(input[i] && i <= output_size) {
+ output[i] = (char)toupper(input[i]);
+ i++;
+ }
+ output[i] = '\0';
+ return strlen(output);
+ }
+
+ return 0;
+}
+
+const char *getEnvDirList(const char *prefix, const char *suffix) {
+ size_t lenSimple = 0, lenFull = 0, plen = 0, slen = 0, blen = 0;
+ char *envConfigPathSimple = NULL, *envConfigPathFull = NULL;
+ char *upperPrefix = NULL, *upperSuffix = NULL, *upperBinderName = NULL;
+ char *envDirList = NULL;
+ const char *binderName = GetBinderName(), *envDirListSimple = NULL;
+ const char *envDirListFull = NULL;
+
+ if(! prefix || ! suffix)
+ return NULL;
+
+ plen = strlen(prefix);
+ upperPrefix = alloca(plen + 1);
+
+ slen = strlen(suffix);
+ upperSuffix = alloca(slen + 1);
+
+ blen = strlen(binderName);
+ upperBinderName = alloca(blen + 1);
+
+ lenSimple = plen + slen + 1;
+ envConfigPathSimple = alloca(lenSimple + 1);
+
+ lenFull = slen + blen + slen + 2;
+ envConfigPathFull = alloca(lenFull + 1);
+
+ if(toUpperString(prefix, upperPrefix, plen) != plen ||
+ toUpperString(suffix, upperSuffix, slen) != slen ||
+ toUpperString(binderName, upperBinderName, blen) != blen ||
+ (snprintf(envConfigPathSimple, lenSimple + 1, "%s_%s", upperPrefix, upperSuffix) >= lenSimple + 1) ||
+ (snprintf(envConfigPathFull, lenFull + 1, "%s_%s_%s", upperPrefix, upperBinderName, upperSuffix) >= lenFull + 1)) {
+ return NULL;
+ }
+
+ envDirListFull = getenv(envConfigPathFull);
+ envDirListSimple = getenv(envConfigPathSimple);
+ if(envDirListSimple && envDirListFull) {
+ lenFull = strlen(envDirListSimple) + strlen(envDirListFull) + 1;
+ envDirList = malloc(lenFull + 1);
+ snprintf(envDirList, lenFull + 1, "%s:%s", envDirListFull, envDirListSimple);
+ }
+ else {
+ envDirList = envDirListSimple ? strdup(envDirListSimple) : envDirListFull ? strdup(envDirListFull) : NULL;
+ }
+
+ return envDirList;
+}