1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "precompiled_desktop.hxx" 29*cdf0e10cSrcweir #include "sal/config.h" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <cstddef> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN 34*cdf0e10cSrcweir #if defined _MSC_VER 35*cdf0e10cSrcweir #pragma warning(push, 1) 36*cdf0e10cSrcweir #endif 37*cdf0e10cSrcweir #include <windows.h> 38*cdf0e10cSrcweir #include <shlwapi.h> 39*cdf0e10cSrcweir #if defined _MSC_VER 40*cdf0e10cSrcweir #pragma warning(pop) 41*cdf0e10cSrcweir #endif 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #include "tools/pathutils.hxx" 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir #include "extendloaderenvironment.hxx" 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir namespace { 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir void fail() { 50*cdf0e10cSrcweir LPWSTR buf = NULL; 51*cdf0e10cSrcweir FormatMessageW( 52*cdf0e10cSrcweir FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, 53*cdf0e10cSrcweir GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL); 54*cdf0e10cSrcweir MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR); 55*cdf0e10cSrcweir LocalFree(buf); 56*cdf0e10cSrcweir TerminateProcess(GetCurrentProcess(), 255); 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir bool contains(WCHAR const * paths, WCHAR const * path, WCHAR const * pathEnd) { 60*cdf0e10cSrcweir WCHAR const * q = path; 61*cdf0e10cSrcweir for (WCHAR const * p = paths;; ++p) { 62*cdf0e10cSrcweir WCHAR c = *p; 63*cdf0e10cSrcweir switch (c) { 64*cdf0e10cSrcweir case L'\0': 65*cdf0e10cSrcweir return q == pathEnd; 66*cdf0e10cSrcweir case L';': 67*cdf0e10cSrcweir if (q == pathEnd) { 68*cdf0e10cSrcweir return true; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir q = path; 71*cdf0e10cSrcweir break; 72*cdf0e10cSrcweir default: 73*cdf0e10cSrcweir if (q != NULL) { 74*cdf0e10cSrcweir if (q != pathEnd && *q == c) { 75*cdf0e10cSrcweir ++q; 76*cdf0e10cSrcweir } else { 77*cdf0e10cSrcweir q = NULL; 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir break; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir namespace desktop_win32 { 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory) { 90*cdf0e10cSrcweir if (!GetModuleFileNameW(NULL, iniDirectory, MAX_PATH)) { 91*cdf0e10cSrcweir fail(); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir WCHAR * iniDirEnd = tools::filename(iniDirectory); 94*cdf0e10cSrcweir WCHAR name[MAX_PATH + MY_LENGTH(L".bin")]; 95*cdf0e10cSrcweir // hopefully std::size_t is large enough to not overflow 96*cdf0e10cSrcweir WCHAR * nameEnd = name; 97*cdf0e10cSrcweir for (WCHAR * p = iniDirEnd; *p != L'\0'; ++p) { 98*cdf0e10cSrcweir *nameEnd++ = *p; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir if (!(nameEnd - name >= 4 && nameEnd[-4] == L'.' && 101*cdf0e10cSrcweir (nameEnd[-3] == L'E' || nameEnd[-3] == L'e') && 102*cdf0e10cSrcweir (nameEnd[-2] == L'X' || nameEnd[-2] == L'x') && 103*cdf0e10cSrcweir (nameEnd[-1] == L'E' || nameEnd[-1] == L'e'))) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir *nameEnd = L'.'; 106*cdf0e10cSrcweir nameEnd += 4; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir nameEnd[-3] = 'b'; 109*cdf0e10cSrcweir nameEnd[-2] = 'i'; 110*cdf0e10cSrcweir nameEnd[-1] = 'n'; 111*cdf0e10cSrcweir tools::buildPath(binPath, iniDirectory, iniDirEnd, name, nameEnd - name); 112*cdf0e10cSrcweir *iniDirEnd = L'\0'; 113*cdf0e10cSrcweir WCHAR path[MAX_PATH]; 114*cdf0e10cSrcweir WCHAR * pathEnd = tools::buildPath( 115*cdf0e10cSrcweir path, iniDirectory, iniDirEnd, MY_STRING(L"..\\basis-link")); 116*cdf0e10cSrcweir if (pathEnd == NULL) { 117*cdf0e10cSrcweir fail(); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir std::size_t const maxEnv = 32767; 120*cdf0e10cSrcweir WCHAR pad[2 * MAX_PATH + maxEnv]; 121*cdf0e10cSrcweir // hopefully std::size_t is large enough to not overflow 122*cdf0e10cSrcweir WCHAR * padEnd = NULL; 123*cdf0e10cSrcweir WCHAR env[maxEnv]; 124*cdf0e10cSrcweir DWORD n = GetEnvironmentVariableW(L"PATH", env, maxEnv); 125*cdf0e10cSrcweir if (n >= maxEnv || n == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) { 126*cdf0e10cSrcweir fail(); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir env[n] = L'\0'; 129*cdf0e10cSrcweir bool exclude1; 130*cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 131*cdf0e10cSrcweir if (pathEnd == NULL) { 132*cdf0e10cSrcweir if (GetLastError() != ERROR_FILE_NOT_FOUND) { 133*cdf0e10cSrcweir fail(); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir // This path is only taken by testtool.exe in basis program directory; 136*cdf0e10cSrcweir // its PATH needs to include the brand program directory: 137*cdf0e10cSrcweir pathEnd = tools::buildPath( 138*cdf0e10cSrcweir path, iniDirectory, iniDirEnd, MY_STRING(L"..")); 139*cdf0e10cSrcweir if (pathEnd == NULL) { 140*cdf0e10cSrcweir fail(); 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir padEnd = tools::buildPath( 143*cdf0e10cSrcweir pad, path, pathEnd, MY_STRING(L"\\..\\program")); 144*cdf0e10cSrcweir if (padEnd == NULL) { 145*cdf0e10cSrcweir fail(); 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir exclude1 = contains(env, pad, padEnd); 148*cdf0e10cSrcweir } else { 149*cdf0e10cSrcweir exclude1 = true; 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir WCHAR * pad2 = exclude1 ? pad : padEnd + 1; 152*cdf0e10cSrcweir pathEnd = tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")); 153*cdf0e10cSrcweir if (pathEnd == NULL) { 154*cdf0e10cSrcweir fail(); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 157*cdf0e10cSrcweir if (pathEnd == NULL) { 158*cdf0e10cSrcweir fail(); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir padEnd = tools::buildPath(pad2, path, pathEnd, MY_STRING(L"\\bin")); 161*cdf0e10cSrcweir if (padEnd == NULL) { 162*cdf0e10cSrcweir fail(); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir bool exclude2 = contains(env, pad2, padEnd); 165*cdf0e10cSrcweir if (!(exclude1 && exclude2)) { 166*cdf0e10cSrcweir if (!(exclude1 || exclude2)) { 167*cdf0e10cSrcweir pad2[-1] = L';'; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir WCHAR * p = exclude2 ? pad2 - 1 : padEnd; 170*cdf0e10cSrcweir if (n != 0) { 171*cdf0e10cSrcweir *p++ = L';'; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir for (DWORD i = 0; i <= n; ++i) { 174*cdf0e10cSrcweir *p++ = env[i]; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir if (!SetEnvironmentVariableW(L"PATH", pad)) { 177*cdf0e10cSrcweir fail(); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir } 183