<commit_before>#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "readline.h"
#include "stringop.h"
#include "list.h"
#include "log.h"
#include "commands.h"
#include "config.h"
#include "layout.h"

struct sway_config *config;

static bool exists(const char *path) {
	return access(path, R_OK) != -1;
}

static char *get_config_path() {
	char *name = "/.sway/config";
	const char *home = getenv("HOME");

	// Check home dir
	sway_log(L_DEBUG, "Trying to find config in ~/.sway/config");
	char *temp = malloc(strlen(home) + strlen(name) + 1);
	strcpy(temp, home);
	strcat(temp, name);
	if (exists(temp)) {
		return temp;
	}

	// Check XDG_CONFIG_HOME with fallback to ~/.config/
	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config");
	char *xdg_config_home = getenv("XDG_CONFIG_HOME");
	if (xdg_config_home == NULL) {
		sway_log(L_DEBUG, "Falling back to ~/.config/sway/config");
		name = "/.config/sway/config";
		temp = malloc(strlen(home) + strlen(name) + 1);
		strcpy(temp, home);
		strcat(temp, name);
	} else {
		name = "/sway/config";
		temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
		strcpy(xdg_config_home, home);
		strcat(temp, name);
	}
	if (exists(temp)) {
		return temp;
	}

	// Check /etc/
	sway_log(L_DEBUG, "Trying to find config in /etc/sway/config");
	strcpy(temp, "/etc/sway/config");
	if (exists(temp)) {
		return temp;
	}

	// Check XDG_CONFIG_DIRS
	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
	char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
	if (xdg_config_dirs != NULL) {
		list_t *paths = split_string(xdg_config_dirs, ":");
		name = "/sway/config";
		int i;
		for (i = 0; i < paths->length; i++ ) {
			temp = malloc(strlen(paths->items[i]) + strlen(name) + 1);
			strcpy(temp, paths->items[i]);
			strcat(temp, name);
			if (exists(temp)) {
				free_flat_list(paths);
				return temp;
			}
		}
		free_flat_list(paths);
	}

	//Now fall back to i3 paths and try the same thing
	name = "/.i3/config";
	sway_log(L_DEBUG, "Trying to find config in ~/.i3/config");
	temp = malloc(strlen(home) + strlen(name) + 1);
	strcpy(temp, home);
	strcat(temp, name);
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config");
	if (xdg_config_home == NULL) {
		sway_log(L_DEBUG, "Falling back to ~/.config/i3/config");
		name = "/.config/i3/config";
		temp = malloc(strlen(home) + strlen(name) + 1);
		strcpy(temp, home);
		strcat(temp, name);
	} else {
		name = "/i3/config";
		temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
		strcpy(xdg_config_home, home);
		strcat(temp, name);
	}
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in /etc/i3/config");
	strcpy(temp, "/etc/i3/config");
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
	if (xdg_config_dirs != NULL) {
		list_t *paths = split_string(xdg_config_dirs, ":");
		name = "/i3/config";
		int i;<commit_msg>fixed string building for XDG_CONFIG_HOME config loading<commit_after>#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "readline.h"
#include "stringop.h"
#include "list.h"
#include "log.h"
#include "commands.h"
#include "config.h"
#include "layout.h"

struct sway_config *config;

static bool exists(const char *path) {
	return access(path, R_OK) != -1;
}

static char *get_config_path() {
	char *name = "/.sway/config";
	const char *home = getenv("HOME");

	// Check home dir
	sway_log(L_DEBUG, "Trying to find config in ~/.sway/config");
	char *temp = malloc(strlen(home) + strlen(name) + 1);
	strcpy(temp, home);
	strcat(temp, name);
	if (exists(temp)) {
		return temp;
	}

	// Check XDG_CONFIG_HOME with fallback to ~/.config/
	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config");
	char *xdg_config_home = getenv("XDG_CONFIG_HOME");
	if (xdg_config_home == NULL) {
		sway_log(L_DEBUG, "Falling back to ~/.config/sway/config");
		name = "/.config/sway/config";
		temp = malloc(strlen(home) + strlen(name) + 1);
		strcpy(temp, home);
		strcat(temp, name);
	} else {
		name = "/sway/config";
		temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
		strcpy(temp, xdg_config_home);
		strcat(temp, name);
	}
	if (exists(temp)) {
		return temp;
	}

	// Check /etc/
	sway_log(L_DEBUG, "Trying to find config in /etc/sway/config");
	strcpy(temp, "/etc/sway/config");
	if (exists(temp)) {
		return temp;
	}

	// Check XDG_CONFIG_DIRS
	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
	char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
	if (xdg_config_dirs != NULL) {
		list_t *paths = split_string(xdg_config_dirs, ":");
		name = "/sway/config";
		int i;
		for (i = 0; i < paths->length; i++ ) {
			temp = malloc(strlen(paths->items[i]) + strlen(name) + 1);
			strcpy(temp, paths->items[i]);
			strcat(temp, name);
			if (exists(temp)) {
				free_flat_list(paths);
				return temp;
			}
		}
		free_flat_list(paths);
	}

	//Now fall back to i3 paths and try the same thing
	name = "/.i3/config";
	sway_log(L_DEBUG, "Trying to find config in ~/.i3/config");
	temp = malloc(strlen(home) + strlen(name) + 1);
	strcpy(temp, home);
	strcat(temp, name);
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config");
	if (xdg_config_home == NULL) {
		sway_log(L_DEBUG, "Falling back to ~/.config/i3/config");
		name = "/.config/i3/config";
		temp = malloc(strlen(home) + strlen(name) + 1);
		strcpy(temp, home);
		strcat(temp, name);
	} else {
		name = "/i3/config";
		temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
		strcpy(temp, xdg_config_home);
		strcat(temp, name);
	}
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in /etc/i3/config");
	strcpy(temp, "/etc/i3/config");
	if (exists(temp)) {
		return temp;
	}

	sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
	if (xdg_config_dirs != NULL) {
		list_t *paths = split_string(xdg_config_dirs, ":");
		name = "/i3/config";
		int i;
{'fraction_non_alphanumeric': 0.16828266228430566, 'fraction_numerical': 0.006244864420706655, 'mean_word_length': 4.197267292912041, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6459283', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2466, 'n_words': 555}