From 44eb4163234089e8830b018dbcf6a08333eab202 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Fri, 7 May 2021 23:05:24 +0200 Subject: [PATCH] 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 --- winsup/cygwin/dcrt0.cc | 17 ++++++++++------- winsup/cygwin/fhandler_console.cc | 11 +++++------ winsup/cygwin/globals.cc | 6 +++--- winsup/cygwin/path.cc | 18 +++++------------- winsup/cygwin/uinfo.cc | 13 +------------ 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 3bbee4d5a..6f4723bb0 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -722,20 +722,23 @@ init_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'\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__ system_wow64_directory_length = - GetSystemWow64DirectoryW (system_wow64_directory, MAX_PATH); + GetSystemWow64DirectoryW (system_wow64_directory, MAX_PATH); if (system_wow64_directory_length) { system_wow64_directory[system_wow64_directory_length++] = L'\\'; 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__ */ } } diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc index ebe89fc9f..b3eae6a5a 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -112,12 +112,11 @@ beep () L"Apps", L".Default", L".Default", L".Current", NULL); if (r.created ()) { - PWCHAR buf = NULL; - UINT len = GetSystemWindowsDirectoryW (buf, 0) * sizeof (WCHAR); - buf = (PWCHAR) alloca (len += sizeof (ding)); - UINT res = GetSystemWindowsDirectoryW (buf, len); - if (res && res <= len) - r.set_string (L"", wcscat (buf, ding)); + tmp_pathbuf tp; + + PWCHAR ding_path = tp.w_get (); + wcpcpy (wcpcpy (ding_path, windows_directory), ding); + r.set_string (L"", ding_path); } MessageBeep (MB_OK); } diff --git a/winsup/cygwin/globals.cc b/winsup/cygwin/globals.cc index 0b9559ea7..d0c6046a5 100644 --- a/winsup/cygwin/globals.cc +++ b/winsup/cygwin/globals.cc @@ -23,12 +23,12 @@ HMODULE NO_COPY hntdll; int NO_COPY sigExeced; WCHAR windows_system_directory[MAX_PATH]; UINT windows_system_directory_length; -#ifdef __i386__ WCHAR system_wow64_directory[MAX_PATH]; 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; -#endif /* __i386__ */ +UNICODE_STRING windows_directory_path; WCHAR global_progname[NT_MAX_PATH]; /* program exit the program */ diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc index b13377a48..dd7048486 100644 --- a/winsup/cygwin/path.cc +++ b/winsup/cygwin/path.cc @@ -3462,19 +3462,11 @@ restart: file than the final path. Oh well... */ if (!fs.is_remote_drive () && wincap.is_wow64 ()) { - static UNICODE_STRING wpath; - UNICODE_STRING udpath; - - /* Create UNICODE_STRING for Windows dir. */ - RtlInitCountedUnicodeString (&wpath, windows_directory, - 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)) + /* windows_directory_path is stored without trailing backslash, + so we have to check this explicitely. */ + if (RtlEqualUnicodePathPrefix (&upath, &windows_directory_path, TRUE) + && upath.Buffer[windows_directory_path.Length / sizeof (WCHAR)] + == L'\\') goto file_not_symlink; } #endif /* __i386__ */ diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc index c0a0f6163..6aae33c92 100644 --- a/winsup/cygwin/uinfo.cc +++ b/winsup/cygwin/uinfo.cc @@ -564,18 +564,7 @@ const char * cygheap_user::env_systemroot (const char *name, size_t namelen) { if (!psystemroot) - { - 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"); - } + sys_wcstombs_alloc (&psystemroot, HEAP_STR, windows_directory); return psystemroot; }