diff --git a/build.zig b/build.zig index facbe1f209..0a7795d615 100644 --- a/build.zig +++ b/build.zig @@ -78,7 +78,6 @@ pub fn build(b: &Builder) !void { exe.linkSystemLibrary("c"); b.default_step.dependOn(&exe.step); - b.default_step.dependOn(docs_step); const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") ?? false; if (!skip_self_hosted) { diff --git a/doc/docgen.zig b/doc/docgen.zig index 71a06d719b..9272258cf1 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -31,10 +31,10 @@ pub fn main() !void { const out_file_name = try (args_it.next(allocator) ?? @panic("expected output arg")); defer allocator.free(out_file_name); - var in_file = try io.File.openRead(in_file_name, allocator); + var in_file = try io.File.openRead(allocator, in_file_name); defer in_file.close(); - var out_file = try io.File.openWrite(out_file_name, allocator); + var out_file = try io.File.openWrite(allocator, out_file_name); defer out_file.close(); var file_in_stream = io.FileInStream.init(&in_file); @@ -723,7 +723,7 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var try out.print("
{}
", escaped_source); const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name); const tmp_source_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_ext); - try io.writeFile(tmp_source_file_name, trimmed_raw_source, null); + try io.writeFile(allocator, tmp_source_file_name, trimmed_raw_source); switch (code.id) { Code.Id.Exe => |expected_outcome| { diff --git a/example/cat/main.zig b/example/cat/main.zig index 203567644d..97fbada7a9 100644 --- a/example/cat/main.zig +++ b/example/cat/main.zig @@ -20,7 +20,7 @@ pub fn main() !void { } else if (arg[0] == '-') { return usage(exe); } else { - var file = io.File.openRead(arg, null) catch |err| { + var file = io.File.openRead(allocator, arg) catch |err| { warn("Unable to open file: {}\n", @errorName(err)); return err; }; diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 7b4a5e959e..cfed6c3db0 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -16,15 +16,6 @@ const c = @import("c.zig"); const default_zig_cache_name = "zig-cache"; -pub fn main() !void { - main2() catch |err| { - if (err != error.InvalidCommandLineArguments) { - warn("{}\n", @errorName(err)); - } - return err; - }; -} - const Cmd = enum { None, Build, @@ -35,21 +26,25 @@ const Cmd = enum { Targets, }; -fn badArgs(comptime format: []const u8, args: ...) error { - var stderr = try io.getStdErr(); +fn badArgs(comptime format: []const u8, args: ...) noreturn { + var stderr = io.getStdErr() catch std.os.exit(1); var stderr_stream_adapter = io.FileOutStream.init(&stderr); const stderr_stream = &stderr_stream_adapter.stream; - try stderr_stream.print(format ++ "\n\n", args); - try printUsage(&stderr_stream_adapter.stream); - return error.InvalidCommandLineArguments; + stderr_stream.print(format ++ "\n\n", args) catch std.os.exit(1); + printUsage(&stderr_stream_adapter.stream) catch std.os.exit(1); + std.os.exit(1); } -pub fn main2() !void { +pub fn main() !void { const allocator = std.heap.c_allocator; const args = try os.argsAlloc(allocator); defer os.argsFree(allocator, args); + if (args.len >= 2 and mem.eql(u8, args[1], "fmt")) { + return fmtMain(allocator, args[2..]); + } + var cmd = Cmd.None; var build_kind: Module.Kind = undefined; var build_mode: builtin.Mode = builtin.Mode.Debug; @@ -169,7 +164,7 @@ pub fn main2() !void { } else if (mem.eql(u8, arg, "--pkg-end")) { @panic("TODO --pkg-end"); } else if (arg_i + 1 >= args.len) { - return badArgs("expected another argument after {}", arg); + badArgs("expected another argument after {}", arg); } else { arg_i += 1; if (mem.eql(u8, arg, "--output")) { @@ -184,7 +179,7 @@ pub fn main2() !void { } else if (mem.eql(u8, args[arg_i], "off")) { color = ErrColor.Off; } else { - return badArgs("--color options are 'auto', 'on', or 'off'"); + badArgs("--color options are 'auto', 'on', or 'off'"); } } else if (mem.eql(u8, arg, "--emit")) { if (mem.eql(u8, args[arg_i], "asm")) { @@ -194,7 +189,7 @@ pub fn main2() !void { } else if (mem.eql(u8, args[arg_i], "llvm-ir")) { emit_file_type = Emit.LlvmIr; } else { - return badArgs("--emit options are 'asm', 'bin', or 'llvm-ir'"); + badArgs("--emit options are 'asm', 'bin', or 'llvm-ir'"); } } else if (mem.eql(u8, arg, "--name")) { out_name_arg = args[arg_i]; @@ -262,7 +257,7 @@ pub fn main2() !void { } else if (mem.eql(u8, arg, "--test-cmd")) { @panic("TODO --test-cmd"); } else { - return badArgs("invalid argument: {}", arg); + badArgs("invalid argument: {}", arg); } } } else if (cmd == Cmd.None) { @@ -285,18 +280,18 @@ pub fn main2() !void { cmd = Cmd.Test; build_kind = Module.Kind.Exe; } else { - return badArgs("unrecognized command: {}", arg); + badArgs("unrecognized command: {}", arg); } } else switch (cmd) { Cmd.Build, Cmd.TranslateC, Cmd.Test => { if (in_file_arg == null) { in_file_arg = arg; } else { - return badArgs("unexpected extra parameter: {}", arg); + badArgs("unexpected extra parameter: {}", arg); } }, Cmd.Version, Cmd.Zen, Cmd.Targets => { - return badArgs("unexpected extra parameter: {}", arg); + badArgs("unexpected extra parameter: {}", arg); }, Cmd.None => unreachable, } @@ -333,15 +328,15 @@ pub fn main2() !void { // } switch (cmd) { - Cmd.None => return badArgs("expected command"), + Cmd.None => badArgs("expected command"), Cmd.Zen => return printZen(), Cmd.Build, Cmd.Test, Cmd.TranslateC => { if (cmd == Cmd.Build and in_file_arg == null and objects.len == 0 and asm_files.len == 0) { - return badArgs("expected source file argument or at least one --object or --assembly argument"); + badArgs("expected source file argument or at least one --object or --assembly argument"); } else if ((cmd == Cmd.TranslateC or cmd == Cmd.Test) and in_file_arg == null) { - return badArgs("expected source file argument"); + badArgs("expected source file argument"); } else if (cmd == Cmd.Build and build_kind == Module.Kind.Obj and objects.len != 0) { - return badArgs("When building an object file, --object arguments are invalid"); + badArgs("When building an object file, --object arguments are invalid"); } const root_name = switch (cmd) { @@ -351,9 +346,9 @@ pub fn main2() !void { } else if (in_file_arg) |in_file_path| { const basename = os.path.basename(in_file_path); var it = mem.split(basename, "."); - break :x it.next() ?? return badArgs("file name cannot be empty"); + break :x it.next() ?? badArgs("file name cannot be empty"); } else { - return badArgs("--name [name] not provided and unable to infer"); + badArgs("--name [name] not provided and unable to infer"); } }, Cmd.Test => "test", @@ -428,7 +423,7 @@ pub fn main2() !void { module.linker_rdynamic = rdynamic; if (mmacosx_version_min != null and mios_version_min != null) { - return badArgs("-mmacosx-version-min and -mios-version-min options not allowed together"); + badArgs("-mmacosx-version-min and -mios-version-min options not allowed together"); } if (mmacosx_version_min) |ver| { @@ -477,6 +472,7 @@ fn printUsage(stream: var) !void { \\ build-exe [source] create executable from source or object files \\ build-lib [source] create library from source or object files \\ build-obj [source] create object from source or assembly + \\ fmt [file] parse file and render in canonical zig format \\ translate-c [source] convert c code to zig code \\ targets list available compilation targets \\ test [source] create and run a test build @@ -564,6 +560,15 @@ fn printZen() !void { ); } +fn fmtMain(allocator: &mem.Allocator, file_paths: []const []const u8) !void { + for (file_paths) |file_path| { + var file = try io.File.openRead(allocator, file_path); + defer file.close(); + + warn("opened {} (todo tokenize and parse and render)\n", file_path); + } +} + /// Caller must free result fn resolveZigLibDir(allocator: &mem.Allocator, zig_install_prefix_arg: ?[]const u8) ![]u8 { if (zig_install_prefix_arg) |zig_install_prefix| { @@ -588,7 +593,7 @@ fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) ![]u8 const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig"); defer allocator.free(test_index_file); - var file = try io.File.openRead(test_index_file, allocator); + var file = try io.File.openRead(allocator, test_index_file); file.close(); return test_zig_dir; diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig index 245c395ece..bdb268cf2f 100644 --- a/src-self-hosted/module.zig +++ b/src-self-hosted/module.zig @@ -213,7 +213,7 @@ pub const Module = struct { }; errdefer self.allocator.free(root_src_real_path); - const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) catch |err| { + const source_code = io.readFileAllocExtra(self.allocator, root_src_real_path, 3) catch |err| { try printError("unable to open '{}': {}", root_src_real_path, err); return err; }; diff --git a/std/build.zig b/std/build.zig index ee1990a349..edd91697aa 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1890,7 +1890,7 @@ pub const WriteFileStep = struct { warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); return err; }; - io.writeFile(full_path, self.data, self.builder.allocator) catch |err| { + io.writeFile(self.builder.allocator, full_path, self.data) catch |err| { warn("unable to write {}: {}\n", full_path, @errorName(err)); return err; }; diff --git a/std/cstr.zig b/std/cstr.zig index 86d59b11dd..f905b57c91 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -39,8 +39,7 @@ fn testCStrFnsImpl() void { assert(len(c"123456789") == 9); } -/// Returns a mutable slice with exactly the same size which is guaranteed to -/// have a null byte after it. +/// Returns a mutable slice with 1 more byte of length which is a null byte. /// Caller owns the returned memory. pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) ![]u8 { const result = try allocator.alloc(u8, slice.len + 1); diff --git a/std/debug/index.zig b/std/debug/index.zig index 2bb03a6706..9c27d84375 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -265,7 +265,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace { } fn printLineFromFile(allocator: &mem.Allocator, out_stream: var, line_info: &const LineInfo) !void { - var f = try io.File.openRead(line_info.file_name, allocator); + var f = try io.File.openRead(allocator, line_info.file_name); defer f.close(); // TODO fstat and make sure that the file has the correct size diff --git a/std/io.zig b/std/io.zig index 504dcdef80..9fca6aa6f7 100644 --- a/std/io.zig +++ b/std/io.zig @@ -110,19 +110,16 @@ pub const File = struct { const OpenError = os.WindowsOpenError || os.PosixOpenError; - /// `path` may need to be copied in memory to add a null terminating byte. In this case - /// a fixed size buffer of size std.os.max_noalloc_path_len is an attempted solution. If the fixed - /// size buffer is too small, and the provided allocator is null, error.NameTooLong is returned. - /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. + /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. /// Call close to clean up. - pub fn openRead(path: []const u8, allocator: ?&mem.Allocator) OpenError!File { + pub fn openRead(allocator: &mem.Allocator, path: []const u8) OpenError!File { if (is_posix) { const flags = system.O_LARGEFILE|system.O_RDONLY; - const fd = try os.posixOpen(path, flags, 0, allocator); + const fd = try os.posixOpen(allocator, path, flags, 0); return openHandle(fd); } else if (is_windows) { - const handle = try os.windowsOpen(path, system.GENERIC_READ, system.FILE_SHARE_READ, - system.OPEN_EXISTING, system.FILE_ATTRIBUTE_NORMAL, allocator); + const handle = try os.windowsOpen(allocator, path, system.GENERIC_READ, system.FILE_SHARE_READ, + system.OPEN_EXISTING, system.FILE_ATTRIBUTE_NORMAL); return openHandle(handle); } else { unreachable; @@ -130,25 +127,22 @@ pub const File = struct { } /// Calls `openWriteMode` with 0o666 for the mode. - pub fn openWrite(path: []const u8, allocator: ?&mem.Allocator) !File { - return openWriteMode(path, 0o666, allocator); + pub fn openWrite(allocator: &mem.Allocator, path: []const u8) !File { + return openWriteMode(allocator, path, 0o666); } - /// `path` may need to be copied in memory to add a null terminating byte. In this case - /// a fixed size buffer of size std.os.max_noalloc_path_len is an attempted solution. If the fixed - /// size buffer is too small, and the provided allocator is null, error.NameTooLong is returned. - /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. + /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. /// Call close to clean up. - pub fn openWriteMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) !File { + pub fn openWriteMode(allocator: &mem.Allocator, path: []const u8, mode: usize) !File { if (is_posix) { const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC; - const fd = try os.posixOpen(path, flags, mode, allocator); + const fd = try os.posixOpen(allocator, path, flags, mode); return openHandle(fd); } else if (is_windows) { - const handle = try os.windowsOpen(path, system.GENERIC_WRITE, + const handle = try os.windowsOpen(allocator, path, system.GENERIC_WRITE, system.FILE_SHARE_WRITE|system.FILE_SHARE_READ|system.FILE_SHARE_DELETE, - system.CREATE_ALWAYS, system.FILE_ATTRIBUTE_NORMAL, allocator); + system.CREATE_ALWAYS, system.FILE_ATTRIBUTE_NORMAL); return openHandle(handle); } else { unreachable; @@ -521,24 +515,21 @@ pub fn OutStream(comptime Error: type) type { }; } -/// `path` may need to be copied in memory to add a null terminating byte. In this case -/// a fixed size buffer of size `std.os.max_noalloc_path_len` is an attempted solution. If the fixed -/// size buffer is too small, and the provided allocator is null, `error.NameTooLong` is returned. -/// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. -pub fn writeFile(path: []const u8, data: []const u8, allocator: ?&mem.Allocator) !void { - var file = try File.openWrite(path, allocator); +/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. +pub fn writeFile(allocator: &mem.Allocator, path: []const u8, data: []const u8) !void { + var file = try File.openWrite(allocator, path); defer file.close(); try file.write(data); } /// On success, caller owns returned buffer. -pub fn readFileAlloc(path: []const u8, allocator: &mem.Allocator) ![]u8 { - return readFileAllocExtra(path, allocator, 0); +pub fn readFileAlloc(allocator: &mem.Allocator, path: []const u8) ![]u8 { + return readFileAllocExtra(allocator, path, 0); } /// On success, caller owns returned buffer. /// Allocates extra_len extra bytes at the end of the file buffer, which are uninitialized. -pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len: usize) ![]u8 { - var file = try File.openRead(path, allocator); +pub fn readFileAllocExtra(allocator: &mem.Allocator, path: []const u8, extra_len: usize) ![]u8 { + var file = try File.openRead(allocator, path); defer file.close(); const size = try file.getEndPos(); diff --git a/std/io_test.zig b/std/io_test.zig index ea4b816b01..7cf67a51f2 100644 --- a/std/io_test.zig +++ b/std/io_test.zig @@ -13,7 +13,7 @@ test "write a file, read it, then delete it" { rng.fillBytes(data[0..]); const tmp_file_name = "temp_test_file.txt"; { - var file = try io.File.openWrite(tmp_file_name, allocator); + var file = try io.File.openWrite(allocator, tmp_file_name); defer file.close(); var file_out_stream = io.FileOutStream.init(&file); @@ -25,7 +25,7 @@ test "write a file, read it, then delete it" { try buf_stream.flush(); } { - var file = try io.File.openRead(tmp_file_name, allocator); + var file = try io.File.openRead(allocator, tmp_file_name); defer file.close(); const file_size = try file.getEndPos(); diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 0b3040cdc9..7b0be8ef82 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -360,11 +360,14 @@ pub const ChildProcess = struct { errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); }; const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); - const dev_null_fd = if (any_ignore) - try os.posixOpen("/dev/null", posix.O_RDWR, 0, null) - else - undefined - ; + const dev_null_fd = if (any_ignore) blk: { + const dev_null_path = "/dev/null"; + var fixed_buffer_mem: [dev_null_path.len + 1]u8 = undefined; + var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); + break :blk try os.posixOpen(&fixed_allocator.allocator, "/dev/null", posix.O_RDWR, 0); + } else blk: { + break :blk undefined; + }; defer { if (any_ignore) os.close(dev_null_fd); } var env_map_owned: BufMap = undefined; @@ -466,12 +469,15 @@ pub const ChildProcess = struct { self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); - const nul_handle = if (any_ignore) - try os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, - windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null) - else - undefined - ; + const nul_handle = if (any_ignore) blk: { + const nul_file_path = "NUL"; + var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined; + var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); + break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, + windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL); + } else blk: { + break :blk undefined; + }; defer { if (any_ignore) os.close(nul_handle); } if (any_ignore) { try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); diff --git a/std/os/index.zig b/std/os/index.zig index a2da2ec673..b8d5f073e0 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -15,7 +15,6 @@ pub const posix = switch(builtin.os) { else => @compileError("Unsupported OS"), }; -pub const max_noalloc_path_len = 1024; pub const ChildProcess = @import("child_process.zig").ChildProcess; pub const path = @import("path.zig"); @@ -265,32 +264,14 @@ pub const PosixOpenError = error { Unexpected, }; -/// ::file_path may need to be copied in memory to add a null terminating byte. In this case -/// a fixed size buffer of size ::max_noalloc_path_len is an attempted solution. If the fixed -/// size buffer is too small, and the provided allocator is null, ::error.NameTooLong is returned. -/// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. +/// ::file_path needs to be copied in memory to add a null terminating byte. /// Calls POSIX open, keeps trying if it gets interrupted, and translates /// the return value into zig errors. -pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Allocator) PosixOpenError!i32 { - var stack_buf: [max_noalloc_path_len]u8 = undefined; - var path0: []u8 = undefined; - var need_free = false; +pub fn posixOpen(allocator: &Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { + const path_with_null = try cstr.addNullByte(allocator, file_path); + defer allocator.free(path_with_null); - if (file_path.len < stack_buf.len) { - path0 = stack_buf[0..file_path.len + 1]; - } else if (allocator) |a| { - path0 = try a.alloc(u8, file_path.len + 1); - need_free = true; - } else { - return error.NameTooLong; - } - defer if (need_free) { - (??allocator).free(path0); - }; - mem.copy(u8, path0, file_path); - path0[file_path.len] = 0; - - return posixOpenC(path0.ptr, flags, perm); + return posixOpenC(path_with_null.ptr, flags, perm); } pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) !i32 { @@ -784,11 +765,11 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [ try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path[dest_path.len..], rand_buf); - var out_file = try io.File.openWriteMode(tmp_path, mode, allocator); + var out_file = try io.File.openWriteMode(allocator, tmp_path, mode); defer out_file.close(); errdefer _ = deleteFile(allocator, tmp_path); - var in_file = try io.File.openRead(source_path, allocator); + var in_file = try io.File.openRead(allocator, source_path); defer in_file.close(); var buf: [page_size]u8 = undefined; @@ -1074,7 +1055,7 @@ pub const Dir = struct { }; pub fn open(allocator: &Allocator, dir_path: []const u8) !Dir { - const fd = try posixOpen(dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0, allocator); + const fd = try posixOpen(allocator, dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0); return Dir { .allocator = allocator, .fd = fd, @@ -1642,13 +1623,16 @@ pub fn unexpectedErrorWindows(err: windows.DWORD) (error{Unexpected}) { pub fn openSelfExe() !io.File { switch (builtin.os) { Os.linux => { - return io.File.openRead("/proc/self/exe", null); + const proc_file_path = "/proc/self/exe"; + var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined; + var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); + return io.File.openRead(&fixed_allocator.allocator, proc_file_path); }, Os.macosx, Os.ios => { - var fixed_buffer_mem: [darwin.PATH_MAX]u8 = undefined; + var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined; var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); const self_exe_path = try selfExePath(&fixed_allocator.allocator); - return io.File.openRead(self_exe_path, null); + return io.File.openRead(&fixed_allocator.allocator, self_exe_path); }, else => @compileError("Unsupported OS"), } diff --git a/std/os/path.zig b/std/os/path.zig index 5a7428c220..0ea5d5a753 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -1161,7 +1161,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) ![]u8 { return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); }, Os.linux => { - const fd = try os.posixOpen(pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0, allocator); + const fd = try os.posixOpen(allocator, pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0); defer os.close(fd); var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; diff --git a/std/os/windows/util.zig b/std/os/windows/util.zig index c19a5789e7..5af318b7b0 100644 --- a/std/os/windows/util.zig +++ b/std/os/windows/util.zig @@ -89,34 +89,17 @@ pub const OpenError = error { PipeBusy, Unexpected, OutOfMemory, - NameTooLong, }; -/// `file_path` may need to be copied in memory to add a null terminating byte. In this case -/// a fixed size buffer of size ::max_noalloc_path_len is an attempted solution. If the fixed -/// size buffer is too small, and the provided allocator is null, ::error.NameTooLong is returned. -/// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. -pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_mode: windows.DWORD, - creation_disposition: windows.DWORD, flags_and_attrs: windows.DWORD, allocator: ?&mem.Allocator) +/// `file_path` needs to be copied in memory to add a null terminating byte, hence the allocator. +pub fn windowsOpen(allocator: &mem.Allocator, file_path: []const u8, desired_access: windows.DWORD, share_mode: windows.DWORD, + creation_disposition: windows.DWORD, flags_and_attrs: windows.DWORD) OpenError!windows.HANDLE { - var stack_buf: [os.max_noalloc_path_len]u8 = undefined; - var path0: []u8 = undefined; - var need_free = false; - defer if (need_free) (??allocator).free(path0); + const path_with_null = try cstr.addNullByte(allocator, file_path); + defer allocator.free(path_with_null); - if (file_path.len < stack_buf.len) { - path0 = stack_buf[0..file_path.len + 1]; - } else if (allocator) |a| { - path0 = try a.alloc(u8, file_path.len + 1); - need_free = true; - } else { - return error.NameTooLong; - } - mem.copy(u8, path0, file_path); - path0[file_path.len] = 0; - - const result = windows.CreateFileA(path0.ptr, desired_access, share_mode, null, creation_disposition, + const result = windows.CreateFileA(path_with_null.ptr, desired_access, share_mode, null, creation_disposition, flags_and_attrs, null); if (result == windows.INVALID_HANDLE_VALUE) { diff --git a/test/tests.zig b/test/tests.zig index c12775aa85..19a4f82b74 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1049,7 +1049,7 @@ pub const GenHContext = struct { warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name); const full_h_path = b.pathFromRoot(self.h_path); - const actual_h = try io.readFileAlloc(full_h_path, b.allocator); + const actual_h = try io.readFileAlloc(b.allocator, full_h_path); for (self.case.expected_lines.toSliceConst()) |expected_line| { if (mem.indexOf(u8, actual_h, expected_line) == null) {