@@ -18,9 +18,20 @@ import Foundation
18
18
/// - args: The executable arguments.
19
19
public func exec( path: String , args: [ String ] ) throws {
20
20
let cArgs = CStringArray ( args)
21
+ #if os(Windows)
22
+ guard cArgs. cArray. withUnsafeBufferPointer ( {
23
+ $0. withMemoryRebound ( to: UnsafePointer< Int8>? . self , {
24
+ _execv ( path, $0. baseAddress) != - 1
25
+ } )
26
+ } )
27
+ else {
28
+ throw SystemError . exec ( errno, path: path, args: args)
29
+ }
30
+ #else
21
31
guard execv ( path, cArgs. cArray) != - 1 else {
22
32
throw SystemError . exec ( errno, path: path, args: args)
23
33
}
34
+ #endif
24
35
}
25
36
26
37
// MARK: Utility function for searching for executables
@@ -163,13 +174,23 @@ public enum SystemError: Swift.Error {
163
174
case waitpid( Int32 )
164
175
}
165
176
177
+ #if os(Windows)
178
+ import func SPMLibc. strerror_s
179
+ #else
166
180
import func SPMLibc. strerror_r
181
+ #endif
167
182
import var SPMLibc. EINVAL
168
183
import var SPMLibc. ERANGE
169
184
170
185
extension SystemError : CustomStringConvertible {
171
186
public var description : String {
172
187
func strerror( _ errno: Int32 ) -> String {
188
+ #if os(Windows)
189
+ let cap = 128
190
+ var buf = [ Int8] ( repeating: 0 , count: cap)
191
+ let _ = SPMLibc . strerror_s ( & buf, 128 , errno)
192
+ return " \( String ( cString: buf) ) ( \( errno) ) "
193
+ #else
173
194
var cap = 64
174
195
while cap <= 16 * 1024 {
175
196
var buf = [ Int8] ( repeating: 0 , count: cap)
@@ -187,6 +208,7 @@ extension SystemError: CustomStringConvertible {
187
208
return " \( String ( cString: buf) ) ( \( errno) ) "
188
209
}
189
210
fatalError ( " strerror_r error: \( ERANGE) " )
211
+ #endif
190
212
}
191
213
192
214
switch self {
0 commit comments