summaryrefslogtreecommitdiffstats
path: root/third_party/rust/whatsys/c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/whatsys/c
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/whatsys/c')
-rw-r--r--third_party/rust/whatsys/c/windows.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/third_party/rust/whatsys/c/windows.c b/third_party/rust/whatsys/c/windows.c
new file mode 100644
index 0000000000..47ce0e63dc
--- /dev/null
+++ b/third_party/rust/whatsys/c/windows.c
@@ -0,0 +1,86 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <windows.h>
+
+/**
+ * Based on code from sys-info: https://crates.io/crates/sys-info
+ * Original licenses: MIT
+ * Original author: Siyu Wang
+ * License file: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE
+ */
+
+/**
+ * Get the OS release version.
+ *
+ * This uses [`GetVersionEx`] internally.
+ * Depending on how the application is compiled
+ * it might return different values for the same operating system.
+ *
+ * On Windows 10 the example will return the Windows 8 OS version value `6.2`.
+ * If compiled into an application which explicitly targets Windows 10, it will
+ * return `10.0`.
+ *
+ * [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
+ *
+ * ## Arguments
+ *
+ * * `outbuf` - Pointer to an allocated buffer of size `outlen`.
+ * The OS version will be written here, null-terminated.
+ * If the version is longer than the buffer it will be truncated.
+ * * `outlen` - Size of the buffer.
+ *
+ * ## Return value
+ *
+ * Returns the number of bytes written.
+ */
+int get_os_release(char *outbuf, size_t outlen) {
+ assert(outlen > 1);
+
+ OSVERSIONINFO osvi;
+
+ ZeroMemory(&osvi, sizeof(osvi));
+ osvi.dwOSVersionInfoSize = sizeof(osvi);
+
+ int written = 0;
+ if (GetVersionEx(&osvi)) {
+ written = snprintf(outbuf, outlen, "%ld.%ld", osvi.dwMajorVersion,
+ osvi.dwMinorVersion);
+ } else {
+ int res = strncpy_s(outbuf, outlen, "unknown", strlen("unknown"));
+ if (res != 0) {
+ return 0;
+ }
+ written = strlen("unknown");
+ }
+
+ // If the output buffer is smaller than the version (or "unknown"),
+ // we only wrote until the buffer was full.
+ return min(written, (int)outlen - 1);
+}
+
+/**
+ * Get the windows build version.
+ *
+ * Works similarly to `get_os_release`, but returns the `dwBuildNumber` field from the
+ * struct returned by `GetVersionEx`.
+ *
+ * [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
+ *
+ * ## Return value
+ *
+ * Returns the build number as an int.
+ */
+int get_build_number() {
+ OSVERSIONINFO osvi;
+
+ ZeroMemory(&osvi, sizeof(osvi));
+ osvi.dwOSVersionInfoSize = sizeof(osvi);
+
+ if (GetVersionEx(&osvi)) {
+ return osvi.dwBuildNumber;
+ }
+
+ return 0;
+}
+