1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Dir = std.fs.Dir;
pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 {
if (std.os.getenv("XDG_DATA_HOME")) |data_home| {
return std.fs.path.join(allocator, &.{ data_home, app_name });
}
if (std.os.getenv("HOME")) |home| {
return std.fs.path.join(allocator, &.{ home, ".local", "share", app_name });
}
return error.HomeNotFound;
}
pub fn getConfigHome(allocator: Allocator, app_name: []const u8) ![]u8 {
if (std.os.getenv("XDG_CONFIG_HOME")) |config_home| {
return std.fs.path.join(allocator, &.{ config_home, app_name });
}
if (std.os.getenv("HOME")) |home| {
return std.fs.path.join(allocator, &.{ home, ".config", app_name });
}
return error.HomeNotFound;
}
pub fn getBinHome(allocator: Allocator) ![]u8 {
if (std.os.getenv("HOME")) |home| {
return std.fs.path.join(allocator, &.{ home, ".local", "bin" });
}
return error.HomeNotFound;
}
pub fn getConfigDirs(allocator: Allocator, app_name: []const u8) ![][]u8 {
var list = ArrayList([]u8).init(allocator);
errdefer for (list.items) |s| allocator.free(s);
defer list.deinit();
if (std.os.getenv("XDG_CONFIG_DIRS")) |config_dirs| {
var it = std.mem.split(u8, config_dirs, ":");
while (it.next()) |config_dir| {
try list.append(try std.fs.path.join(allocator, &.{ config_dir, app_name }));
}
} else {
try list.append(try std.fs.path.join(allocator, &.{ "/etc/xdg", app_name }));
}
return list.toOwnedSlice();
}
pub fn getAllConfigDirs(allocator: Allocator, app_name: []const u8) ![][]u8 {
var list = ArrayList([]u8).init(allocator);
errdefer for (list.items) |s| allocator.free(s);
defer list.deinit();
try list.append(try getConfigHome(allocator, app_name));
const config_dirs = try getConfigDirs(allocator, app_name);
defer allocator.free(config_dirs);
try list.appendSlice(config_dirs);
return list.toOwnedSlice();
}
pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir {
const data_home = try getDataHome(allocator, app_name);
defer allocator.free(data_home);
return try std.fs.cwd().makeOpenPath(data_home, .{});
}
pub fn openBinHome(allocator: Allocator) !Dir {
const bin_home = try getBinHome(allocator);
defer allocator.free(bin_home);
return try std.fs.cwd().makeOpenPath(bin_home, .{});
}
|