|
36 | 36 | #include <psapi.h> |
37 | 37 | #include <tlhelp32.h> |
38 | 38 | #include <windows.h> |
| 39 | +#include <shlobj.h> |
| 40 | +#include <objbase.h> |
39 | 41 |
|
40 | 42 |
|
41 | 43 | /* |
@@ -72,7 +74,7 @@ void uv__util_init() { |
72 | 74 | InitializeCriticalSection(&process_title_lock); |
73 | 75 |
|
74 | 76 | /* Retrieve high-resolution timer frequency |
75 | | - * and precompute its reciprocal. |
| 77 | + * and precompute its reciprocal. |
76 | 78 | */ |
77 | 79 | if (QueryPerformanceFrequency(&perf_frequency)) { |
78 | 80 | hrtime_interval_ = 1.0 / perf_frequency.QuadPart; |
@@ -801,8 +803,8 @@ static int is_windows_version_or_greater(DWORD os_major, |
801 | 803 |
|
802 | 804 | /* Perform the test. */ |
803 | 805 | return (int) VerifyVersionInfo( |
804 | | - &osvi, |
805 | | - VER_MAJORVERSION | VER_MINORVERSION | |
| 806 | + &osvi, |
| 807 | + VER_MAJORVERSION | VER_MINORVERSION | |
806 | 808 | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, |
807 | 809 | condition_mask); |
808 | 810 | } |
@@ -870,7 +872,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses_ptr, |
870 | 872 | flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | |
871 | 873 | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX; |
872 | 874 | } |
873 | | - |
| 875 | + |
874 | 876 |
|
875 | 877 | /* Fetch the size of the adapters reported by windows, and then get the */ |
876 | 878 | /* list itself. */ |
@@ -1053,14 +1055,14 @@ int uv_interface_addresses(uv_interface_address_t** addresses_ptr, |
1053 | 1055 | prefix->PrefixLength <= prefix_len) |
1054 | 1056 | continue; |
1055 | 1057 |
|
1056 | | - if (address_prefix_match(sa->sa_family, sa, |
| 1058 | + if (address_prefix_match(sa->sa_family, sa, |
1057 | 1059 | prefix->Address.lpSockaddr, prefix->PrefixLength)) { |
1058 | 1060 | prefix_len = prefix->PrefixLength; |
1059 | 1061 | } |
1060 | 1062 | } |
1061 | 1063 |
|
1062 | 1064 | /* If there is no matching prefix information, return a single-host |
1063 | | - * subnet mask (e.g. 255.255.255.255 for IPv4). |
| 1065 | + * subnet mask (e.g. 255.255.255.255 for IPv4). |
1064 | 1066 | */ |
1065 | 1067 | if (!prefix_len) |
1066 | 1068 | prefix_len = (sa->sa_family == AF_INET6) ? 128 : 32; |
@@ -1153,3 +1155,67 @@ int uv_getrusage(uv_rusage_t *uv_rusage) { |
1153 | 1155 |
|
1154 | 1156 | return 0; |
1155 | 1157 | } |
| 1158 | + |
| 1159 | + |
| 1160 | +int uv_os_homedir(char* buffer, size_t* size) { |
| 1161 | + wchar_t* path; |
| 1162 | + size_t bufsize; |
| 1163 | + size_t len; |
| 1164 | + int r; |
| 1165 | + |
| 1166 | + if (buffer == NULL || size == NULL || *size == 0) |
| 1167 | + return UV_EINVAL; |
| 1168 | + |
| 1169 | + /* Check if the USERPROFILE environment variable is set first */ |
| 1170 | + path = malloc(*size * sizeof(WCHAR)); |
| 1171 | + |
| 1172 | + if (path == NULL) |
| 1173 | + return UV_ENOMEM; |
| 1174 | + |
| 1175 | + len = GetEnvironmentVariableW(L"USERPROFILE", path, *size); |
| 1176 | + |
| 1177 | + if (len == 0) { |
| 1178 | + r = GetLastError(); |
| 1179 | + free(path); |
| 1180 | + |
| 1181 | + if (r != ERROR_ENVVAR_NOT_FOUND) |
| 1182 | + return uv_translate_sys_error(r); |
| 1183 | + } else { |
| 1184 | + if (len > *size) { |
| 1185 | + free(path); |
| 1186 | + *size = len - 1; |
| 1187 | + return UV_ENOBUFS; |
| 1188 | + } |
| 1189 | + |
| 1190 | + bufsize = uv_utf16_to_utf8(path, -1, buffer, *size); |
| 1191 | + assert(len + 1 == bufsize); |
| 1192 | + free(path); |
| 1193 | + *size = len; |
| 1194 | + |
| 1195 | + return 0; |
| 1196 | + } |
| 1197 | + |
| 1198 | + /* USERPROFILE is not set, so call SHGetKnownFolderPath() */ |
| 1199 | + if (SHGetKnownFolderPath(&FOLDERID_Profile, 0, NULL, &path) != S_OK) |
| 1200 | + return uv_translate_sys_error(GetLastError()); |
| 1201 | + |
| 1202 | + bufsize = uv_utf16_to_utf8(path, -1, buffer, *size); |
| 1203 | + |
| 1204 | + if (bufsize == 0) { |
| 1205 | + r = GetLastError(); |
| 1206 | + |
| 1207 | + if (r == ERROR_INSUFFICIENT_BUFFER) { |
| 1208 | + *size = wcslen(path); |
| 1209 | + CoTaskMemFree(path); |
| 1210 | + return UV_ENOBUFS; |
| 1211 | + } |
| 1212 | + |
| 1213 | + CoTaskMemFree(path); |
| 1214 | + return uv_translate_sys_error(r); |
| 1215 | + } |
| 1216 | + |
| 1217 | + CoTaskMemFree(path); |
| 1218 | + *size = bufsize - 1; |
| 1219 | + |
| 1220 | + return 0; |
| 1221 | +} |
0 commit comments