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 {
89*599cc5b4SOliver-Rainer Wittmann     const char* SUBKEYNAME = "Software\\OpenOffice\\UNO\\InstallPath";
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     char* path = NULL;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     /* read the key's default value from HKEY_CURRENT_USER */
94cdf0e10cSrcweir     path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     if ( path == NULL )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         /* read the key's default value from HKEY_LOCAL_MACHINE */
99cdf0e10cSrcweir         path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     return path;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir #else
106cdf0e10cSrcweir 
107cdf0e10cSrcweir #include <unistd.h>
108cdf0e10cSrcweir #include <limits.h>
109cdf0e10cSrcweir 
110cdf0e10cSrcweir /*
111cdf0e10cSrcweir  * Gets the installation path from the PATH environment variable.
112cdf0e10cSrcweir  *
113cdf0e10cSrcweir  * <p>An installation is found, if the executable 'soffice' or a symbolic link
114cdf0e10cSrcweir  * is in one of the directories listed in the PATH environment variable.</p>
115cdf0e10cSrcweir  *
116cdf0e10cSrcweir  * @return the installation path or NULL, if no installation was found or
117cdf0e10cSrcweir  *         if an error occured
118cdf0e10cSrcweir  */
119cdf0e10cSrcweir static char* platformSpecific()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     const int SEPARATOR = '/';
122cdf0e10cSrcweir     const char* PATHSEPARATOR = ":";
123cdf0e10cSrcweir     const char* PATHVARNAME = "PATH";
124cdf0e10cSrcweir     const char* APPENDIX = "/soffice";
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     char* path = NULL;
127cdf0e10cSrcweir     char* env = NULL;
128cdf0e10cSrcweir     char* str = NULL;
129cdf0e10cSrcweir     char* dir = NULL;
130cdf0e10cSrcweir     char* file = NULL;
131cdf0e10cSrcweir     char* resolved = NULL;
132cdf0e10cSrcweir 	char* sep = NULL;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     char buffer[PATH_MAX];
135cdf0e10cSrcweir     int pos;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     /* get the value of the PATH environment variable */
138cdf0e10cSrcweir     env = getenv( PATHVARNAME );
139cdf0e10cSrcweir 	str = (char*) malloc( strlen( env ) + 1 );
140cdf0e10cSrcweir 	strcpy( str, env );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     /* get the tokens separated by ':' */
143cdf0e10cSrcweir     dir = strtok( str, PATHSEPARATOR );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	while ( dir )
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir         /* construct soffice file path */
148cdf0e10cSrcweir         file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
149cdf0e10cSrcweir 		strcpy( file, dir );
150cdf0e10cSrcweir 		strcat( file, APPENDIX );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		/* check existence of soffice file */
153cdf0e10cSrcweir 		if ( !access( file, F_OK ) )
154cdf0e10cSrcweir 		{
155cdf0e10cSrcweir             /* resolve symbolic link */
156cdf0e10cSrcweir 			resolved = realpath( file, buffer );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             if ( resolved != NULL )
159cdf0e10cSrcweir 			{
160cdf0e10cSrcweir                 /* get path to program directory */
161cdf0e10cSrcweir 				sep = strrchr( resolved, SEPARATOR );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 				if ( sep != NULL )
164cdf0e10cSrcweir 				{
165cdf0e10cSrcweir                     pos = sep - resolved;
166cdf0e10cSrcweir                     path = (char*) malloc( pos + 1 );
167cdf0e10cSrcweir                     strncpy( path, resolved, pos );
168cdf0e10cSrcweir                     path[ pos ] = '\0';
169cdf0e10cSrcweir                     free( file );
170cdf0e10cSrcweir                     break;
171cdf0e10cSrcweir 				}
172cdf0e10cSrcweir 			}
173cdf0e10cSrcweir 		}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 		dir = strtok( NULL, PATHSEPARATOR );
176cdf0e10cSrcweir         free( file );
177cdf0e10cSrcweir 	}
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	free( str );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     return path;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir #endif
185cdf0e10cSrcweir 
186cdf0e10cSrcweir char const* cppuhelper_detail_findSofficePath()
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     const char* UNOPATHVARNAME = "UNO_PATH";
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     char* path = NULL;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     /* get the installation path from the UNO_PATH environment variable */
193cdf0e10cSrcweir     path = getenv( UNOPATHVARNAME );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     if ( path == NULL || strlen( path ) == 0 )
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir 		path = platformSpecific();
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     return path;
201cdf0e10cSrcweir }
202