1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
25 #include <cppuhelper/findsofficepath.h>
29 #define WIN32_LEAN_AND_MEAN
33 * Gets the installation path from the Windows Registry for the specified
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
)
49 /* open the specified registry key */
50 if ( RegOpenKeyExW( hroot
, subKeyName
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
55 /* find the type and size of the default value */
56 if ( RegQueryValueExW( hkey
, NULL
, NULL
, &type
, NULL
, &size
) != ERROR_SUCCESS
)
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
)
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 */
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
);
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
);
109 /* read the key's default value from HKEY_LOCAL_MACHINE */
110 path
= getPathFromRegistryKey( HKEY_LOCAL_MACHINE
, SUBKEYNAME
);
121 * Gets the installation path from the PATH environment variable.
123 * <p>An installation is found, if the executable 'soffice' or a symbolic link
124 * is in one of the directories listed in the PATH environment variable.</p>
126 * @return the installation path or NULL, if no installation was found or
127 * if an error occurred
129 static char* platformSpecific(void)
131 const char* UNOPATHVARNAME
= "UNO_PATH";
133 /* get the installation path from the UNO_PATH environment variable */
134 char* env
= getenv(UNOPATHVARNAME
);
136 const int SEPARATOR
= '/';
137 const char* PATHSEPARATOR
= ":";
138 const char* PATHVARNAME
= "PATH";
139 const char* APPENDIX
= "/libreoffice";
146 char buffer
[PATH_MAX
];
154 /* get the value of the PATH environment variable */
155 env
= getenv( PATHVARNAME
);
163 /* get the tokens separated by ':' */
164 dir
= strtok( str
, PATHSEPARATOR
);
168 /* construct soffice file path */
169 char* resolved
= NULL
;
170 char* file
= (char*) malloc( strlen( dir
) + strlen( APPENDIX
) + 1 );
178 strcat( file
, APPENDIX
);
180 /* resolve symbolic link */
181 resolved
= realpath( file
, buffer
);
182 if ( resolved
!= NULL
)
184 /* get path to program directory */
185 sep
= strrchr( resolved
, SEPARATOR
);
189 pos
= sep
- resolved
;
190 path
= (char*) malloc( pos
+ 1 );
191 strncpy( path
, resolved
, pos
);
198 dir
= strtok( NULL
, PATHSEPARATOR
);
214 cppuhelper_detail_findSofficePath()
216 return platformSpecific();
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */