1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | /* |
3 | * This file is part of the LibreOffice project. |
4 | * |
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/. |
8 | * |
9 | * This file incorporates work covered by the following license notice: |
10 | * |
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 . |
18 | */ |
19 | |
20 | #include "sal/config.h" |
21 | |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | |
25 | #if defined WNT |
26 | |
27 | #define WIN32_LEAN_AND_MEAN |
28 | #include <windows.h> |
29 | |
30 | /* |
31 | * Gets the installation path from the Windows Registry for the specified |
32 | * registry key. |
33 | * |
34 | * @param hroot open handle to predefined root registry key |
35 | * @param subKeyName name of the subkey to open |
36 | * |
37 | * @return the installation path or NULL, if no installation was found or |
38 | * if an error occurred |
39 | */ |
40 | static char* getPathFromRegistryKey( HKEY hroot, const char* subKeyName ) |
41 | { |
42 | HKEY hkey; |
43 | DWORD type; |
44 | char* data = NULL; |
45 | DWORD size; |
46 | |
47 | /* open the specified registry key */ |
48 | if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS ) |
49 | { |
50 | return NULL; |
51 | } |
52 | |
53 | /* find the type and size of the default value */ |
54 | if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS ) |
55 | { |
56 | RegCloseKey( hkey ); |
57 | return NULL; |
58 | } |
59 | |
60 | /* get memory to hold the default value */ |
61 | data = (char*) malloc( size ); |
62 | |
63 | /* read the default value */ |
64 | if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS ) |
65 | { |
66 | RegCloseKey( hkey ); |
67 | return NULL; |
68 | } |
69 | |
70 | /* release registry key handle */ |
71 | RegCloseKey( hkey ); |
72 | |
73 | return data; |
74 | } |
75 | |
76 | /* |
77 | * Gets the installation path from the Windows Registry. |
78 | * |
79 | * @return the installation path or NULL, if no installation was found or |
80 | * if an error occurred |
81 | */ |
82 | static char* platformSpecific() |
83 | { |
84 | const char* SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath" ; |
85 | |
86 | char* path = NULL; |
87 | |
88 | /* read the key's default value from HKEY_CURRENT_USER */ |
89 | path = getPathFromRegistryKey( HKEY_CURRENT_USER, SUBKEYNAME ); |
90 | |
91 | if ( path == NULL ) |
92 | { |
93 | /* read the key's default value from HKEY_LOCAL_MACHINE */ |
94 | path = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, SUBKEYNAME ); |
95 | } |
96 | |
97 | return path; |
98 | } |
99 | |
100 | #else |
101 | |
102 | #include <unistd.h> |
103 | #include <limits.h> |
104 | |
105 | /* |
106 | * Gets the installation path from the PATH environment variable. |
107 | * |
108 | * <p>An installation is found, if the executable 'soffice' or a symbolic link |
109 | * is in one of the directories listed in the PATH environment variable.</p> |
110 | * |
111 | * @return the installation path or NULL, if no installation was found or |
112 | * if an error occurred |
113 | */ |
114 | static char* platformSpecific() |
115 | { |
116 | const int SEPARATOR = '/'; |
117 | const char* PATHSEPARATOR = ":" ; |
118 | const char* PATHVARNAME = "PATH" ; |
119 | const char* APPENDIX = "/libreoffice" ; |
120 | |
121 | char* path = NULL; |
122 | char* env = NULL; |
123 | char* str = NULL; |
124 | char* dir = NULL; |
125 | char* file = NULL; |
126 | char* resolved = NULL; |
127 | char* sep = NULL; |
128 | |
129 | char buffer[PATH_MAX]; |
130 | int pos; |
131 | |
132 | /* get the value of the PATH environment variable */ |
133 | env = getenv( PATHVARNAME ); |
134 | str = (char*) malloc( strlen( env ) + 1 ); |
135 | strcpy( str, env ); |
136 | |
137 | /* get the tokens separated by ':' */ |
138 | dir = strtok( str, PATHSEPARATOR ); |
139 | |
140 | while ( dir ) |
141 | { |
142 | /* construct soffice file path */ |
143 | file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); |
144 | strcpy( file, dir ); |
145 | strcat( file, APPENDIX ); |
146 | |
147 | /* check existence of soffice file */ |
148 | if ( !access( file, F_OK ) ) |
149 | { |
150 | /* resolve symbolic link */ |
151 | resolved = realpath( file, buffer ); |
152 | |
153 | if ( resolved != NULL ) |
154 | { |
155 | /* get path to program directory */ |
156 | sep = strrchr( resolved, SEPARATOR ); |
157 | |
158 | if ( sep != NULL ) |
159 | { |
160 | pos = sep - resolved; |
161 | path = (char*) malloc( pos + 1 ); |
162 | strncpy( path, resolved, pos ); |
163 | path[ pos ] = '\0'; |
164 | free( file ); |
165 | break; |
166 | } |
167 | } |
168 | } |
169 | |
170 | dir = strtok( NULL, PATHSEPARATOR ); |
171 | free( file ); |
172 | } |
173 | |
174 | free( str ); |
175 | |
176 | return path; |
177 | } |
178 | |
179 | #endif |
180 | |
181 | char const* cppuhelper_detail_findSofficePath() |
182 | { |
183 | const char* UNOPATHVARNAME = "UNO_PATH" ; |
184 | |
185 | char* path = NULL; |
186 | |
187 | /* get the installation path from the UNO_PATH environment variable */ |
188 | path = getenv( UNOPATHVARNAME ); |
189 | |
190 | if (!path || !path[0]) |
191 | path = platformSpecific(); |
192 | |
193 | return path; |
194 | } |
195 | |
196 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
197 | |