diff --git a/Sources/CSystem/include/CSystemShared.h b/Sources/CSystem/include/CSystemShared.h index ccc05b1..1ee3a6a 100644 --- a/Sources/CSystem/include/CSystemShared.h +++ b/Sources/CSystem/include/CSystemShared.h @@ -7,5 +7,8 @@ See https://swift.org/LICENSE.txt for license information */ +#if !defined(_WIN32) extern int csystem_posix_pipe2(int fildes[2], int flag); +#endif + extern int csystem_posix_dup3(int fildes, int fildes2, int flag); diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index 433cc78..4130582 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -28,6 +28,8 @@ #endif // Wrappers are required because _GNU_SOURCE causes a conflict with other imports when defined in CSystemLinux.h + +#if !defined(_WIN32) extern int csystem_posix_pipe2(int fildes[2], int flag) { #ifdef HAVE_PIPE2_DUP3 return pipe2(fildes, flag); @@ -36,6 +38,8 @@ extern int csystem_posix_pipe2(int fildes[2], int flag) { return -1; #endif } +#endif // !defined(_WIN32) + extern int csystem_posix_dup3(int fildes, int fildes2, int flag) { #ifdef HAVE_PIPE2_DUP3 return dup3(fildes, fildes2, flag); diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index 86f36bb..7155fa3 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -324,7 +324,6 @@ extension FileDescriptor { /// Options that specify behavior for a newly-created pipe. @frozen - @available(Windows, unavailable) @available(macOS, unavailable) @available(iOS, unavailable) @available(tvOS, unavailable) @@ -350,6 +349,7 @@ extension FileDescriptor { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "nonBlocking") public static var O_NONBLOCK: PipeOptions { nonBlocking } +#endif // !os(Windows) /// Indicates that executing a program closes the file. /// @@ -369,7 +369,13 @@ extension FileDescriptor { @available(*, unavailable, renamed: "closeOnExec") public static var O_CLOEXEC: PipeOptions { closeOnExec } -#if !os(WASI) && !os(Linux) && !os(Android) +#if os(Windows) + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "closeOnExec") + public static var O_NOINHERIT: PipeOptions { closeOnExec } +#endif + +#if !os(Windows) && !os(WASI) && !os(Linux) && !os(Android) /// Indicates that forking a program closes the file. /// /// Normally, file descriptors remain open @@ -387,7 +393,6 @@ extension FileDescriptor { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "closeOnFork") public static var O_CLOFORK: PipeOptions { closeOnFork } -#endif #endif } diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index f69374f..7dfcb9e 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -524,7 +524,6 @@ extension FileDescriptor { /// /// The corresponding C function is `pipe2`. @_alwaysEmitIntoClient - @available(Windows, unavailable) @available(macOS, unavailable) @available(iOS, unavailable) @available(tvOS, unavailable) @@ -550,7 +549,6 @@ extension FileDescriptor { } } - @available(Windows, unavailable) @available(macOS, unavailable) @available(iOS, unavailable) @available(tvOS, unavailable) diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 8f9fc48..477fc9a 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -619,10 +619,16 @@ internal var _O_DIRECTORY: CInt { internal var _O_SYMLINK: CInt { O_SYMLINK } #endif -#if !os(Windows) @_alwaysEmitIntoClient -internal var _O_CLOEXEC: CInt { O_CLOEXEC } +internal var _O_CLOEXEC: CInt { + #if os(Windows) + O_NOINHERIT + #else + O_CLOEXEC + #endif +} +#if !os(Windows) @_alwaysEmitIntoClient internal var _O_CLOFORK: CInt { #if !os(WASI) && !os(Linux) && !os(Android) && !canImport(Darwin) diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index ca9b42d..c649aa3 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -206,6 +206,13 @@ internal func pipe( return _pipe(fds, bytesReserved, _O_BINARY | _O_NOINHERIT); } +@inline(__always) +internal func csystem_posix_pipe2( + _ fds: UnsafeMutablePointer, bytesReserved: UInt32 = 0, _ oflag: Int32 +) -> CInt { + return _pipe(fds, bytesReserved, _O_BINARY | oflag) +} + @inline(__always) internal func ftruncate(_ fd: Int32, _ length: off_t) -> Int32 { let handle: intptr_t = _get_osfhandle(fd)