j5

aditionnal tools for the s6 service manager
Log | Files | Refs | README

commit ea833da3e10638d0782863435a56bad787aa1964
parent 6af65e1f770383d11e3b86041fa43d7d04bd939a
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 27 Jan 2019 15:56:21 +0100

take advantage of being fully awake to fix the replacement function

Diffstat:
Mj5-envdir.c | 23++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/j5-envdir.c b/j5-envdir.c @@ -40,28 +40,33 @@ replace(stralloc *sa, char const *src, size_t siz, char const *pre, genalloc con int found = 0; int ret = 0; - do { + while (1) { i = mem_mem(src, siz, pre, len_pre); if (!stralloc_catb(sa, src, i)) goto error; if (i == siz) break; - src += i + len_pre; - siz -= i + len_pre; + src += i; + siz -= i; for (size_t k = 0; k < genalloc_len(ga, keyval); ++k) { keyval *kv = genalloc_get(ga, keyval, k); size_t len_key = str_len(kv->key); - if (siz < len_key + len_suf) continue; - if (mem_diff(src + len_key, suf, len_suf)) continue; - if (mem_equal(src, kv->key, len_key)) { + if (siz < len_pre + len_key + len_suf) continue; + if (mem_diff(src + len_pre + len_key, suf, len_suf)) continue; + if (mem_equal(src + len_pre, kv->key, len_key)) { if (!stralloc_cat(sa, &kv->val)) goto error; - siz -= len_key + len_suf; - src += len_key + len_suf; + siz -= len_pre + len_key + len_suf; + src += len_pre + len_key + len_suf; found = 1; break; } } - } while (found); + if (!found) { + if (!stralloc_catb(sa, src, len_pre)) goto error; + src += len_pre; + siz -= len_pre; + } + } ret = 1; error: