— Scoopta 4 days ago
Replaced a lot of concats and copies with utils_concat and strdup
2 files changed, 12 insertions(+), 32 deletions(-)

M src/bar.c
M src/main.c
M src/bar.c +2 -6
@@ 187,9 187,7 @@
 }
 
 static void* get_plugin_func(const char* prefix, const char* suffix) {
-	void* fun_name = malloc(strlen(prefix) + strlen(suffix) + 1);
-	strcpy(fun_name, prefix);
-	strcat(fun_name, suffix);
+	char* fun_name = utils_concat(prefix, suffix);
 	void* fun = dlsym(RTLD_DEFAULT, fun_name);
 	free(fun_name);
 	return fun;

          
@@ 361,9 359,7 @@
 struct map* props = map_init();
 		for(size_t count = 0; count < arg_count; ++count) {
 			const char* arg = arg_names[count];
-			char* hyphen_name = malloc(strlen(arg) + 2);
-			strcpy(hyphen_name, "-");
-			strcat(hyphen_name, arg);
+			char* hyphen_name = utils_concat("-", arg);
 			map_put(props, arg, config_get(config, plugin_name, hyphen_name, NULL));
 			free(hyphen_name);
 		}

          
M src/main.c +10 -26
@@ 147,25 147,18 @@
 const char* config_dir = "/.config/rootbar";
 	const char* color_f = "/.cache/wal/colors";
 
-	CONFIG_LOCATION = malloc(strlen(home_dir) + strlen(config_dir) + 1);
-	strcpy(CONFIG_LOCATION, home_dir);
-	strcat(CONFIG_LOCATION, config_dir);
+	CONFIG_LOCATION = utils_concat(home_dir, config_dir);
 	config = map_init();
 
-	COLORS_LOCATION = malloc(strlen(home_dir) + strlen(color_f) + 1);
-	strcpy(COLORS_LOCATION, home_dir);
-	strcat(COLORS_LOCATION, color_f);
+	COLORS_LOCATION = utils_concat(home_dir, color_f);
 
 	//Check if --conf was specified
 	char* config_path;
 	if(config_str == NULL) {
 		const char* config_f = "/config";
-		config_path = malloc(strlen(CONFIG_LOCATION) + strlen(config_f) + 1);
-		strcpy(config_path, CONFIG_LOCATION);
-		strcat(config_path, config_f);
+		config_path = utils_concat(CONFIG_LOCATION, config_f);
 	} else {
-		config_path = malloc(strlen(config_str) + 1);
-		strcpy(config_path, config_str);
+		config_path = strdup(config_str);
 	}
 	if(access(config_path, R_OK) == 0) {
 		config_load(config, config_path);

          
@@ 181,9 174,7 @@
 style_str = map_get(config, "stylesheet");
 		if(style_str == NULL) {
 			const char* style_f = "/style.css";
-			stylesheet = malloc(strlen(CONFIG_LOCATION) + strlen(style_f) + 1);
-			strcpy(stylesheet, CONFIG_LOCATION);
-			strcat(stylesheet, style_f);
+			stylesheet = utils_concat(CONFIG_LOCATION, style_f);
 		} else {
 			stylesheet = malloc(strlen(CONFIG_LOCATION) + strlen(style_str) + 2);
 			strcpy(stylesheet, CONFIG_LOCATION);

          
@@ 191,8 182,7 @@
 strcat(stylesheet, style_str);
 		}
 	} else {
-		stylesheet = malloc(strlen(style_str) + 1);
-		strcpy(stylesheet, style_str);
+		stylesheet = strdup(style_str);
 	}
 
 	//Check if --color was specified

          
@@ 200,8 190,7 @@
 if(color_str == NULL) {
 		color_str = map_get(config, "colors");
 		if(color_str == NULL) {
-			color_path = malloc(strlen(COLORS_LOCATION) + 1);
-			strcpy(color_path, COLORS_LOCATION);
+			color_path = strdup(COLORS_LOCATION);
 		} else {
 			color_path = malloc(strlen(CONFIG_LOCATION) + strlen(color_str) + 2);
 			strcpy(color_path, CONFIG_LOCATION);

          
@@ 209,8 198,7 @@
 strcat(color_path, color_str);
 		}
 	} else {
-		color_path = malloc(strlen(color_str) + 1);
-		strcpy(color_path, color_str);
+		color_path = strdup(color_str);
 	}
 	gtk_init(&argc, &argv);
 	if(access(stylesheet, R_OK) == 0) {

          
@@ 255,9 243,7 @@
 const char* rootbar_color = "--rootbar-color";
 			char count_str[3];
 			snprintf(count_str, 3, "%lu", count--);
-			char* needle = malloc(strlen(rootbar_color) + strlen(count_str) + 1);
-			strcpy(needle, rootbar_color);
-			strcat(needle, count_str);
+			char* needle = utils_concat(rootbar_color, count_str);
 			size_t color_len = strlen(color);
 			size_t needle_len = strlen(needle);
 			if(color_len > needle_len) {

          
@@ 280,9 266,7 @@
 continue;
 			}
 			const char* rootbar_rgb_color = "--rootbar-rgb-color";
-			needle = malloc(strlen(rootbar_rgb_color) + strlen(count_str) + 1);
-			strcpy(needle, rootbar_rgb_color);
-			strcat(needle, count_str);
+			needle = utils_concat(rootbar_rgb_color, count_str);
 			needle_len = strlen(needle);
 			replace = strstr(data, needle);
 			while(replace != NULL) {