From 36e220e2ffcb35100c11568cb49800a718a8b338 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Sat, 30 Aug 2025 02:09:51 +0200 Subject: [PATCH] Remove usage of std.c.stat() for xauth code Signed-off-by: AnErrupTion --- src/auth.zig | 42 ++++++++++++++++++++++-------------------- src/main.zig | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/auth.zig b/src/auth.zig index 5621d21..22b5abe 100644 --- a/src/auth.zig +++ b/src/auth.zig @@ -307,35 +307,37 @@ fn getXPid(display_num: u8) !i32 { fn createXauthFile(pwd: [:0]const u8) ![]const u8 { var xauth_buf: [100]u8 = undefined; - var xauth_dir: [:0]const u8 = undefined; + var xauth_dir: []const u8 = undefined; const xdg_rt_dir = std.posix.getenv("XDG_RUNTIME_DIR"); var xauth_file: []const u8 = "lyxauth"; - if (xdg_rt_dir == null) { + if (xdg_rt_dir == null) no_rt_dir: { const xdg_cfg_home = std.posix.getenv("XDG_CONFIG_HOME"); - var sb: std.c.Stat = undefined; - if (xdg_cfg_home == null) { - xauth_dir = try std.fmt.bufPrintZ(&xauth_buf, "{s}/.config", .{pwd}); - _ = std.c.stat(xauth_dir, &sb); - const mode = sb.mode & std.posix.S.IFMT; - if (mode == std.posix.S.IFDIR) { - xauth_dir = try std.fmt.bufPrintZ(&xauth_buf, "{s}/ly", .{xauth_dir}); - } else { + if (xdg_cfg_home == null) no_cfg_home: { + xauth_dir = try std.fmt.bufPrint(&xauth_buf, "{s}/.config", .{pwd}); + + var dir = std.fs.cwd().openDir(xauth_dir, .{}) catch { + // xauth_dir isn't a directory xauth_dir = pwd; xauth_file = ".lyxauth"; - } + break :no_cfg_home; + }; + dir.close(); + + // xauth_dir is a directory, use it to store Xauthority + xauth_dir = try std.fmt.bufPrint(&xauth_buf, "{s}/ly", .{xauth_dir}); } else { - xauth_dir = try std.fmt.bufPrintZ(&xauth_buf, "{s}/ly", .{xdg_cfg_home.?}); + xauth_dir = try std.fmt.bufPrint(&xauth_buf, "{s}/ly", .{xdg_cfg_home.?}); } - _ = std.c.stat(xauth_dir, &sb); - const mode = sb.mode & std.posix.S.IFMT; - if (mode != std.posix.S.IFDIR) { - std.posix.mkdir(xauth_dir, 777) catch { - xauth_dir = pwd; - xauth_file = ".lyxauth"; - }; - } + const file = std.fs.cwd().openFile(xauth_dir, .{}) catch break :no_rt_dir; + file.close(); + + // xauth_dir is a file, create the parent directory + std.posix.mkdir(xauth_dir, 777) catch { + xauth_dir = pwd; + xauth_file = ".lyxauth"; + }; } else { xauth_dir = xdg_rt_dir.?; } diff --git a/src/main.zig b/src/main.zig index d3f3b70..ef02fa7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -540,7 +540,7 @@ pub fn main() !void { var format_buf: [16:0]u8 = undefined; var clock_buf: [32:0]u8 = undefined; // We need the slice/c-string returned by `bufPrintZ`. - const format: [:0]const u8 = try std.fmt.bufPrintZ(&format_buf, "{s}{s}{s}{s}", .{ + const format = try std.fmt.bufPrintZ(&format_buf, "{s}{s}{s}{s}", .{ if (config.bigclock_12hr) "%I" else "%H", ":%M", if (config.bigclock_seconds) ":%S" else "",