Skip to content

Commit af3bfa2

Browse files
committed
Use uname() for OS version string on FreeBSD. Note possible implementation for Android. Return a specific platform name when on any platform #if os() knows about that doesn't have a more specialized implementation.
1 parent 65e524d commit af3bfa2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Sources/Foundation/ProcessInfo.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ open class ProcessInfo: NSObject {
159159
versionString += " (build \(osVersionInfo.dwBuildNumber))"
160160
// For now we ignore the `szCSDVersion`, `wServicePackMajor`, and `wServicePackMinor` values.
161161
return versionString
162+
#elseif os(FreeBSD)
163+
// Try to get a release version from `uname -r`.
164+
var versionString = "FreeBSD"
165+
var utsNameBuffer = utsname()
166+
if uname(&utsNameBuffer) == 0 {
167+
let release = withUnsafePointer(to: &utsNameBuffer.release.0) { String(cString: $0) }
168+
if !release.isEmpty {
169+
versionString += " \(release)"
170+
}
171+
}
172+
return versionString
173+
#elseif os(OpenBSD)
174+
// TODO: `uname -r` probably works here too.
175+
return "OpenBSD"
176+
#elseif os(Android)
177+
/// In theory, we need to do something like this:
178+
///
179+
/// var versionString = "Android"
180+
/// let property = String(unsafeUninitializedCapacity: PROP_VALUE_MAX) { buf in
181+
/// __system_property_get("ro.build.description", buf.baseAddress!)
182+
/// }
183+
/// if !property.isEmpty {
184+
/// versionString += " \(property)"
185+
/// }
186+
/// return versionString
187+
return "Android"
188+
#elseif os(PS4)
189+
return "PS4"
190+
#elseif os(Cygwin)
191+
// TODO: `uname -r` probably works here too.
192+
return "Cygwin"
193+
#elseif os(Haiku)
194+
return "Haiku"
195+
#elseif os(WASI)
196+
return "WASI"
162197
#else
163198
// On other systems at least return something.
164199
return "Unknown"

0 commit comments

Comments
 (0)