blob: b857ba5af906bf3e953bc27537464ed88e1af8c4 (
plain) (
blame)
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
|
const std = @import("std");
const Allocator = std.mem.Allocator;
pub fn resolvePath(allocator: Allocator, name: []const u8) ![]u8 {
return std.fs.cwd().realpathAlloc(allocator, name) catch |err| switch (err) {
error.FileNotFound => if (name.len == 0) {
return error.FileNotFound;
} else if (name[0] == std.fs.path.sep) {
// Already is an absolute path
return allocator.dupe(u8, name);
} else if (name[0] == '~') {
if (name.len == 1) {
return error.FileNotFound;
} else if (name[1] == std.fs.path.sep) {
var env_map = try std.process.getEnvMap(allocator);
defer env_map.deinit();
if (env_map.get("HOME")) |home| {
return try std.fs.path.join(allocator, &.{ home, name[2..] });
} else {
std.log.err("No $HOME environment variable available!", .{});
return error.FileNotFound;
}
} else {
// TODO: Currently only doing ~/paths no ~user/paths
return error.FileNotFound;
}
} else {
const cwd = try std.process.getCwdAlloc(allocator);
defer allocator.free(cwd);
return std.fmt.allocPrint(allocator, "{s}{s}{s}", .{cwd, std.fs.path.sep_str, name});
},
else => return err,
};
}
|