diff --git a/std/os/get_app_data_dir.zig b/std/os/get_app_data_dir.zig index b5efdb826..e8ae5dd49 100644 --- a/std/os/get_app_data_dir.zig +++ b/std/os/get_app_data_dir.zig @@ -35,15 +35,21 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD else => return error.AppDataDirUnavailable, } }, - // TODO for macos it should be "~/Library/Application Support/" - else => { - const home_dir = os.getEnvVarOwned(allocator, "HOME") catch |err| switch (err) { - error.OutOfMemory => return error.OutOfMemory, - error.EnvironmentVariableNotFound => return error.AppDataDirUnavailable, // TODO look in /etc/passwd + builtin.Os.macosx => { + const home_dir = os.getEnvPosix("HOME") orelse { + // TODO look in /etc/passwd + return error.AppDataDirUnavailable; + }; + return os.path.join(allocator, home_dir, "Library", "Application Support", appname); + }, + builtin.Os.linux => { + const home_dir = os.getEnvPosix("HOME") orelse { + // TODO look in /etc/passwd + return error.AppDataDirUnavailable; }; - defer allocator.free(home_dir); return os.path.join(allocator, home_dir, ".local", "share", appname); }, + else => @compileError("Unsupported OS"), } } @@ -53,8 +59,11 @@ fn utf16lePtrSlice(ptr: [*]const u16) []const u16 { return ptr[0..index]; } -test "getAppDataDir" { - const result = try getAppDataDir(std.debug.global_allocator, "zig"); - std.debug.warn("{}...", result); +test "std.os.getAppDataDir" { + var buf: [512]u8 = undefined; + const allocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; + + // We can't actually validate the result + _ = getAppDataDir(allocator, "zig") catch return; } diff --git a/std/os/index.zig b/std/os/index.zig index cb4358af4..87053fd8d 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -498,6 +498,7 @@ pub var linux_aux_raw = []usize{0} ** 38; pub var posix_environ_raw: [][*]u8 = undefined; /// Caller must free result when done. +/// TODO make this go through libc when we have it pub fn getEnvMap(allocator: *Allocator) !BufMap { var result = BufMap.init(allocator); errdefer result.deinit(); @@ -541,6 +542,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { } } +/// TODO make this go through libc when we have it pub fn getEnvPosix(key: []const u8) ?[]const u8 { for (posix_environ_raw) |ptr| { var line_i: usize = 0; @@ -563,6 +565,7 @@ pub const GetEnvVarOwnedError = error{ }; /// Caller must free returned memory. +/// TODO make this go through libc when we have it pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 { if (is_windows) { const key_with_null = try cstr.addNullByte(allocator, key);