From 261797ec5692b4958d806cf5f639f1cf8f890928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Mon, 11 Apr 2022 12:38:36 +0200 Subject: [PATCH] client: refactor: add send_string_list() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use it to send ‘overrides’ and ‘envp’ --- client.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/client.c b/client.c index ad88fe5b..7d524204 100644 --- a/client.c +++ b/client.c @@ -126,6 +126,22 @@ free_string_list(string_list_t *string_list) } } +static bool +send_string_list(int fd, const string_list_t *string_list) +{ + tll_foreach(*string_list, it) { + const struct client_string s = {it->item.len}; + if (sendall(fd, &s, sizeof(s)) < 0 || + sendall(fd, it->item.str, s.len) < 0) + { + LOG_ERRNO("failed to send setup packet to server"); + return false; + } + } + + return true; +} + int main(int argc, char *const *argv) { @@ -461,16 +477,8 @@ main(int argc, char *const *argv) } /* Send overrides */ - tll_foreach(overrides, it) { - const struct string *o = &it->item; - struct client_string s = {o->len}; - if (sendall(fd, &s, sizeof(s)) < 0 || - sendall(fd, o->str, o->len) < 0) - { - LOG_ERRNO("failed to send setup packet (overrides) to server"); - goto err; - } - } + if (!send_string_list(fd, &overrides)) + goto err; /* Send argv[] */ for (size_t i = 0; i < argc; i++) { @@ -483,16 +491,8 @@ main(int argc, char *const *argv) } /* Send environment */ - tll_foreach(envp, it) { - const struct string *e = &it->item; - struct client_string s = {e->len}; - if (sendall(fd, &s, sizeof(s)) < 0 || - sendall(fd, e->str, e->len) < 0) - { - LOG_ERRNO("failed to send setup packet (envp) to server"); - goto err; - } - } + if (!send_string_list(fd, &envp)) + goto err; struct sigaction sa = {.sa_handler = &sig_handler}; sigemptyset(&sa.sa_mask);