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_cppuhelper.hxx" 29*cdf0e10cSrcweir #include "sal/config.h" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <stdlib.h> 32*cdf0e10cSrcweir #include <string.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #if defined WNT 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN 37*cdf0e10cSrcweir #include <windows.h> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir /* 40*cdf0e10cSrcweir * Gets the installation path from the Windows Registry for the specified 41*cdf0e10cSrcweir * registry key. 42*cdf0e10cSrcweir * 43*cdf0e10cSrcweir * @param hroot open handle to predefined root registry key 44*cdf0e10cSrcweir * @param subKeyName name of the subkey to open 45*cdf0e10cSrcweir * 46*cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or 47*cdf0e10cSrcweir * if an error occured 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName ) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir HKEY hkey; 52*cdf0e10cSrcweir DWORD type; 53*cdf0e10cSrcweir char* data = NULL; 54*cdf0e10cSrcweir DWORD size; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /* open the specified registry key */ 57*cdf0e10cSrcweir if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS ) 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir return NULL; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /* find the type and size of the default value */ 63*cdf0e10cSrcweir if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS ) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir RegCloseKey( hkey ); 66*cdf0e10cSrcweir return NULL; 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir /* get memory to hold the default value */ 70*cdf0e10cSrcweir data = (char*) malloc( size ); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir /* read the default value */ 73*cdf0e10cSrcweir if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS ) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir RegCloseKey( hkey ); 76*cdf0e10cSrcweir return NULL; 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /* release registry key handle */ 80*cdf0e10cSrcweir RegCloseKey( hkey ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir return data; 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir /* 86*cdf0e10cSrcweir * Gets the installation path from the Windows Registry. 87*cdf0e10cSrcweir * 88*cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or 89*cdf0e10cSrcweir * if an error occured 90*cdf0e10cSrcweir */ 91*cdf0e10cSrcweir static char* platformSpecific() 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir const char* SUBKEYNAME = "Software\\OpenOffice.org\\UNO\\InstallPath"; 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir char* path = NULL; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir /* read the key's default value from HKEY_CURRENT_USER */ 98*cdf0e10cSrcweir path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME ); 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir if ( path == NULL ) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir /* read the key's default value from HKEY_LOCAL_MACHINE */ 103*cdf0e10cSrcweir path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME ); 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir return path; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir #else 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir #include <unistd.h> 112*cdf0e10cSrcweir #include <limits.h> 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir /* 115*cdf0e10cSrcweir * Gets the installation path from the PATH environment variable. 116*cdf0e10cSrcweir * 117*cdf0e10cSrcweir * <p>An installation is found, if the executable 'soffice' or a symbolic link 118*cdf0e10cSrcweir * is in one of the directories listed in the PATH environment variable.</p> 119*cdf0e10cSrcweir * 120*cdf0e10cSrcweir * @return the installation path or NULL, if no installation was found or 121*cdf0e10cSrcweir * if an error occured 122*cdf0e10cSrcweir */ 123*cdf0e10cSrcweir static char* platformSpecific() 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir const int SEPARATOR = '/'; 126*cdf0e10cSrcweir const char* PATHSEPARATOR = ":"; 127*cdf0e10cSrcweir const char* PATHVARNAME = "PATH"; 128*cdf0e10cSrcweir const char* APPENDIX = "/soffice"; 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir char* path = NULL; 131*cdf0e10cSrcweir char* env = NULL; 132*cdf0e10cSrcweir char* str = NULL; 133*cdf0e10cSrcweir char* dir = NULL; 134*cdf0e10cSrcweir char* file = NULL; 135*cdf0e10cSrcweir char* resolved = NULL; 136*cdf0e10cSrcweir char* sep = NULL; 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir char buffer[PATH_MAX]; 139*cdf0e10cSrcweir int pos; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir /* get the value of the PATH environment variable */ 142*cdf0e10cSrcweir env = getenv( PATHVARNAME ); 143*cdf0e10cSrcweir str = (char*) malloc( strlen( env ) + 1 ); 144*cdf0e10cSrcweir strcpy( str, env ); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir /* get the tokens separated by ':' */ 147*cdf0e10cSrcweir dir = strtok( str, PATHSEPARATOR ); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir while ( dir ) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir /* construct soffice file path */ 152*cdf0e10cSrcweir file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); 153*cdf0e10cSrcweir strcpy( file, dir ); 154*cdf0e10cSrcweir strcat( file, APPENDIX ); 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir /* check existence of soffice file */ 157*cdf0e10cSrcweir if ( !access( file, F_OK ) ) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir /* resolve symbolic link */ 160*cdf0e10cSrcweir resolved = realpath( file, buffer ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir if ( resolved != NULL ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir /* get path to program directory */ 165*cdf0e10cSrcweir sep = strrchr( resolved, SEPARATOR ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir if ( sep != NULL ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir pos = sep - resolved; 170*cdf0e10cSrcweir path = (char*) malloc( pos + 1 ); 171*cdf0e10cSrcweir strncpy( path, resolved, pos ); 172*cdf0e10cSrcweir path[ pos ] = '\0'; 173*cdf0e10cSrcweir free( file ); 174*cdf0e10cSrcweir break; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir dir = strtok( NULL, PATHSEPARATOR ); 180*cdf0e10cSrcweir free( file ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir free( str ); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir return path; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir #endif 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir char const* cppuhelper_detail_findSofficePath() 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir const char* UNOPATHVARNAME = "UNO_PATH"; 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir char* path = NULL; 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir /* get the installation path from the UNO_PATH environment variable */ 197*cdf0e10cSrcweir path = getenv( UNOPATHVARNAME ); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir if ( path == NULL || strlen( path ) == 0 ) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir path = platformSpecific(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir return path; 205*cdf0e10cSrcweir } 206