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