update dev300-m58
[ooovba.git] / cppuhelper / source / findsofficepath.c
blob63adaf19676cc11b0c9eda6d1b056924856f57d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: findsofficepath.c,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_cppuhelper.hxx"
32 #include "sal/config.h"
34 #include <stdlib.h>
35 #include <string.h>
37 #if defined WNT
39 #define WIN32_LEAN_AND_MEAN
40 #include <windows.h>
43 * Gets the installation path from the Windows Registry for the specified
44 * registry key.
46 * @param hroot open handle to predefined root registry key
47 * @param subKeyName name of the subkey to open
49 * @return the installation path or NULL, if no installation was found or
50 * if an error occured
52 static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
54 HKEY hkey;
55 DWORD type;
56 char* data = NULL;
57 DWORD size;
59 /* open the specified registry key */
60 if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
62 return NULL;
65 /* find the type and size of the default value */
66 if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
68 RegCloseKey( hkey );
69 return NULL;
72 /* get memory to hold the default value */
73 data = (char*) malloc( size );
75 /* read the default value */
76 if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
78 RegCloseKey( hkey );
79 return NULL;
82 /* release registry key handle */
83 RegCloseKey( hkey );
85 return data;
89 * Gets the installation path from the Windows Registry.
91 * @return the installation path or NULL, if no installation was found or
92 * if an error occured
94 static char* platformSpecific()
96 const char* SUBKEYNAME = "Software\\OpenOffice.org\\UNO\\InstallPath";
98 char* path = NULL;
100 /* read the key's default value from HKEY_CURRENT_USER */
101 path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
103 if ( path == NULL )
105 /* read the key's default value from HKEY_LOCAL_MACHINE */
106 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
109 return path;
112 #else
114 #include <unistd.h>
115 #include <limits.h>
118 * Gets the installation path from the PATH environment variable.
120 * <p>An installation is found, if the executable 'soffice' or a symbolic link
121 * is in one of the directories listed in the PATH environment variable.</p>
123 * @return the installation path or NULL, if no installation was found or
124 * if an error occured
126 static char* platformSpecific()
128 const int SEPARATOR = '/';
129 const char* PATHSEPARATOR = ":";
130 const char* PATHVARNAME = "PATH";
131 const char* APPENDIX = "/soffice";
133 char* path = NULL;
134 char* env = NULL;
135 char* str = NULL;
136 char* dir = NULL;
137 char* file = NULL;
138 char* resolved = NULL;
139 char* sep = NULL;
141 char buffer[PATH_MAX];
142 int pos;
144 /* get the value of the PATH environment variable */
145 env = getenv( PATHVARNAME );
146 str = (char*) malloc( strlen( env ) + 1 );
147 strcpy( str, env );
149 /* get the tokens separated by ':' */
150 dir = strtok( str, PATHSEPARATOR );
152 while ( dir )
154 /* construct soffice file path */
155 file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
156 strcpy( file, dir );
157 strcat( file, APPENDIX );
159 /* check existence of soffice file */
160 if ( !access( file, F_OK ) )
162 /* resolve symbolic link */
163 resolved = realpath( file, buffer );
165 if ( resolved != NULL )
167 /* get path to program directory */
168 sep = strrchr( resolved, SEPARATOR );
170 if ( sep != NULL )
172 pos = sep - resolved;
173 path = (char*) malloc( pos + 1 );
174 strncpy( path, resolved, pos );
175 path[ pos ] = '\0';
176 free( file );
177 break;
182 dir = strtok( NULL, PATHSEPARATOR );
183 free( file );
186 free( str );
188 return path;
191 #endif
193 char const* cppuhelper_detail_findSofficePath()
195 const char* UNOPATHVARNAME = "UNO_PATH";
197 char* path = NULL;
199 /* get the installation path from the UNO_PATH environment variable */
200 path = getenv( UNOPATHVARNAME );
202 if ( path == NULL || strlen( path ) == 0 )
204 path = platformSpecific();
207 return path;