diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index 3d6ed27538..ebb2ec82d8 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -1046,11 +1046,13 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { } pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { - const resolved_from = try resolveWindows(allocator, &[_][]const u8{from}); + const cwd = try process.getCwdAlloc(allocator); + defer allocator.free(cwd); + const resolved_from = try resolveWindows(allocator, &[_][]const u8{ cwd, from }); defer allocator.free(resolved_from); var clean_up_resolved_to = true; - const resolved_to = try resolveWindows(allocator, &[_][]const u8{to}); + const resolved_to = try resolveWindows(allocator, &[_][]const u8{ cwd, to }); defer if (clean_up_resolved_to) allocator.free(resolved_to); const parsed_from = windowsParsePath(resolved_from); @@ -1096,12 +1098,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! var result_index: usize = 0; while (result_index < up_index_end) { - result[result_index] = '.'; - result_index += 1; - result[result_index] = '.'; - result_index += 1; - result[result_index] = '\\'; - result_index += 1; + result[result_index..][0..3].* = "..\\".*; + result_index += 3; } // shave off the trailing slash result_index -= 1; @@ -1121,10 +1119,11 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! } pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { - const resolved_from = try resolvePosix(allocator, &[_][]const u8{from}); + const cwd = try process.getCwdAlloc(allocator); + defer allocator.free(cwd); + const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from }); defer allocator.free(resolved_from); - - const resolved_to = try resolvePosix(allocator, &[_][]const u8{to}); + const resolved_to = try resolvePosix(allocator, &[_][]const u8{ cwd, to }); defer allocator.free(resolved_to); var from_it = mem.tokenize(u8, resolved_from, "/"); @@ -1146,12 +1145,8 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] var result_index: usize = 0; while (result_index < up_index_end) { - result[result_index] = '.'; - result_index += 1; - result[result_index] = '.'; - result_index += 1; - result[result_index] = '/'; - result_index += 1; + result[result_index..][0..3].* = "../".*; + result_index += 3; } if (to_rest.len == 0) { // shave off the trailing slash