From 8775497cae6f58bb83f136bb973fcb9d6de8588e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?= Date: Mon, 26 Dec 2022 14:53:18 -0600 Subject: [PATCH] module/dwl: update the title when process a new batch of info instead of call strtok_r and looping until `string` is NULL --- modules/dwl.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/modules/dwl.c b/modules/dwl.c index a03a345..5fa1023 100644 --- a/modules/dwl.c +++ b/modules/dwl.c @@ -170,9 +170,12 @@ process_line(char *line, struct module *module) else if (index == 2) { if (strcmp(string, "title") == 0) { line_mode = LINE_MODE_TITLE; - /* cleanup title for every new batch */ + /* Update the title here, to avoid allocate and free memory on + * every iteration (the line is separated by spaces, then we + * join it again) a bit suboptimal, isn't it?) */ free(private->title); - private->title = NULL; + private->title = strdup(save_pointer); + break; } else if (strcmp(string, "fullscreen") == 0) line_mode = LINE_MODE_FULLSCREEN; else if (strcmp(string, "floating") == 0) @@ -222,20 +225,11 @@ process_line(char *line, struct module *module) dwl_tag->urgent = mask & urgent; } } - } else if (line_mode == LINE_MODE_TITLE) { - if (!private->title) { - private->title = strdup(string); - } else { - char *buf = NULL; - if (asprintf(&buf, "%s %s", private->title, string) < 0) { - LOG_ERRNO("asprintf"); - break; - } - free(private->title); - private->title = buf; - } } else switch (line_mode) { + case LINE_MODE_TITLE: + assert(false); /* unreachable */ + break; case LINE_MODE_FULLSCREEN: private->fullscreen = (strcmp(string, "0") != 0); break;