Skip to content

Commit 26841f7

Browse files
committed
mingw: add windows_build() for OS build number
Add unsigned windows_build() to read the Windows OS build number from the registry and cache the result. This post explains the methods available to read the build number, with the registry being the most reasonable one: https://stackoverflow.com/a/47583450/262458 . This post explains how to read the registry in C/C++: https://stackoverflow.com/a/35717/262458 . This is used in a subsequent commit to set $env:TERM correctly for newer OSes. Signed-off-by: Rafael Kitover <[email protected]>
1 parent 158a30d commit 26841f7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

compat/mingw.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "../git-compat-util.h"
22
#include "win32.h"
33
#include <conio.h>
4+
#include <string.h>
45
#include <wchar.h>
56
#include <winioctl.h>
67
#include "../strbuf.h"
@@ -3337,6 +3338,45 @@ static int is_system32_path(const char *path)
33373338
return 1;
33383339
}
33393340

3341+
/*
3342+
* Extract the Windows OS build number from the registry, this is
3343+
* best method of those available here:
3344+
* https://stackoverflow.com/a/47583450/262458
3345+
*
3346+
* This post explains how to read the registry using WinAPI:
3347+
* https://stackoverflow.com/a/35717/262458
3348+
*/
3349+
unsigned windows_build(void)
3350+
{
3351+
static unsigned is_cached = 0;
3352+
static unsigned cached_value = 0;
3353+
3354+
if (is_cached)
3355+
return cached_value;
3356+
3357+
HKEY hkey;
3358+
ULONG res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hkey);
3359+
3360+
if (res != ERROR_SUCCESS) goto err;
3361+
3362+
char buf[512];
3363+
int bufsize = sizeof(buf);
3364+
3365+
res = RegQueryValueExA(hkey, "CurrentBuildNumber", 0, NULL, (LPSTR)buf, &bufsize);
3366+
3367+
if (res != ERROR_SUCCESS) goto err;
3368+
3369+
cached_value = atoi(buf);
3370+
is_cached = 1;
3371+
3372+
return cached_value;
3373+
3374+
err:
3375+
cached_value = 0;
3376+
is_cached = 1;
3377+
return 0;
3378+
}
3379+
33403380
static void setup_windows_environment(void)
33413381
{
33423382
char *tmp = getenv("TMPDIR");

compat/mingw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,8 @@ int err_win_to_posix(DWORD winerr);
714714
* Check current process is inside Windows Container.
715715
*/
716716
int is_inside_windows_container(void);
717+
718+
/*
719+
* Get Windows build number from the registry.
720+
*/
721+
unsigned windows_build(void);

0 commit comments

Comments
 (0)