Skip to content

Commit 34a5854

Browse files
author
Jan Philipp Hafer
committed
windows: ptrToInt and back the handle (*anyopaque)
1 parent 08f11c2 commit 34a5854

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

lib/std/child_process.zig

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,13 +1633,22 @@ test "creating a child process with stdin and stdout behavior set to StdIo.Pipe"
16331633
const child_str2 =
16341634
\\ const std = @import("std");
16351635
\\ const builtin = @import("builtin");
1636+
\\ const windowsPtrDigits = std.math.log10(std.math.maxInt(usize));
1637+
\\ const otherPtrDigits = std.math.log10(std.math.maxInt(u32)) + 1;
16361638
\\ pub fn main() !void {
16371639
\\ const alloc = std.testing.allocator;
16381640
\\ const stdin = std.io.getStdIn();
16391641
\\ const stdin_reader = stdin.reader();
16401642
\\ const stdin_cont = try stdin_reader.readUntilDelimiterAlloc(alloc, '\n', 2_000);
16411643
\\ defer alloc.free(stdin_cont);
1642-
\\ var file_handle = try std.fmt.parseInt(std.os.fd_t, stdin_cont, 11);
1644+
\\ var file_handle = file_handle: {
1645+
\\ if (builtin.target.os.tag == .windows) {
1646+
\\ var handle_int = try std.fmt.parseInt(usize, stdin_cont, windowsPtrDigits);
1647+
\\ break :file_handle @intToPtr(std.os.windows.HANDLE, handle_int);
1648+
\\ } else {
1649+
\\ break :file_handle try std.fmt.parseInt(std.os.fd_t, stdin_cont, otherPtrDigits);
1650+
\\ }
1651+
\\ };
16431652
\\ var extra_stream_in = std.fs.File{ .handle = file_handle };
16441653
\\ defer extra_stream_in.close();
16451654
\\ const extra_str_in_rd = extra_stream_in.reader();
@@ -1650,15 +1659,32 @@ const child_str2 =
16501659
;
16511660

16521661
fn testPipeInfo(self: *ChildProcess) ChildProcess.SpawnError!void {
1662+
const windowsPtrDigits: usize = std.math.log10(math.maxInt(usize));
1663+
const otherPtrDigits: usize = std.math.log10(math.maxInt(u32)) + 1; // +1 for sign
16531664
if (self.extra_streams) |extra_streams| {
16541665
for (extra_streams) |*extra| {
1655-
var buf: [11]u8 = [_]u8{0} ** 11; // -2**32 has 11 digits
1666+
const size = comptime size: {
1667+
if (builtin.target.os.tag == .windows) {
1668+
break :size windowsPtrDigits;
1669+
} else {
1670+
break :size otherPtrDigits;
1671+
}
1672+
};
1673+
var buf = comptime [_]u8{0} ** size;
16561674
var s_chpipe_h: []u8 = undefined;
16571675
std.debug.assert(extra.direction == .parent_to_child);
1676+
const handle = handle: {
1677+
if (builtin.target.os.tag == .windows) {
1678+
// handle is *anyopaque and there is no other way to cast
1679+
break :handle @ptrToInt(extra.*.input.?.handle);
1680+
} else {
1681+
break :handle extra.*.input.?.handle;
1682+
}
1683+
};
16581684
s_chpipe_h = std.fmt.bufPrint(
16591685
buf[0..],
16601686
"{d}",
1661-
.{extra.*.input.?.handle},
1687+
.{handle},
16621688
) catch unreachable;
16631689
self.stdin.?.writer().writeAll(s_chpipe_h) catch unreachable;
16641690
self.stdin.?.writer().writeAll("\n") catch unreachable;

0 commit comments

Comments
 (0)