merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / cppuhelper / source / findsofficepath.c
blob0a0cea0ff8b3a98849ad9b546b335d9ad11ab8ec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "precompiled_cppuhelper.hxx"
30 #include "sal/config.h"
32 #include <stdlib.h>
33 #include <string.h>
35 #if defined WNT
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
41 * Gets the installation path from the Windows Registry for the specified
42 * registry key.
44 * @param hroot open handle to predefined root registry key
45 * @param subKeyName name of the subkey to open
47 * @return the installation path or NULL, if no installation was found or
48 * if an error occured
50 static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName )
52 HKEY hkey;
53 DWORD type;
54 char* data = NULL;
55 DWORD size;
57 /* open the specified registry key */
58 if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
60 return NULL;
63 /* find the type and size of the default value */
64 if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
66 RegCloseKey( hkey );
67 return NULL;
70 /* get memory to hold the default value */
71 data = (char*) malloc( size );
73 /* read the default value */
74 if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
76 RegCloseKey( hkey );
77 return NULL;
80 /* release registry key handle */
81 RegCloseKey( hkey );
83 return data;
87 * Gets the installation path from the Windows Registry.
89 * @return the installation path or NULL, if no installation was found or
90 * if an error occured
92 static char* platformSpecific()
94 const char* SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath";
96 char* path = NULL;
98 /* read the key's default value from HKEY_CURRENT_USER */
99 path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
101 if ( path == NULL )
103 /* read the key's default value from HKEY_LOCAL_MACHINE */
104 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
107 return path;
110 #else
112 #include <unistd.h>
113 #include <limits.h>
116 * Gets the installation path from the PATH environment variable.
118 * <p>An installation is found, if the executable 'soffice' or a symbolic link
119 * is in one of the directories listed in the PATH environment variable.</p>
121 * @return the installation path or NULL, if no installation was found or
122 * if an error occured
124 static char* platformSpecific()
126 const int SEPARATOR = '/';
127 const char* PATHSEPARATOR = ":";
128 const char* PATHVARNAME = "PATH";
129 const char* APPENDIX = "/soffice";
131 char* path = NULL;
132 char* env = NULL;
133 char* str = NULL;
134 char* dir = NULL;
135 char* file = NULL;
136 char* resolved = NULL;
137 char* sep = NULL;
139 char buffer[PATH_MAX];
140 int pos;
142 /* get the value of the PATH environment variable */
143 env = getenv( PATHVARNAME );
144 str = (char*) malloc( strlen( env ) + 1 );
145 strcpy( str, env );
147 /* get the tokens separated by ':' */
148 dir = strtok( str, PATHSEPARATOR );
150 while ( dir )
152 /* construct soffice file path */
153 file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
154 strcpy( file, dir );
155 strcat( file, APPENDIX );
157 /* check existence of soffice file */
158 if ( !access( file, F_OK ) )
160 /* resolve symbolic link */
161 resolved = realpath( file, buffer );
163 if ( resolved != NULL )
165 /* get path to program directory */
166 sep = strrchr( resolved, SEPARATOR );
168 if ( sep != NULL )
170 pos = sep - resolved;
171 path = (char*) malloc( pos + 1 );
172 strncpy( path, resolved, pos );
173 path[ pos ] = '\0';
174 free( file );
175 break;
180 dir = strtok( NULL, PATHSEPARATOR );
181 free( file );
184 free( str );
186 return path;
189 #endif
191 char const* cppuhelper_detail_findSofficePath()
193 const char* UNOPATHVARNAME = "UNO_PATH";
195 char* path = NULL;
197 /* get the installation path from the UNO_PATH environment variable */
198 path = getenv( UNOPATHVARNAME );
200 if ( path == NULL || strlen( path ) == 0 )
202 path = platformSpecific();
205 return path;
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */