Skip to content

Commit

Permalink
extract posix functions from std/os.zig to std/os/posix.zig
Browse files Browse the repository at this point in the history
See #2380
  • Loading branch information
andrewrk committed May 19, 2019
1 parent b660134 commit 65b03db
Show file tree
Hide file tree
Showing 20 changed files with 2,726 additions and 2,493 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ set(ZIG_STD_FILES
"os/time.zig"
"os/uefi.zig"
"os/wasi.zig"
"os/wasi/core.zig"
"os/windows.zig"
"os/windows/advapi32.zig"
"os/windows/errno.zig"
"os/windows/error.zig"
"os/windows/kernel32.zig"
"os/windows/ntdll.zig"
Expand Down
2 changes: 1 addition & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const std = @import("std");

pub fn main() !void {
// If this program is run without stdout attached, exit with an error.
const stdout_file = try std.io.getStdOut();
const stdout_file = try std.os.File.stdout();
// If this program encounters pipe failure when printing to stdout, exit
// with an error.
try stdout_file.write("Hello, world!\n");
Expand Down
8 changes: 2 additions & 6 deletions example/hello_world/hello.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const std = @import("std");

pub fn main() !void {
// If this program is run without stdout attached, exit with an error.
const stdout_file = try std.io.getStdOut();
// If this program encounters pipe failure when printing to stdout, exit
// with an error.
try stdout_file.write("Hello, world!\n");
pub fn main() void {
std.debug.warn("Hello, world!\n");
}
8 changes: 2 additions & 6 deletions example/hello_world/hello_libc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ const c = @cImport({
// See https://github.com/ziglang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("stdio.h");
@cInclude("string.h");
});

const msg = c"Hello, world!\n";

export fn main(argc: c_int, argv: **u8) c_int {
if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;

export fn main(argc: c_int, argv: [*]?[*]u8) c_int {
c.fprintf(c.stderr, c"Hello, world!\n");
return 0;
}
22 changes: 16 additions & 6 deletions std/c.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const builtin = @import("builtin");
const Os = builtin.Os;

pub const is_the_target = builtin.link_libc;

pub use switch (builtin.os) {
Os.linux => @import("c/linux.zig"),
Os.windows => @import("c/windows.zig"),
Os.macosx, Os.ios => @import("c/darwin.zig"),
Os.freebsd => @import("c/freebsd.zig"),
Os.netbsd => @import("c/netbsd.zig"),
.linux => @import("c/linux.zig"),
.windows => @import("c/windows.zig"),
.macosx, .ios, .tvos, .watchos => @import("c/darwin.zig"),
.freebsd => @import("c/freebsd.zig"),
.netbsd => @import("c/netbsd.zig"),
else => struct {},
};

pub fn getErrno(rc: var) u12 {
if (rc == -1) {
return @intCast(u12, _errno().*);
} else {
return 0;
}
}

// TODO https://github.com/ziglang/zig/issues/265 on this whole file

pub const FILE = @OpaqueType();
Expand Down Expand Up @@ -56,6 +65,7 @@ pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
pub extern "c" fn rmdir(path: [*]const u8) c_int;
pub extern "c" fn getenv(name: [*]const u8) ?[*]u8;

pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
pub extern "c" fn malloc(usize) ?*c_void;
Expand Down
9 changes: 1 addition & 8 deletions std/event/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ pub const Server = struct {
},
};
} else |err| switch (err) {
error.ProcessFdQuotaExceeded => {
errdefer os.emfile_promise_queue.remove(&self.waiting_for_emfile_node);
suspend {
self.waiting_for_emfile_node = PromiseNode.init(@handle());
os.emfile_promise_queue.append(&self.waiting_for_emfile_node);
}
continue;
},
error.ProcessFdQuotaExceeded => @panic("TODO handle this error"),
error.ConnectionAborted => continue,

error.FileDescriptorNotASocket => unreachable,
Expand Down
37 changes: 12 additions & 25 deletions std/io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ const testing = std.testing;
const is_posix = builtin.os != builtin.Os.windows;
const is_windows = builtin.os == builtin.Os.windows;

const GetStdIoErrs = os.WindowsGetStdHandleErrs;

pub fn getStdErr() GetStdIoErrs!File {
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_ERROR_HANDLE) else if (is_posix) os.posix.STDERR_FILENO else unreachable;
return File.openHandle(handle);
}

pub fn getStdOut() GetStdIoErrs!File {
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE) else if (is_posix) os.posix.STDOUT_FILENO else unreachable;
return File.openHandle(handle);
}

pub fn getStdIn() GetStdIoErrs!File {
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_INPUT_HANDLE) else if (is_posix) os.posix.STDIN_FILENO else unreachable;
return File.openHandle(handle);
}

pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream;
pub const SliceSeekableInStream = @import("io/seekable_stream.zig").SliceSeekableInStream;
pub const COutStream = @import("io/c_out_stream.zig").COutStream;
Expand Down Expand Up @@ -1116,10 +1099,12 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
pub const Stream = InStream(Error);

pub fn init(in_stream: *Stream) Self {
return Self{ .in_stream = switch (packing) {
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
.Byte => in_stream,
} };
return Self{
.in_stream = switch (packing) {
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
.Byte => in_stream,
},
};
}

pub fn alignToByte(self: *Self) void {
Expand Down Expand Up @@ -1325,10 +1310,12 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
pub const Stream = OutStream(Error);

pub fn init(out_stream: *Stream) Self {
return Self{ .out_stream = switch (packing) {
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
.Byte => out_stream,
} };
return Self{
.out_stream = switch (packing) {
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
.Byte => out_stream,
},
};
}

/// Flushes any unwritten bits to the stream
Expand Down
Loading

0 comments on commit 65b03db

Please sign in to comment.