diff options
| author | Samadi van Koten <samadi@vktec.org.uk> | 2021-06-07 22:06:45 +0100 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2021-06-14 12:17:12 +0300 |
| commit | ee048d65692c6db9d084da951d3113af7db367aa (patch) | |
| tree | 932e8e4c154ddd42fe6c212a9aec9ca84d167c2f /lib/std | |
| parent | 2d4c4396524d7572d2869d79a209e5772a33a59b (diff) | |
| download | zig-ee048d65692c6db9d084da951d3113af7db367aa.tar.gz zig-ee048d65692c6db9d084da951d3113af7db367aa.zip | |
Add std.os.dup()
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/os.zig | 10 | ||||
| -rw-r--r-- | lib/std/os/linux.zig | 4 |
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig index 072771b000..c3fb42f782 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1282,6 +1282,16 @@ pub fn openatW(dir_fd: fd_t, file_path_w: []const u16, flags: u32, mode: mode_t) }; } +pub fn dup(old_fd: fd_t) !fd_t { + const rc = system.dup(old_fd); + return switch (errno(rc)) { + 0 => return @intCast(fd_t, rc), + EMFILE => error.ProcessFdQuotaExceeded, + EBADF => unreachable, // invalid file descriptor + else => |err| return unexpectedErrno(err), + }; +} + pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { while (true) { switch (errno(system.dup2(old_fd, new_fd))) { diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index dfc17d730b..4b3454de52 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -94,6 +94,10 @@ pub fn getErrno(r: usize) u12 { return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; } +pub fn dup(old: i32) usize { + return syscall1(.dup, @bitCast(usize, @as(isize, old))); +} + pub fn dup2(old: i32, new: i32) usize { if (@hasField(SYS, "dup2")) { return syscall2(.dup2, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new))); |
