summaryrefslogtreecommitdiff
path: root/libs/xdg/xdg.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2022-04-25 05:09:55 +0300
committerGravatar Uko Kokņevičs2022-04-25 23:34:05 +0300
commitd303b53f2ced75703bf022a5d337ee3ba530b288 (patch)
treef2e64057120d01ee8a821596ea01b8fc37c53c2c /libs/xdg/xdg.zig
downloadzup-0.1.0.tar.gz
zup-0.1.0.tar.xz
zup-0.1.0.zip
Initial commit0.1.0
Diffstat (limited to 'libs/xdg/xdg.zig')
-rw-r--r--libs/xdg/xdg.zig36
1 files changed, 36 insertions, 0 deletions
diff --git a/libs/xdg/xdg.zig b/libs/xdg/xdg.zig
new file mode 100644
index 0000000..8466066
--- /dev/null
+++ b/libs/xdg/xdg.zig
@@ -0,0 +1,36 @@
1const std = @import("std");
2
3const Allocator = std.mem.Allocator;
4const Dir = std.fs.Dir;
5
6pub fn getDataHome(allocator: Allocator, app_name: []const u8) ![]u8 {
7 if (std.os.getenv("XDG_DATA_HOME")) |data_home| {
8 return std.fs.path.join(allocator, &.{ data_home, app_name });
9 }
10
11 if (std.os.getenv("HOME")) |home| {
12 return std.fs.path.join(allocator, &.{ home, ".local", "share", app_name });
13 }
14
15 return error.HomeNotFound;
16}
17
18pub fn getBinHome(allocator: Allocator) ![]u8 {
19 if (std.os.getenv("HOME")) |home| {
20 return std.fs.path.join(allocator, &.{ home, ".local", "bin" });
21 }
22
23 return error.HomeNotFound;
24}
25
26pub fn openDataHome(allocator: Allocator, app_name: []const u8) !Dir {
27 var data_home = try getDataHome(allocator, app_name);
28 defer allocator.free(data_home);
29 return try std.fs.cwd().makeOpenPath(data_home, .{});
30}
31
32pub fn openBinHome(allocator: Allocator) !Dir {
33 var bin_home = try getBinHome(allocator);
34 defer allocator.free(bin_home);
35 return try std.fs.cwd().makeOpenPath(bin_home, .{});
36}