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 char* getPathFromRegistryKey( HKEY hroot
, const char* subKeyName
)
49 /* open the specified registry key */
50 if ( RegOpenKeyEx( hroot
, subKeyName
, 0, KEY_READ
, &hkey
) != ERROR_SUCCESS
)
55 /* find the type and size of the default value */
56 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, NULL
, &size
) != ERROR_SUCCESS
)
62 /* get memory to hold the default value */
63 data
= (char*) malloc( size
);
65 /* read the default value */
66 if ( RegQueryValueEx( hkey
, NULL
, NULL
, &type
, (LPBYTE
) data
, &size
) != ERROR_SUCCESS
)
72 /* release registry key handle */
79 * Gets the installation path from the Windows Registry.
81 * @return the installation path or NULL, if no installation was found or
82 * if an error occurred
84 static char* platformSpecific()
86 const char* SUBKEYNAME
= "Software\\LibreOffice\\UNO\\InstallPath";
90 /* read the key's default value from HKEY_CURRENT_USER */
91 path
= getPathFromRegistryKey( HKEY_CURRENT_USER
, SUBKEYNAME
);
95 /* read the key's default value from HKEY_LOCAL_MACHINE */
96 path
= getPathFromRegistryKey( HKEY_LOCAL_MACHINE
, SUBKEYNAME
);
108 * Gets the installation path from the PATH environment variable.
110 * <p>An installation is found, if the executable 'soffice' or a symbolic link
111 * is in one of the directories listed in the PATH environment variable.</p>
113 * @return the installation path or NULL, if no installation was found or
114 * if an error occurred
116 static char* platformSpecific()
118 const int SEPARATOR
= '/';
119 const char* PATHSEPARATOR
= ":";
120 const char* PATHVARNAME
= "PATH";
121 const char* APPENDIX
= "/libreoffice";
128 char* resolved
= NULL
;
131 char buffer
[PATH_MAX
];
134 /* get the value of the PATH environment variable */
135 env
= getenv( PATHVARNAME
);
143 /* get the tokens separated by ':' */
144 dir
= strtok( str
, PATHSEPARATOR
);
148 /* construct soffice file path */
149 file
= (char*) malloc( strlen( dir
) + strlen( APPENDIX
) + 1 );
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
);
172 pos
= sep
- resolved
;
173 path
= (char*) malloc( pos
+ 1 );
174 strncpy( path
, resolved
, pos
);
182 dir
= strtok( NULL
, PATHSEPARATOR
);
193 char const* cppuhelper_detail_findSofficePath()
195 const char* UNOPATHVARNAME
= "UNO_PATH";
199 /* get the installation path from the UNO_PATH environment variable */
200 path
= getenv( UNOPATHVARNAME
);
202 if (!path
|| !path
[0])
203 path
= platformSpecific();
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */