Repository: microsoft/terminal
File: src/server/ConsoleShimPolicy.cpp (C++)

Code before:
        // For whatever reason, wil::GetModuleFileNameExW leaves trailing nulls, so get rid of them.
        clientName.erase(std::find(clientName.begin(), clientName.end(), '\0'), clientName.end());

        // Convert to lower case, just in case
        std::transform(clientName.begin(), clientName.end(), clientName.begin(), std::towlower);

        _isCmd = clientName.compare(L"cmd.exe") == 0;

        // For powershell, we need both Windows PowersShell (powershell.exe) and
        // PowerShell Core (pwsh.exe). If PowerShell Core is ever updated to use
        // ^[[3J for Clear-Host, then it won't ever hit the shim code path, but
        // we're keeping this for the long tail of pwsh versions that still
        // _don't_ use that sequence.
        const auto isInboxPowershell = clientName.compare(L"powershell.exe") == 0;
        const auto isPwsh = clientName.compare(L"pwsh.exe") == 0;
        _isPowershell = isInboxPowershell || isPwsh;

        // Inside Windows, we are guaranteed that we're building alongside a new (good) inbox PowerShell.
        // Therefore, we can default _requiresVtColorQuirk to false.
#ifndef __INSIDE_WINDOWS
        // Outside of Windows, we need to check the OS version: PowerShell was fixed in early Iron builds.
        static auto doesInboxPowershellVersionRequireQuirk = [] {
            OSVERSIONINFOEXW osver{};
            osver.dwOSVersionInfoSize = sizeof(osver);
            osver.dwBuildNumber = 20348; // Windows Server 2022 RTM

            DWORDLONG dwlConditionMask = 0;
            VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_LESS);

            return VerifyVersionInfoW(&osver, VER_BUILDNUMBER, dwlConditionMask) != FALSE;
        }();
        _requiresVtColorQuirk = isInboxPowershell && doesInboxPowershellVersionRequireQuirk;
        // All modern versions of pwsh.exe have been fixed, and we can direct users to update.
#endif
    }
    CATCH_LOG();
}

// Method Description:
// - Returns true if the connected client application is literally "cmd.exe". If
//   it is, we'll need to enable certain compatibility shims.
// Arguments:
// - <none>
// Return Value:
// - rue iff the process is "cmd.exe"
bool ConsoleShimPolicy::IsCmdExe() const noexcept
{
    return _isCmd;
}

// Method Description:

Review comment (bug):
All this code is necessary to call `VerifyVersionInfoW` (122 bytes in x64). Meanwhile it could actually be just 1 line of code resulting in 2 lines of assembly ([source1](https://stackoverflow.com/questions/52616221/how-to-use-ntcurrentteb-without-windows-header-files), [source2](https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/pebteb/peb/index.htm), 24 bytes) 🥺: https://godbolt.org/z/nf3G1o7b5

Code after:

        _isCmd = til::equals_insensitive_ascii(clientName, L"cmd.exe");

        // For powershell, we need both Windows Powershell (powershell.exe) and
        // Powershell Core (pwsh.exe). If Powershell Core is ever updated to use
        // ^[[3J for Clear-Host, then it won't ever hit the shim code path, but
        // we're keeping this for the long tail of pwsh versions that still
        // _don't_ use that sequence.
        const auto isInboxPowershell = til::equals_insensitive_ascii(clientName, L"powershell.exe");
        const auto isPwsh = til::equals_insensitive_ascii(clientName, L"pwsh.exe");
        _isPowershell = isInboxPowershell || isPwsh;

        // Inside Windows, we are guaranteed that we're building alongside a new (good) inbox Powershell.
        // Therefore, we can default _requiresVtColorQuirk to false.
#ifndef __INSIDE_WINDOWS
        // Outside of Windows, we need to check the OS version: Powershell was fixed in early Iron builds.
        static auto doesInboxPowershellVersionRequireQuirk = [] {
            OSVERSIONINFOEXW osver{};
            osver.dwOSVersionInfoSize = sizeof(osver);
            osver.dwBuildNumber = 20348; // Windows Server 2022 RTM

            DWORDLONG dwlConditionMask = 0;
            VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_LESS);

            return VerifyVersionInfoW(&osver, VER_BUILDNUMBER, dwlConditionMask) != FALSE;
        }();
        _requiresVtColorQuirk = isInboxPowershell && doesInboxPowershellVersionRequireQuirk;
        // All modern versions of pwsh.exe have been fixed, and we can direct users to update.
#endif
    }
    CATCH_LOG();
}

// Method Description:
// - Returns true if the connected client application is literally "cmd.exe". If
//   it is, we'll need to enable certain compatibility shims.
// Arguments:
// - <none>
// Return Value:
// - rue iff the process is "cmd.exe"
bool ConsoleShimPolicy::IsCmdExe() const noexcept
{
    return _isCmd;
}

// Method Description:
// - Returns true if the connected client application is literally
//   "powershell.exe" or "pwsh.exe". If it is, we'll need to enable certain
//   compatibility shims.
// Arguments:
// - <none>