Skip to content

Commit dadf766

Browse files
gmittertaciidgh
authored andcommitted
Implement Basic/misc.swift on Windows
1 parent caea53a commit dadf766

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/Basic/misc.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ import Foundation
1818
/// - args: The executable arguments.
1919
public func exec(path: String, args: [String]) throws {
2020
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
2131
guard execv(path, cArgs.cArray) != -1 else {
2232
throw SystemError.exec(errno, path: path, args: args)
2333
}
34+
#endif
2435
}
2536

2637
// MARK: Utility function for searching for executables
@@ -163,13 +174,23 @@ public enum SystemError: Swift.Error {
163174
case waitpid(Int32)
164175
}
165176

177+
#if os(Windows)
178+
import func SPMLibc.strerror_s
179+
#else
166180
import func SPMLibc.strerror_r
181+
#endif
167182
import var SPMLibc.EINVAL
168183
import var SPMLibc.ERANGE
169184

170185
extension SystemError: CustomStringConvertible {
171186
public var description: String {
172187
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
173194
var cap = 64
174195
while cap <= 16 * 1024 {
175196
var buf = [Int8](repeating: 0, count: cap)
@@ -187,6 +208,7 @@ extension SystemError: CustomStringConvertible {
187208
return "\(String(cString: buf)) (\(errno))"
188209
}
189210
fatalError("strerror_r error: \(ERANGE)")
211+
#endif
190212
}
191213

192214
switch self {

0 commit comments

Comments
 (0)