Skip to content

Commit 65b03db

Browse files
committed
extract posix functions from std/os.zig to std/os/posix.zig
See #2380
1 parent b660134 commit 65b03db

20 files changed

+2726
-2493
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,9 @@ set(ZIG_STD_FILES
621621
"os/time.zig"
622622
"os/uefi.zig"
623623
"os/wasi.zig"
624-
"os/wasi/core.zig"
625624
"os/windows.zig"
626625
"os/windows/advapi32.zig"
626+
"os/windows/errno.zig"
627627
"os/windows/error.zig"
628628
"os/windows/kernel32.zig"
629629
"os/windows/ntdll.zig"

doc/langref.html.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ const std = @import("std");
195195

196196
pub fn main() !void {
197197
// If this program is run without stdout attached, exit with an error.
198-
const stdout_file = try std.io.getStdOut();
198+
const stdout_file = try std.os.File.stdout();
199199
// If this program encounters pipe failure when printing to stdout, exit
200200
// with an error.
201201
try stdout_file.write("Hello, world!\n");

example/hello_world/hello.zig

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const std = @import("std");
22

3-
pub fn main() !void {
4-
// If this program is run without stdout attached, exit with an error.
5-
const stdout_file = try std.io.getStdOut();
6-
// If this program encounters pipe failure when printing to stdout, exit
7-
// with an error.
8-
try stdout_file.write("Hello, world!\n");
3+
pub fn main() void {
4+
std.debug.warn("Hello, world!\n");
95
}

example/hello_world/hello_libc.zig

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ const c = @cImport({
22
// See https://github.com/ziglang/zig/issues/515
33
@cDefine("_NO_CRT_STDIO_INLINE", "1");
44
@cInclude("stdio.h");
5-
@cInclude("string.h");
65
});
76

8-
const msg = c"Hello, world!\n";
9-
10-
export fn main(argc: c_int, argv: **u8) c_int {
11-
if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;
12-
7+
export fn main(argc: c_int, argv: [*]?[*]u8) c_int {
8+
c.fprintf(c.stderr, c"Hello, world!\n");
139
return 0;
1410
}

std/c.zig

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
const builtin = @import("builtin");
2-
const Os = builtin.Os;
2+
3+
pub const is_the_target = builtin.link_libc;
34

45
pub use switch (builtin.os) {
5-
Os.linux => @import("c/linux.zig"),
6-
Os.windows => @import("c/windows.zig"),
7-
Os.macosx, Os.ios => @import("c/darwin.zig"),
8-
Os.freebsd => @import("c/freebsd.zig"),
9-
Os.netbsd => @import("c/netbsd.zig"),
6+
.linux => @import("c/linux.zig"),
7+
.windows => @import("c/windows.zig"),
8+
.macosx, .ios, .tvos, .watchos => @import("c/darwin.zig"),
9+
.freebsd => @import("c/freebsd.zig"),
10+
.netbsd => @import("c/netbsd.zig"),
1011
else => struct {},
1112
};
1213

14+
pub fn getErrno(rc: var) u12 {
15+
if (rc == -1) {
16+
return @intCast(u12, _errno().*);
17+
} else {
18+
return 0;
19+
}
20+
}
21+
1322
// TODO https://github.com/ziglang/zig/issues/265 on this whole file
1423

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

6070
pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
6171
pub extern "c" fn malloc(usize) ?*c_void;

std/event/net.zig

+1-8
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,7 @@ pub const Server = struct {
8989
},
9090
};
9191
} else |err| switch (err) {
92-
error.ProcessFdQuotaExceeded => {
93-
errdefer os.emfile_promise_queue.remove(&self.waiting_for_emfile_node);
94-
suspend {
95-
self.waiting_for_emfile_node = PromiseNode.init(@handle());
96-
os.emfile_promise_queue.append(&self.waiting_for_emfile_node);
97-
}
98-
continue;
99-
},
92+
error.ProcessFdQuotaExceeded => @panic("TODO handle this error"),
10093
error.ConnectionAborted => continue,
10194

10295
error.FileDescriptorNotASocket => unreachable,

std/io.zig

+12-25
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ const testing = std.testing;
1818
const is_posix = builtin.os != builtin.Os.windows;
1919
const is_windows = builtin.os == builtin.Os.windows;
2020

21-
const GetStdIoErrs = os.WindowsGetStdHandleErrs;
22-
23-
pub fn getStdErr() GetStdIoErrs!File {
24-
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_ERROR_HANDLE) else if (is_posix) os.posix.STDERR_FILENO else unreachable;
25-
return File.openHandle(handle);
26-
}
27-
28-
pub fn getStdOut() GetStdIoErrs!File {
29-
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE) else if (is_posix) os.posix.STDOUT_FILENO else unreachable;
30-
return File.openHandle(handle);
31-
}
32-
33-
pub fn getStdIn() GetStdIoErrs!File {
34-
const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_INPUT_HANDLE) else if (is_posix) os.posix.STDIN_FILENO else unreachable;
35-
return File.openHandle(handle);
36-
}
37-
3821
pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream;
3922
pub const SliceSeekableInStream = @import("io/seekable_stream.zig").SliceSeekableInStream;
4023
pub const COutStream = @import("io/c_out_stream.zig").COutStream;
@@ -1116,10 +1099,12 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
11161099
pub const Stream = InStream(Error);
11171100

11181101
pub fn init(in_stream: *Stream) Self {
1119-
return Self{ .in_stream = switch (packing) {
1120-
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
1121-
.Byte => in_stream,
1122-
} };
1102+
return Self{
1103+
.in_stream = switch (packing) {
1104+
.Bit => BitInStream(endian, Stream.Error).init(in_stream),
1105+
.Byte => in_stream,
1106+
},
1107+
};
11231108
}
11241109

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

13271312
pub fn init(out_stream: *Stream) Self {
1328-
return Self{ .out_stream = switch (packing) {
1329-
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
1330-
.Byte => out_stream,
1331-
} };
1313+
return Self{
1314+
.out_stream = switch (packing) {
1315+
.Bit => BitOutStream(endian, Stream.Error).init(out_stream),
1316+
.Byte => out_stream,
1317+
},
1318+
};
13321319
}
13331320

13341321
/// Flushes any unwritten bits to the stream

0 commit comments

Comments
 (0)