authorgravatar for ziga.zeljko@gmail.comŽiga Željko <ziga.zeljko@gmail.com> 2022-01-31 16:12:50+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-31 22:54:30+01:00
log5210b9074ca68c23da474b0afcaa4ce11f7cc57c
treeade7a233e0e07c129901ae00115a632c1c2c4296
parent66cf011aa91a66fd8b1489d186152522ccd27550

os,wasi: use wasi-libc if available


2 files changed, 52 insertions(+), 19 deletions(-)

lib/std/c/wasi.zig+41-8
...@@ -8,6 +8,15 @@ pub fn _errno() *c_int {...@@ -8,6 +8,15 @@ pub fn _errno() *c_int {
8 return &errno;8 return &errno;
9}9}
1010
11pub const AT = wasi.AT;
12pub const CLOCK = wasi.CLOCK;
13pub const E = wasi.E;
14pub const IOV_MAX = wasi.IOV_MAX;
15pub const LOCK = wasi.LOCK;
16pub const S = wasi.S;
17pub const STDERR_FILENO = wasi.STDERR_FILENO;
18pub const STDIN_FILENO = wasi.STDIN_FILENO;
19pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
11pub const fd_t = wasi.fd_t;20pub const fd_t = wasi.fd_t;
12pub const pid_t = c_int;21pub const pid_t = c_int;
13pub const uid_t = u32;22pub const uid_t = u32;
...@@ -17,14 +26,6 @@ pub const ino_t = wasi.ino_t;...@@ -17,14 +26,6 @@ pub const ino_t = wasi.ino_t;
17pub const mode_t = wasi.mode_t;26pub const mode_t = wasi.mode_t;
18pub const time_t = wasi.time_t;27pub const time_t = wasi.time_t;
19pub const timespec = wasi.timespec;28pub const timespec = wasi.timespec;
20pub const STDERR_FILENO = wasi.STDERR_FILENO;
21pub const STDIN_FILENO = wasi.STDIN_FILENO;
22pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
23pub const E = wasi.E;
24pub const CLOCK = wasi.CLOCK;
25pub const S = wasi.S;
26pub const IOV_MAX = wasi.IOV_MAX;
27pub const AT = wasi.AT;
2829
29pub const Stat = extern struct {30pub const Stat = extern struct {
30 dev: i32,31 dev: i32,
...@@ -93,8 +94,40 @@ pub const O = struct {...@@ -93,8 +94,40 @@ pub const O = struct {
93 pub const WRONLY = (0x10000000);94 pub const WRONLY = (0x10000000);
94};95};
9596
97pub const F = struct {
98 pub const GETFD = 1;
99 pub const SETFD = 2;
100 pub const GETFL = 3;
101 pub const SETFL = 4;
102};
103
104pub const FD_CLOEXEC = 1;
105
106pub const F_OK = 0;
107pub const X_OK = 1;
108pub const W_OK = 2;
109pub const R_OK = 4;
110
96pub const SEEK = struct {111pub const SEEK = struct {
97 pub const SET: wasi.whence_t = .SET;112 pub const SET: wasi.whence_t = .SET;
98 pub const CUR: wasi.whence_t = .CUR;113 pub const CUR: wasi.whence_t = .CUR;
99 pub const END: wasi.whence_t = .END;114 pub const END: wasi.whence_t = .END;
100};115};
116
117pub const nfds_t = usize;
118
119pub const pollfd = extern struct {
120 fd: fd_t,
121 events: i16,
122 revents: i16,
123};
124
125pub const POLL = struct {
126 pub const RDNORM = 0x1;
127 pub const WRNORM = 0x2;
128 pub const IN = RDNORM;
129 pub const OUT = WRNORM;
130 pub const ERR = 0x1000;
131 pub const HUP = 0x2000;
132 pub const NVAL = 0x4000;
133};
lib/std/os.zig+11-11
...@@ -238,7 +238,7 @@ pub fn close(fd: fd_t) void {...@@ -238,7 +238,7 @@ pub fn close(fd: fd_t) void {
238 if (builtin.os.tag == .windows) {238 if (builtin.os.tag == .windows) {
239 return windows.CloseHandle(fd);239 return windows.CloseHandle(fd);
240 }240 }
241 if (builtin.os.tag == .wasi) {241 if (builtin.os.tag == .wasi and !builtin.link_libc) {
242 _ = wasi.fd_close(fd);242 _ = wasi.fd_close(fd);
243 return;243 return;
244 }244 }
...@@ -1395,7 +1395,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t...@@ -1395,7 +1395,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t
1395/// `file_path` is relative to the open directory handle `dir_fd`.1395/// `file_path` is relative to the open directory handle `dir_fd`.
1396/// See also `openatZ`.1396/// See also `openatZ`.
1397pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t {1397pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t {
1398 if (builtin.os.tag == .wasi) {1398 if (builtin.os.tag == .wasi and !builtin.link_libc) {
1399 @compileError("use openatWasi instead");1399 @compileError("use openatWasi instead");
1400 }1400 }
1401 if (builtin.os.tag == .windows) {1401 if (builtin.os.tag == .windows) {
...@@ -1760,7 +1760,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {...@@ -1760,7 +1760,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
1760 if (builtin.os.tag == .windows) {1760 if (builtin.os.tag == .windows) {
1761 return windows.GetCurrentDirectory(out_buffer);1761 return windows.GetCurrentDirectory(out_buffer);
1762 }1762 }
1763 if (builtin.os.tag == .wasi) {1763 if (builtin.os.tag == .wasi and !builtin.link_libc) {
1764 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");1764 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
1765 }1765 }
17661766
...@@ -1804,7 +1804,7 @@ pub const SymLinkError = error{...@@ -1804,7 +1804,7 @@ pub const SymLinkError = error{
1804/// If `sym_link_path` exists, it will not be overwritten.1804/// If `sym_link_path` exists, it will not be overwritten.
1805/// See also `symlinkZ.1805/// See also `symlinkZ.
1806pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {1806pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
1807 if (builtin.os.tag == .wasi) {1807 if (builtin.os.tag == .wasi and !builtin.link_libc) {
1808 @compileError("symlink is not supported in WASI; use symlinkat instead");1808 @compileError("symlink is not supported in WASI; use symlinkat instead");
1809 }1809 }
1810 if (builtin.os.tag == .windows) {1810 if (builtin.os.tag == .windows) {
...@@ -2021,7 +2021,7 @@ pub const UnlinkError = error{...@@ -2021,7 +2021,7 @@ pub const UnlinkError = error{
2021/// Delete a name and possibly the file it refers to.2021/// Delete a name and possibly the file it refers to.
2022/// See also `unlinkZ`.2022/// See also `unlinkZ`.
2023pub fn unlink(file_path: []const u8) UnlinkError!void {2023pub fn unlink(file_path: []const u8) UnlinkError!void {
2024 if (builtin.os.tag == .wasi) {2024 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2025 @compileError("unlink is not supported in WASI; use unlinkat instead");2025 @compileError("unlink is not supported in WASI; use unlinkat instead");
2026 } else if (builtin.os.tag == .windows) {2026 } else if (builtin.os.tag == .windows) {
2027 const file_path_w = try windows.sliceToPrefixedFileW(file_path);2027 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
...@@ -2175,7 +2175,7 @@ pub const RenameError = error{...@@ -2175,7 +2175,7 @@ pub const RenameError = error{
21752175
2176/// Change the name or location of a file.2176/// Change the name or location of a file.
2177pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {2177pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
2178 if (builtin.os.tag == .wasi) {2178 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2179 @compileError("rename is not supported in WASI; use renameat instead");2179 @compileError("rename is not supported in WASI; use renameat instead");
2180 } else if (builtin.os.tag == .windows) {2180 } else if (builtin.os.tag == .windows) {
2181 const old_path_w = try windows.sliceToPrefixedFileW(old_path);2181 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
...@@ -2469,7 +2469,7 @@ pub const MakeDirError = error{...@@ -2469,7 +2469,7 @@ pub const MakeDirError = error{
2469/// Create a directory.2469/// Create a directory.
2470/// `mode` is ignored on Windows.2470/// `mode` is ignored on Windows.
2471pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {2471pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
2472 if (builtin.os.tag == .wasi) {2472 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2473 @compileError("mkdir is not supported in WASI; use mkdirat instead");2473 @compileError("mkdir is not supported in WASI; use mkdirat instead");
2474 } else if (builtin.os.tag == .windows) {2474 } else if (builtin.os.tag == .windows) {
2475 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);2475 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
...@@ -2539,7 +2539,7 @@ pub const DeleteDirError = error{...@@ -2539,7 +2539,7 @@ pub const DeleteDirError = error{
25392539
2540/// Deletes an empty directory.2540/// Deletes an empty directory.
2541pub fn rmdir(dir_path: []const u8) DeleteDirError!void {2541pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
2542 if (builtin.os.tag == .wasi) {2542 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2543 @compileError("rmdir is not supported in WASI; use unlinkat instead");2543 @compileError("rmdir is not supported in WASI; use unlinkat instead");
2544 } else if (builtin.os.tag == .windows) {2544 } else if (builtin.os.tag == .windows) {
2545 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);2545 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
...@@ -2600,7 +2600,7 @@ pub const ChangeCurDirError = error{...@@ -2600,7 +2600,7 @@ pub const ChangeCurDirError = error{
2600/// Changes the current working directory of the calling process.2600/// Changes the current working directory of the calling process.
2601/// `dir_path` is recommended to be a UTF-8 encoded string.2601/// `dir_path` is recommended to be a UTF-8 encoded string.
2602pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {2602pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
2603 if (builtin.os.tag == .wasi) {2603 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2604 @compileError("chdir is not supported in WASI");2604 @compileError("chdir is not supported in WASI");
2605 } else if (builtin.os.tag == .windows) {2605 } else if (builtin.os.tag == .windows) {
2606 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;2606 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
...@@ -2684,7 +2684,7 @@ pub const ReadLinkError = error{...@@ -2684,7 +2684,7 @@ pub const ReadLinkError = error{
2684/// Read value of a symbolic link.2684/// Read value of a symbolic link.
2685/// The return value is a slice of `out_buffer` from index 0.2685/// The return value is a slice of `out_buffer` from index 0.
2686pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {2686pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
2687 if (builtin.os.tag == .wasi) {2687 if (builtin.os.tag == .wasi and !builtin.link_libc) {
2688 @compileError("readlink is not supported in WASI; use readlinkat instead");2688 @compileError("readlink is not supported in WASI; use readlinkat instead");
2689 } else if (builtin.os.tag == .windows) {2689 } else if (builtin.os.tag == .windows) {
2690 const file_path_w = try windows.sliceToPrefixedFileW(file_path);2690 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
...@@ -4631,7 +4631,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE...@@ -4631,7 +4631,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
4631 const pathname_w = try windows.sliceToPrefixedFileW(pathname);4631 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
4632 return realpathW(pathname_w.span(), out_buffer);4632 return realpathW(pathname_w.span(), out_buffer);
4633 }4633 }
4634 if (builtin.os.tag == .wasi) {4634 if (builtin.os.tag == .wasi and !builtin.link_libc) {
4635 @compileError("Use std.fs.wasi.PreopenList to obtain valid Dir handles instead of using absolute paths");4635 @compileError("Use std.fs.wasi.PreopenList to obtain valid Dir handles instead of using absolute paths");
4636 }4636 }
4637 const pathname_c = try toPosixPath(pathname);4637 const pathname_c = try toPosixPath(pathname);