diff options
| author | Marc Tiehuis <marctiehuis@gmail.com> | 2017-09-10 05:48:44 +1200 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-09-09 13:48:44 -0400 |
| commit | 67a31befa672890e23ef530fa4376a787edff892 (patch) | |
| tree | f05e45ad3628acfb469fa9f2c4d43a64c894088f /std/os | |
| parent | 850a1d205484055cc2dd194370fb54631b915528 (diff) | |
| download | zig-67a31befa672890e23ef530fa4376a787edff892.tar.gz zig-67a31befa672890e23ef530fa4376a787edff892.zip | |
Add exit function (#450)
Diffstat (limited to 'std/os')
| -rw-r--r-- | std/os/index.zig | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/std/os/index.zig b/std/os/index.zig index 2912be2cc8..1a5e98ea9f 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -112,6 +112,30 @@ pub coldcc fn abort() -> noreturn { } } +/// Exits the program cleanly with the specified status code. +pub coldcc fn exit(status: i32) -> noreturn { + if (builtin.link_libc) { + c.exit(status); + } + switch (builtin.os) { + Os.linux, Os.darwin, Os.macosx, Os.ios => { + posix.exit(status) + }, + Os.windows => { + // Map a possibly negative status code to a non-negative status for the systems default + // integer width. + const p_status = if (@sizeOf(c_uint) < @sizeOf(u32)) { + @truncate(c_uint, @bitCast(u32, status)) + } else { + c_uint(@bitCast(u32, status)) + }; + + windows.ExitProcess(p_status) + }, + else => @compileError("Unsupported OS"), + } +} + /// Calls POSIX close, and keeps trying if it gets interrupted. pub fn posixClose(fd: i32) { while (true) { |
