1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
)) {
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
) {
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')))
63 tools::buildPath(binPath
, iniDirectory
, iniDirEnd
, name
, nameEnd
- name
);
65 std::size_t const maxEnv
= 32767;
67 DWORD n
= GetEnvironmentVariableW(L
"PATH", env
, maxEnv
);
68 if ((n
>= maxEnv
|| n
== 0) && GetLastError() != ERROR_ENVVAR_NOT_FOUND
) {
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);
81 for (DWORD i
= 0; i
<= n
; ++i
) {
87 if (!SetEnvironmentVariableW(L
"PATH", pad
)) {
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */