Skip to content

Commit dbd89a6

Browse files
committed
Fix posix.W in process/Child.zig
add Deprecated linux.W fns Signed-off-by: Bernard Assan <[email protected]>
1 parent 7abc39a commit dbd89a6

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

lib/std/os/linux.zig

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,46 +3660,85 @@ pub const R_OK = 4;
36603660

36613661
pub const W = packed struct(u32) {
36623662
nohang: bool = false,
3663-
untraced_or_stopped: packed union {
3664-
untraced: bool,
3665-
stopped: bool,
3666-
} = @bitCast(false),
3663+
stopped: bool = false,
36673664
exited: bool = false,
36683665
continued: bool = false,
36693666
_5: u20 = 0,
36703667
nowait: bool = false,
36713668
_26: u7 = 0,
3669+
/// alias to stopped
3670+
pub const untraced: W = .{ .stopped = true };
36723671

3673-
// Deprecated aliases
3672+
fn toInt(s: W) u32 {
3673+
return @bitCast(s);
3674+
}
3675+
3676+
/// matches EXITSTATUS in C
3677+
pub fn exitStatus(s: W) u8 {
3678+
return @intCast((s.toInt() & 0xff00) >> 8);
3679+
}
3680+
3681+
/// matches TERMSIG in C
3682+
pub fn termSig(s: W) u32 {
3683+
return s.toInt() & 0x7f;
3684+
}
3685+
3686+
/// matches STOPSIG in C
3687+
pub fn stopSig(s: W) u32 {
3688+
return exitStatus(s);
3689+
}
3690+
3691+
/// matches IFEXITED in C
3692+
pub fn ifExited(s: W) bool {
3693+
return termSig(s) == 0;
3694+
}
3695+
3696+
/// matches IFSTOPPED in C
3697+
pub fn ifStopped(s: W) bool {
3698+
return @as(u16, @truncate(((s.toInt() & 0xffff) *% 0x10001) >> 8)) > 0x7f00;
3699+
}
3700+
3701+
/// matches IFSIGNALED in C
3702+
pub fn ifSignaled(s: W) bool {
3703+
return (s.toInt() & 0xffff) -% 1 < 0xff;
3704+
}
3705+
3706+
// Deprecated constants
36743707
pub const NOHANG: u32 = @bitCast(W{ .nohang = true });
3675-
pub const UNTRACED: u32 = @bitCast(W{ .untraced_or_stopped = .{ .untraced = true } });
3676-
pub const STOPPED: u32 = @bitCast(W{ .untraced_or_stopped = .{ .stopped = true } });
3708+
pub const STOPPED: u32 = @bitCast(W{ .stopped = true });
3709+
pub const UNTRACED: u32 = @bitCast(untraced);
36773710
pub const EXITED: u32 = @bitCast(W{ .exited = true });
36783711
pub const CONTINUED: u32 = @bitCast(W{ .continued = true });
36793712
pub const NOWAIT: u32 = @bitCast(W{ .nowait = true });
36803713

3681-
pub fn EXITSTATUS(s: W) u8 {
3682-
return @intCast((@as(u32, @bitCast(s)) & 0xff00) >> 8);
3714+
/// DEPRECATED alias to exitStatus
3715+
pub fn EXITSTATUS(s: u32) u8 {
3716+
return exitStatus(@bitCast(s));
36833717
}
36843718

3685-
pub fn TERMSIG(s: W) u32 {
3686-
return @as(u32, @bitCast(s)) & 0x7f;
3719+
/// DEPRECATED alias to termSig
3720+
pub fn TERMSIG(s: u32) u32 {
3721+
return termSig(@bitCast(s));
36873722
}
36883723

3689-
pub fn STOPSIG(s: W) u32 {
3690-
return EXITSTATUS(s);
3724+
/// DEPRECATED alias to stopSig
3725+
pub fn STOPSIG(s: u32) u32 {
3726+
return stopSig(@bitCast(s));
36913727
}
36923728

3693-
pub fn IFEXITED(s: W) bool {
3694-
return TERMSIG(s) == 0;
3729+
/// DEPRECATED alias to ifExited
3730+
pub fn IFEXITED(s: u32) bool {
3731+
return ifExited(@bitCast(s));
36953732
}
36963733

3697-
pub fn IFSTOPPED(s: W) bool {
3698-
return @as(u16, @truncate(((@as(u32, @bitCast(s)) & 0xffff) *% 0x10001) >> 8)) > 0x7f00;
3734+
/// DEPRECATED alias to ifStopped
3735+
pub fn IFSTOPPED(s: u32) bool {
3736+
return ifStopped(@bitCast(s));
36993737
}
37003738

3701-
pub fn IFSIGNALED(s: W) bool {
3702-
return (s & 0xffff) -% 1 < 0xff;
3739+
/// DEPRECATED alias to ifSignaled
3740+
pub fn IFSIGNALED(s: u32) bool {
3741+
return ifSignaled(@bitCast(s));
37033742
}
37043743
};
37053744

lib/std/process/Child.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,15 @@ fn cleanupStreams(self: *ChildProcess) void {
514514
}
515515

516516
fn statusToTerm(status: u32) Term {
517-
return if (posix.W.IFEXITED(status))
518-
Term{ .Exited = posix.W.EXITSTATUS(status) }
519-
else if (posix.W.IFSIGNALED(status))
520-
Term{ .Signal = posix.W.TERMSIG(status) }
521-
else if (posix.W.IFSTOPPED(status))
522-
Term{ .Stopped = posix.W.STOPSIG(status) }
517+
const w: posix.W = @bitCast(status);
518+
return if (w.ifExited())
519+
.{ .Exited = w.exitStatus() }
520+
else if (w.ifSignaled())
521+
.{ .Signal = w.termSig() }
522+
else if (w.ifStopped())
523+
.{ .Stopped = w.stopSig() }
523524
else
524-
Term{ .Unknown = status };
525+
.{ .Unknown = status };
525526
}
526527

527528
fn spawnPosix(self: *ChildProcess) SpawnError!void {

0 commit comments

Comments
 (0)