update std code for MacOS

closes #264
This commit is contained in:
Andrew Kelley 2017-02-23 16:13:57 -05:00
parent b1ace32f23
commit fe3063e58c
3 changed files with 15 additions and 6 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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;