oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

respect xdg config, and recursively mkdir if .config doesnt exist.

Commit
718b4b5005f5c04def0bfed5d5e1956b88a71903
Parent
721f67a
Author
tonybanters <tonybanters@gmail.com>
Date
2026-02-18 18:12:23

Diff

diff --git a/src/main.zig b/src/main.zig
index b744635..bad41a0 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -105,9 +105,13 @@ fn print_help() void {
 }
 
 fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
-    const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
-    const config_path = try std.fs.path.join(allocator, &.{ home, ".config", "oxwm", "config.lua" });
+    const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: {
+        const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
+        break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
+    };
+    defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
 
+    const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
     return config_path;
 }
 
@@ -117,6 +121,20 @@ fn init_config(allocator: std.mem.Allocator) void {
 
     const template = @embedFile("templates/config.lua");
 
+    if (std.fs.path.dirname(config_path)) |dir_path| {
+        var root = std.fs.openDirAbsolute("/", .{}) catch |err| {
+            std.debug.print("error: could not open root directory: {}\n", .{err});
+            return;
+        };
+        defer root.close();
+
+        const relative_path = std.mem.trimLeft(u8, dir_path, "/");
+        root.makePath(relative_path) catch |err| {
+            std.debug.print("error: could not create config directory: {}\n", .{err});
+            return;
+        };
+    }
+
     const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
         std.debug.print("error: could not create config file: {}\n", .{err});
         return;