| ... | ... | @@ -18,6 +18,43 @@ pub fn getErrno(r: usize) E { |
| 18 | 18 | const int = if (signed_r > -4096 and signed_r < 0) -signed_r else 0; |
| 19 | 19 | return @as(E, @enumFromInt(int)); |
| 20 | 20 | } |
| 21 | // The max bytes that can be in the errstr buff |
| 22 | pub const ERRMAX = 128; |
| 23 | var errstr_buf: [ERRMAX]u8 = undefined; |
| 24 | /// Gets whatever the last errstr was |
| 25 | pub fn errstr() []const u8 { |
| 26 | _ = syscall_bits.syscall2(.ERRSTR, @intFromPtr(&errstr_buf), ERRMAX); |
| 27 | return std.mem.span(@as([*:0]u8, @ptrCast(&errstr_buf))); |
| 28 | } |
| 29 | pub const Plink = anyopaque; |
| 30 | pub const Tos = extern struct { |
| 31 | /// Per process profiling |
| 32 | prof: extern struct { |
| 33 | /// known to be 0(ptr) |
| 34 | pp: *Plink, |
| 35 | /// known to be 4(ptr) |
| 36 | next: *Plink, |
| 37 | last: *Plink, |
| 38 | first: *Plink, |
| 39 | pid: u32, |
| 40 | what: u32, |
| 41 | }, |
| 42 | /// cycle clock frequency if there is one, 0 otherwise |
| 43 | cyclefreq: u64, |
| 44 | /// cycles spent in kernel |
| 45 | kcycles: i64, |
| 46 | /// cycles spent in process (kernel + user) |
| 47 | pcycles: i64, |
| 48 | /// might as well put the pid here |
| 49 | pid: u32, |
| 50 | clock: u32, |
| 51 | // top of stack is here |
| 52 | }; |
| 53 | |
| 54 | pub var tos: *Tos = undefined; // set in start.zig |
| 55 | pub fn getpid() u32 { |
| 56 | return tos.pid; |
| 57 | } |
| 21 | 58 | pub const SIG = struct { |
| 22 | 59 | /// hangup |
| 23 | 60 | pub const HUP = 1; |
| ... | ... | @@ -143,26 +180,27 @@ pub const SYS = enum(usize) { |
| 143 | 180 | }; |
| 144 | 181 | |
| 145 | 182 | pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { |
| 146 | | return syscall_bits.syscall3(._WRITE, @bitCast(@as(isize, fd)), @intFromPtr(buf), count); |
| 183 | return syscall_bits.syscall4(.PWRITE, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, @bitCast(@as(isize, -1))); |
| 147 | 184 | } |
| 148 | | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { |
| 149 | | return syscall_bits.syscall4(.PWRITE, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, offset); |
| 185 | pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: isize) usize { |
| 186 | return syscall_bits.syscall4(.PWRITE, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, @bitCast(offset)); |
| 150 | 187 | } |
| 151 | 188 | |
| 152 | 189 | pub fn read(fd: i32, buf: [*]const u8, count: usize) usize { |
| 153 | | return syscall_bits.syscall3(._READ, @bitCast(@as(isize, fd)), @intFromPtr(buf), count); |
| 190 | return syscall_bits.syscall4(.PREAD, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, @bitCast(@as(isize, -1))); |
| 154 | 191 | } |
| 155 | | pub fn pread(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { |
| 156 | | return syscall_bits.syscall4(.PREAD, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, offset); |
| 192 | pub fn pread(fd: i32, buf: [*]const u8, count: usize, offset: isize) usize { |
| 193 | return syscall_bits.syscall4(.PREAD, @bitCast(@as(isize, fd)), @intFromPtr(buf), count, @bitCast(offset)); |
| 157 | 194 | } |
| 158 | 195 | |
| 159 | | pub fn open(path: [*:0]const u8, omode: mode_t) usize { |
| 160 | | return syscall_bits.syscall2(.OPEN, @intFromPtr(path), @bitCast(@as(isize, omode))); |
| 196 | pub fn open(path: [*:0]const u8, flags: u32) usize { |
| 197 | return syscall_bits.syscall2(.OPEN, @intFromPtr(path), @bitCast(@as(isize, flags))); |
| 161 | 198 | } |
| 162 | 199 | |
| 163 | | pub fn openat(dirfd: i32, path: [*:0]const u8, _: u32, omode: mode_t) usize { |
| 200 | pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, _: mode_t) usize { |
| 201 | // we skip perms because only create supports perms |
| 164 | 202 | if (dirfd == AT.FDCWD) { // openat(AT_FDCWD, ...) == open(...) |
| 165 | | return open(path, omode); |
| 203 | return open(path, flags); |
| 166 | 204 | } |
| 167 | 205 | var dir_path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| 168 | 206 | var total_path_buf: [std.fs.MAX_PATH_BYTES + 1]u8 = undefined; |
| ... | ... | @@ -174,7 +212,7 @@ pub fn openat(dirfd: i32, path: [*:0]const u8, _: u32, omode: mode_t) usize { |
| 174 | 212 | const total_path = std.fs.path.join(alloc, &.{ dir_path, std.mem.span(path) }) catch unreachable; // the allocation shouldn't fail because it should not exceed MAX_PATH_BYTES |
| 175 | 213 | fba.reset(); |
| 176 | 214 | const total_path_z = alloc.dupeZ(u8, total_path) catch unreachable; // should not exceed MAX_PATH_BYTES + 1 |
| 177 | | return open(total_path_z.ptr, omode); |
| 215 | return open(total_path_z.ptr, flags); |
| 178 | 216 | } |
| 179 | 217 | |
| 180 | 218 | pub fn fd2path(fd: i32, buf: [*]u8, nbuf: usize) usize { |