@@ -44,32 +44,6 @@ pub const windows = @import("os/windows.zig");
4444pub const posix_spawn = @import ("os/posix_spawn.zig" );
4545pub const ptrace = @import ("os/ptrace.zig" );
4646
47- /// Pipe read side
48- pub const pipe_rd = 0 ;
49- /// Pipe write side
50- pub const pipe_wr = 1 ;
51-
52- pub const windowsPtrDigits : usize = std .math .log10 (math .maxInt (usize ));
53- pub const otherPtrDigits : usize = std .math .log10 (math .maxInt (u32 )) + 1 ; // +1 for sign
54- pub const handleCharSize = if (builtin .target .os .tag == .windows ) windowsPtrDigits else otherPtrDigits ;
55-
56- pub fn handleToString (handle : fd_t , buf : []u8 ) std.fmt.BufPrintError ! []u8 {
57- var s_handle : []u8 = undefined ;
58- const handle_int =
59- // handle is *anyopaque or an integer on unix-likes Kernels.
60- if (builtin .target .os .tag == .windows ) @ptrToInt (handle ) else handle ;
61- s_handle = try std .fmt .bufPrint (buf [0.. ], "{d}" , .{handle_int });
62- return s_handle ;
63- }
64-
65- pub fn stringToHandle (s_handle : []const u8 ) std.fmt.ParseIntError ! std.os.fd_t {
66- var file_handle : std.os.fd_t = if (builtin .target .os .tag == .windows )
67- @intToPtr (windows .HANDLE , try std .fmt .parseInt (usize , s_handle , 10 ))
68- else
69- try std .fmt .parseInt (std .os .fd_t , s_handle , 10 );
70- return file_handle ;
71- }
72-
7347comptime {
7448 assert (@import ("std" ) == std ); // std lib tests require --zig-lib-dir
7549}
@@ -314,6 +288,27 @@ pub fn close(fd: fd_t) void {
314288 }
315289}
316290
291+ pub const windowsPtrDigits = 19 ; // log10(max(usize))
292+ pub const unixoidPtrDigits = 10 ; // log10(max(u32)) + 1 for sign
293+ pub const handleCharSize = if (builtin .target .os .tag == .windows ) windowsPtrDigits else unixoidPtrDigits ;
294+
295+ pub fn handleToString (handle : fd_t , buf : []u8 ) std.fmt.BufPrintError ! []u8 {
296+ var s_handle : []u8 = undefined ;
297+ const handle_int =
298+ // handle is *anyopaque or an integer on unix-likes Kernels.
299+ if (builtin .target .os .tag == .windows ) @ptrToInt (handle ) else handle ;
300+ s_handle = try std .fmt .bufPrint (buf [0.. ], "{d}" , .{handle_int });
301+ return s_handle ;
302+ }
303+
304+ pub fn stringToHandle (s_handle : []const u8 ) std.fmt.ParseIntError ! std.os.fd_t {
305+ var handle : std.os.fd_t = if (builtin .target .os .tag == .windows )
306+ @intToPtr (windows .HANDLE , try std .fmt .parseInt (usize , s_handle , 10 ))
307+ else
308+ try std .fmt .parseInt (std .os .fd_t , s_handle , 10 );
309+ return handle ;
310+ }
311+
317312pub const FChmodError = error {
318313 AccessDenied ,
319314 InputOutput ,
@@ -4869,25 +4864,41 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
48694864 }
48704865}
48714866
4867+ const IsInheritableError = FcntlError || windows .GetHandleInformationError ;
4868+
4869+ /// Whether inheritence is enabled or CLOEXEC is not set.
4870+ pub inline fn isInheritable (handle : fd_t ) IsInheritableError ! bool {
4871+ if (builtin .os .tag == .windows ) {
4872+ var handle_flags : windows.DWORD = undefined ;
4873+ try windows .GetHandleInformation (handle , & handle_flags );
4874+ return handle_flags & windows .HANDLE_FLAG_INHERIT != 0 ;
4875+ } else {
4876+ const fcntl_flags = try fcntl (handle , F .GETFD , 0 );
4877+ return fcntl_flags & FD_CLOEXEC == 0 ;
4878+ }
4879+ }
4880+
48724881const EnableInheritanceError = FcntlError || windows .SetHandleInformationError ;
48734882
4874- pub inline fn enableInheritance (file_handle : fd_t ) EnableInheritanceError ! void {
4883+ /// Enables inheritence or sets CLOEXEC.
4884+ pub inline fn enableInheritance (handle : fd_t ) EnableInheritanceError ! void {
48754885 if (builtin .os .tag == .windows ) {
4876- try windows .SetHandleInformation (file_handle , windows .HANDLE_FLAG_INHERIT , 1 );
4886+ try windows .SetHandleInformation (handle , windows .HANDLE_FLAG_INHERIT , 1 );
48774887 } else {
4878- var flags = try fcntl (file_handle , F .GETFD , 0 );
4888+ var flags = try fcntl (handle , F .GETFD , 0 );
48794889 flags &= ~ @as (u32 , FD_CLOEXEC );
4880- _ = try fcntl (file_handle , F .SETFD , flags );
4890+ _ = try fcntl (handle , F .SETFD , flags );
48814891 }
48824892}
48834893
48844894const DisableInheritanceError = FcntlError || windows .SetHandleInformationError ;
48854895
4886- pub inline fn disableInheritance (file_handle : fd_t ) DisableInheritanceError ! void {
4896+ /// Disables inheritence or unsets CLOEXEC.
4897+ pub inline fn disableInheritance (handle : fd_t ) DisableInheritanceError ! void {
48874898 if (builtin .os .tag == .windows ) {
4888- try windows .SetHandleInformation (file_handle , windows .HANDLE_FLAG_INHERIT , 0 );
4899+ try windows .SetHandleInformation (handle , windows .HANDLE_FLAG_INHERIT , 0 );
48894900 } else {
4890- _ = try fcntl (file_handle , F .SETFD , FD_CLOEXEC );
4901+ _ = try fcntl (handle , F .SETFD , FD_CLOEXEC );
48914902 }
48924903}
48934904
0 commit comments