cli/util/
os.rs

1/*---------------------------------------------------------------------------------------------
2 *  Copyright (c) Microsoft Corporation. All rights reserved.
3 *  Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6#[cfg(windows)]
7pub fn os_release() -> Result<String, std::io::Error> {
8	// The windows API *had* nice GetVersionEx/A APIs, but these were deprecated
9	// in Winodws 8 and there's no newer win API to get version numbers. So
10	// instead read the registry.
11
12	use winreg::{enums::HKEY_LOCAL_MACHINE, RegKey};
13
14	let key = RegKey::predef(HKEY_LOCAL_MACHINE)
15		.open_subkey(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion")?;
16
17	let major: u32 = key.get_value("CurrentMajorVersionNumber")?;
18	let minor: u32 = key.get_value("CurrentMinorVersionNumber")?;
19	let build: String = key.get_value("CurrentBuild")?;
20
21	Ok(format!("{}.{}.{}", major, minor, build))
22}
23
24#[cfg(unix)]
25pub fn os_release() -> Result<String, std::io::Error> {
26	use std::{ffi::CStr, mem};
27
28	unsafe {
29		let mut ret = mem::MaybeUninit::zeroed();
30
31		if libc::uname(ret.as_mut_ptr()) != 0 {
32			return Err(std::io::Error::last_os_error());
33		}
34
35		let ret = ret.assume_init();
36		let c_str: &CStr = CStr::from_ptr(ret.release.as_ptr());
37		Ok(c_str.to_string_lossy().into_owned())
38	}
39}