Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / desktop / win32 / source / loader.cxx
blob4425c1e697d918f1024a3f765e4bd958628e9988
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/pathutils.hxx>
21 #include "loader.hxx"
22 #include <cassert>
24 namespace {
26 void fail()
28 LPWSTR buf = nullptr;
29 FormatMessageW(
30 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
31 GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, nullptr);
32 MessageBoxW(nullptr, buf, nullptr, MB_OK | MB_ICONERROR);
33 HeapFree(GetProcessHeap(), 0, buf);
34 TerminateProcess(GetCurrentProcess(), 255);
39 namespace desktop_win32 {
41 void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory) {
42 if (!GetModuleFileNameW(nullptr, iniDirectory, MAX_PATH)) {
43 fail();
45 WCHAR * iniDirEnd = tools::filename(iniDirectory);
46 WCHAR name[MAX_PATH + MY_LENGTH(L".bin")];
47 // hopefully std::size_t is large enough to not overflow
48 WCHAR * nameEnd = name;
49 for (WCHAR * p = iniDirEnd; *p != L'\0'; ++p) {
50 *nameEnd++ = *p;
52 if (!(nameEnd - name >= 4 && nameEnd[-4] == L'.' &&
53 (nameEnd[-3] == L'E' || nameEnd[-3] == L'e') &&
54 (nameEnd[-2] == L'X' || nameEnd[-2] == L'x') &&
55 (nameEnd[-1] == L'E' || nameEnd[-1] == L'e')))
57 *nameEnd = L'.';
58 nameEnd += 4;
60 nameEnd[-3] = 'b';
61 nameEnd[-2] = 'i';
62 nameEnd[-1] = 'n';
63 tools::buildPath(binPath, iniDirectory, iniDirEnd, name, nameEnd - name);
64 *iniDirEnd = L'\0';
65 std::size_t const maxEnv = 32767;
66 WCHAR env[maxEnv];
67 DWORD n = GetEnvironmentVariableW(L"PATH", env, maxEnv);
68 if ((n >= maxEnv || n == 0) && GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
69 fail();
71 // must be first in PATH to override other entries
72 assert(*(iniDirEnd - 1) == L'\\'); // hence -1 below
73 if (wcsncmp(env, iniDirectory, iniDirEnd - iniDirectory - 1) != 0
74 || env[iniDirEnd - iniDirectory - 1] != L';')
76 WCHAR pad[MAX_PATH + maxEnv];
77 // hopefully std::size_t is large enough to not overflow
78 WCHAR * p = commandLineAppend(pad, iniDirectory, iniDirEnd - iniDirectory - 1);
79 if (n != 0) {
80 *p++ = L';';
81 for (DWORD i = 0; i <= n; ++i) {
82 *p++ = env[i];
84 } else {
85 *p++ = L'\0';
87 if (!SetEnvironmentVariableW(L"PATH", pad)) {
88 fail();
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */