@@ -44,32 +44,6 @@ pub const windows = @import("os/windows.zig");
44
44
pub const posix_spawn = @import ("os/posix_spawn.zig" );
45
45
pub const ptrace = @import ("os/ptrace.zig" );
46
46
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
-
73
47
comptime {
74
48
assert (@import ("std" ) == std ); // std lib tests require --zig-lib-dir
75
49
}
@@ -314,6 +288,27 @@ pub fn close(fd: fd_t) void {
314
288
}
315
289
}
316
290
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
+
317
312
pub const FChmodError = error {
318
313
AccessDenied ,
319
314
InputOutput ,
@@ -4869,25 +4864,41 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
4869
4864
}
4870
4865
}
4871
4866
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
+
4872
4881
const EnableInheritanceError = FcntlError || windows .SetHandleInformationError ;
4873
4882
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 {
4875
4885
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 );
4877
4887
} else {
4878
- var flags = try fcntl (file_handle , F .GETFD , 0 );
4888
+ var flags = try fcntl (handle , F .GETFD , 0 );
4879
4889
flags &= ~ @as (u32 , FD_CLOEXEC );
4880
- _ = try fcntl (file_handle , F .SETFD , flags );
4890
+ _ = try fcntl (handle , F .SETFD , flags );
4881
4891
}
4882
4892
}
4883
4893
4884
4894
const DisableInheritanceError = FcntlError || windows .SetHandleInformationError ;
4885
4895
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 {
4887
4898
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 );
4889
4900
} else {
4890
- _ = try fcntl (file_handle , F .SETFD , FD_CLOEXEC );
4901
+ _ = try fcntl (handle , F .SETFD , FD_CLOEXEC );
4891
4902
}
4892
4903
}
4893
4904
0 commit comments