Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / cppuhelper / source / findsofficepath.c
blobf91f753c61eb0ddcc3e6193a8dd1825133cf54c4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <cppuhelper/findsofficepath.h>
27 #if defined(_WIN32)
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
33 * Gets the installation path from the Windows Registry for the specified
34 * registry key.
36 * @param hroot open handle to predefined root registry key
37 * @param subKeyName name of the subkey to open
39 * @return the installation path or NULL, if no installation was found or
40 * if an error occurred
42 static wchar_t* getPathFromRegistryKey( HKEY hroot, const wchar_t* subKeyName )
44 HKEY hkey;
45 DWORD type;
46 wchar_t* data = NULL;
47 DWORD size;
49 /* open the specified registry key */
50 if ( RegOpenKeyExW( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
52 return NULL;
55 /* find the type and size of the default value */
56 if ( RegQueryValueExW( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
58 RegCloseKey( hkey );
59 return NULL;
62 /* get memory to hold the default value */
63 data = (wchar_t*) malloc( size + sizeof(wchar_t) );
65 /* read the default value */
66 if ( RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
68 RegCloseKey( hkey );
69 free( data );
70 return NULL;
73 // According to https://msdn.microsoft.com/en-us/ms724911, If the data has the REG_SZ,
74 // REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the
75 // proper terminating null characters
76 data[size / sizeof(wchar_t)] = 0;
78 /* release registry key handle */
79 RegCloseKey( hkey );
81 return data;
85 * Gets the installation path from the Windows Registry.
87 * @return the installation path or NULL, if no installation was found or
88 * if an error occurred
90 static wchar_t* platformSpecific()
92 const wchar_t* UNOPATHVARNAME = L"UNO_PATH";
94 /* get the installation path from the UNO_PATH environment variable */
95 wchar_t* env = _wgetenv(UNOPATHVARNAME);
97 if (env && env[0])
99 return wcsdup(env);
102 const wchar_t* SUBKEYNAME = L"Software\\LibreOffice\\UNO\\InstallPath";
104 /* read the key's default value from HKEY_CURRENT_USER */
105 wchar_t* path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME );
107 if ( path == NULL )
109 /* read the key's default value from HKEY_LOCAL_MACHINE */
110 path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME );
113 return path;
116 #else
118 #include <unistd.h>
119 #include <limits.h>
122 * Gets the installation path from the PATH environment variable.
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>
127 * @return the installation path or NULL, if no installation was found or
128 * if an error occurred
130 static char* platformSpecific(void)
132 const char* UNOPATHVARNAME = "UNO_PATH";
134 /* get the installation path from the UNO_PATH environment variable */
135 char* env = getenv(UNOPATHVARNAME);
137 const int SEPARATOR = '/';
138 const char* PATHSEPARATOR = ":";
139 const char* PATHVARNAME = "PATH";
140 const char* APPENDIX = "/libreoffice";
142 char* path = NULL;
143 char* str = NULL;
144 char* dir = NULL;
145 char* sep = NULL;
147 char buffer[PATH_MAX];
148 int pos;
150 if (env && env[0])
152 return strdup(env);
155 /* get the value of the PATH environment variable */
156 env = getenv( PATHVARNAME );
157 if (env == NULL)
158 return NULL;
160 str = strdup( env );
161 if (str == NULL)
162 return NULL;
164 /* get the tokens separated by ':' */
165 dir = strtok( str, PATHSEPARATOR );
167 while ( dir )
169 /* construct soffice file path */
170 char* resolved = NULL;
171 char* file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
172 if (file == NULL)
174 free(str);
175 return NULL;
178 strcpy( file, dir );
179 strcat( file, APPENDIX );
181 /* resolve symbolic link */
182 resolved = realpath( file, buffer );
183 if ( resolved != NULL )
185 /* get path to program directory */
186 sep = strrchr( resolved, SEPARATOR );
188 if ( sep != NULL )
190 pos = sep - resolved;
191 path = (char*) malloc( pos + 1 );
192 strncpy( path, resolved, pos );
193 path[ pos ] = '\0';
194 free( file );
195 break;
199 dir = strtok( NULL, PATHSEPARATOR );
200 free( file );
203 free( str );
205 return path;
208 #endif
210 #if defined(_WIN32)
211 wchar_t*
212 #else
213 char*
214 #endif
215 cppuhelper_detail_findSofficePath()
217 return platformSpecific();
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */