]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/string-helpers.c: add strdup_printf()
authorJohan Malm <jgm323@gmail.com>
Sun, 25 Jun 2023 08:00:41 +0000 (09:00 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Mon, 26 Jun 2023 05:30:33 +0000 (06:30 +0100)
include/common/string-helpers.h
src/common/string-helpers.c
src/config/rcxml.c
src/config/session.c

index 55b940d098b93dbcaead5688b82f1773c337e2d8..731f93fee6f39658e39c14f3ce6629d22dae3ef6 100644 (file)
@@ -16,4 +16,15 @@ char *string_strip(char *s);
  */
 void string_truncate_at_pattern(char *buf, const char *pattern);
 
+/**
+ * strdup_printf - allocate and write to buffer in printf format
+ * @fmt: printf-style format.
+ *
+ * Similar to the standard C sprintf() function but safer as it calculates the
+ * maximum space required and allocates memory to hold the output.
+ * The user must free the returned string.
+ * Returns NULL on error.
+ */
+char *strdup_printf(const char *fmt, ...);
+
 #endif /* LABWC_STRING_HELPERS_H */
index 3900d673e7010805d6c2401d29f8c2cf0ddc505d..01ea09f4688bf955bac3bde42044a0f324735919 100644 (file)
@@ -1,7 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <ctype.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include "common/mem.h"
 #include "common/string-helpers.h"
 
 static void
@@ -37,3 +39,32 @@ string_truncate_at_pattern(char *buf, const char *pattern)
        }
        *p = '\0';
 }
+
+char *
+strdup_printf(const char *fmt, ...)
+{
+       size_t size = 0;
+       char *p = NULL;
+       va_list ap;
+
+       va_start(ap, fmt);
+       int n = vsnprintf(p, size, fmt, ap);
+       va_end(ap);
+
+       if (n < 0) {
+               return NULL;
+       }
+
+       size = (size_t)n + 1;
+       p = xzalloc(size);
+
+       va_start(ap, fmt);
+       n = vsnprintf(p, size, fmt, ap);
+       va_end(ap);
+
+       if (n < 0) {
+               free(p);
+               return NULL;
+       }
+       return p;
+}
index cde4fb30621701317cdd096ace3977337658eeea..fafb7df73063a5e50035567c2283225a229384f8 100644 (file)
@@ -1096,11 +1096,9 @@ post_processing(void)
        int nr_workspaces = wl_list_length(&rc.workspace_config.workspaces);
        if (nr_workspaces < rc.workspace_config.min_nr_workspaces) {
                struct workspace *workspace;
-               char workspace_name[32];
                for (int i = nr_workspaces; i < rc.workspace_config.min_nr_workspaces; i++) {
                        workspace = znew(*workspace);
-                       snprintf(workspace_name, sizeof(workspace_name), "Workspace %d", i + 1);
-                       workspace->name = xstrdup(workspace_name);
+                       workspace->name = strdup_printf("Workspace %d", i + 1);
                        wl_list_append(&rc.workspace_config.workspaces, &workspace->link);
                }
        }
index 09f4dc015d2135a6e7761f47f652c36904dbb967..e66a22beb1412d0f792fdb2c9b347a7ec17aed53 100644 (file)
@@ -79,12 +79,7 @@ build_path(const char *dir, const char *filename)
        if (string_empty(dir) || string_empty(filename)) {
                return NULL;
        }
-       int len = strlen(dir) + strlen(filename) + 2;
-       char *buffer = znew_n(char, len);
-       strcat(buffer, dir);
-       strcat(buffer, "/");
-       strcat(buffer, filename);
-       return buffer;
+       return strdup_printf("%s/%s", dir, filename);
 }
 
 static void
@@ -98,19 +93,11 @@ update_activation_env(const char *env_keys)
        }
        wlr_log(WLR_INFO, "Updating dbus execution environment");
 
-       char *cmd;
-       const char *dbus = "dbus-update-activation-environment ";
-       const char *systemd = "systemctl --user import-environment ";
-
-       cmd = znew_n(char, strlen(dbus) + strlen(env_keys) + 1);
-       strcat(cmd, dbus);
-       strcat(cmd, env_keys);
+       char *cmd = strdup_printf("dbus-update-activation-environment %s", env_keys);
        spawn_async_no_shell(cmd);
        free(cmd);
 
-       cmd = znew_n(char, strlen(systemd) + strlen(env_keys) + 1);
-       strcat(cmd, systemd);
-       strcat(cmd, env_keys);
+       cmd = strdup_printf("systemctl --user import-environment %s", env_keys);
        spawn_async_no_shell(cmd);
        free(cmd);
 }
@@ -148,10 +135,7 @@ session_autostart_init(const char *dir)
                goto out;
        }
        wlr_log(WLR_INFO, "run autostart file %s", autostart);
-       int len = strlen(autostart) + 4;
-       char *cmd = znew_n(char, len);
-       strcat(cmd, "sh ");
-       strcat(cmd, autostart);
+       char *cmd = strdup_printf("sh %s", autostart);
        spawn_async_no_shell(cmd);
        free(cmd);
 out: