authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-23 16:13:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-23 16:13:57-05:00
logfe3063e58cf81287a26219903f2720439dfeb7e6
treed07f638d136f35c276c58ac8259cdb84d383ddfb
parentb1ace32f2373914d92459934b9f0697baca9fe83

update std code for MacOS

closes #264

3 files changed, 15 insertions(+), 6 deletions(-)

std/bootstrap.zig+8-5
......@@ -2,8 +2,6 @@
22
33const root = @import("@root");
44const std = @import("std");
5const linux = std.linux;
6const cstr = std.cstr;
75
86const want_start_symbol = switch(@compileVar("os")) {
97 Os.linux => true,
......@@ -11,6 +9,11 @@ const want_start_symbol = switch(@compileVar("os")) {
119};
1210const want_main_symbol = !want_start_symbol;
1311
12const exit = switch(@compileVar("os")) {
13 Os.linux => std.linux.exit,
14 Os.darwin => std.darwin.exit,
15};
16
1417var argc: usize = undefined;
1518var argv: &&u8 = undefined;
1619
......@@ -35,14 +38,14 @@ fn callMain() -> %void {
3538 const args = @alloca([]u8, argc);
3639 for (args) |_, i| {
3740 const ptr = argv[i];
38 args[i] = ptr[0...cstr.len(ptr)];
41 args[i] = ptr[0...std.cstr.len(ptr)];
3942 }
4043 return root.main(args);
4144}
4245
4346fn callMainAndExit() -> unreachable {
44 callMain() %% linux.exit(1);
45 linux.exit(0);
47 callMain() %% exit(1);
48 exit(0);
4649}
4750
4851export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
std/darwin.zig+6-1
......@@ -48,6 +48,11 @@ pub const SIGPWR = 30;
4848pub const SIGSYS = 31;
4949pub const SIGUNUSED = SIGSYS;
5050
51pub fn exit(status: usize) -> unreachable {
52 arch.syscall1(arch.SYS_exit, status);
53 @unreachable()
54}
55
5156/// Get the errno from a syscall return value, or 0 for no error.
5257pub fn getErrno(r: usize) -> usize {
5358 const signed_r = *(&isize)(&r);
......@@ -67,7 +72,7 @@ pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize {
6772}
6873
6974pub fn open(path: []const u8, flags: usize, perm: usize) -> usize {
70 var buf: [path.len + 1]u8 = undefined;
75 const buf = @alloca(u8, path.len + 1);
7176 @memcpy(&buf[0], &path[0], path.len);
7277 buf[path.len] = 0;
7378 return open_c(buf.ptr, flags, perm);
std/darwin_x86_64.zig+1
......@@ -11,6 +11,7 @@ pub const SYSCALL_CLASS_DIAG = 4; // Diagnostics
1111
1212// TODO: use the above constants to create the below values
1313
14pub const SYS_exit = 0x2000001;
1415pub const SYS_read = 0x2000003;
1516pub const SYS_write = 0x2000004;
1617pub const SYS_open = 0x2000005;