diff --git a/config.c b/config.c index 46b3d2d2..bf3c4175 100644 --- a/config.c +++ b/config.c @@ -356,9 +356,9 @@ open_config(void) /* First, check XDG_CONFIG_HOME (or .config, if unset) */ if (xdg_config_home != NULL && xdg_config_home[0] != '\0') - path = xstrjoin(xdg_config_home, "/foot/foot.ini", 0); + path = xstrjoin(xdg_config_home, "/foot/foot.ini"); else if (home_dir != NULL) - path = xstrjoin(home_dir, "/.config/foot/foot.ini", 0); + path = xstrjoin(home_dir, "/.config/foot/foot.ini"); if (path != NULL) { LOG_DBG("checking for %s", path); @@ -383,7 +383,7 @@ open_config(void) conf_dir = strtok(NULL, ":")) { free(path); - path = xstrjoin(conf_dir, "/foot/foot.ini", 0); + path = xstrjoin(conf_dir, "/foot/foot.ini"); LOG_DBG("checking for %s", path); int fd = open(path, O_RDONLY | O_CLOEXEC); @@ -843,7 +843,7 @@ parse_section_main(struct context *ctx) return false; } - _include_path = xasprintf("%s/%s", home_dir, value + 2); + _include_path = xstrjoin3(home_dir, "/", value + 2); include_path = _include_path; } else include_path = value; @@ -2931,7 +2931,7 @@ get_server_socket_path(void) const char *wayland_display = getenv("WAYLAND_DISPLAY"); if (wayland_display == NULL) { - return xstrjoin(xdg_runtime, "/foot.sock", 0); + return xstrjoin(xdg_runtime, "/foot.sock"); } return xasprintf("%s/foot-%s.sock", xdg_runtime, wayland_display); diff --git a/main.c b/main.c index a6ec1cb7..f46d712e 100644 --- a/main.c +++ b/main.c @@ -258,7 +258,7 @@ main(int argc, char *const *argv) break; case 't': - tll_push_back(overrides, xstrjoin("term=", optarg, 0)); + tll_push_back(overrides, xstrjoin("term=", optarg)); break; case 'L': @@ -266,11 +266,11 @@ main(int argc, char *const *argv) break; case 'T': - tll_push_back(overrides, xstrjoin("title=", optarg, 0)); + tll_push_back(overrides, xstrjoin("title=", optarg)); break; case 'a': - tll_push_back(overrides, xstrjoin("app-id=", optarg, 0)); + tll_push_back(overrides, xstrjoin("app-id=", optarg)); break; case 'D': { @@ -284,7 +284,7 @@ main(int argc, char *const *argv) } case 'f': { - char *font_override = xstrjoin("font=", optarg, 0); + char *font_override = xstrjoin("font=", optarg); tll_push_back(overrides, font_override); break; } @@ -658,15 +658,19 @@ out: UNITTEST { - char *s = xstrjoin("foo", "bar", 0); + char *s = xstrjoin("foo", "bar"); xassert(streq(s, "foobar")); free(s); - s = xstrjoin("foo", "bar", ' '); + s = xstrjoin3("foo", " ", "bar"); xassert(streq(s, "foo bar")); free(s); - s = xstrjoin("foo", "bar", ','); + s = xstrjoin3("foo", ",", "bar"); xassert(streq(s, "foo,bar")); free(s); + + s = xstrjoin3("foo", "bar", "baz"); + xassert(streq(s, "foobarbaz")); + free(s); } diff --git a/notify.c b/notify.c index 9d2d3c71..15139c40 100644 --- a/notify.c +++ b/notify.c @@ -77,7 +77,7 @@ write_icon_file(const void *data, size_t data_sz, int *fd, char **filename, LOG_DBG("wrote icon data to %s", name); *filename = xstrdup(name); - *symbolic_name = xasprintf("file://%s", *filename); + *symbolic_name = xstrjoin("file://", *filename); return true; } diff --git a/osc.c b/osc.c index 3ae8a051..28d8b7b8 100644 --- a/osc.c +++ b/osc.c @@ -797,7 +797,7 @@ kitty_notification(struct terminal *term, char *string) else { /* Append, comma separated */ char *old_category = category; - category = xstrjoin(old_category, decoded, ','); + category = xstrjoin3(old_category, ",", decoded); free(decoded); free(old_category); } @@ -940,7 +940,7 @@ kitty_notification(struct terminal *term, char *string) category = NULL; /* Prevent double free */ } else { /* Append, comma separated */ - char *new_category = xstrjoin(notif->category, category, ','); + char *new_category = xstrjoin3(notif->category, ",", category); free(notif->category); notif->category = new_category; } @@ -959,7 +959,7 @@ kitty_notification(struct terminal *term, char *string) payload = NULL; } else { char *old = *ptr; - *ptr = xstrjoin(old, payload, 0); + *ptr = xstrjoin(old, payload); free(old); } break; @@ -1032,7 +1032,7 @@ kitty_notification(struct terminal *term, char *string) alive_ids = xstrdup(item_id); else { char *old_alive_ids = alive_ids; - alive_ids = xstrjoin(old_alive_ids, item_id, ','); + alive_ids = xstrjoin3(old_alive_ids, ",", item_id); free(old_alive_ids); } } diff --git a/slave.c b/slave.c index 00105adb..bf1ca8e0 100644 --- a/slave.c +++ b/slave.c @@ -55,7 +55,7 @@ find_file_in_path(const char *file) path != NULL; path = strtok(NULL, ":")) { - char *full = xasprintf("%s/%s", path, file); + char *full = xstrjoin3(path, "/", file); if (access(full, F_OK) == 0) { free(path_list); return full; @@ -329,7 +329,7 @@ add_to_env(struct environ *env, const char *name, const char *value) if (env->envp == NULL) setenv(name, value, 1); else { - char *e = xasprintf("%s=%s", name, value); + char *e = xstrjoin3(name, "=", value); /* Search for existing variable. If found, replace it with the new value */ diff --git a/terminal.c b/terminal.c index 16de2d65..5699e6b5 100644 --- a/terminal.c +++ b/terminal.c @@ -994,7 +994,7 @@ reload_fonts(struct terminal *term, bool resize_grid) snprintf(size, sizeof(size), ":size=%.2f", term->font_sizes[i][j].pt_size * scale); - names[i][j] = xstrjoin(font->pattern, size, 0); + names[i][j] = xstrjoin(font->pattern, size); } } @@ -1021,9 +1021,9 @@ reload_fonts(struct terminal *term, bool resize_grid) char *attrs[4] = { [0] = dpi, /* Takes ownership */ - [1] = xstrjoin(dpi, !custom_bold ? ":weight=bold" : "", 0), - [2] = xstrjoin(dpi, !custom_italic ? ":slant=italic" : "", 0), - [3] = xstrjoin(dpi, !custom_bold_italic ? ":weight=bold:slant=italic" : "", 0), + [1] = xstrjoin(dpi, !custom_bold ? ":weight=bold" : ""), + [2] = xstrjoin(dpi, !custom_italic ? ":slant=italic" : ""), + [3] = xstrjoin(dpi, !custom_bold_italic ? ":weight=bold:slant=italic" : ""), }; struct fcft_font *fonts[4]; diff --git a/xmalloc.h b/xmalloc.h index 76db7e1b..8a2c208f 100644 --- a/xmalloc.h +++ b/xmalloc.h @@ -25,16 +25,25 @@ xmemdup(const void *ptr, size_t size) } static inline char * -xstrjoin(const char *s1, const char *s2, char delim) +xstrjoin(const char *s1, const char *s2) { size_t n1 = strlen(s1); - size_t n2 = delim > 0 ? 1 : 0; - size_t n3 = strlen(s2); - - char *joined = xmalloc(n1 + n2 + n3 + 1); + size_t n2 = strlen(s2); + char *joined = xmalloc(n1 + n2 + 1); memcpy(joined, s1, n1); - if (delim > 0) - joined[n1] = delim; - memcpy(joined + n1 + n2, s2, n3 + 1); + memcpy(joined + n1, s2, n2 + 1); + return joined; +} + +static inline char * +xstrjoin3(const char *s1, const char *s2, const char *s3) +{ + size_t n1 = strlen(s1); + size_t n2 = strlen(s2); + size_t n3 = strlen(s3); + char *joined = xmalloc(n1 + n2 + n3 + 1); + memcpy(joined, s1, n1); + memcpy(joined + n1, s2, n2); + memcpy(joined + n1 + n2, s3, n3 + 1); return joined; }