Commit 5210b9074c

Žiga Željko <ziga.zeljko@gmail.com>
2022-01-31 16:12:50
os,wasi: use wasi-libc if available
1 parent 66cf011
Changed files (2)
lib
lib/std/c/wasi.zig
@@ -8,6 +8,15 @@ pub fn _errno() *c_int {
     return &errno;
 }
 
+pub const AT = wasi.AT;
+pub const CLOCK = wasi.CLOCK;
+pub const E = wasi.E;
+pub const IOV_MAX = wasi.IOV_MAX;
+pub const LOCK = wasi.LOCK;
+pub const S = wasi.S;
+pub const STDERR_FILENO = wasi.STDERR_FILENO;
+pub const STDIN_FILENO = wasi.STDIN_FILENO;
+pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
 pub const fd_t = wasi.fd_t;
 pub const pid_t = c_int;
 pub const uid_t = u32;
@@ -17,14 +26,6 @@ pub const ino_t = wasi.ino_t;
 pub const mode_t = wasi.mode_t;
 pub const time_t = wasi.time_t;
 pub const timespec = wasi.timespec;
-pub const STDERR_FILENO = wasi.STDERR_FILENO;
-pub const STDIN_FILENO = wasi.STDIN_FILENO;
-pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
-pub const E = wasi.E;
-pub const CLOCK = wasi.CLOCK;
-pub const S = wasi.S;
-pub const IOV_MAX = wasi.IOV_MAX;
-pub const AT = wasi.AT;
 
 pub const Stat = extern struct {
     dev: i32,
@@ -93,8 +94,40 @@ pub const O = struct {
     pub const WRONLY = (0x10000000);
 };
 
+pub const F = struct {
+    pub const GETFD = 1;
+    pub const SETFD = 2;
+    pub const GETFL = 3;
+    pub const SETFL = 4;
+};
+
+pub const FD_CLOEXEC = 1;
+
+pub const F_OK = 0;
+pub const X_OK = 1;
+pub const W_OK = 2;
+pub const R_OK = 4;
+
 pub const SEEK = struct {
     pub const SET: wasi.whence_t = .SET;
     pub const CUR: wasi.whence_t = .CUR;
     pub const END: wasi.whence_t = .END;
 };
+
+pub const nfds_t = usize;
+
+pub const pollfd = extern struct {
+    fd: fd_t,
+    events: i16,
+    revents: i16,
+};
+
+pub const POLL = struct {
+    pub const RDNORM = 0x1;
+    pub const WRNORM = 0x2;
+    pub const IN = RDNORM;
+    pub const OUT = WRNORM;
+    pub const ERR = 0x1000;
+    pub const HUP = 0x2000;
+    pub const NVAL = 0x4000;
+};
lib/std/os.zig
@@ -238,7 +238,7 @@ pub fn close(fd: fd_t) void {
     if (builtin.os.tag == .windows) {
         return windows.CloseHandle(fd);
     }
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         _ = wasi.fd_close(fd);
         return;
     }
@@ -1395,7 +1395,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t
 /// `file_path` is relative to the open directory handle `dir_fd`.
 /// See also `openatZ`.
 pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("use openatWasi instead");
     }
     if (builtin.os.tag == .windows) {
@@ -1760,7 +1760,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
     if (builtin.os.tag == .windows) {
         return windows.GetCurrentDirectory(out_buffer);
     }
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
     }
 
@@ -1804,7 +1804,7 @@ pub const SymLinkError = error{
 /// If `sym_link_path` exists, it will not be overwritten.
 /// See also `symlinkZ.
 pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("symlink is not supported in WASI; use symlinkat instead");
     }
     if (builtin.os.tag == .windows) {
@@ -2021,7 +2021,7 @@ pub const UnlinkError = error{
 /// Delete a name and possibly the file it refers to.
 /// See also `unlinkZ`.
 pub fn unlink(file_path: []const u8) UnlinkError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("unlink is not supported in WASI; use unlinkat instead");
     } else if (builtin.os.tag == .windows) {
         const file_path_w = try windows.sliceToPrefixedFileW(file_path);
@@ -2175,7 +2175,7 @@ pub const RenameError = error{
 
 /// Change the name or location of a file.
 pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("rename is not supported in WASI; use renameat instead");
     } else if (builtin.os.tag == .windows) {
         const old_path_w = try windows.sliceToPrefixedFileW(old_path);
@@ -2469,7 +2469,7 @@ pub const MakeDirError = error{
 /// Create a directory.
 /// `mode` is ignored on Windows.
 pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("mkdir is not supported in WASI; use mkdirat instead");
     } else if (builtin.os.tag == .windows) {
         const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
@@ -2539,7 +2539,7 @@ pub const DeleteDirError = error{
 
 /// Deletes an empty directory.
 pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("rmdir is not supported in WASI; use unlinkat instead");
     } else if (builtin.os.tag == .windows) {
         const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
@@ -2600,7 +2600,7 @@ pub const ChangeCurDirError = error{
 /// Changes the current working directory of the calling process.
 /// `dir_path` is recommended to be a UTF-8 encoded string.
 pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("chdir is not supported in WASI");
     } else if (builtin.os.tag == .windows) {
         var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
@@ -2684,7 +2684,7 @@ pub const ReadLinkError = error{
 /// Read value of a symbolic link.
 /// The return value is a slice of `out_buffer` from index 0.
 pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("readlink is not supported in WASI; use readlinkat instead");
     } else if (builtin.os.tag == .windows) {
         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
         const pathname_w = try windows.sliceToPrefixedFileW(pathname);
         return realpathW(pathname_w.span(), out_buffer);
     }
-    if (builtin.os.tag == .wasi) {
+    if (builtin.os.tag == .wasi and !builtin.link_libc) {
         @compileError("Use std.fs.wasi.PreopenList to obtain valid Dir handles instead of using absolute paths");
     }
     const pathname_c = try toPosixPath(pathname);