Cygwin: fetch Windows directory on all platforms and use throughout

Rather than fetching the system Windows directory at dll init time
only on 32 bit, fetch it on all platforms.  Store as WCHAR and
UNICODE_STRING.  Use where appropriate to simplify code.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2021-05-07 23:05:24 +02:00
parent 19d59ce75d
commit 44eb416323
5 changed files with 24 additions and 41 deletions

View File

@ -722,6 +722,15 @@ init_windows_system_directory ()
api_fatal ("can't find windows system directory"); api_fatal ("can't find windows system directory");
windows_system_directory[windows_system_directory_length++] = L'\\'; windows_system_directory[windows_system_directory_length++] = L'\\';
windows_system_directory[windows_system_directory_length] = L'\0'; windows_system_directory[windows_system_directory_length] = L'\0';
/* We need the Windows dir with NT prefix in path.cc. Note that we
don't append a backslash, because we need the dir without backslash
for the environment. */
wcpcpy (windows_directory_buf, L"\\??\\");
windows_directory_length =
GetSystemWindowsDirectoryW (windows_directory, MAX_PATH - 4);
RtlInitCountedUnicodeString (&windows_directory_path,
windows_directory_buf,
(windows_directory_length + 4) * sizeof (WCHAR));
#ifdef __i386__ #ifdef __i386__
system_wow64_directory_length = system_wow64_directory_length =
GetSystemWow64DirectoryW (system_wow64_directory, MAX_PATH); GetSystemWow64DirectoryW (system_wow64_directory, MAX_PATH);
@ -730,12 +739,6 @@ init_windows_system_directory ()
system_wow64_directory[system_wow64_directory_length++] = L'\\'; system_wow64_directory[system_wow64_directory_length++] = L'\\';
system_wow64_directory[system_wow64_directory_length] = L'\0'; system_wow64_directory[system_wow64_directory_length] = L'\0';
} }
/* We need the Windows dir in path.cc. */
wcscpy (windows_directory, windows_system_directory);
windows_directory_length = windows_system_directory_length - 1;
windows_directory[windows_directory_length] = L'\0';
while (windows_directory[windows_directory_length - 1] != L'\\')
windows_directory[--windows_directory_length] = L'\0';
#endif /* __i386__ */ #endif /* __i386__ */
} }
} }

View File

@ -112,12 +112,11 @@ beep ()
L"Apps", L".Default", L".Default", L".Current", NULL); L"Apps", L".Default", L".Default", L".Current", NULL);
if (r.created ()) if (r.created ())
{ {
PWCHAR buf = NULL; tmp_pathbuf tp;
UINT len = GetSystemWindowsDirectoryW (buf, 0) * sizeof (WCHAR);
buf = (PWCHAR) alloca (len += sizeof (ding)); PWCHAR ding_path = tp.w_get ();
UINT res = GetSystemWindowsDirectoryW (buf, len); wcpcpy (wcpcpy (ding_path, windows_directory), ding);
if (res && res <= len) r.set_string (L"", ding_path);
r.set_string (L"", wcscat (buf, ding));
} }
MessageBeep (MB_OK); MessageBeep (MB_OK);
} }

View File

@ -23,12 +23,12 @@ HMODULE NO_COPY hntdll;
int NO_COPY sigExeced; int NO_COPY sigExeced;
WCHAR windows_system_directory[MAX_PATH]; WCHAR windows_system_directory[MAX_PATH];
UINT windows_system_directory_length; UINT windows_system_directory_length;
#ifdef __i386__
WCHAR system_wow64_directory[MAX_PATH]; WCHAR system_wow64_directory[MAX_PATH];
UINT system_wow64_directory_length; UINT system_wow64_directory_length;
WCHAR windows_directory[MAX_PATH]; WCHAR windows_directory_buf[MAX_PATH];
PWCHAR windows_directory = windows_directory_buf + 4;
UINT windows_directory_length; UINT windows_directory_length;
#endif /* __i386__ */ UNICODE_STRING windows_directory_path;
WCHAR global_progname[NT_MAX_PATH]; WCHAR global_progname[NT_MAX_PATH];
/* program exit the program */ /* program exit the program */

View File

@ -3462,19 +3462,11 @@ restart:
file than the final path. Oh well... */ file than the final path. Oh well... */
if (!fs.is_remote_drive () && wincap.is_wow64 ()) if (!fs.is_remote_drive () && wincap.is_wow64 ())
{ {
static UNICODE_STRING wpath; /* windows_directory_path is stored without trailing backslash,
UNICODE_STRING udpath; so we have to check this explicitely. */
if (RtlEqualUnicodePathPrefix (&upath, &windows_directory_path, TRUE)
/* Create UNICODE_STRING for Windows dir. */ && upath.Buffer[windows_directory_path.Length / sizeof (WCHAR)]
RtlInitCountedUnicodeString (&wpath, windows_directory, == L'\\')
windows_directory_length * sizeof (WCHAR));
/* Create a UNICODE_STRING from incoming path, splitting
off the leading "\\??\\" */
RtlInitCountedUnicodeString (&udpath, upath.Buffer + 4,
upath.Length - 4 * sizeof (WCHAR));
/* Are we below Windows dir? Skip the check for inner
symlinks. */
if (RtlEqualUnicodePathPrefix (&udpath, &wpath, TRUE))
goto file_not_symlink; goto file_not_symlink;
} }
#endif /* __i386__ */ #endif /* __i386__ */

View File

@ -564,18 +564,7 @@ const char *
cygheap_user::env_systemroot (const char *name, size_t namelen) cygheap_user::env_systemroot (const char *name, size_t namelen)
{ {
if (!psystemroot) if (!psystemroot)
{ sys_wcstombs_alloc (&psystemroot, HEAP_STR, windows_directory);
int size = GetSystemWindowsDirectoryW (NULL, 0);
if (size > 0)
{
WCHAR wsystemroot[size];
size = GetSystemWindowsDirectoryW (wsystemroot, size);
if (size > 0)
sys_wcstombs_alloc (&psystemroot, HEAP_STR, wsystemroot);
}
if (size <= 0)
debug_printf ("GetSystemWindowsDirectoryW(), %E");
}
return psystemroot; return psystemroot;
} }