[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Accept %DATADIR% anywhere in data load path
|
From: |
Tim Rühsen |
|
Subject: |
[PATCH] Accept %DATADIR% anywhere in data load path |
|
Date: |
Thu, 9 Apr 2020 11:18:57 +0200 |
2020-04-09 Tim Rühsen <address@hidden>
* lib/pk-utils.h: Add declaration of pk_str_replace.
* lib/pk-utils.c: Add implementation of pk_str_replace.
* lib/pkl.c (pkl_resolve_module): Make use of pk_str_replace.
---
ChangeLog | 6 ++++++
lib/pk-utils.c | 42 ++++++++++++++++++++++++++++++++++++++++++
lib/pk-utils.h | 3 +++
lib/pkl.c | 20 +++++++++++---------
4 files changed, 62 insertions(+), 9 deletions(-)
diff --git a/lib/pk-utils.c b/lib/pk-utils.c
index 74dc5447..676435fb 100644
--- a/lib/pk-utils.c
+++ b/lib/pk-utils.c
@@ -157,3 +157,45 @@ pk_str_concat(const char *s0, ...)
return res;
}
+
+/* Replace all occurrences of 'search' in 'in' by 'replace'.
+ * Return 'in' in case 'search' was not found, else
+ * return a new allocated string with the replaced sequences.
+ * Return NULL on allocation failure.
+ */
+char *
+pk_str_replace (const char *in, const char *search, const char *replace)
+{
+ const char *s, *e;
+ char *out, *d;
+ int num = 0;
+
+ /* count number of occurrences of 'search' in 'in' */
+ for (s = in; (s = strstr (s, search)); s++, num++)
+ ;
+
+ if (!num)
+ return (char *) in;
+
+ size_t search_len = strlen (search);
+ size_t replace_len = strlen (replace);
+ size_t in_len = strlen (in);
+
+ d = out = malloc (in_len + (replace_len - search_len) * num + 1);
+ if (!out)
+ return NULL;
+
+ for (s = in; (e = strstr (s, search)); s = e + search_len)
+ {
+ memcpy (d, s, e - s);
+ d += e - s;
+
+ memcpy (d, replace, replace_len);
+ d += replace_len;
+ }
+
+ /* copy rest of 'in' + trailing zero */
+ strcpy (d, s);
+
+ return out;
+}
diff --git a/lib/pk-utils.h b/lib/pk-utils.h
index 46286ca3..256aa877 100644
--- a/lib/pk-utils.h
+++ b/lib/pk-utils.h
@@ -53,4 +53,7 @@ void pk_print_binary (uint64_t val, int size, int sign);
/* Concatenate string arguments into an malloc'ed string. */
char *pk_str_concat(const char *s0, ...);
+/* Replace all occurrences of 'search' in 'in' by 'replace'. */
+char *pk_str_replace (const char *in, const char *search, const char *replace);
+
#endif /* ! PK_UTILS_H */
diff --git a/lib/pkl.c b/lib/pkl.c
index 3547255e..75db7b35 100644
--- a/lib/pkl.c
+++ b/lib/pkl.c
@@ -478,6 +478,7 @@ pkl_resolve_module (pkl_compiler compiler,
int filename_p)
{
const char *load_path;
+ char *full_filename = NULL;
/* Get the load path from the run-time environment. */
{
@@ -503,30 +504,31 @@ pkl_resolve_module (pkl_compiler compiler,
/* Traverse the directories in the load path and try to load the
requested module. */
{
- char *full_filename;
const char *ext = filename_p ? "" : ".pk";
const char *s, *e;
- for (s = load_path, e = s; *e; s = e + 1)
+ char *fixed_load_path = pk_str_replace (load_path, "%DATADIR%",
PKGDATADIR);
+
+ for (s = fixed_load_path, e = s; *e; s = e + 1)
{
/* Ignore empty entries. */
if ((e = strchrnul (s, ':')) == s)
continue;
- if (!strncmp (s, "%DATADIR%", e - s))
- full_filename = pk_str_concat (PKGDATADIR, s + sizeof ("%DATADIR%")
- 1,
- "/", module, ext, NULL);
- else
- asprintf (&full_filename, "%.*s/%s%s", (int) (e - s), s, module,
ext);
+ asprintf (&full_filename, "%.*s/%s%s", (int) (e - s), s, module, ext);
if (pk_file_readable (full_filename) == NULL)
- return full_filename;
+ break;
free (full_filename);
+ full_filename = NULL;
}
+
+ if (fixed_load_path != load_path)
+ free (fixed_load_path);
}
- return NULL;
+ return full_filename;
}
int
--
2.26.0
- [PATCH] Accept %DATADIR% anywhere in data load path,
Tim Rühsen <=