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 const int SEPARATOR = '/'; 133 const char* PATHSEPARATOR = ":"; 134 const char* PATHVARNAME = "PATH"; 135 const char* APPENDIX = "/soffice"; 136 137 char* path = NULL; 138 char* env = NULL; 139 char* str = NULL; 140 char* dir = NULL; 141 char* file = NULL; 142 char* resolved = NULL; 143 char* sep = NULL; 144 145 char buffer[PATH_MAX]; 146 int pos; 147 148 #ifdef MACOSX 149 /* On MacOS we have no soffice link under /usr/bin but the default office location is known 150 and we check this only 151 */ 152 const char* MACDEFAULTOFFICEPATH = "/Applications/OpenOffice.app/Contents/MacOS"; 153 const char* MACDEFAULTSOFFICE = "/Applications/OpenOffice.app/Contents/MacOS/soffice"; 154 155 if ( !access( MACDEFAULTSOFFICE, F_OK ) ) 156 { 157 path = (char*) malloc( MACDEFAULTOFFICEPATH + 1 ); 158 strcpy( path, MACDEFAULTOFFICEPATH); 159 } 160 return path; 161 #else 162 /* get the value of the PATH environment variable */ 163 env = getenv( PATHVARNAME ); 164 str = (char*) malloc( strlen( env ) + 1 ); 165 strcpy( str, env ); 166 167 /* get the tokens separated by ':' */ 168 dir = strtok( str, PATHSEPARATOR ); 169 170 while ( dir ) 171 { 172 /* construct soffice file path */ 173 file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); 174 strcpy( file, dir ); 175 strcat( file, APPENDIX ); 176 177 /* check existence of soffice file */ 178 if ( !access( file, F_OK ) ) 179 { 180 /* resolve symbolic link */ 181 resolved = realpath( file, buffer ); 182 183 if ( resolved != NULL ) 184 { 185 /* get path to program directory */ 186 sep = strrchr( resolved, SEPARATOR ); 187 188 if ( sep != NULL ) 189 { 190 pos = sep - resolved; 191 path = (char*) malloc( pos + 1 ); 192 strncpy( path, resolved, pos ); 193 path[ pos ] = '\0'; 194 free( file ); 195 break; 196 } 197 } 198 } 199 200 dir = strtok( NULL, PATHSEPARATOR ); 201 free( file ); 202 } 203 204 free( str ); 205 206 return path; 207 #endif 208 } 209 210 #endif 211 212 char const* cppuhelper_detail_findSofficePath() 213 { 214 const char* UNOPATHVARNAME = "UNO_PATH"; 215 216 char* path = NULL; 217 218 /* get the installation path from the UNO_PATH environment variable */ 219 path = getenv( UNOPATHVARNAME ); 220 221 if ( path == NULL || strlen( path ) == 0 ) 222 { 223 path = platformSpecific(); 224 } 225 226 return path; 227 } 228