From 5344ff3d5f58593f749a2b36cf11b5d4f8485d88 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 18 Jan 2024 17:06:59 +0100 Subject: Update cbs.h again --- cbs.h | 41 ++++++++++++++++++++++------------------- make.c | 15 ++++++--------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cbs.h b/cbs.h index b94f7b7..bcf82df 100644 --- a/cbs.h +++ b/cbs.h @@ -209,13 +209,17 @@ static int cmdwait(pid_t); static void cmdput(cmd_t); static void cmdputf(FILE *, cmd_t); -/* Expand the environment variable s using /bin/sh expansion rules and store the - results in the given string vector. If the environment variable is NULL or - empty, then store the strings specified by the array p of length n. +/* Expand the environment variable s using /bin/sh expansion rules and append + the results in the given string vector. If the environment variable is NULL + or empty, then store the strings specified by the array p of length n. env_or_default() is the same as env_or_defaultv() but you provide p as - variadic arguments. */ -static void env_or_defaultv(struct strv *, const char *s, char **p, size_t n); + variadic arguments. + + If the pointer p is null, then no default value is appended to the given + string vector when the environment variable is not present. */ +static void env_or_defaultv(struct strv *, const char *s, char *_Nullable *p, + size_t n); #define env_or_default(sv, s, ...) \ env_or_defaultv((sv), (s), _vtoa(__VA_ARGS__), lengthof(_vtoa(__VA_ARGS__))) @@ -614,21 +618,20 @@ env_or_defaultv(struct strv *sv, const char *s, char **p, size_t n) die("wordexp"); } - sv->buf = bufalloc(NULL, we.we_wordc, sizeof(*sv->buf)); - for (size_t i = 0; i < we.we_wordc; i++) { - if (!(sv->buf[i] = strdup(we.we_wordv[i]))) - die("strdup"); - } - sv->len = we.we_wordc; - wordfree(&we); - } else { - sv->buf = bufalloc(NULL, n, sizeof(*sv->buf)); - for (size_t i = 0; i < n; i++) { - if (!(sv->buf[i] = strdup(p[i]))) - die("strdup"); - } - sv->len = n; + p = we.we_wordv; + n = we.we_wordc; + } else if (!p) + return; + + sv->buf = bufalloc(sv->buf, sv->len + n, sizeof(*sv->buf)); + for (size_t i = 0; i < n; i++) { + if (!(sv->buf[sv->len + i] = strdup(p[i]))) + die("strdup"); } + sv->len += n; + + if (ev && *ev) + wordfree(&we); } bool diff --git a/make.c b/make.c index 48596a4..6e42d6b 100644 --- a/make.c +++ b/make.c @@ -80,21 +80,19 @@ main(int argc, char **argv) cmdprc(c); } } else { - struct strv cc = {0}; - struct strv cflags = {0}; + struct strv sv = {0}; - env_or_default(&cc, "CC", CC); + env_or_default(&sv, "CC", CC); if (dflag) - env_or_default(&cflags, "CFLAGS", CFLAGS, CFLAGS_DEBUG); + env_or_default(&sv, "CFLAGS", CFLAGS, CFLAGS_DEBUG); else - env_or_default(&cflags, "CFLAGS", CFLAGS, CFLAGS_RELEASE); + env_or_default(&sv, "CFLAGS", CFLAGS, CFLAGS_RELEASE); for (int i = 0; i < 2; i++) { char buf[] = "-DGIT_GRAB=X"; buf[sizeof(buf) - 2] = i + '0'; - cmdaddv(&c, cc.buf, cc.len); - cmdaddv(&c, cflags.buf, cflags.len); + cmdaddv(&c, sv.buf, sv.len); cmdadd(&c, buf); if (!Pflag) { struct strv pc = {0}; @@ -109,8 +107,7 @@ main(int argc, char **argv) cmdprc(c); } - strvfree(&cc); - strvfree(&cflags); + strvfree(&sv); } return EXIT_SUCCESS; -- cgit v1.2.3