windows: use cross platform consts where available

This commit is contained in:
Will Pragnell
2023-09-15 16:01:33 -07:00
parent d700fb6fc7
commit 8f2ab46e1e
2 changed files with 10 additions and 9 deletions

View File

@ -223,8 +223,6 @@ pub fn getData(self: Command, comptime DT: type) ?*DT {
/// Search for "cmd" in the PATH and return the absolute path. This will
/// always allocate if there is a non-null result. The caller must free the
/// resulting value.
///
/// TODO: windows
pub fn expandPath(alloc: Allocator, cmd: []const u8) !?[]u8 {
// If the command already contains a slash, then we return it as-is
// because it is assumed to be absolute or relative.
@ -243,8 +241,7 @@ pub fn expandPath(alloc: Allocator, cmd: []const u8) !?[]u8 {
defer if (builtin.os.tag == .windows) alloc.free(PATH);
var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const path_separator = if (builtin.os.tag == .windows) ";" else ":";
var it = std.mem.tokenize(u8, PATH, path_separator);
var it = std.mem.tokenize(u8, PATH, &[_]u8{std.fs.path.delimiter});
var seen_eacces = false;
while (it.next()) |search_path| {
// We need enough space in our path buffer to store this
@ -253,7 +250,7 @@ pub fn expandPath(alloc: Allocator, cmd: []const u8) !?[]u8 {
// Copy in the full path
mem.copy(u8, &path_buf, search_path);
path_buf[search_path.len] = if (builtin.os.tag == .windows) '\\' else '/';
path_buf[search_path.len] = std.fs.path.sep;
mem.copy(u8, path_buf[search_path.len + 1 ..], cmd);
path_buf[path_len] = 0;
const full_path = path_buf[0..path_len :0];

View File

@ -55,10 +55,14 @@ pub fn fixMaxFiles() void {
pub fn tmpDir() ?[]const u8 {
if (builtin.os.tag == .windows) {
// TODO: what is a good fallback path on windows?
const w_temp = std.os.getenvW(std.unicode.utf8ToUtf16LeStringLiteral("TMP")) orelse return null;
var buf = [_]u8{0} ** 256; // 256 is the maximum path length on windows
const len = std.unicode.utf16leToUtf8(buf[0..], w_temp[0..w_temp.len]) catch {
log.warn("failed to convert temp dir path from windows string", .{});
const v = std.os.getenvW(std.unicode.utf8ToUtf16LeStringLiteral("TMP")) orelse return null;
// MAX_PATH is very likely sufficient, but it's theoretically possible for someone to
// configure their os to allow paths as big as std.os.windows.PATH_MAX_WIDE, which is MUCH
// larger. Even if they did that, though, it's very unlikey that their Temp dir will use
// such a long path. We can switch if we see any issues, though it seems fairly unlikely.
var buf = [_]u8{0} ** std.os.windows.MAX_PATH;
const len = std.unicode.utf16leToUtf8(buf[0..], v[0..v.len]) catch |e| {
log.warn("failed to convert temp dir path from windows string: {}", .{e});
return null;
};
return buf[0..len];