1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include "precompiled_cppuhelper.hxx" 25 #include "sal/config.h" 26 27 #include <stdlib.h> 28 #include <string.h> 29 30 #if defined WNT 31 32 #define WIN32_LEAN_AND_MEAN 33 #include <windows.h> 34 35 /* 36 * Gets the installation path from the Windows Registry for the specified 37 * registry key. 38 * 39 * @param hroot open handle to predefined root registry key 40 * @param subKeyName name of the subkey to open 41 * 42 * @return the installation path or NULL, if no installation was found or 43 * if an error occured 44 */ 45 static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName ) 46 { 47 HKEY hkey; 48 DWORD type; 49 char* data = NULL; 50 DWORD size; 51 52 /* open the specified registry key */ 53 if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS ) 54 { 55 return NULL; 56 } 57 58 /* find the type and size of the default value */ 59 if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS ) 60 { 61 RegCloseKey( hkey ); 62 return NULL; 63 } 64 65 /* get memory to hold the default value */ 66 data = (char*) malloc( size ); 67 68 /* read the default value */ 69 if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS ) 70 { 71 RegCloseKey( hkey ); 72 return NULL; 73 } 74 75 /* release registry key handle */ 76 RegCloseKey( hkey ); 77 78 return data; 79 } 80 81 /* 82 * Gets the installation path from the Windows Registry. 83 * 84 * @return the installation path or NULL, if no installation was found or 85 * if an error occured 86 */ 87 static char* platformSpecific() 88 { 89 const char* SUBKEYNAME = "Software\\OpenOffice\\UNO\\InstallPath"; 90 const char* SUBKEYNAME64 = "Software\\Wow6432Node\\OpenOffice\\UNO\\InstallPath"; 91 92 char* path = NULL; 93 94 /* read the key's default value from HKEY_CURRENT_USER */ 95 path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME ); 96 97 if ( path == NULL ) 98 { 99 /* read the key's default value from HKEY_LOCAL_USER 64 */ 100 path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME64 ); 101 } 102 if ( path == NULL ) 103 { 104 /* read the key's default value from HKEY_LOCAL_MACHINE */ 105 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME ); 106 } 107 if ( path == NULL ) 108 { 109 /* read the key's default value from HKEY_LOCAL_MACHINE 64*/ 110 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME64 ); 111 } 112 113 return path; 114 } 115 116 #else 117 118 #include <unistd.h> 119 #include <limits.h> 120 #include <stdio.h> 121 /* 122 * Gets the installation path from the PATH environment variable. 123 * 124 * <p>An installation is found, if the executable 'soffice' or a symbolic link 125 * is in one of the directories listed in the PATH environment variable.</p> 126 * 127 * @return the installation path or NULL, if no installation was found or 128 * if an error occured 129 */ 130 static char* platformSpecific() 131 { 132 char* path = NULL; 133 134 #ifdef MACOSX 135 /* On MacOS we have no soffice link under /usr/bin but the default office location is known 136 and we check this only 137 */ 138 const char* MACDEFAULTOFFICEPATH = "/Applications/OpenOffice.app/Contents/MacOS"; 139 const char* MACDEFAULTSOFFICE = "/Applications/OpenOffice.app/Contents/MacOS/soffice"; 140 141 if ( !access( MACDEFAULTSOFFICE, F_OK ) ) 142 { 143 path = (char*) malloc( strlen(MACDEFAULTOFFICEPATH) + 1 ); 144 strcpy( path, MACDEFAULTOFFICEPATH); 145 } 146 return path; 147 #else 148 const int SEPARATOR = '/'; 149 const char* PATHSEPARATOR = ":"; 150 const char* PATHVARNAME = "PATH"; 151 const char* APPENDIX = "/soffice"; 152 153 char* env = NULL; 154 char* str = NULL; 155 char* dir = NULL; 156 char* file = NULL; 157 char* resolved = NULL; 158 char* sep = NULL; 159 160 char buffer[PATH_MAX]; 161 int pos; 162 163 /* get the value of the PATH environment variable */ 164 env = getenv( PATHVARNAME ); 165 str = (char*) malloc( strlen( env ) + 1 ); 166 strcpy( str, env ); 167 168 /* get the tokens separated by ':' */ 169 dir = strtok( str, PATHSEPARATOR ); 170 171 while ( dir ) 172 { 173 /* construct soffice file path */ 174 file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); 175 strcpy( file, dir ); 176 strcat( file, APPENDIX ); 177 178 /* check existence of soffice file */ 179 if ( !access( file, F_OK ) ) 180 { 181 /* resolve symbolic link */ 182 resolved = realpath( file, buffer ); 183 184 if ( resolved != NULL ) 185 { 186 /* get path to program directory */ 187 sep = strrchr( resolved, SEPARATOR ); 188 189 if ( sep != NULL ) 190 { 191 pos = sep - resolved; 192 path = (char*) malloc( pos + 1 ); 193 strncpy( path, resolved, pos ); 194 path[ pos ] = '\0'; 195 free( file ); 196 break; 197 } 198 } 199 } 200 201 dir = strtok( NULL, PATHSEPARATOR ); 202 free( file ); 203 } 204 205 free( str ); 206 207 return path; 208 #endif 209 } 210 211 #endif 212 213 char const* cppuhelper_detail_findSofficePath() 214 { 215 const char* UNOPATHVARNAME = "UNO_PATH"; 216 217 char* path = NULL; 218 219 /* get the installation path from the UNO_PATH environment variable */ 220 path = getenv( UNOPATHVARNAME ); 221 222 if ( path == NULL || strlen( path ) == 0 ) 223 { 224 path = platformSpecific(); 225 } 226 227 return path; 228 } 229