Skip to content

PackagePlugin: silence a deprecation warning on Windows #7109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/PackagePlugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ internal func close(_ fd: CInt) -> CInt {
internal func fileno(_ fh: UnsafeMutablePointer<FILE>?) -> CInt {
return _fileno(fh)
}

internal func strerror(_ errno: CInt) -> String? {
// MSDN indicates that the returned string can have a maximum of 94
// characters, so allocate 95 characters.
return withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: 95) {
let result = _wcserror_s($0.baseAddress, $0.count, errno)
guard result == 0, let baseAddress = $0.baseAddress else { return nil }
return String(decodingCString: baseAddress, as: UTF16.self)
}
}
#endif

//
Expand Down Expand Up @@ -267,8 +277,12 @@ extension Plugin {

// Private function to construct an error message from an `errno` code.
fileprivate static func describe(errno: Int32) -> String {
#if os(Windows)
return strerror(errno) ?? String(errno)
#else
if let cStr = strerror(errno) { return String(cString: cStr) }
return String(describing: errno)
#endif
}
}

Expand Down