diff --git a/std/bootstrap.zig b/std/bootstrap.zig index 17e32a96f4..f520834654 100644 --- a/std/bootstrap.zig +++ b/std/bootstrap.zig @@ -2,8 +2,6 @@ const root = @import("@root"); const std = @import("std"); -const linux = std.linux; -const cstr = std.cstr; const want_start_symbol = switch(@compileVar("os")) { Os.linux => true, @@ -11,6 +9,11 @@ const want_start_symbol = switch(@compileVar("os")) { }; const want_main_symbol = !want_start_symbol; +const exit = switch(@compileVar("os")) { + Os.linux => std.linux.exit, + Os.darwin => std.darwin.exit, +}; + var argc: usize = undefined; var argv: &&u8 = undefined; @@ -35,14 +38,14 @@ fn callMain() -> %void { const args = @alloca([]u8, argc); for (args) |_, i| { const ptr = argv[i]; - args[i] = ptr[0...cstr.len(ptr)]; + args[i] = ptr[0...std.cstr.len(ptr)]; } return root.main(args); } fn callMainAndExit() -> unreachable { - callMain() %% linux.exit(1); - linux.exit(0); + callMain() %% exit(1); + exit(0); } export fn main(c_argc: i32, c_argv: &&u8) -> i32 { diff --git a/std/darwin.zig b/std/darwin.zig index 9f00cc06dc..c1697fd10d 100644 --- a/std/darwin.zig +++ b/std/darwin.zig @@ -48,6 +48,11 @@ pub const SIGPWR = 30; pub const SIGSYS = 31; pub const SIGUNUSED = SIGSYS; +pub fn exit(status: usize) -> unreachable { + arch.syscall1(arch.SYS_exit, status); + @unreachable() +} + /// Get the errno from a syscall return value, or 0 for no error. pub fn getErrno(r: usize) -> usize { const signed_r = *(&isize)(&r); @@ -67,7 +72,7 @@ pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize { } pub fn open(path: []const u8, flags: usize, perm: usize) -> usize { - var buf: [path.len + 1]u8 = undefined; + const buf = @alloca(u8, path.len + 1); @memcpy(&buf[0], &path[0], path.len); buf[path.len] = 0; return open_c(buf.ptr, flags, perm); diff --git a/std/darwin_x86_64.zig b/std/darwin_x86_64.zig index 742f937978..ea6cc5a37a 100644 --- a/std/darwin_x86_64.zig +++ b/std/darwin_x86_64.zig @@ -11,6 +11,7 @@ pub const SYSCALL_CLASS_DIAG = 4; // Diagnostics // TODO: use the above constants to create the below values +pub const SYS_exit = 0x2000001; pub const SYS_read = 0x2000003; pub const SYS_write = 0x2000004; pub const SYS_open = 0x2000005;